1
2
3
4
5
6 package net.kwfgrid.gworkflowdl.protocol.xupdate;
7
8 import net.kwfgrid.gworkflowdl.protocol.xml.GWDLNamespace;
9 import net.kwfgrid.gworkflowdl.structure.*;
10 import net.kwfgrid.jxupdate.jaxen.jbind.*;
11 import net.kwfgrid.jxupdate.xupdate.ObjectFactory;
12 import org.apache.log4j.Logger;
13 import org.jdom.Element;
14
15 import java.util.Iterator;
16 import java.util.List;
17
18 /***
19 * Implementation of <code>ObjectFactory</code> to invoke XUpdate modifications on a Workflow.
20 * The implementation of this class has to be kept up to date with the XSD of GWorkflowDL.
21 */
22 public class WorkflowObjectFactory implements ObjectFactory, GWDLNamespace {
23 private static final Logger logger = Logger.getLogger(WorkflowObjectFactory.class);
24 protected BindingFactory _bfact;
25
26 /***
27 * Constructor.
28 */
29 public WorkflowObjectFactory(BindingFactory bfact) {
30 _bfact = bfact;
31 }
32
33 /***
34 * Cloning a node will also clone its bound object. The parent of the cloned node will be <code>null</code>.
35 */
36 public Object clone(Object document, Object parent, Object obj) throws InstantiationException {
37
38 if (logger.isDebugEnabled()) {
39 logger.debug("WorkflowObjectFactory.clone: Cloning object " + obj + ".");
40 }
41
42 try {
43 if (obj instanceof JElement) {
44 JElement element = (JElement) obj;
45 String name = element.getName();
46 if ("tokenClass".equals(name)) {
47 JElement elementclone = _bfact.createElement((JElement) parent, "tokenClass", GWDL_NS, null);
48 int i = 0;
49 Iterator attributes = element.getAttributes().iterator();
50 while (attributes.hasNext()) {
51 JAttribute attributeclone = (JAttribute) clone(document, elementclone, attributes.next());
52 elementclone.addAttribute(attributeclone, i);
53 i++;
54 }
55 i = 0;
56 Iterator owls = element.getChildren().iterator();
57 while (owls.hasNext()) {
58 JElement owl = (JElement) clone(document, elementclone, owls.next());
59 elementclone.addChild(owl, i);
60 i++;
61 }
62 return elementclone;
63 }
64 Object objclone = null;
65 if ("transition".equals(name)) {
66 objclone = cloneTransition((Transition) element.getObject());
67 } else if ("readPlace".equals(name)) {
68 objclone = cloneEdge((Edge) element.getObject());
69 } else if ("inputPlace".equals(name)) {
70 objclone = cloneEdge((Edge) element.getObject());
71 } else if ("writePlace".equals(name)) {
72 objclone = cloneEdge((Edge) element.getObject());
73 } else if ("outputPlace".equals(name)) {
74 objclone = cloneEdge((Edge) element.getObject());
75 } else if ("operation".equals(name)) {
76 objclone = cloneOperation((Operation) element.getObject());
77 } else if ("operationClass".equals(name)) {
78 objclone = cloneOperationClass((OperationClass) element.getObject());
79 } else if ("operationCandidate".equals(name)) {
80 objclone = cloneOperationCandidate((OperationCandidate) element.getObject());
81 } else if ("place".equals(name)) {
82 objclone = clonePlace((Place) element.getObject());
83 } else if ("token".equals(name)) {
84 objclone = cloneToken((Token) element.getObject());
85 } else if ("data".equals(name)) {
86 objclone = cloneData((Data) element.getObject());
87 } else if ("description".equals(name)) {
88 objclone = cloneString((String) element.getObject());
89 } else if ("condition".equals(name)) {
90 objclone = cloneString((String) element.getObject());
91 } else if ("property".equals(name)) {
92 objclone = cloneProperty((Property) element.getObject());
93 } else if ("owl".equals(name)) {
94 objclone = cloneString((String) element.getObject());
95 }
96 if (objclone != null)
97 return _bfact.createElement((JNode) parent, element.getName(), element.getNamespaceURI(), objclone);
98 } else if (obj instanceof JAttribute) {
99 JAttribute attribute = (JAttribute) obj;
100 String name = attribute.getName();
101 Object objclone = null;
102 if ("selected".equals(name)) {
103 objclone = new Boolean(((Boolean) attribute.getValue()).booleanValue());
104 } else if ("ID".equals(name)) {
105 objclone = cloneString((String) attribute.getValue());
106 } else if ("capacity".equals(name)) {
107 objclone = cloneInt((Integer) attribute.getValue());
108 } else if ("placeID".equals(name)) {
109 objclone = cloneString((String) attribute.getValue());
110 } else if ("edgeExpression".equals(name)) {
111 objclone = cloneString((String) attribute.getValue());
112 } else if ("type".equals(name)) {
113 objclone = cloneString((String) attribute.getValue());
114 } else if ("name".equals(name)) {
115 objclone = cloneString((String) attribute.getValue());
116 } else if ("operationName".equals(name)) {
117 objclone = cloneString((String) attribute.getValue());
118 } else if ("resourceName".equals(name)) {
119 objclone = cloneString((String) attribute.getValue());
120 } else if ("quality".equals(name)) {
121 obj = cloneFloat((Float) attribute.getValue());
122 }
123 if (objclone != null)
124 return _bfact.createAttribute((JElement) parent, attribute.getName(), attribute.getNamespaceURI(), objclone);
125
126 }
127 throw new InstantiationException("Can not clone object of type " + obj.getClass() + ".");
128 } catch (BindingException x) {
129 InstantiationException inx = new InstantiationException("Could not instantiate new node.");
130 inx.initCause(x);
131 throw inx;
132 }
133 }
134
135 protected Property cloneProperty(Property p) {
136 Property np = Factory.newProperty();
137 np.setKey(p.getKey());
138 np.setValue(p.getValue());
139 return np;
140 }
141
142 protected String[] cloneStringArray(String[] obj) {
143 if (obj == null) return null;
144 String[] clone = new String[obj.length];
145 for (int i = 0; i < clone.length; i++) {
146 clone[i] = cloneString(obj[i]);
147 }
148 return clone;
149 }
150
151 protected Transition cloneTransition(Transition t) throws InstantiationException {
152 throw new InstantiationException("Transitions can not be cloned.");
153 }
154
155 protected Edge cloneEdge(Edge e) {
156 Edge clone = Factory.newEdge();
157 clone.setPlace(e.getPlace());
158 clone.setExpression(cloneString(e.getExpression()));
159 return clone;
160 }
161
162 protected Operation cloneOperation(Operation o) throws InstantiationException {
163 if (o == null) return null;
164 Operation clone = Factory.newOperation();
165 if (o.get() instanceof OperationClass) {
166 clone.set(cloneOperationClass((OperationClass) o.get()));
167 } else {
168 logger.warn("cloneOperation() not implemented for this type of operation object.");
169 }
170
171 return clone;
172 }
173
174 protected OperationClass cloneOperationClass(OperationClass oc) throws InstantiationException {
175 if (oc == null) return null;
176 OperationClass clone = Factory.newOperationClass();
177 clone.setOwls(cloneStringArray(oc.getOwls()));
178 OperationCandidate[] ocs = oc.getOperationCandidates();
179 OperationCandidate[] ocsclone = new OperationCandidate[ocs.length];
180 for (int i = 0; i < ocs.length; i++) {
181 ocsclone[i] = cloneOperationCandidate(ocs[i]);
182 }
183 clone.setOperationCandidates(ocsclone);
184 return clone;
185 }
186
187 protected OperationCandidate cloneOperationCandidate(OperationCandidate o) {
188 OperationCandidate clone = Factory.newOperationCandidate();
189 clone.setOwls(cloneStringArray(o.getOwls()));
190 clone.setType(cloneString(o.getType()));
191 clone.setOperationName(cloneString(o.getOperationName()));
192 clone.setResourceName(cloneString(o.getResourceName()));
193 clone.setQuality(o.getQuality());
194 clone.setSelected(o.isSelected());
195 return clone;
196 }
197
198 protected Place clonePlace(Place p) throws InstantiationException {
199 throw new InstantiationException("Places can not be cloned.");
200 }
201
202 protected Token cloneToken(Token t) throws InstantiationException {
203 return (Token) t.clone();
204 }
205
206 protected Data cloneData(Data d) throws InstantiationException {
207 return (Data) d.clone();
208 }
209
210 protected String cloneString(String s) {
211 if (s == null) return null;
212 else return new String(s);
213 }
214
215 protected Float cloneFloat(Float f) {
216 if (f == null) return null;
217 else return new Float(f.floatValue());
218 }
219
220 protected Integer cloneInt(Integer i) {
221 if (i == null) return null;
222 else return new Integer(i.intValue());
223 }
224
225 public Object createAttribute(Object document, Object parent, String name, String namespaceuri, String value) throws InstantiationException {
226 if (logger.isDebugEnabled()) {
227 logger.debug("WorkflowObjectFactory.createAttribute: Creating attribute " + name + " with value " + value + ".");
228 }
229
230 try {
231 Object obj = null;
232 if ("selected".equals(name)) {
233 obj = new Boolean(value.equalsIgnoreCase("true"));
234 } else if ("ID".equals(name)) {
235 obj = new String(value);
236 } else if ("capacity".equals(name)) {
237 obj = new Integer(Integer.parseInt(value));
238 } else if ("placeID".equals(name)) {
239 obj = new String(value);
240 } else if ("edgeExpression".equals(name)) {
241 obj = new String(value);
242 } else if ("type".equals(name)) {
243 obj = new String(value);
244 } else if ("name".equals(name)) {
245 obj = new String(value);
246 } else if ("operationName".equals(name)) {
247 obj = new String(value);
248 } else if ("resourceName".equals(name)) {
249 obj = new String(value);
250 } else if ("quality".equals(name)) {
251 obj = new Float(Float.parseFloat(value));
252 }
253 if (obj != null) return _bfact.createAttribute((JElement) parent, name, namespaceuri, obj);
254 } catch (NumberFormatException x) {
255 InstantiationException ix = new InstantiationException("WorkflowObjectFactory.createAttribute: Can not create attribute with name " + name + ".");
256 ix.initCause(x);
257 throw ix;
258 } catch (BindingException x) {
259 InstantiationException inx = new InstantiationException("Could not instantiate new node.");
260 inx.initCause(x);
261 throw inx;
262 }
263 throw new InstantiationException("WorkflowObjectFactory.createAttribute: Can not create attribute with name " + name + ".");
264 }
265
266 public Object createElement(Object doc, Object parent, String name, String namespaceuri, String spec) throws InstantiationException {
267 if (logger.isDebugEnabled()) {
268 logger.debug("WorkflowObjectFactory.createElement: Creating element " + name + " with specification " + spec + ".");
269 }
270
271 JDocument document = (JDocument) doc;
272
273 try {
274 if ("tokenClass".equals(name)) {
275 Element e = JdomString.string2element(spec);
276 JElement tcelement = _bfact.createElement((JNode) parent, name, namespaceuri, null);
277 String type = e.getAttributeValue("type");
278 if (type != null) tcelement.addAttribute(_bfact.createAttribute(tcelement, "type", null, type), 0);
279 List owls = e.getChildren("owl", JdomString.wfSpace);
280 if (owls != null) {
281 int id = 0;
282 Iterator i = owls.iterator();
283 while (i.hasNext()) {
284 Element owl = (Element) i.next();
285 tcelement.addChild(_bfact.createElement(tcelement, "owl", namespaceuri, owl.getText()), id);
286 id++;
287 }
288 }
289 return tcelement;
290 }
291
292 Object obj = null;
293 if ("transition".equals(name)) {
294 if (document.getRootElement() == null)
295 throw new InstantiationException("Can not instantiate transition for document without root element.");
296
297 Workflow target = (Workflow) document.getRootElement().getObject();
298
299 if (target == null)
300 throw new InstantiationException("Can not instantiate transition for document with null root object.");
301
302 Element e = JdomString.string2element(spec);
303 obj = TransitionJdom.element2java(target, e);
304 } else if ("readPlace".equals(name)) {
305 Element e = JdomString.string2element(spec);
306 obj = createEdge(document, e);
307 } else if ("inputPlace".equals(name)) {
308 Element e = JdomString.string2element(spec);
309 obj = createEdge(document, e);
310 } else if ("writePlace".equals(name)) {
311 Element e = JdomString.string2element(spec);
312 obj = createEdge(document, e);
313 } else if ("outputPlace".equals(name)) {
314 Element e = JdomString.string2element(spec);
315 obj = createEdge(document, e);
316 } else if ("operation".equals(name)) {
317 Element e = JdomString.string2element(spec);
318 obj = OperationJdom.element2java(e);
319 } else if ("operationClass".equals(name)) {
320 Element e = JdomString.string2element(spec);
321 obj = OperationClassJdom.element2java(e);
322 } else if ("operationCandidate".equals(name)) {
323 Element e = JdomString.string2element(spec);
324 obj = OperationCandidateJdom.element2java(e);
325 } else if ("place".equals(name)) {
326 Element e = JdomString.string2element(spec);
327 obj = PlaceJdom.element2java(e);
328 } else if ("token".equals(name)) {
329 Element e = JdomString.string2element(spec);
330 obj = TokenJdom.element2java(e);
331 } else if ("description".equals(name)) {
332 Element e = JdomString.string2element(spec);
333 obj = e.getText();
334 } else if ("condition".equals(name)) {
335 Element e = JdomString.string2element(spec);
336 obj = e.getText();
337 } else if ("property".equals(name)) {
338 Element e = JdomString.string2element(spec);
339 String key = e.getAttributeValue("name");
340 String value = e.getText();
341 Property p = Factory.newProperty();
342 p.setKey(key);
343 p.setValue(value);
344 obj = p;
345 } else if ("owl".equals(name)) {
346 Element e = JdomString.string2element(spec);
347 obj = e.getText();
348 }
349 if (obj != null) return _bfact.createElement((JNode) parent, name, namespaceuri, obj);
350 } catch (WorkflowFormatException x) {
351 InstantiationException ix = new InstantiationException("WorkflowObjectFactory.createElement: Can not create element with name " + name + ".");
352 ix.initCause(x);
353 throw ix;
354 } catch (CapacityException x) {
355 InstantiationException ix = new InstantiationException("WorkflowObjectFactory.createElement: Can not create element with name " + name + ".");
356 ix.initCause(x);
357 throw ix;
358 } catch (BindingException x) {
359 InstantiationException inx = new InstantiationException("Could not instantiate new node.");
360 inx.initCause(x);
361 throw inx;
362 }
363 throw new InstantiationException("WorkflowObjectFactory.createElement: Can not create element with name " + name + ".");
364 }
365
366 /***
367 * Create a new edge from a JDOM Element.
368 */
369 protected Edge createEdge(JDocument document, Element e) throws InstantiationException {
370 if (document.getRootElement() == null)
371 throw new InstantiationException("Can not instantiate edge for document without root element.");
372
373 Workflow target = (Workflow) document.getRootElement().getObject();
374
375 if (target == null)
376 throw new InstantiationException("Can not instantiate edge for document with null root object.");
377
378 Edge edge = Factory.newEdge();
379 edge.setExpression(e.getAttributeValue("edgeExpression"));
380 edge.setPlace(target.getPlace(e.getAttributeValue("placeID")));
381 return edge;
382 }
383 }