1 package org.jbpm.bpel.par; 2 3 import java.io.FileInputStream ; 4 import java.io.IOException ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.zip.ZipInputStream ; 8 9 import javax.naming.Context ; 10 import javax.naming.InitialContext ; 11 import javax.naming.NameNotFoundException ; 12 import javax.naming.NamingException ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 import org.jbpm.JbpmConfiguration; 18 import org.jbpm.graph.def.ProcessDefinition; 19 import org.jbpm.jpdl.par.ProcessArchive; 20 import org.jbpm.jpdl.xml.Problem; 21 22 import org.jbpm.bpel.service.def.BpelEndpointInfo; 23 24 27 public class JndiProcessDeployer { 28 29 private Context deployContext; 30 31 public static final String DEPLOY_CONTEXT_PROP = "jbpm.deploy.jndi.context"; 32 public static final String PROCESS_DEFINITION_NAME = "processDefinition"; 33 34 private static final Log log = LogFactory.getLog(JndiProcessDeployer.class); 35 36 public JndiProcessDeployer(Context namingContext) { 37 try { 38 String contextName = JbpmConfiguration.getString(DEPLOY_CONTEXT_PROP); 39 try { 40 deployContext = (Context ) namingContext.lookup(contextName); 41 } 42 catch (NameNotFoundException e) { 43 deployContext = namingContext.createSubcontext(contextName); 45 } 46 } 47 catch (NamingException e) { 48 throw new RuntimeException ("could not get deployment context", e); 49 } 50 } 51 52 public void deployProcessArchive(ProcessArchive archive) { 53 ProcessDefinition definition = archive.parseProcessDefinition(); 54 List problems = archive.getProblems(); 56 if (problems.isEmpty()) { 57 deployProcessDefinition(definition); 59 } 60 } 61 62 public void deployProcessDefinition(ProcessDefinition definition) { 63 String name = definition.getName(); 64 if ( name == null ) { 65 throw new RuntimeException ("process definition must have a name"); 66 } 67 try { 68 Context processContext = getOrCreateProcessContext(name); 69 processContext.bind(PROCESS_DEFINITION_NAME, definition); 71 log.info("process definition deployed: " + name); 72 } 73 catch (NamingException e) { 74 throw new RuntimeException ("could not deploy process definition: " + name, e); 75 } 76 } 77 78 public void undeployProcessDefinition(String processName) { 79 try { 80 Context processContext = getProcessContext(processName); 81 processContext.unbind(PROCESS_DEFINITION_NAME); 82 log.info("process definition undeployed: " + processName); 83 } 84 catch (NamingException e) { 85 throw new RuntimeException ("could not undeploy process definition: " + processName, e); 86 } 87 } 88 89 public ProcessDefinition findProcessDefinition(String processName) { 90 try { 91 Context processContext = getProcessContext(processName); 92 return (ProcessDefinition) processContext.lookup(PROCESS_DEFINITION_NAME); 93 } 94 catch (NamingException e) { 95 throw new RuntimeException ("could not find process definition: " + processName, e); 96 } 97 } 98 99 public void deployEndpointInfo(String processName, String partnerLinkName, BpelEndpointInfo endpointInfo) { 100 try { 101 Context processContext = getOrCreateProcessContext(processName); 102 processContext.bind(partnerLinkName, endpointInfo); 103 } 104 catch (NamingException e) { 105 throw new RuntimeException ("could not deploy endpoint info: " + partnerLinkName, e); 106 } 107 } 108 109 public void undeployEndpointInfo(String processName, String partnerLinkName) { 110 try { 111 Context processContext = getProcessContext(processName); 112 processContext.unbind(partnerLinkName); 113 } 114 catch (NamingException e) { 115 throw new RuntimeException ("could not undeploy endpoint info: " + partnerLinkName, e); 116 } 117 } 118 119 public BpelEndpointInfo findEndpointInfo(String processName, String partnerLinkName) { 120 try { 121 Context processContext = getProcessContext(processName); 122 return (BpelEndpointInfo) processContext.lookup(partnerLinkName); 123 } 124 catch (NamingException e) { 125 throw new RuntimeException ("could not find endpoint info: " + partnerLinkName, e); 126 } 127 } 128 129 private Context getProcessContext(String processName) throws NamingException { 130 return (Context ) deployContext.lookup(processName); 131 } 132 133 private Context getOrCreateProcessContext(String processName) throws NamingException { 134 Context processContext; 135 try { 136 processContext = getProcessContext(processName); 137 } 138 catch (NameNotFoundException e) { 139 processContext = deployContext.createSubcontext(processName); 141 } 142 return processContext; 143 } 144 145 public static void main(String [] args) { 146 int argCount = args.length; 147 if (argCount == 0) { 148 log.info("usage: JndiProcessDeployer filenames (to deploy archives)"); 149 log.info(" or JndiProcessDeployer -undeploy processnames (to undeploy definitions)"); 150 return; 151 } 152 Context initialContext = null; 153 try { 154 initialContext = new InitialContext (); 155 JndiProcessDeployer deployer = new JndiProcessDeployer(initialContext); 156 if ("-undeploy".equals(args[0])) { 157 undeployProcessDefinitions(deployer, args); 158 } 159 else { 160 deployProcessArchives(deployer, args); 161 } 162 } 163 catch (Exception e) { 164 log.error("deployment failed", e); 165 } 166 finally { 167 if (initialContext != null) { 168 try { 169 initialContext.close(); 170 } 171 catch (NamingException e) { 172 log.warn("could not close naming context", e); 173 } 174 } 175 } 176 } 177 178 private static void deployProcessArchives(JndiProcessDeployer deployer, String [] fileNames) { 179 for (int i = 0, n = fileNames.length; i < n; i++) { 180 String fileName = fileNames[i]; 181 ZipInputStream stream = null; 182 try { 183 stream = new ZipInputStream (new FileInputStream (fileName)); 184 deployer.deployProcessArchive(new ProcessArchive(stream)); 185 } 186 catch (IOException e) { 187 log.error("could not read file: " + fileName, e); 188 } 189 finally { 190 if (stream != null) { 191 try { 192 stream.close(); 193 } 194 catch (IOException e) { 195 log.warn("could not close file: " + fileName, e); 196 } 197 } 198 } 199 } 200 } 201 202 private static void undeployProcessDefinitions(JndiProcessDeployer deployer, String [] processNames) { 203 for (int i = 1, n = processNames.length; i < n; i++) { 204 String processName = processNames[i]; 205 try { 206 deployer.undeployProcessDefinition(processName); 207 } 208 catch (RuntimeException e) { 209 log.error("could not undeploy process definition: " + processName, e); 210 } 211 } 212 } 213 } 214 | Popular Tags |