KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > services > DeploymentManagerService


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.deployment.services;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.BufferedOutputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLConnection JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38
39 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
40 import javax.enterprise.deploy.spi.exceptions.TargetException JavaDoc;
41
42 import org.jboss.deployers.plugins.structure.AbstractDeploymentContext;
43 import org.jboss.deployers.spi.attachments.Attachments;
44 import org.jboss.deployers.spi.deployment.MainDeployer;
45 import org.jboss.deployers.spi.structure.DeploymentContext;
46 import org.jboss.deployers.spi.structure.DeploymentState;
47 import org.jboss.deployment.spi.SerializableTargetModuleID;
48 import org.jboss.logging.Logger;
49 import org.jboss.util.file.Files;
50 import org.jboss.virtual.VFS;
51 import org.jboss.virtual.VirtualFile;
52
53 /**
54  * A service that supports the JSR-88 DeploymentManager operations.
55  *
56  * @author Scott.Stark@jboss.org
57  * @version $Revision: 58240 $
58  */

59 public class DeploymentManagerService
60    implements DeploymentManagerServiceMBean
61 {
62    private static Logger log = Logger.getLogger(DeploymentManagerService.class);
63
64    /**
65     * Map<deployURL, TargetID> of the deployment modules.
66     */

67    private Map JavaDoc<String JavaDoc, SerializableTargetModuleID> moduleMap =
68       Collections.synchronizedMap(new HashMap JavaDoc<String JavaDoc, SerializableTargetModuleID>());
69
70    /**
71     * The type of attachment used to identify a CAR
72     */

73    private Class JavaDoc carDeployerType;
74    /**
75     * The type of attachment used to identify a EAR
76     */

77    private Class JavaDoc earDeployerType;
78    /**
79     * The type of attachment used to identify a EJB
80     */

81    private Class JavaDoc ejbDeployerType;
82    /**
83     * The type of attachment used to identify a RAR
84     */

85    private Class JavaDoc rarDeployerType;
86    /**
87     * The type of attachment used to identify a WAR
88     */

89    private Class JavaDoc warDeployerType;
90    /**
91     * The MainDeployer
92     */

93    private MainDeployer mainDeployer;
94
95    /**
96     * The local directory for uploaded content.
97     */

98    private File JavaDoc uploadDir;
99    private boolean failOnCollision;
100    private boolean deleteOnUndeploy;
101
102    public MainDeployer getMainDeployer()
103    {
104       return mainDeployer;
105    }
106    public void setMainDeployer(MainDeployer mainDeployer)
107    {
108       this.mainDeployer = mainDeployer;
109    }
110
111    public Class JavaDoc getCarDeployerType()
112    {
113       return carDeployerType;
114    }
115    public void setCarDeployerType(Class JavaDoc carDeployerType)
116    {
117       this.carDeployerType = carDeployerType;
118    }
119    public Class JavaDoc getEarDeployerType()
120    {
121       return earDeployerType;
122    }
123    public void setEarDeployerType(Class JavaDoc earDeployerType)
124    {
125       this.earDeployerType = earDeployerType;
126    }
127    public Class JavaDoc getEjbDeployerType()
128    {
129       return ejbDeployerType;
130    }
131    public void setEjbDeployerType(Class JavaDoc ejbDeployerType)
132    {
133       this.ejbDeployerType = ejbDeployerType;
134    }
135    public Class JavaDoc getRarDeployerType()
136    {
137       return rarDeployerType;
138    }
139    public void setRarDeployerType(Class JavaDoc rarDeployerType)
140    {
141       this.rarDeployerType = rarDeployerType;
142    }
143    public Class JavaDoc getWarDeployerType()
144    {
145       return warDeployerType;
146    }
147    public void setWarDeployerType(Class JavaDoc warDeployerType)
148    {
149       this.warDeployerType = warDeployerType;
150    }
151
152    public void setModuleMap(Map JavaDoc<String JavaDoc, SerializableTargetModuleID> moduleMap)
153    {
154       this.moduleMap = moduleMap;
155    }
156    public File JavaDoc getUploadDir()
157    {
158       return this.uploadDir;
159    }
160
161    public void setUploadDir(File JavaDoc uploadDir)
162    {
163       this.uploadDir = uploadDir;
164    }
165
166    public boolean isDeleteOnUndeploy()
167    {
168       return deleteOnUndeploy;
169    }
170
171    public void setDeleteOnUndeploy(boolean deleteOnUndeploy)
172    {
173       this.deleteOnUndeploy = deleteOnUndeploy;
174    }
175
176    public boolean isFailOnCollision()
177    {
178       return failOnCollision;
179    }
180
181    public void setFailOnCollision(boolean failOnCollision)
182    {
183       this.failOnCollision = failOnCollision;
184    }
185
186    public Map JavaDoc getModuleMap()
187    {
188       return Collections.unmodifiableMap(moduleMap);
189    }
190
191    public void deploy(SerializableTargetModuleID moduleID) throws Exception JavaDoc
192    {
193       String JavaDoc url = moduleID.getModuleID();
194
195       // Create a status object
196
URL JavaDoc deployURL = new URL JavaDoc(url);
197       URLConnection JavaDoc conn = deployURL.openConnection();
198       int contentLength = conn.getContentLength();
199       log.debug("Begin deploy, url: " + deployURL + ", contentLength: " + contentLength);
200
201       // Create the local path
202
File JavaDoc path = new File JavaDoc(deployURL.getFile());
203       String JavaDoc archive = path.getName();
204       File JavaDoc deployFile = new File JavaDoc(uploadDir, archive);
205       if (failOnCollision && deployFile.exists())
206          throw new IOException JavaDoc("deployURL(" + deployURL + ") collides with: " + deployFile.getPath());
207
208       if (deployFile.exists())
209          Files.delete(deployFile);
210
211       File JavaDoc parentFile = deployFile.getParentFile();
212       if (parentFile.exists() == false)
213       {
214          if (parentFile.mkdirs() == false)
215             throw new IOException JavaDoc("Failed to create local path: " + parentFile);
216       }
217
218       InputStream JavaDoc is = conn.getInputStream();
219       BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(is);
220       byte[] buffer = new byte[4096];
221       FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(deployFile);
222       BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fos);
223       int count = 0;
224       while ((count = bis.read(buffer)) > 0)
225       {
226          bos.write(buffer, 0, count);
227       }
228       bis.close();
229       bos.close();
230
231       moduleMap.put(url, moduleID);
232    }
233
234    public void start(String JavaDoc url) throws Exception JavaDoc
235    {
236       SerializableTargetModuleID moduleID = (SerializableTargetModuleID)moduleMap.get(url);
237       if (moduleID == null)
238          throw new IOException JavaDoc("deployURL(" + url + ") has not been distributed");
239
240       URL JavaDoc deployURL = new URL JavaDoc(url);
241       // Build the local path
242
File JavaDoc path = new File JavaDoc(deployURL.getFile());
243       String JavaDoc archive = path.getName();
244       File JavaDoc deployFile = new File JavaDoc(uploadDir, archive);
245       if (deployFile.exists() == false)
246          throw new IOException JavaDoc("deployURL(" + url + ") has no local archive");
247
248       VirtualFile root = VFS.getRoot(deployFile.toURI());
249       DeploymentContext context = new AbstractDeploymentContext(root);
250       mainDeployer.addDeploymentContext(context);
251       mainDeployer.process();
252       moduleID.setRunning(true);
253       moduleID.clearChildModuleIDs();
254       // Repopulate the child modules
255
fillChildrenTargetModuleID(moduleID, context);
256    }
257
258    public void stop(String JavaDoc url) throws Exception JavaDoc
259    {
260       SerializableTargetModuleID moduleID = (SerializableTargetModuleID)moduleMap.get(url);
261       if (moduleID == null)
262          throw new IOException JavaDoc("deployURL(" + url + ") has not been distributed");
263
264       URL JavaDoc deployURL = new URL JavaDoc(url);
265       // Build the local path
266
File JavaDoc path = new File JavaDoc(deployURL.getFile());
267       String JavaDoc archive = path.getName();
268       File JavaDoc deployFile = new File JavaDoc(uploadDir, archive);
269       if (deployFile.exists() == false)
270          throw new IOException JavaDoc("deployURL(" + url + ") has no local archive");
271
272       VirtualFile root = VFS.getRoot(deployFile.toURI());
273       mainDeployer.removeDeploymentContext(AbstractDeploymentContext.getDeploymentName(root));
274       mainDeployer.process();
275       moduleID.setRunning(false);
276    }
277
278    public void undeploy(String JavaDoc url) throws Exception JavaDoc
279    {
280       SerializableTargetModuleID moduleID = (SerializableTargetModuleID)moduleMap.get(url);
281       if (moduleID == null)
282          return;
283
284       // If the module is still running, stop it
285
if (moduleID.isRunning())
286          stop(url);
287
288       URL JavaDoc deployURL = new URL JavaDoc(url);
289       // Build the local path
290
File JavaDoc path = new File JavaDoc(deployURL.getFile());
291       String JavaDoc archive = path.getName();
292       File JavaDoc deployFile = new File JavaDoc(uploadDir, archive);
293       if (deployFile.exists() == false)
294          throw new IOException JavaDoc("deployURL(" + url + ") has not been distributed");
295
296       if (deleteOnUndeploy)
297          Files.delete(deployFile);
298
299       moduleMap.remove(url);
300    }
301
302    public SerializableTargetModuleID[] getAvailableModules(int moduleType)
303       throws TargetException JavaDoc
304    {
305       ArrayList JavaDoc<SerializableTargetModuleID> matches = new ArrayList JavaDoc<SerializableTargetModuleID>();
306       Iterator JavaDoc<SerializableTargetModuleID> modules = moduleMap.values().iterator();
307       while (modules.hasNext())
308       {
309          SerializableTargetModuleID module = modules.next();
310          if (module.getModuleType() == moduleType)
311             matches.add(module);
312       }
313
314       SerializableTargetModuleID[] ids = new SerializableTargetModuleID[matches.size()];
315       matches.toArray(ids);
316       return ids;
317    }
318
319    private void fillChildrenTargetModuleID(SerializableTargetModuleID moduleID,
320          DeploymentContext info)
321    {
322       Set JavaDoc<DeploymentContext> children = info.getChildren();
323       for(DeploymentContext ctx : children)
324       {
325          try
326          {
327             ModuleType JavaDoc type = getModuleType(ctx);
328             // Discard unknown ModuleTypes
329
if (type != null)
330             {
331                String JavaDoc module = ctx.getName();
332                // TODO, DEPLOYED may not be the same as running?
333
boolean isRunning = ctx.getState() == DeploymentState.DEPLOYED;
334                SerializableTargetModuleID child = new SerializableTargetModuleID(moduleID, module, type.getValue(), isRunning);
335                moduleID.addChildTargetModuleID(child);
336                fillChildrenTargetModuleID(child, ctx);
337             }
338          }
339          catch (UnsupportedOperationException JavaDoc e)
340          {
341             if (log.isTraceEnabled())
342                log.trace("Ignoring", e);
343          }
344       }
345    }
346
347    /**
348     * Determine the module type by looking for an attachment that identifies
349     * the deployer type.
350     *
351     * @param info
352     * @return
353     */

354    private ModuleType JavaDoc getModuleType(DeploymentContext info)
355    {
356       ModuleType JavaDoc type = null;
357
358       Attachments attachments = info.getTransientAttachments();
359       if (attachments.getAttachment(carDeployerType) != null)
360       {
361          type = ModuleType.CAR;
362       }
363       else if (attachments.getAttachment(ejbDeployerType) != null)
364       {
365          type = ModuleType.EJB;
366       }
367       else if (attachments.getAttachment(earDeployerType) != null)
368       {
369          type = ModuleType.EAR;
370       }
371       else if (attachments.getAttachment(rarDeployerType) != null)
372       {
373          type = ModuleType.RAR;
374       }
375       else if (attachments.getAttachment(warDeployerType) != null)
376       {
377          type = ModuleType.WAR;
378       }
379
380       return type;
381    }
382
383 }
384
Popular Tags