KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > spi > DeploymentWorker


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.spi;
23
24 import java.io.File JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import javax.enterprise.deploy.shared.CommandType JavaDoc;
31 import javax.enterprise.deploy.shared.StateType JavaDoc;
32 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
33 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
34
35 import org.jboss.deployment.spi.status.ProgressObjectImpl;
36 import org.jboss.logging.Logger;
37
38 /**
39  * A thread that deployes the given deployment on all targets contained in the
40  * progress object. It sends events to the progress object.
41  *
42  * @author thomas.diesler@jboss.org
43  * @version $Revision: 38480 $
44  */

45 public class DeploymentWorker extends Thread JavaDoc
46 {
47    // deployment logging
48
private static final Logger log = Logger.getLogger(DeploymentWorker.class);
49
50    private ProgressObjectImpl progress;
51
52    public DeploymentWorker(ProgressObject JavaDoc progress)
53    {
54       this.progress = (ProgressObjectImpl)progress;
55    }
56
57    /**
58     * Deploy the module on each given target
59     */

60    public void run()
61    {
62
63       CommandType JavaDoc cmdType = progress.getDeploymentStatus().getCommand();
64       TargetModuleID JavaDoc[] modules = progress.getResultTargetModuleIDs();
65       for (int i = 0; i < modules.length; i++)
66       {
67          TargetModuleID JavaDoc moduleid = modules[i];
68          JBossTarget target = (JBossTarget)moduleid.getTarget();
69          try
70          {
71             progress.sendProgressEvent(StateType.RUNNING, "Operation " + cmdType + " started", moduleid);
72             if (cmdType == CommandType.DISTRIBUTE)
73             {
74                target.deploy(moduleid);
75                deleteDeployment(moduleid);
76             }
77             else if (cmdType == CommandType.START)
78             {
79                target.start(moduleid);
80             }
81             else if (cmdType == CommandType.STOP)
82             {
83                target.stop(moduleid);
84             }
85             else if (cmdType == CommandType.UNDEPLOY)
86             {
87                target.undeploy(moduleid);
88                deleteDeployment(moduleid);
89             }
90             progress.sendProgressEvent(StateType.COMPLETED, "Operation " + cmdType + " completed", moduleid);
91          }
92          catch (Exception JavaDoc e)
93          {
94             String JavaDoc message = "Operation " + cmdType + " failed on target " + target;
95             StringWriter JavaDoc sw = new StringWriter JavaDoc();
96             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
97             pw.println(message);
98             e.printStackTrace(pw);
99             pw.close();
100             message = sw.toString();
101             progress.sendProgressEvent(StateType.FAILED, message, moduleid);
102             log.error(message, e);
103          }
104       }
105    }
106
107    private void deleteDeployment(TargetModuleID JavaDoc moduleid) throws MalformedURLException JavaDoc
108    {
109       File JavaDoc deployment = new File JavaDoc(new URL JavaDoc(moduleid.getModuleID()).getPath());
110       if (deployment.exists())
111       {
112          if (!deployment.delete())
113          {
114             log.warn("Cannot delete deployment file " + deployment + ", will be deleted on exit");
115             deployment.deleteOnExit();
116          }
117       }
118       else
119       {
120          log.error("Deployment does not exist: " + deployment);
121       }
122    }
123 }
124
Popular Tags