KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.InetAddress JavaDoc;
29 import java.net.UnknownHostException JavaDoc;
30
31 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
32 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
33 import javax.enterprise.deploy.spi.exceptions.TargetException JavaDoc;
34
35 import org.jboss.logging.Logger;
36
37 /**
38  * A Target interface represents a single logical core server of one instance of a J2EE platform product.
39  * It is a designator for a server and the implied location to copy a configured application for the server to access.
40  *
41  * @author thomas.diesler@jboss.org
42  * @version $Revision: 38480 $
43  */

44 public class LocalhostTarget implements JBossTarget
45 {
46    // deployment logging
47
private static final Logger log = Logger.getLogger(LocalhostTarget.class);
48
49    /**
50     * Get the target's description
51     *
52     * @return the description
53     */

54    public String JavaDoc getDescription()
55    {
56       return "JBoss localhost deployment target";
57    }
58
59    /**
60     * Get the target's name
61     *
62     * @return the name
63     */

64    public String JavaDoc getName()
65    {
66       return "localhost";
67    }
68
69    /**
70     * Get the target's host name
71     */

72    public String JavaDoc getHostName()
73    {
74       try
75       {
76          return InetAddress.getLocalHost().getHostName();
77       }
78       catch (UnknownHostException JavaDoc e)
79       {
80          log.error("Cannot obtain localhost", e);
81          return null;
82       }
83    }
84
85    /**
86     * Deploy a given module
87     */

88    public void deploy(TargetModuleID JavaDoc targetModuleID) throws Exception JavaDoc
89    {
90       // get the server deploydir
91
String JavaDoc deployDir = getDeploydir();
92
93       FileOutputStream JavaDoc outs = null;
94       FileInputStream JavaDoc ins = null;
95       try
96       {
97          File JavaDoc deployableFile = new File JavaDoc(targetModuleID.getModuleID());
98          File JavaDoc targetFile = new File JavaDoc(deployDir + "/" + deployableFile.getName());
99          log.info("Writing deployableFile: " + deployableFile.getAbsolutePath() + " to: " + targetFile.getAbsolutePath());
100          outs = new FileOutputStream JavaDoc(targetFile);
101          ins = new FileInputStream JavaDoc(deployableFile);
102          JarUtils.copyStream(outs, ins);
103          log.info("Waiting 10 seconds for deploy to finish...");
104          Thread.sleep(10000);
105       }
106       finally
107       {
108          try
109          {
110             if (outs != null)
111             {
112                outs.close();
113             }
114             if (ins != null)
115             {
116                ins.close();
117             }
118          }
119          catch (IOException JavaDoc e)
120          {
121             // ignore this one
122
}
123       }
124    }
125
126    /**
127     * Start a given module
128     */

129    public void start(TargetModuleID JavaDoc targetModuleID) throws Exception JavaDoc
130    {
131    }
132
133    /**
134     * Stop a given module
135     */

136    public void stop(TargetModuleID JavaDoc targetModuleID) throws Exception JavaDoc
137    {
138    }
139
140    /**
141     * Undeploy a given module
142     */

143    public void undeploy(TargetModuleID JavaDoc targetModuleID) throws Exception JavaDoc
144    {
145       // get the server deploydir
146
String JavaDoc deployDir = getDeploydir();
147       File JavaDoc file = new File JavaDoc(deployDir + "/" + targetModuleID.getModuleID());
148       file.delete();
149    }
150
151    /**
152     * Retrieve the list of all J2EE application modules running or not running on the identified targets.
153     */

154    public TargetModuleID JavaDoc[] getAvailableModules(ModuleType JavaDoc moduleType) throws TargetException JavaDoc
155    {
156       return null;
157    }
158
159    /**
160     * Get the server deploydir
161     */

162    private String JavaDoc getDeploydir()
163    {
164       //[todo] replace this System property lookup
165
String JavaDoc deployDir = System.getProperty("jboss.deploy.dir");
166       if (deployDir == null)
167       {
168          String JavaDoc j2eeHome = System.getProperty("J2EE_HOME");
169          if (j2eeHome == null)
170             throw new RuntimeException JavaDoc("Cannot obtain system property: jboss.deploy.dir or J2EE_HOME");
171          deployDir = j2eeHome + "/server/cts/deploy";
172       }
173       log.info("Using deploy dir: " + deployDir);
174       return deployDir;
175    }
176 }
177
Popular Tags