KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > par > JndiProcessDeployer


1 package org.jbpm.bpel.par;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.zip.ZipInputStream JavaDoc;
8
9 import javax.naming.Context JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.naming.NameNotFoundException JavaDoc;
12 import javax.naming.NamingException JavaDoc;
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 /**
25  * deploys process archives.
26  */

27 public class JndiProcessDeployer {
28   
29   private Context JavaDoc deployContext;
30   
31   public static final String JavaDoc DEPLOY_CONTEXT_PROP = "jbpm.deploy.jndi.context";
32   public static final String JavaDoc PROCESS_DEFINITION_NAME = "processDefinition";
33   
34   private static final Log log = LogFactory.getLog(JndiProcessDeployer.class);
35   
36   public JndiProcessDeployer(Context JavaDoc namingContext) {
37     try {
38       String JavaDoc contextName = JbpmConfiguration.getString(DEPLOY_CONTEXT_PROP);
39       try {
40         deployContext = (Context JavaDoc) namingContext.lookup(contextName);
41       }
42       catch (NameNotFoundException JavaDoc e) {
43         // the deployment context does not exist, create it
44
deployContext = namingContext.createSubcontext(contextName);
45       }
46     }
47     catch (NamingException JavaDoc e) {
48       throw new RuntimeException JavaDoc("could not get deployment context", e);
49     }
50   }
51
52   public void deployProcessArchive(ProcessArchive archive) {
53     ProcessDefinition definition = archive.parseProcessDefinition();
54     // check for problems reading the process archive
55
List JavaDoc problems = archive.getProblems();
56     if (problems.isEmpty()) {
57       // no problems, proceed to deploy definition
58
deployProcessDefinition(definition);
59     }
60   }
61   
62   public void deployProcessDefinition(ProcessDefinition definition) {
63     String JavaDoc name = definition.getName();
64     if ( name == null ) {
65       throw new RuntimeException JavaDoc("process definition must have a name");
66     }
67     try {
68       Context JavaDoc processContext = getOrCreateProcessContext(name);
69       // bind process to the deployment context
70
processContext.bind(PROCESS_DEFINITION_NAME, definition);
71       log.info("process definition deployed: " + name);
72     }
73     catch (NamingException JavaDoc e) {
74       throw new RuntimeException JavaDoc("could not deploy process definition: " + name, e);
75     }
76   }
77   
78   public void undeployProcessDefinition(String JavaDoc processName) {
79     try {
80       Context JavaDoc processContext = getProcessContext(processName);
81       processContext.unbind(PROCESS_DEFINITION_NAME);
82       log.info("process definition undeployed: " + processName);
83     }
84     catch (NamingException JavaDoc e) {
85       throw new RuntimeException JavaDoc("could not undeploy process definition: " + processName, e);
86     }
87   }
88   
89   public ProcessDefinition findProcessDefinition(String JavaDoc processName) {
90     try {
91       Context JavaDoc processContext = getProcessContext(processName);
92       return (ProcessDefinition) processContext.lookup(PROCESS_DEFINITION_NAME);
93     }
94     catch (NamingException JavaDoc e) {
95       throw new RuntimeException JavaDoc("could not find process definition: " + processName, e);
96     }
97   }
98   
99   public void deployEndpointInfo(String JavaDoc processName, String JavaDoc partnerLinkName, BpelEndpointInfo endpointInfo) {
100     try {
101       Context JavaDoc processContext = getOrCreateProcessContext(processName);
102       processContext.bind(partnerLinkName, endpointInfo);
103     }
104     catch (NamingException JavaDoc e) {
105       throw new RuntimeException JavaDoc("could not deploy endpoint info: " + partnerLinkName, e);
106     }
107   }
108   
109   public void undeployEndpointInfo(String JavaDoc processName, String JavaDoc partnerLinkName) {
110     try {
111       Context JavaDoc processContext = getProcessContext(processName);
112       processContext.unbind(partnerLinkName);
113     }
114     catch (NamingException JavaDoc e) {
115       throw new RuntimeException JavaDoc("could not undeploy endpoint info: " + partnerLinkName, e);
116     }
117   }
118   
119   public BpelEndpointInfo findEndpointInfo(String JavaDoc processName, String JavaDoc partnerLinkName) {
120     try {
121       Context JavaDoc processContext = getProcessContext(processName);
122       return (BpelEndpointInfo) processContext.lookup(partnerLinkName);
123     }
124     catch (NamingException JavaDoc e) {
125       throw new RuntimeException JavaDoc("could not find endpoint info: " + partnerLinkName, e);
126     }
127   }
128   
129   private Context JavaDoc getProcessContext(String JavaDoc processName) throws NamingException JavaDoc {
130     return (Context JavaDoc) deployContext.lookup(processName);
131   }
132   
133   private Context JavaDoc getOrCreateProcessContext(String JavaDoc processName) throws NamingException JavaDoc {
134     Context JavaDoc processContext;
135     try {
136       processContext = getProcessContext(processName);
137     }
138     catch (NameNotFoundException JavaDoc e) {
139       // the process context does not exist, create it
140
processContext = deployContext.createSubcontext(processName);
141     }
142     return processContext;
143   }
144   
145   public static void main(String JavaDoc[] 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 JavaDoc initialContext = null;
153     try {
154       initialContext = new InitialContext JavaDoc();
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 JavaDoc e) {
164       log.error("deployment failed", e);
165     }
166     finally {
167       if (initialContext != null) {
168         try {
169           initialContext.close();
170         }
171         catch (NamingException JavaDoc e) {
172           log.warn("could not close naming context", e);
173         }
174       }
175     }
176   }
177   
178   private static void deployProcessArchives(JndiProcessDeployer deployer, String JavaDoc[] fileNames) {
179     for (int i = 0, n = fileNames.length; i < n; i++) {
180       String JavaDoc fileName = fileNames[i];
181       ZipInputStream JavaDoc stream = null;
182       try {
183         stream = new ZipInputStream JavaDoc(new FileInputStream JavaDoc(fileName));
184         deployer.deployProcessArchive(new ProcessArchive(stream));
185       }
186       catch (IOException JavaDoc 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 JavaDoc e) {
195             log.warn("could not close file: " + fileName, e);
196           }
197         }
198       }
199     }
200   }
201   
202   private static void undeployProcessDefinitions(JndiProcessDeployer deployer, String JavaDoc[] processNames) {
203     for (int i = 1, n = processNames.length; i < n; i++) {
204       String JavaDoc processName = processNames[i];
205       try {
206         deployer.undeployProcessDefinition(processName);
207       }
208       catch (RuntimeException JavaDoc e) {
209         log.error("could not undeploy process definition: " + processName, e);
210       }
211     }
212   }
213 }
214
Popular Tags