1 package org.jboss.jbpm.bar; 2 3 import java.io.File ; 4 import java.net.URL ; 5 import java.util.Hashtable ; 6 import java.util.Iterator ; 7 import java.util.Map ; 8 9 import javax.management.MBeanServer ; 10 import javax.management.MalformedObjectNameException ; 11 import javax.management.ObjectName ; 12 import javax.naming.Context ; 13 import javax.naming.InitialContext ; 14 import javax.naming.NamingException ; 15 16 import org.jboss.deployment.DeploymentException; 17 import org.jboss.deployment.DeploymentInfo; 18 import org.jboss.deployment.SubDeployerSupport; 19 import org.xml.sax.InputSource ; 20 21 import org.jbpm.bpel.bar.BarApplication; 22 import org.jbpm.bpel.def.BpelDefinition; 23 import org.jbpm.bpel.messager.MessagerService; 24 import org.jbpm.bpel.par.JndiProcessDeployer; 25 import org.jbpm.bpel.xml.BarDescriptorReader; 26 import org.jbpm.bpel.xml.ProblemCollector; 27 28 33 public class BARDeployer extends SubDeployerSupport implements BARDeployerMBean { 34 35 36 private Map deploymentsMap = new Hashtable (); 37 38 public static final String DESCRIPTOR_NAME = "META-INF/bpel-application.xml"; 39 40 private static final Object MESSAGER_KEY = "messager"; 41 42 47 public BARDeployer() { 48 setSuffixes(new String [] { "bar" }); 49 setRelativeOrder(RELATIVE_ORDER_400); 50 } 51 52 55 public Iterator getLaunchedProcesses() { 56 return deploymentsMap.values().iterator(); 57 } 58 59 protected ObjectName getObjectName(MBeanServer server, ObjectName name) throws MalformedObjectNameException { 60 return name == null ? OBJECT_NAME : name; 61 } 62 63 protected void stopService() throws Exception { 64 for (Iterator processLaunchIt = deploymentsMap.values().iterator(); processLaunchIt.hasNext();) { 66 try { 67 stop((DeploymentInfo) processLaunchIt.next()); 68 } 69 catch (DeploymentException e) { 70 log.warn(e); 71 } 72 } 73 super.stopService(); 75 } 76 77 protected void destroyService() throws Exception { 78 DeploymentInfo[] processLaunches = (DeploymentInfo[]) deploymentsMap.values().toArray( 80 new DeploymentInfo[deploymentsMap.size()]); 81 for (int i = 0; i < processLaunches.length; i++) { 82 try { 83 destroy(processLaunches[i]); 84 } 85 catch (DeploymentException e) { 86 log.warn(e); 87 } 88 } 89 super.destroyService(); 90 } 91 92 public boolean accepts(DeploymentInfo di) { 93 boolean accepts = true; 94 String urlStr = di.url.getFile(); 96 if (!urlStr.endsWith("bar") && !urlStr.endsWith("bar/")) { 97 accepts = false; 98 } 99 URL dd = di.localCl.findResource(DESCRIPTOR_NAME); 101 if (dd == null) { 102 accepts = false; 103 } 104 log.debug("accepts> url=" + di.url + ", accepted=" + accepts); 105 return accepts; 106 } 107 108 public void init(DeploymentInfo di) throws DeploymentException { 109 try { 110 if ("file".equalsIgnoreCase(di.url.getProtocol())) { 111 File file = new File (di.url.getFile()); 112 if (!file.isDirectory()) { 113 di.watch = di.url; 115 } 116 else { 117 di.watch = new URL (di.url, DESCRIPTOR_NAME); 119 } 120 } 121 else { 122 di.watch = di.url; 124 } 125 } 126 catch (Exception e) { 127 throw new DeploymentException("problem initializing module: " + di.shortName, e); 128 } 129 super.init(di); 131 } 132 133 public void create(DeploymentInfo di) throws DeploymentException { 134 Context initialContext = null; 135 136 try { 137 String descriptorURL = di.localCl.getResource(DESCRIPTOR_NAME).toString(); 139 BarApplication application = new BarApplication(); 140 BarDescriptorReader reader = BarDescriptorReader.getInstance(); 141 ProblemCollector readerProblems = new ProblemCollector(descriptorURL); 142 reader.setProblemHandler(readerProblems); 143 reader.read(application, new InputSource (descriptorURL)); 144 if(readerProblems.hasErrors()) throw new Exception ("Problems found while reading descriptor"); 145 146 initialContext = new InitialContext (); 148 JndiProcessDeployer deployer = new JndiProcessDeployer(initialContext); 149 BpelDefinition process = (BpelDefinition) deployer.findProcessDefinition(application.getName()); 150 MessagerService messager = MessagerService.buildMessagerService(application, process, initialContext); 152 di.context.put(MESSAGER_KEY, messager); 154 deploymentsMap.put(di.url, di); 156 } 157 catch (Exception e) { 158 destroy(di); 159 throw new DeploymentException("problem creating module: " + di.shortName, e); 160 } 161 finally { 162 if (initialContext != null) { 163 try { 164 initialContext.close(); 165 } 166 catch (NamingException e) { 167 log.warn("could not close initial context", e); 168 } 169 } 170 } 171 super.create(di); 173 } 174 175 public void start(DeploymentInfo di) throws DeploymentException { 176 try { 178 MessagerService messager = (MessagerService) di.context.get(MESSAGER_KEY); 179 messager.start(); 180 log.info("BPEL application started: " + messager.getProcess().getName()); 181 } 182 catch (Exception e) { 183 stop(di); 184 destroy(di); 185 throw new DeploymentException("problem starting module: " + di.shortName, e); 186 } 187 super.start(di); 189 } 190 191 public void stop(DeploymentInfo di) throws DeploymentException { 192 MessagerService messager = (MessagerService) di.context.get(MESSAGER_KEY); 194 if (messager != null) { 195 try { 196 messager.stop(); 197 log.info("BPEL application stopped: " + messager.getProcess().getName()); 198 } 199 catch (RuntimeException e) { 200 log.error("problem stopping module: " + di.shortName, e); 201 } 202 } 203 super.stop(di); 205 } 206 207 public void destroy(DeploymentInfo di) throws DeploymentException { 208 deploymentsMap.remove(di.url); 210 MessagerService messager = (MessagerService) di.context.get(MESSAGER_KEY); 212 if (messager != null) { 213 Context initialContext = null; 214 try { 215 initialContext = new InitialContext (); 216 messager.destroy(initialContext); 217 } 218 catch(Exception e) { 219 log.error("problem destroying module: " + di.shortName, e); 220 } 221 finally { 222 if (initialContext != null) { 223 try { 224 initialContext.close(); 225 } 226 catch (NamingException e) { 227 log.warn("could not close naming context", e); 228 } 229 } 230 } 231 } 232 super.destroy(di); 234 } 235 } 236
| Popular Tags
|