comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/ProxyArtifact.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/ProxyArtifact.java@933bbc9fc11f
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.artifactdatabase;
10
11 import de.intevation.artifacts.Artifact;
12 import de.intevation.artifacts.CallContext;
13
14 import java.io.IOException;
15 import java.io.OutputStream;
16
17 import org.apache.log4j.Logger;
18
19 import org.w3c.dom.Document;
20
21 /**
22 * The proxy artifact is a wrapper around another artifact. It simply forwards
23 * the interface calls to this underlaying artifact.
24 * The reason for using proxy artifacts is enable the workflow to exchange
25 * artifacts at any time by something else without losing the concrete
26 * artifact. From the outside it always looks like there is only one
27 * distinct artifact.<br>
28 *
29 * An inner artifact is able to replace itself by indirectly hand over
30 * the replacement via the call context to the proxy artifact.<br>
31 * To do so the proxied artifact has to call
32 * <code>callContext.getContextValue(EPLACE_PROXY, replacement);</code>.
33 * After the current call (describe, feed, advance and out) of the proxied
34 * artifact is finished the proxy artifact replaces the former proxied artifact
35 * with the replacement.
36 *
37 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
38 */
39 public class ProxyArtifact
40 extends DefaultArtifact
41 {
42 /**
43 * Key to signal that the proxied artifact should be replaced.
44 */
45 public static final Object REPLACE_PROXY = new Object();
46
47 private static Logger logger = Logger.getLogger(ProxyArtifact.class);
48
49 /**
50 * The proxied artifact.
51 */
52 protected Artifact proxied;
53
54 /**
55 * Default constructor.
56 */
57 public ProxyArtifact() {
58 }
59
60 /**
61 * Constructor to create a new proxy artifact around a given artifact.
62 * @param proxied The artifact to be proxied.
63 */
64 public ProxyArtifact(Artifact proxied) {
65 this.proxied = proxied;
66 }
67
68 /**
69 * The currently proxied artifact.
70 * @return The proxied artifact.
71 */
72 public Artifact getProxied() {
73 return proxied;
74 }
75
76 /**
77 * Explicitly set the proxied artifacts.
78 * @param proxied
79 */
80 public void setProxied(Artifact proxied) {
81 this.proxied = proxied;
82 }
83
84 @Override
85 public void setIdentifier(String identifier) {
86 this.identifier = identifier;
87
88 if (proxied != null)
89 proxied.setIdentifier(identifier);
90 }
91
92 /**
93 * Method to check if the current proxied artifact should be replaced
94 * by a new one coming from the call context.
95 * @param callContext
96 */
97 protected void checkReplacement(CallContext callContext) {
98 Object replacement = callContext.getContextValue(REPLACE_PROXY);
99 if (replacement instanceof Artifact) {
100 setProxied((Artifact)replacement);
101 }
102 }
103
104 @Override
105 public String hash() {
106 return proxied != null
107 ? proxied.hash()
108 : super.hash();
109 }
110
111 @Override
112 public Document describe(Document data, CallContext context) {
113 try {
114 return proxied != null
115 ? proxied.describe(data, context)
116 : super.describe(data, context);
117 }
118 finally {
119 checkReplacement(context);
120 }
121 }
122
123 @Override
124 public Document advance(Document target, CallContext context) {
125 try {
126 return proxied != null
127 ? proxied.advance(target, context)
128 : super.advance(target, context);
129 }
130 finally {
131 checkReplacement(context);
132 }
133 }
134
135 @Override
136 public Document feed(Document target, CallContext context) {
137 try {
138 return proxied != null
139 ? proxied.feed(target, context)
140 : super.feed(target, context);
141 }
142 finally {
143 checkReplacement(context);
144 }
145 }
146
147 @Override
148 public void out(
149 Document format,
150 OutputStream out,
151 CallContext context
152 )
153 throws IOException
154 {
155 try {
156 if (proxied != null) {
157 proxied.out(format, out, context);
158 }
159 else {
160 super.out(format, out, context);
161 }
162 }
163 finally {
164 checkReplacement(context);
165 }
166 }
167
168 @Override
169 public void endOfLife(Object context) {
170 if (proxied != null) {
171 proxied.endOfLife(context);
172 }
173 else {
174 super.endOfLife(context);
175 }
176 }
177
178 @Override
179 public void cleanup(Object context) {
180 if (proxied != null)
181 proxied.cleanup(context);
182 else
183 super.cleanup(context);
184 }
185 }
186 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org