View Javadoc

1   /*
2    * $Id: BPELElementFactory.java,v 1.1 2004/12/15 14:18:09 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.model.xmltobpel;
10  
11  import java.util.Hashtable;
12  import java.util.Map;
13  
14  import javax.xml.namespace.QName;
15  
16  import bexee.model.StandardAttributes;
17  import bexee.model.activity.Assign;
18  import bexee.model.activity.Invoke;
19  import bexee.model.activity.Receive;
20  import bexee.model.activity.Reply;
21  import bexee.model.activity.Sequence;
22  import bexee.model.activity.Switch;
23  import bexee.model.activity.impl.AssignImpl;
24  import bexee.model.activity.impl.InvokeImpl;
25  import bexee.model.activity.impl.ReceiveImpl;
26  import bexee.model.activity.impl.ReplyImpl;
27  import bexee.model.activity.impl.SequenceImpl;
28  import bexee.model.activity.impl.SwitchImpl;
29  import bexee.model.elements.BpelCase;
30  import bexee.model.elements.Correlation;
31  import bexee.model.elements.CorrelationPattern;
32  import bexee.model.elements.From;
33  import bexee.model.elements.PartnerLink;
34  import bexee.model.elements.To;
35  import bexee.model.elements.Variable;
36  import bexee.model.elements.Variables;
37  import bexee.model.elements.impl.BpelCaseImpl;
38  import bexee.model.elements.impl.CorrelationImpl;
39  import bexee.model.elements.impl.CorrelationPatternImpl;
40  import bexee.model.elements.impl.FromImpl;
41  import bexee.model.elements.impl.PartnerLinkImpl;
42  import bexee.model.elements.impl.RolesImpl;
43  import bexee.model.elements.impl.ToImpl;
44  import bexee.model.elements.impl.VariableImpl;
45  import bexee.model.elements.impl.VariablesImpl;
46  import bexee.model.expression.impl.BooleanExpressionImpl;
47  import bexee.model.process.BPELProcess;
48  import bexee.model.process.Process;
49  import bexee.model.process.impl.ProcessImpl;
50  import bexee.util.BooleanUtils;
51  import bexee.util.StringUtils;
52  
53  /***
54   * This is a factory for the creation of BPEL process elements. The
55   * 
56   * @author Pawel Kowalski
57   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:09 $
58   */
59  public class BPELElementFactory {
60  
61      //***************************************************/
62      // instances accessor
63      //***************************************************/
64  
65      private static Map instances = new Hashtable();
66  
67      /***
68       * Create a new factory instance or if one already exists for this
69       * BPELProcessm, retrieve the existing one.
70       * 
71       * @param process
72       * @return
73       */
74      public static BPELElementFactory getInstance(BPELProcess process) {
75          if (instances.get(process) == null) {
76              BPELElementFactory elementFactory = new BPELElementFactory();
77              instances.put(process, elementFactory);
78          }
79          return (BPELElementFactory) instances.get(process);
80      }
81  
82      //***************************************************/
83      // the element factory
84      //***************************************************/
85  
86      private Map variables;
87  
88      private Map partnerLinks;
89  
90      private Map nameSpaces;
91  
92      //***************************************************/
93      // ctor's
94      //***************************************************/
95  
96      /***
97       * Create a BPELElementFactory object and initialize it.
98       */
99      public BPELElementFactory() {
100         variables = new Hashtable();
101         partnerLinks = new Hashtable();
102         nameSpaces = new Hashtable();
103     }
104 
105     //***************************************************/
106     // create methods
107     //***************************************************/
108 
109     //***************************************************/
110     // create process
111     //***************************************************/
112 
113     /***
114      * Create a Process instance with all possible attributes. Default values
115      * will be set when an attribute is empty or null.
116      * 
117      * @param name
118      * @param targetNamespace
119      * @param queryLanguage
120      * @param expressionLanguage
121      * @param suppressJoinFailure
122      * @param enableInstanceCompensation
123      * @param abstractProcess
124      * @param xmlns
125      * @param nameSpaces
126      * @return
127      */
128     public Process createProcess(String name, String targetNamespace,
129             String queryLanguage, String expressionLanguage,
130             String suppressJoinFailure, String enableInstanceCompensation,
131             String abstractProcess, String xmlns, Map nameSpaces) {
132 
133         Process process = new ProcessImpl(name, targetNamespace, queryLanguage,
134                 expressionLanguage, suppressJoinFailure,
135                 enableInstanceCompensation);
136 
137         if (nameSpaces != null) {
138             this.nameSpaces = nameSpaces;
139         }
140 
141         return process;
142     }
143 
144     //***************************************************/
145     // create process elements
146     //***************************************************/
147 
148     /***
149      * Create a PartnerLink with all possible attributes. Register the created
150      * PartnerLink for retrieval by an Activity using PartnerLinks.
151      * 
152      * @param partnerLinkName
153      * @param partnerLinkType
154      * @param myRole
155      * @param partnerRole
156      * @return
157      */
158     public PartnerLink createPartnerLink(String partnerLinkName,
159             String partnerLinkType, String myRole, String partnerRole) {
160 
161         PartnerLink partnerLink = new PartnerLinkImpl();
162 
163         partnerLink.setName(partnerLinkName);
164         partnerLink.setPartnerLinkType(expandToQName(partnerLinkType));
165         partnerLink.setMyRole(myRole);
166         partnerLink.setPartnerRole(partnerRole);
167 
168         partnerLinks.put(partnerLinkName, partnerLink);
169 
170         return partnerLink;
171     }
172 
173     /***
174      * Describe <code>createVariables</code> method here.
175      * 
176      * @return
177      */
178     public Variables createVariables() {
179         return new VariablesImpl();
180     }
181 
182     /***
183      * Create a Variable with all possible attributes. Register the created
184      * Variable for retrieval by an invoke or receive element.
185      * 
186      * @param name
187      * @param messageType
188      * @param type
189      * @param element
190      * @return a <code>Variable</code> value
191      */
192     public Variable createVariable(String name, String messageType,
193             String type, String element) {
194 
195         Variable variable = new VariableImpl();
196 
197         variable.setName(name);
198         variable.setMessageType(expandToQName(messageType));
199         variable.setType(expandToQName(type));
200         variable.setElement(expandToQName(element));
201 
202         variables.put(name, variable);
203 
204         return variable;
205     }
206 
207     /***
208      * Create a Correlation with all parameters. If the pattern parameter is
209      * null or empty, no CorrelationPattern object will be set.
210      * 
211      * @param set
212      * @param initiate
213      * @param pattern
214      * @return
215      */
216     public Object createCorrelation(String set, String initiate, String pattern) {
217         Correlation correlation = new CorrelationImpl();
218         correlation.setSet(set);
219         correlation.setInitiate(BooleanUtils.yesNoToBoolean(initiate));
220         if (StringUtils.isNullOrEmpty(pattern)) {
221             correlation.setPattern(createCorrelationPattern(pattern));
222         }
223         return correlation;
224     }
225 
226     /***
227      * Create a CorrelationPattern with a patternString.
228      * 
229      * @param patternString
230      *            a <code>String</code> value
231      * @return a CorrelationPattern object
232      */
233     public CorrelationPattern createCorrelationPattern(String patternString) {
234         CorrelationPattern pattern = new CorrelationPatternImpl();
235         pattern.setCorrelationString(patternString);
236         return pattern;
237     }
238 
239     //***************************************************/
240     // create activities
241     //***************************************************/
242 
243     /***
244      * Describe <code>createAssign</code> method here.
245      * 
246      * @param standardAttributes
247      * @return assign BPEL activity
248      */
249     public Assign createAssign(StandardAttributes standardAttributes) {
250         return new AssignImpl(standardAttributes);
251     }
252 
253     /***
254      * Create a new Sequence BPEL activity.
255      * 
256      * @param standardAttributes
257      *            a <code>StandardAttributes</code> value
258      * @return Sequence activity
259      */
260     public Sequence createSequence(StandardAttributes standardAttributes) {
261         return new SequenceImpl(standardAttributes);
262     }
263 
264     /***
265      * Create a Receive Activity with all possible attributes. A Receive created
266      * this way will be linked with an already defined existing Variables.
267      * 
268      * @param standardAttributes
269      *            a <code>StandardAttributes</code> value
270      * @param partnerLinkName
271      *            a <code>String</code> value
272      * @param portTypeString
273      *            a <code>String</code> value
274      * @param operation
275      * @param variableName
276      *            a <code>String</code> value
277      * @param createInstance
278      * @return Receive activity
279      */
280     public Receive createReceive(StandardAttributes standardAttributes,
281             String partnerLinkName, String portTypeString, String operation,
282             String variableName, String createInstance) {
283 
284         PartnerLink partnerLink = retrievePartnerLink(partnerLinkName);
285         QName portType = expandToQName(portTypeString);
286         Variable variable = retrieveVariable(variableName);
287 
288         Receive receive = new ReceiveImpl(standardAttributes, partnerLink,
289                 portType, operation, variable, createInstance);
290 
291         return receive;
292     }
293 
294     /***
295      * Create an Invoke Activity with all possible attributes. An Invoke created
296      * this way will be linked with already defined and existing Variables and
297      * PartnerLinks.
298      * 
299      * @param standardAttributes
300      *            a <code>StandardAttributes</code> value
301      * @param partnerLinkName
302      *            a <code>String</code> value
303      * @param portTypeString
304      *            a <code>String</code> value
305      * @param operation
306      * @param inVariableName
307      *            a <code>String</code> value
308      * @param outVariableName
309      *            a <code>String</code> value
310      * @return
311      */
312     public Object createInvoke(StandardAttributes standardAttributes,
313             String partnerLinkName, String portTypeString, String operation,
314             String inVariableName, String outVariableName) {
315 
316         PartnerLink partnerLink = retrievePartnerLink(partnerLinkName);
317         QName portType = expandToQName(portTypeString);
318         Variable inVariable = retrieveVariable(inVariableName);
319         Variable outVariable = retrieveVariable(outVariableName);
320 
321         Invoke invoke = new InvokeImpl(standardAttributes, partnerLink,
322                 portType, operation, inVariable, outVariable);
323 
324         return invoke;
325     }
326 
327     /***
328      * Describe <code>createReply</code> method here.
329      * 
330      * @param attributes
331      * @param partnerLink
332      * @param portType
333      * @param operation
334      * @param variable
335      * @param faultName
336      * @return
337      */
338     public Reply createReply(StandardAttributes attributes, String partnerLink,
339             String portType, String operation, String variable, String faultName) {
340 
341         Reply reply = new ReplyImpl(attributes);
342 
343         reply.setPartnerLink(retrievePartnerLink(partnerLink));
344         reply.setPortType(expandToQName(portType));
345         reply.setOperation(operation);
346         reply.setVariable(retrieveVariable(variable));
347         reply.setFaultName(expandToQName(faultName));
348 
349         return reply;
350     }
351 
352     /***
353      * Describe <code>createSwitch</code> method here.
354      * 
355      * @param standardAttributes
356      * @return
357      */
358     public Object createSwitch(StandardAttributes standardAttributes) {
359         Switch bpelSwitch = new SwitchImpl(standardAttributes);
360         return bpelSwitch;
361     }
362 
363     //***************************************************/
364     // create elements
365     //***************************************************/
366 
367     /***
368      * Create a From with all possible attributes. Variables and PartnerLinks
369      * will not be created but retrieved from the enclosing process instead.
370      * 
371      * @param variable
372      * @param part
373      * @param property
374      * @param partnerLink
375      * @param endpointReference
376      * @param expression
377      * @param opaque
378      * @return
379      */
380     public From createFrom(String variable, String part, String property,
381             String partnerLink, String endpointReference, String expression,
382             String opaque) {
383 
384         From from = new FromImpl();
385 
386         from.setVariable(retrieveVariable(variable));
387         from.setPart(part);
388         from.setProperty(expandToQName(property));
389         from.setPartnerLink(retrievePartnerLink(partnerLink));
390         from.setEndpointReference(new RolesImpl(endpointReference));
391         from.setExpression(expression);
392         from.setOpaque(BooleanUtils.yesNoToBoolean(opaque));
393 
394         return from;
395     }
396 
397     /***
398      * Create a To with all possible attributes. Variables and PartnerLinks will
399      * not be created but retrieved from the enclosing process instead.
400      * 
401      * @param variable
402      * @param part
403      * @param property
404      * @param partnerLink
405      * @return
406      */
407     public To createTo(String variable, String part, String property,
408             String partnerLink) {
409 
410         To to = new ToImpl();
411 
412         to.setVariable(retrieveVariable(variable));
413         to.setPart(part);
414         to.setProperty(expandToQName(property));
415         to.setPartnerLink(retrievePartnerLink(partnerLink));
416 
417         return to;
418     }
419 
420     /***
421      * Create a new BpelCase for the use within a Switch activity. Create this
422      * BpelCase with a boolean condition used for selecting the BpelCase among
423      * all cases within a Switch.
424      * 
425      * @param condition
426      * @return
427      */
428     public BpelCase createBpelCase(String condition) {
429         BpelCase bpelCase = new BpelCaseImpl();
430         bpelCase.setBooleanExpression(new BooleanExpressionImpl(condition));
431         return bpelCase;
432     }
433 
434     //***************************************************/
435     // retrieve methods
436     //***************************************************/
437 
438     /***
439      * Retrieve an already created PartnerLink for an Activity using
440      * partnerLinks. If the partnerLinkName is null, empty or a PartnerLink with
441      * this name has not been created, this method will return a null value.
442      * 
443      * @param partnerLinkName
444      *            the name of the PartnerLink to be retrieved.
445      * @return a PartnerLink or null.
446      */
447     public PartnerLink retrievePartnerLink(String partnerLinkName) {
448         if (StringUtils.isNullOrEmpty(partnerLinkName)) {
449             return null;
450         }
451         return (PartnerLink) partnerLinks.get(partnerLinkName);
452     }
453 
454     /***
455      * Retrieve an already created Variable for an Activity using variables. If
456      * the variableName parameter is null, empty or the Variable has not been
457      * created, this method will return null value.
458      * 
459      * @param variableName
460      *            the name of the Variable to be retrieved.
461      * @return a Variable or null if the variableName is null, empty or a
462      *         Variable with this name has not yet been created.
463      */
464     public Variable retrieveVariable(String variableName) {
465         if (StringUtils.isNullOrEmpty(variableName)) {
466             return null;
467         }
468         return (Variable) variables.get(variableName);
469     }
470 
471     //***************************************************/
472     // helper methods
473     //***************************************************/
474 
475     /***
476      * @param qName
477      * @return
478      */
479     private QName expandToQName(String qName) {
480         String[] parts = StringUtils.split(qName);
481         String namespaceUri;
482         if (nameSpaces.get(parts[0]) == null) {
483             namespaceUri = "";
484         } else {
485             namespaceUri = (String) nameSpaces.get(parts[0]);
486         }
487         return new QName(namespaceUri, parts[1]);
488     }
489 
490 }