KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jbpm > bar > BARDeployer


1 package org.jboss.jbpm.bar;
2
3 import java.io.File JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import javax.management.MBeanServer JavaDoc;
10 import javax.management.MalformedObjectNameException JavaDoc;
11 import javax.management.ObjectName JavaDoc;
12 import javax.naming.Context JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import javax.naming.NamingException JavaDoc;
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 JavaDoc;
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 /**
29  * @author Juan Cantu
30  * @author Alejandro Guízar
31  * @version $Revision: 1.7 $ $Date: 2005/06/23 20:45:05 $
32  */

33 public class BARDeployer extends SubDeployerSupport implements BARDeployerMBean {
34   
35   /** A map<URL, DeploymentInfo> of current deployments. */
36   private Map JavaDoc deploymentsMap = new Hashtable JavaDoc();
37   
38   public static final String JavaDoc DESCRIPTOR_NAME = "META-INF/bpel-application.xml";
39   
40   private static final Object JavaDoc MESSAGER_KEY = "messager";
41   
42   /**
43    * Default CTOR used to set default values to the Suffixes and RelativeOrder
44    * attributes. Those are read at subdeployer registration time by the MainDeployer
45    * to alter its SuffixOrder.
46    */

47   public BARDeployer() {
48     setSuffixes(new String JavaDoc[] { "bar" });
49     setRelativeOrder(RELATIVE_ORDER_400);
50   }
51   
52   /**
53    * Returns the deployed applications.
54    */

55   public Iterator JavaDoc getLaunchedProcesses() {
56     return deploymentsMap.values().iterator();
57   }
58   
59   protected ObjectName JavaDoc getObjectName(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws MalformedObjectNameException JavaDoc {
60     return name == null ? OBJECT_NAME : name;
61   }
62   
63   protected void stopService() throws Exception JavaDoc {
64     // stop all launches
65
for (Iterator JavaDoc 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     // deregister with MainDeployer
74
super.stopService();
75   }
76   
77   protected void destroyService() throws Exception JavaDoc {
78     // destroy all launches
79
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     // the deployment's root name must end with bar
95
String JavaDoc urlStr = di.url.getFile();
96     if (!urlStr.endsWith("bar") && !urlStr.endsWith("bar/")) {
97       accepts = false;
98     }
99     // it must also contain our deployment descriptor
100
URL JavaDoc 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 JavaDoc file = new File JavaDoc(di.url.getFile());
112         if (!file.isDirectory()) {
113           // if not directory we watch the package
114
di.watch = di.url;
115         }
116         else {
117           // if directory we watch the xml files
118
di.watch = new URL JavaDoc(di.url, DESCRIPTOR_NAME);
119         }
120       }
121       else {
122         // watch the top only, no directory support
123
di.watch = di.url;
124       }
125     }
126     catch (Exception JavaDoc e) {
127       throw new DeploymentException("problem initializing module: " + di.shortName, e);
128     }
129     // invoke super-class initialization
130
super.init(di);
131   }
132   
133   public void create(DeploymentInfo di) throws DeploymentException {
134     Context JavaDoc initialContext = null;
135     
136     try {
137       // read application descriptor
138
String JavaDoc 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 JavaDoc(descriptorURL));
144       if(readerProblems.hasErrors()) throw new Exception JavaDoc("Problems found while reading descriptor");
145       
146       // find the process definition specified by the configuration
147
initialContext = new InitialContext JavaDoc();
148       JndiProcessDeployer deployer = new JndiProcessDeployer(initialContext);
149       BpelDefinition process = (BpelDefinition) deployer.findProcessDefinition(application.getName());
150       // build messager service
151
MessagerService messager = MessagerService.buildMessagerService(application, process, initialContext);
152       // save the messager service in the context of the deployment
153
di.context.put(MESSAGER_KEY, messager);
154       // register launched process
155
deploymentsMap.put(di.url, di);
156     }
157     catch (Exception JavaDoc 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 JavaDoc e) {
167           log.warn("could not close initial context", e);
168         }
169       }
170     }
171     // generate a JMX notification for the create stage
172
super.create(di);
173   }
174   
175   public void start(DeploymentInfo di) throws DeploymentException {
176     // start receiving requests
177
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 JavaDoc e) {
183       stop(di);
184       destroy(di);
185       throw new DeploymentException("problem starting module: " + di.shortName, e);
186     }
187     // generate a JMX notification for the start stage
188
super.start(di);
189   }
190   
191   public void stop(DeploymentInfo di) throws DeploymentException {
192     // stop receiving requests
193
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 JavaDoc e) {
200         log.error("problem stopping module: " + di.shortName, e);
201       }
202     }
203     // generate a JMX notification for the stop stage
204
super.stop(di);
205   }
206   
207   public void destroy(DeploymentInfo di) throws DeploymentException {
208     // remove process
209
deploymentsMap.remove(di.url);
210     // destroy messager service
211
MessagerService messager = (MessagerService) di.context.get(MESSAGER_KEY);
212     if (messager != null) {
213       Context JavaDoc initialContext = null;
214       try {
215         initialContext = new InitialContext JavaDoc();
216         messager.destroy(initialContext);
217       }
218       catch(Exception JavaDoc 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 JavaDoc e) {
227             log.warn("could not close naming context", e);
228           }
229         }
230       }
231     }
232     // generate a JMX notification for the destroy stage
233
super.destroy(di);
234   }
235 }
236
Popular Tags