1 package org.jbpm.bpel.messager; 2 3 import java.util.Collections ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 8 import javax.jms.ConnectionFactory ; 9 import javax.jms.Destination ; 10 import javax.naming.Context ; 11 import javax.naming.NamingException ; 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 32 class MessagerServiceBuilder extends BpelVisitorSupport { 33 34 private final Context namingContext; 35 private final JndiProcessDeployer deployer; 36 37 private String processName; 38 private String connectionFactoryName; 39 private Map scopeConfigurations; 40 41 private Map partnerLinkInfos; 42 43 private static final Log log = LogFactory.getLog(MessagerServiceBuilder.class); 44 45 MessagerServiceBuilder(Context 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 ConnectionFactory connectionFactory = (ConnectionFactory ) namingContext.lookup(connectionFactoryName); 56 log.debug("retrieved jms connection factory: " + connectionFactoryName); 57 scopeConfigurations = new ScopeMatcher().match(process, application); 59 partnerLinkInfos = new HashMap (); 61 visit(process); 62 return new MessagerService(process, connectionFactory, partnerLinkInfos); 64 } 65 catch (NamingException e) { 66 log.error(e); 67 throw new RuntimeException ("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 scopeDestinationName; 77 Map barPartnerLinks; 78 BarScope barScope = (BarScope) scopeConfigurations.get(scope); 80 if (barScope != null) { 81 barPartnerLinks = barScope.getPartnerLinks(); 83 scopeDestinationName = barScope.getDestinationName(); 85 if (scopeDestinationName == null) { 86 scopeDestinationName = findDestinationName(getParent(scope)); 88 } 89 } 90 else { 91 barPartnerLinks = Collections.EMPTY_MAP; 93 scopeDestinationName = findDestinationName(getParent(scope)); 95 } 96 Iterator partnerLinkIt = scope.getPartnerLinks().iterator(); 97 while (partnerLinkIt.hasNext()) { 98 PartnerLinkDefinition partnerLink = (PartnerLinkDefinition) partnerLinkIt.next(); 99 String destinationName = scopeDestinationName; 101 String jndiName = partnerLink.getName(); 103 BarPartnerLink barPartnerLink = 105 (BarPartnerLink) barPartnerLinks.get(partnerLink.getName()); 106 if (barPartnerLink != null) { 107 String destinationOverride = barPartnerLink.getDestinationName(); 109 if (destinationOverride != null) destinationName = destinationOverride; 110 String jndiOverride = barPartnerLink.getNamingContextName(); 112 if (jndiOverride != null) jndiName = jndiOverride; 113 } 114 Destination destination = lookupDestination(destinationName); 116 long partnerLinkId = partnerLink.getId(); 118 bindEndpointInfo(jndiName, partnerLinkId, destinationName); 119 partnerLinkInfos.put(new Long (partnerLinkId), new PartnerLinkInfo(destination, jndiName)); 121 } 122 propagate(scope); 123 } 124 125 protected String findDestinationName(Scope scope) { 126 String 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 lookupDestination(String destinationName) { 137 try { 138 Destination destination = (Destination ) namingContext.lookup(destinationName); 139 log.debug("retrieved jms destination: " + destinationName); 140 return destination; 141 } 142 catch (NamingException e) { 143 log.error(e); 144 throw new RuntimeException ("could not retrieve jms destination", e); 145 } 146 } 147 148 protected void bindEndpointInfo(String endpointInfoName, long partnerLinkId, String destinationName) { 149 BpelEndpointInfo endpointInfo = new BpelEndpointInfo(); 150 endpointInfo.setPartnerLinkId(partnerLinkId); 152 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 |