KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > messager > MessagerServiceBuilder


1 package org.jbpm.bpel.messager;
2
3 import java.util.Collections JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import javax.jms.ConnectionFactory JavaDoc;
9 import javax.jms.Destination JavaDoc;
10 import javax.naming.Context JavaDoc;
11 import javax.naming.NamingException JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import org.jbpm.bpel.bar.BarApplication;
17 import org.jbpm.bpel.bar.BarPartnerLink;
18 import org.jbpm.bpel.bar.BarScope;
19 import org.jbpm.bpel.bar.ScopeMatcher;
20 import org.jbpm.bpel.def.BpelDefinition;
21 import org.jbpm.bpel.def.BpelVisitorSupport;
22 import org.jbpm.bpel.def.CompositeActivity;
23 import org.jbpm.bpel.def.Scope;
24 import org.jbpm.bpel.par.JndiProcessDeployer;
25 import org.jbpm.bpel.service.def.BpelEndpointInfo;
26 import org.jbpm.bpel.service.def.PartnerLinkDefinition;
27
28 /**
29  * @author Alejandro Guízar
30  * @version $Revision: 1.5 $ $Date: 2005/06/06 14:42:28 $
31  */

32 class MessagerServiceBuilder extends BpelVisitorSupport {
33   
34   private final Context JavaDoc namingContext;
35   private final JndiProcessDeployer deployer;
36   
37   private String JavaDoc processName;
38   private String JavaDoc connectionFactoryName;
39   private Map JavaDoc scopeConfigurations;
40   
41   private Map JavaDoc partnerLinkInfos;
42   
43   private static final Log log = LogFactory.getLog(MessagerServiceBuilder.class);
44   
45   MessagerServiceBuilder(Context JavaDoc namingContext) {
46     this.namingContext = namingContext;
47     deployer = new JndiProcessDeployer(namingContext);
48   }
49   
50   public MessagerService buildMessagerService(BarApplication application, BpelDefinition process) {
51     processName = process.getName();
52     connectionFactoryName = application.getConnectionFactoryName();
53     try {
54       // retrieve connection factory
55
ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc) namingContext.lookup(connectionFactoryName);
56       log.debug("retrieved jms connection factory: " + connectionFactoryName);
57       // match scopes with their configurations
58
scopeConfigurations = new ScopeMatcher().match(process, application);
59       // retrieve destinations / bind endpoint infos
60
partnerLinkInfos = new HashMap JavaDoc();
61       visit(process);
62       // create the messager service
63
return new MessagerService(process, connectionFactory, partnerLinkInfos);
64     }
65     catch (NamingException JavaDoc e) {
66       log.error(e);
67       throw new RuntimeException JavaDoc("could not perform naming operation", e);
68     }
69   }
70   
71   public void visit(BpelDefinition process) {
72     visit(process.getScope());
73   }
74   
75   public void visit(Scope scope) {
76     String JavaDoc scopeDestinationName;
77     Map JavaDoc barPartnerLinks;
78     // extract partner link configurations and the default destination name
79
BarScope barScope = (BarScope) scopeConfigurations.get(scope);
80     if (barScope != null) {
81       // take any partner link configurations
82
barPartnerLinks = barScope.getPartnerLinks();
83       // take the destination name, if it exists
84
scopeDestinationName = barScope.getDestinationName();
85       if (scopeDestinationName == null) {
86         // locate a destination name starting with the parent of the current scope
87
scopeDestinationName = findDestinationName(getParent(scope));
88       }
89     }
90     else {
91       // there is no scope configuration, so there are no partner link configurations
92
barPartnerLinks = Collections.EMPTY_MAP;
93       // locate a destination name starting with the parent of the current scope
94
scopeDestinationName = findDestinationName(getParent(scope));
95     }
96     Iterator JavaDoc partnerLinkIt = scope.getPartnerLinks().iterator();
97     while (partnerLinkIt.hasNext()) {
98       PartnerLinkDefinition partnerLink = (PartnerLinkDefinition) partnerLinkIt.next();
99       // the default value for destination name comes from a enclosing scope configuration
100
String JavaDoc destinationName = scopeDestinationName;
101       // the default value for endpoint info name is the partner link name
102
String JavaDoc jndiName = partnerLink.getName();
103       // a partner link configuration may override the defaults
104
BarPartnerLink barPartnerLink =
105         (BarPartnerLink) barPartnerLinks.get(partnerLink.getName());
106       if (barPartnerLink != null) {
107         // override destination name
108
String JavaDoc destinationOverride = barPartnerLink.getDestinationName();
109         if (destinationOverride != null) destinationName = destinationOverride;
110         // override endpoint info name
111
String JavaDoc jndiOverride = barPartnerLink.getNamingContextName();
112         if (jndiOverride != null) jndiName = jndiOverride;
113       }
114       // retrieve destination
115
Destination JavaDoc destination = lookupDestination(destinationName);
116       // bind endpoint info
117
long partnerLinkId = partnerLink.getId();
118       bindEndpointInfo(jndiName, partnerLinkId, destinationName);
119       // register partner link info
120
partnerLinkInfos.put(new Long JavaDoc(partnerLinkId), new PartnerLinkInfo(destination, jndiName));
121     }
122     propagate(scope);
123   }
124   
125   protected String JavaDoc findDestinationName(Scope scope) {
126     String JavaDoc destinationName;
127     for (destinationName = null; scope != null && destinationName == null; scope = getParent(scope)) {
128       BarScope scopeConfig = (BarScope) scopeConfigurations.get(scope);
129       if (scopeConfig != null) {
130         destinationName = scopeConfig.getDestinationName();
131       }
132     }
133     return destinationName;
134   }
135   
136   protected Destination JavaDoc lookupDestination(String JavaDoc destinationName) {
137     try {
138       Destination JavaDoc destination = (Destination JavaDoc) namingContext.lookup(destinationName);
139       log.debug("retrieved jms destination: " + destinationName);
140       return destination;
141     }
142     catch (NamingException JavaDoc e) {
143       log.error(e);
144       throw new RuntimeException JavaDoc("could not retrieve jms destination", e);
145     }
146   }
147   
148   protected void bindEndpointInfo(String JavaDoc endpointInfoName, long partnerLinkId, String JavaDoc destinationName) {
149     BpelEndpointInfo endpointInfo = new BpelEndpointInfo();
150     // partner link id
151
endpointInfo.setPartnerLinkId(partnerLinkId);
152     // jms info
153
endpointInfo.setConnectionFactoryName(connectionFactoryName);
154     endpointInfo.setDestinationName(destinationName);
155     deployer.deployEndpointInfo(processName, endpointInfoName, endpointInfo);
156   }
157   
158   private static Scope getParent(Scope scope) {
159     CompositeActivity cursor = scope.getCompositeActivity();
160     while (cursor != null && !(cursor instanceof Scope)) {
161       cursor = cursor.getCompositeActivity();
162     }
163     return (Scope) cursor;
164   }
165 }
Popular Tags