KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > deployers > ServiceDeployer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.deployers;
23
24 import javax.management.ObjectName JavaDoc;
25
26 import org.jboss.deployers.plugins.deployers.helpers.AbstractSimpleRealDeployer;
27 import org.jboss.deployers.spi.DeploymentException;
28 import org.jboss.deployers.spi.deployer.DeploymentUnit;
29 import org.jboss.mx.loading.RepositoryClassLoader;
30 import org.jboss.system.ServiceContext;
31 import org.jboss.system.ServiceController;
32 import org.jboss.system.metadata.ServiceDeployment;
33 import org.jboss.system.metadata.ServiceMetaData;
34
35 /**
36  * ServiceDeployer.<p>
37  *
38  * This deployer is responsible for deploying services of
39  * type {@link ServiceDeployment}.
40  *
41  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 1.1 $
43  */

44 public class ServiceDeployer extends AbstractSimpleRealDeployer<ServiceMetaData>
45 {
46    /** The service controller */
47    private final ServiceController controller;
48
49    /**
50     * Create a new ServiceDeployer.
51     *
52     * @param controller the service controller
53     * @throws IllegalArgumentException for a null controller
54     */

55    public ServiceDeployer(ServiceController controller)
56    {
57       super(ServiceMetaData.class);
58       if (controller == null)
59          throw new IllegalArgumentException JavaDoc("Null controller");
60       this.controller = controller;
61    }
62
63    public void deploy(DeploymentUnit unit, ServiceMetaData deployment) throws DeploymentException
64    {
65       ObjectName JavaDoc name = deployment.getObjectName();
66       try
67       {
68          ObjectName JavaDoc loaderName = deployment.getClassLoaderName();
69          if (loaderName == null)
70          {
71             ClassLoader JavaDoc cl = unit.getClassLoader();
72             if (cl != null && cl instanceof RepositoryClassLoader)
73                loaderName = ((RepositoryClassLoader) cl).getObjectName();
74          }
75
76          controller.install(deployment, loaderName);
77          ServiceContext context = controller.getServiceContext(name);
78          if (context == null)
79             throw new IllegalStateException JavaDoc("No context for " + name);
80          try
81          {
82             create(context);
83             try
84             {
85                start(context);
86             }
87             catch (Throwable JavaDoc t)
88             {
89                destroy(name);
90                throw t;
91             }
92          }
93          catch (Throwable JavaDoc t)
94          {
95             remove(name);
96             throw t;
97          }
98       }
99       catch (Throwable JavaDoc t)
100       {
101          throw DeploymentException.rethrowAsDeploymentException("Error deploying: " + name, t);
102       }
103    }
104
105    public void undeploy(DeploymentUnit unit, ServiceMetaData deployment)
106    {
107       ObjectName JavaDoc name = deployment.getObjectName();
108       ServiceContext context = controller.getServiceContext(name);
109       if (context != null)
110       {
111          stop(name);
112          destroy(name);
113          remove(name);
114       }
115    }
116    
117    protected void create(ServiceContext context) throws Throwable JavaDoc
118    {
119       controller.create(context.objectName);
120    }
121    
122    protected void start(ServiceContext context) throws Throwable JavaDoc
123    {
124       controller.start(context.objectName);
125    }
126    
127    protected void stop(ObjectName JavaDoc name)
128    {
129       try
130       {
131          controller.stop(name);
132       }
133       catch (Throwable JavaDoc t)
134       {
135          log.warn("Error during stop for " + name, t);
136       }
137    }
138    
139    protected void destroy(ObjectName JavaDoc name)
140    {
141       try
142       {
143          controller.destroy(name);
144       }
145       catch (Throwable JavaDoc t)
146       {
147          log.warn("Error during destroy for " + name, t);
148       }
149    }
150    
151    protected void remove(ObjectName JavaDoc name)
152    {
153       try
154       {
155          controller.remove(name);
156       }
157       catch (Throwable JavaDoc t)
158       {
159          log.warn("Error during destroy for " + name, t);
160       }
161    }
162 }
163
Popular Tags