KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > deployment > jboss > ServerDeployer


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.deployment.jboss;
10
11 import org.jboss.deployment.SubDeployerSupport;
12 import org.jboss.deployment.DeploymentInfo;
13 import org.jboss.deployment.DeploymentException;
14 import org.jboss.portal.server.ServerManager;
15 import org.jboss.portal.server.deployment.PortalWebApp;
16 import org.jboss.portal.common.util.XML;
17 import org.jboss.portal.common.util.Tools;
18 import org.jboss.mx.loading.RepositoryClassLoader;
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.Document JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30 import java.util.regex.Matcher JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33
34 /**
35  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
36  * @version $Revision: 1.5 $
37  */

38 public class ServerDeployer extends SubDeployerSupport implements ServerDeployerMBean
39 {
40
41    /** The server manager. */
42    private ServerManager serverManager;
43
44    /** The configuration of the factories for sub deployments. */
45    private Element JavaDoc factoryConfig;
46
47    /** The factories. */
48    private List JavaDoc factoryEntries;
49
50    public ServerDeployer()
51    {
52       factoryEntries = new ArrayList JavaDoc();
53    }
54
55    /**
56     * This should never be called for server deployment.
57     */

58    public boolean accepts(DeploymentInfo di)
59    {
60       return false;
61    }
62
63    public ServerManager getServetManager()
64    {
65       return serverManager;
66    }
67
68    public void setServerManager(ServerManager serverManager)
69    {
70       this.serverManager = serverManager;
71    }
72
73    public ServerDeployer getDeployer()
74    {
75       return this;
76    }
77
78    public Element JavaDoc getFactoryConfig()
79    {
80       return factoryConfig;
81    }
82
83    public void setFactoryConfig(Element JavaDoc factoryConfig)
84    {
85       this.factoryConfig = factoryConfig;
86    }
87
88    protected void createService() throws Exception JavaDoc
89    {
90       List JavaDoc factoryElts = XML.getChildren(factoryConfig, "factory");
91       for (int i = 0;i < factoryElts.size();i++)
92       {
93          Element JavaDoc factoryElt = (Element JavaDoc)factoryElts.get(i);
94          Element JavaDoc urlPatternElt = (Element JavaDoc)XML.getUniqueChild(factoryElt, "url-pattern", true);
95          Element JavaDoc factoryClassElt = (Element JavaDoc)XML.getUniqueChild(factoryElt, "factory-class", true);
96          String JavaDoc urlPatternAsString = XML.asString(urlPatternElt);
97          String JavaDoc factoryClassAsString = XML.asString(factoryClassElt);
98          Pattern JavaDoc urlPattern = Pattern.compile(urlPatternAsString);
99          Class JavaDoc factoryClass = Thread.currentThread().getContextClassLoader().loadClass(factoryClassAsString);
100          DeploymentFactory factory = (DeploymentFactory)factoryClass.newInstance();
101          FactoryEntry entry = new FactoryEntry(urlPattern, factory);
102          factoryEntries.add(entry);
103       }
104       super.createService();
105    }
106
107    protected void destroyService() throws Exception JavaDoc
108    {
109       super.destroyService();
110       factoryEntries.clear();
111    }
112
113    // findRepositoryClassLoader(pwa.getClassLoader());
114
// findWEBINF
115
protected void processNestedDeployments(DeploymentInfo di) throws DeploymentException
116    {
117       // We only list if it is a directory
118
if (di.isDirectory)
119       {
120          String JavaDoc s = di.url.getFile();
121          File JavaDoc dir = new File JavaDoc(s);
122          File JavaDoc[] children = dir.listFiles();
123          for (int i = 0; i < children.length; i++)
124          {
125             try
126             {
127                File JavaDoc child = children[i];
128                URL JavaDoc url = child.toURL();
129                DeploymentFactory factory = findFactory(url);
130                if (factory != null)
131                {
132                   Deployment blah = (Deployment)di.metaData;
133                   Deployment sdi = factory.newInstance(blah.getServerManager(), url, blah.pwa, blah.appName);
134                   DeploymentInfo sub = new DeploymentInfo(url, di, server);
135                   sub.metaData = sdi;
136                   sub.deployer = this;
137                }
138             }
139             catch (MalformedURLException JavaDoc e)
140             {
141                log.error("Unexpected exception when converting file to URL", e);
142             }
143          }
144       }
145    }
146
147    public DeploymentFactory findFactory(URL JavaDoc url)
148    {
149       String JavaDoc urlAsFile = url.getFile();
150
151       // Browse the factories to find one that could eventually pick up this entry
152
for (int j = 0; j < factoryEntries.size(); j++)
153       {
154          FactoryEntry entry = (FactoryEntry)factoryEntries.get(j);
155          Matcher JavaDoc matcher = entry.urlPattern.matcher(urlAsFile);
156          if (matcher.matches())
157          {
158             return entry.factory;
159          }
160       }
161
162       //
163
return null;
164    }
165
166    public void create(DeploymentInfo di) throws DeploymentException
167    {
168       Deployment deployment = (Deployment)di.metaData;
169       if (deployment != null)
170       {
171          deployment.create();
172       }
173       super.create(di);
174    }
175
176    public void start(DeploymentInfo di) throws DeploymentException
177    {
178       Deployment deployment = (Deployment)di.metaData;
179       if (deployment != null)
180       {
181          deployment.start();
182       }
183       super.start(di);
184    }
185
186    public void stop(DeploymentInfo di) throws DeploymentException
187    {
188       Deployment deployment = (Deployment)di.metaData;
189       if (deployment != null)
190       {
191          deployment.stop();
192       }
193       super.stop(di);
194    }
195
196    public void destroy(DeploymentInfo di) throws DeploymentException
197    {
198       Deployment deployment = (Deployment)di.metaData;
199       if (deployment != null)
200       {
201          deployment.destroy();
202       }
203       super.destroy(di);
204    }
205
206    public void deploy(PortalWebApp pwa) throws DeploymentException
207    {
208       // The app name is the context path a priori
209
String JavaDoc appName = pwa.getContextPath();
210
211       // Look for jboss-app.xml override
212
InputStream JavaDoc in = null;
213       try
214       {
215          in = pwa.getServletContext().getResourceAsStream("/WEB-INF/jboss-app.xml");
216          if (in != null)
217          {
218             Document JavaDoc doc = XML.getDocumentBuilderFactory().newDocumentBuilder().parse(in);
219             Element JavaDoc jbossAppElt = doc.getDocumentElement();
220             Element JavaDoc appNameElt = XML.getUniqueChild(jbossAppElt, "app-name", false);
221             if (appNameElt != null)
222             {
223                appName = XML.asString(appNameElt);
224                log.debug("Detected explicit app name = " + appName + " for application under path " + pwa.getContextPath());
225             }
226          }
227       }
228       catch (IOException JavaDoc e)
229       {
230          log.debug("Cannot read jboss-app.xml", e);
231       }
232       catch (ParserConfigurationException JavaDoc e)
233       {
234          log.debug("Cannot read jboss-app.xml", e);
235       }
236       catch (SAXException JavaDoc e)
237       {
238          log.debug("Cannot read jboss-app.xml", e);
239       }
240       finally
241       {
242          Tools.safeClose(in);
243       }
244
245       // Create the deployment object
246
RepositoryClassLoader rcl = Deployment.findRepositoryClassLoader(pwa.getClassLoader());
247       URL JavaDoc url = Deployment.findWEBINFURL(pwa.getURL());
248       Deployment pwadi = new Deployment(serverManager, url, pwa, appName);
249
250       // Create our deployment info object and pass it to main deployer
251
DeploymentInfo di = new DeploymentInfo(url, null, server);
252       di.ucl = rcl;
253       di.metaData = pwadi;
254       di.deployer = this;
255       mainDeployer.deploy(di);
256    }
257
258    public void undeploy(PortalWebApp pwa) throws DeploymentException
259    {
260       try
261       {
262          URL JavaDoc url = Deployment.findWEBINFURL(pwa.getURL());
263          mainDeployer.undeploy(url);
264       }
265       catch (Exception JavaDoc e)
266       {
267          log.error("Error when undeploying portal web app", e);
268       }
269    }
270
271    /**
272     * A factory entry that will produce custom deployments
273     */

274    private static class FactoryEntry
275    {
276       private Pattern JavaDoc urlPattern;
277       private DeploymentFactory factory;
278       public FactoryEntry(Pattern JavaDoc urlPattern, DeploymentFactory factory)
279       {
280          this.urlPattern = urlPattern;
281          this.factory = factory;
282       }
283    }
284
285 }
286
Popular Tags