KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
25
26 import javax.management.ObjectName JavaDoc;
27
28 import org.jboss.deployers.plugins.deployers.helpers.AbstractComponentDeployer;
29 import org.jboss.deployers.plugins.deployers.helpers.SimpleDeploymentVisitor;
30 import org.jboss.deployers.spi.DeploymentException;
31 import org.jboss.deployers.spi.deployer.DeploymentUnit;
32 import org.jboss.system.metadata.ServiceDeployment;
33 import org.jboss.system.metadata.ServiceMetaData;
34 import org.jboss.system.metadata.ServiceMetaDataParser;
35 import org.w3c.dom.Element JavaDoc;
36
37 /**
38  * ServiceDeployer.<p>
39  *
40  * This deployer is responsible for deploying services of
41  * type {@link ServiceDeployment}.
42  *
43  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 1.1 $
45  */

46 public class ServiceDeploymentDeployer extends AbstractComponentDeployer<ServiceDeployment, ServiceMetaData>
47 {
48    /**
49     * Create a new ServiceDeploymentDeployer.
50     */

51    public ServiceDeploymentDeployer()
52    {
53       setDeploymentVisitor(new ServiceDeploymentVisitor());
54       setComponentVisitor(new ServiceMetaDataVisitor());
55    }
56
57    protected static void addServiceComponent(DeploymentUnit unit, ServiceMetaData service)
58    {
59       ObjectName JavaDoc objectName = service.getObjectName();
60       String JavaDoc name = objectName.getCanonicalName();
61       DeploymentUnit component = unit.addComponent(name);
62       component.addAttachment(ServiceMetaData.class.getName(), service);
63    }
64
65    protected static void removeServiceComponent(DeploymentUnit unit, ServiceMetaData service)
66    {
67       ObjectName JavaDoc objectName = service.getObjectName();
68       String JavaDoc name = objectName.getCanonicalName();
69       unit.removeComponent(name);
70    }
71    
72    /**
73     * ServiceDeploymentVisitor.
74     */

75    public class ServiceDeploymentVisitor implements SimpleDeploymentVisitor<ServiceDeployment>
76    {
77       public Class JavaDoc<ServiceDeployment> getVisitorType()
78       {
79          return ServiceDeployment.class;
80       }
81
82       public void deploy(DeploymentUnit unit, ServiceDeployment deployment) throws DeploymentException
83       {
84          try
85          {
86             List JavaDoc<ServiceMetaData> services = deployment.getServices();
87             if (services == null)
88             {
89                Element JavaDoc config = deployment.getConfig();
90                if (config == null)
91                {
92                   log.debug("Service deployment has no services: " + deployment.getName());
93                   return;
94                }
95                
96                ServiceMetaDataParser parser = new ServiceMetaDataParser(config);
97                services = parser.parse();
98                deployment.setServices(services);
99             }
100
101             if (services == null || services.isEmpty())
102                return;
103             
104             for (ServiceMetaData service : services)
105                addServiceComponent(unit, service);
106          }
107          catch (Throwable JavaDoc t)
108          {
109             throw DeploymentException.rethrowAsDeploymentException("Error deploying: " + deployment.getName(), t);
110          }
111       }
112
113       public void undeploy(DeploymentUnit unit, ServiceDeployment deployment)
114       {
115          List JavaDoc<ServiceMetaData> services = deployment.getServices();
116          if (services == null)
117             return;
118          
119          for (ServiceMetaData service : services)
120          {
121             ObjectName JavaDoc objectName = service.getObjectName();
122             String JavaDoc name = objectName.getCanonicalName();
123             unit.removeComponent(name);
124          }
125       }
126    }
127
128    /**
129     * ServiceMetaDataVisitor.
130     */

131    public static class ServiceMetaDataVisitor implements SimpleDeploymentVisitor<ServiceMetaData>
132    {
133       public Class JavaDoc<ServiceMetaData> getVisitorType()
134       {
135          return ServiceMetaData.class;
136       }
137
138       public void deploy(DeploymentUnit unit, ServiceMetaData deployment) throws DeploymentException
139       {
140          addServiceComponent(unit, deployment);
141       }
142
143       public void undeploy(DeploymentUnit unit, ServiceMetaData deployment)
144       {
145          removeServiceComponent(unit, deployment);
146       }
147    }
148 }
149
Popular Tags