KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > server > profileservice > ProfileServiceBootstrap


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.system.server.profileservice;
23
24 import java.net.URL JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Enumeration JavaDoc;
27
28 import org.jboss.beans.metadata.plugins.AbstractBeanMetaData;
29 import org.jboss.dependency.spi.ControllerContext;
30 import org.jboss.deployers.spi.DeploymentException;
31 import org.jboss.deployers.spi.IncompleteDeploymentException;
32 import org.jboss.deployers.spi.IncompleteDeployments;
33 import org.jboss.deployers.spi.IncompleteDeploymentsBuilder;
34 import org.jboss.deployers.spi.deployment.MainDeployer;
35 import org.jboss.deployers.spi.structure.DeploymentContext;
36 import org.jboss.kernel.Kernel;
37 import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
38 import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer;
39 import org.jboss.kernel.spi.dependency.KernelController;
40 import org.jboss.profileservice.spi.Profile;
41 import org.jboss.profileservice.spi.ProfileKey;
42 import org.jboss.profileservice.spi.ProfileService;
43 import org.jboss.system.server.Server;
44
45 /**
46  * An extension of the kernel basic bootstrap that boots the mc from
47  * {profileName}/deployer-beans.xml descriptors using the BasicXMLDeployer, and
48  * loads additional deployments using the ProfileService.
49  *
50  * @author Scott.Stark@jboss.org
51  * @version $Revision$
52  */

53 public class ProfileServiceBootstrap extends BasicBootstrap
54 {
55    /** The bootstrap file */
56    public static String JavaDoc BOOTSTRAP_XML_NAME = "bootstrap-beans.xml";
57
58    /** The name of the profile that is being booted */
59    protected String JavaDoc profileName;
60
61    /** The Server instance bootstraping the profile service */
62    protected Server server;
63
64    /** The kernel deployer used to load the server deployers */
65    protected BasicXMLDeployer kernelDeployer;
66
67    /** The controller */
68    protected KernelController controller;
69    
70    /** The server MainDeployer */
71    protected MainDeployer mainDeployer;
72
73    /** The server ProfileService */
74    protected ProfileService profileService;
75
76    /** The deployer beans prefix */
77    protected String JavaDoc deployerBeansPrefix;
78
79    /** An explicit deployer-beans.xml URL to deploy */
80    protected URL JavaDoc bootstrapURL;
81
82    /**
83     * Create a new ProfileServiceBootstrap.
84     */

85    public ProfileServiceBootstrap()
86    {
87       this("default", null);
88    }
89
90    /**
91     * Create a new ProfileServiceBootstrap.
92     *
93     * @param name the name
94     * @param server the server instance
95     */

96    public ProfileServiceBootstrap(String JavaDoc name, Server server)
97    {
98       this.profileName = name;
99       this.server = server;
100    }
101
102    public Server getServer()
103    {
104       return server;
105    }
106
107    /**
108     * Return the MainDeployer bean.
109     *
110     * @return the MainDeployer bean if bootstrap succeeded, null otherwise.
111     */

112    public MainDeployer getMainDeployer()
113    {
114       return mainDeployer;
115    }
116
117    /**
118     * Return the ProfileService bean.
119     *
120     * @return the ProfileService bean if bootstrap succeeded, null otherwise
121     */

122    public ProfileService getProfileService()
123    {
124       return profileService;
125    }
126
127    /**
128     * Return the bootstrap XML kernel deployer.
129     *
130     * @return BasicXMLDeployer instance.
131     */

132    public BasicXMLDeployer getKernelDeployer()
133    {
134       return kernelDeployer;
135    }
136
137    /**
138     * Get the deployer bean prefix
139     *
140     * @return the prefix
141     */

142    public String JavaDoc getDeployerBeansPrefix()
143    {
144       return deployerBeansPrefix;
145    }
146
147    /**
148     * Set the prefix to add to the DEPLOYERS_XML_NAME in order to create the
149     * classpath resource to look for bootstrap descriptors.
150     *
151     * @param prefix - the prefix for the bootstrap descriptor resource. If null
152     * or empty, a value of profileName + "/" is used.
153     */

154    public void setDeployerBeansPrefix(String JavaDoc prefix)
155    {
156       if (prefix != null && prefix.length() == 0)
157          prefix = null;
158       this.deployerBeansPrefix = prefix;
159    }
160
161    /**
162     * Get the bootstrap url
163     *
164     * @return the bootstrap url
165     */

166    public URL JavaDoc getBootstrapURL()
167    {
168       return bootstrapURL;
169    }
170
171    /**
172     * Set the explicit kernel bootstrap deployer-beans.xml URL. If set, not
173     * resource discovery will be preformed.
174     *
175     * @param bootstrapURL - URL to the bootstrap deployer-beans.xml
176     */

177    public void setBootstrapURL(URL JavaDoc bootstrapURL)
178    {
179       this.bootstrapURL = bootstrapURL;
180    }
181
182    @Override JavaDoc
183    public void bootstrap() throws Throwable JavaDoc
184    {
185       // Bootstrap the core kernel
186
super.bootstrap();
187
188       kernelDeployer = new BasicXMLDeployer(getKernel());
189
190       if (bootstrapURL == null)
191       {
192          ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
193
194          String JavaDoc bootstrapResName;
195          if (deployerBeansPrefix == null)
196             bootstrapResName = profileName + "/" + BOOTSTRAP_XML_NAME;
197          else
198             bootstrapResName = deployerBeansPrefix + BOOTSTRAP_XML_NAME;
199          
200          log.debug("Scanning for bootstrap resources: " + bootstrapResName);
201          Enumeration JavaDoc<URL JavaDoc> descriptors = cl.getResources(bootstrapResName);
202          while (descriptors.hasMoreElements())
203          {
204             URL JavaDoc url = descriptors.nextElement();
205             log.debug("Found: "+url);
206             deploy(url);
207          }
208
209          String JavaDoc metaInfBootstrapResName = "META-INF/" + bootstrapResName;
210          log.debug("Scanning for bootstrap resources: " + metaInfBootstrapResName);
211          descriptors = cl.getResources(metaInfBootstrapResName);
212          while (descriptors.hasMoreElements())
213          {
214             URL JavaDoc url = descriptors.nextElement();
215             log.debug("Found: "+url);
216             deploy(url);
217          }
218       }
219       else
220       {
221          deploy(bootstrapURL);
222       }
223
224       Kernel kernel = getKernel();
225       controller = kernel.getController();
226
227       // Register the Server instance in the kernel
228
if (server != null)
229       {
230          AbstractBeanMetaData metaData = new AbstractBeanMetaData("JBossServer", server.getClass().getName());
231          controller.install(metaData, server);
232       }
233
234       // Validate that everything is ok
235
checkIncomplete();
236
237       // Get the beans TODO injection!
238
profileService = getBean(controller, "ProfileService", ProfileService.class);
239       log.debug("Using ProfileService: " + profileService);
240       mainDeployer = getBean(controller, "MainDeployer", MainDeployer.class);
241       log.debug("Using MainDeployer: " + mainDeployer);
242
243       // Load the profile beans
244
try
245       {
246          loadProfile(profileName);
247       }
248       catch (IncompleteDeploymentException e)
249       {
250          log.error("Failed to load profile: " + e.getMessage());
251       }
252       catch (Exception JavaDoc e)
253       {
254          log.error("Failed to load profile: ", e);
255       }
256    }
257
258    /**
259     * Get a bean
260     *
261     * @param <T> the expected type
262     * @param controller the controller
263     * @param name the bean name
264     * @param expectedType the expected type
265     * @return the bean
266     * @throws IllegalStateException if the bean is not installed or has the wrong type
267     */

268    protected <T> T getBean(KernelController controller, Object JavaDoc name, Class JavaDoc<T> expectedType)
269    {
270       ControllerContext context = controller.getInstalledContext(name);
271       if (context == null)
272          throw new IllegalStateException JavaDoc("Context not installed: " + name);
273       Object JavaDoc result = context.getTarget();
274       if (result == null)
275          throw new IllegalStateException JavaDoc("No target for " + name);
276       if (expectedType.isInstance(result) == false)
277          throw new IllegalStateException JavaDoc(name + " expected " + expectedType.getName() + " was " + result.getClass().getName());
278       return expectedType.cast(result);
279    }
280    
281    /**
282     * Bootstrap deployment of the
283     *
284     * @param url the deployment url
285     * @throws Throwable for any error
286     */

287    protected void deploy(URL JavaDoc url) throws Throwable JavaDoc
288    {
289       log.debug("Deploying bootstrap config: " + url);
290       kernelDeployer.deploy(url);
291    }
292
293    /**
294     * Undeploy a url
295     *
296     * @param url the deployment url
297     */

298    protected void undeploy(URL JavaDoc url)
299    {
300       try
301       {
302          log.debug("Undeploying bootstrap config: " + url);
303          kernelDeployer.undeploy(url);
304       }
305       catch (Throwable JavaDoc t)
306       {
307          log.warn("Error during undeployment: " + url, t);
308       }
309    }
310
311    /**
312     * Load the deployments associated with the named profile and deploy them
313     * using the MainDeployer.
314     *
315     * @param name
316     * @throws Throwable
317     * @throws NullPointerException if either the MainDeployer or ProfileService
318     * have not been injected.
319     */

320    protected void loadProfile(String JavaDoc name) throws Throwable JavaDoc
321    {
322       MainDeployer deployer = getMainDeployer();
323       if (deployer == null)
324          throw new NullPointerException JavaDoc("MainDeployer has not been set");
325       ProfileService ps = getProfileService();
326       if (ps == null)
327          throw new NullPointerException JavaDoc("ProfileService has not been set");
328
329       // Load the named profile
330
ProfileKey key = new ProfileKey(name);
331       Profile profile = ps.getProfile(key);
332
333       // HACK
334
DeploymentContext first = null;
335       
336       // Deploy the bootstrap
337
Collection JavaDoc<DeploymentContext> boostraps = profile.getBootstraps();
338       for (DeploymentContext d : boostraps)
339       {
340          deployer.addDeploymentContext(d);
341          if (first == null)
342             first = d;
343       }
344       deployer.process();
345       checkIncomplete();
346
347       Thread JavaDoc thread = Thread.currentThread();
348       ClassLoader JavaDoc old = thread.getContextClassLoader();
349       ClassLoader JavaDoc cl = first != null ? first.getClassLoader() : null;
350       if (cl != null)
351          thread.setContextClassLoader(cl);
352       try
353       {
354          
355          // Deploy the profile deployers
356
Collection JavaDoc<DeploymentContext> profileDeployers = profile.getDeployers();
357          for (DeploymentContext d : profileDeployers)
358             deployer.addDeploymentContext(d);
359          deployer.process();
360          checkIncomplete();
361
362          // Deploy the profile deployments
363
Collection JavaDoc<DeploymentContext> profileDeployments = profile.getDeployments();
364          for (DeploymentContext d : profileDeployments)
365             deployer.addDeploymentContext(d);
366          deployer.process();
367          checkIncomplete();
368       }
369       finally
370       {
371          thread.setContextClassLoader(old);
372       }
373    }
374    
375    /**
376     * Check whether we are incomplete
377     *
378     * @throws DeploymentException the deployment exception
379     */

380    protected void checkIncomplete() throws DeploymentException
381    {
382       IncompleteDeployments incomplete = IncompleteDeploymentsBuilder.build(mainDeployer, controller);
383       if (incomplete.isIncomplete())
384          throw new IncompleteDeploymentException(incomplete);
385    }
386 }
387
Popular Tags