KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > system > controller > SimpleSARDeployer


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.test.system.controller;
23
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ListIterator JavaDoc;
28 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
29
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34
35 import org.jboss.deployment.DeploymentException;
36 import org.jboss.logging.Logger;
37 import org.jboss.mx.util.ObjectNameFactory;
38 import org.jboss.system.ServiceControllerMBean;
39 import org.jboss.util.xml.JBossEntityResolver;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.xml.sax.InputSource JavaDoc;
43
44 /**
45  * SimpleSARDeployer.
46  *
47  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
48  * @version $Revision: 1.1 $
49  */

50 public class SimpleSARDeployer
51 {
52    private static Logger log = Logger.getLogger(SimpleSARDeployer.class);
53    
54    public static ObjectName JavaDoc classLoaderObjectName = ObjectNameFactory.create("test:classloader=test");
55    
56    private ServiceControllerMBean serviceController;
57
58    private List JavaDoc<ObjectName JavaDoc> deployed = new CopyOnWriteArrayList JavaDoc<ObjectName JavaDoc>();
59
60    private List JavaDoc<ObjectName JavaDoc> tempDeployed = new CopyOnWriteArrayList JavaDoc<ObjectName JavaDoc>();
61    
62    private DocumentBuilder JavaDoc parser;
63    
64    public SimpleSARDeployer(MBeanServer JavaDoc server, ServiceControllerMBean serviceController) throws Exception JavaDoc
65    {
66       this.serviceController = serviceController;
67       
68       TestClassLoader classLoader = new TestClassLoader();
69       server.registerMBean(classLoader, classLoaderObjectName);
70
71       DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
72       parser = factory.newDocumentBuilder();
73    }
74
75    public List JavaDoc<ObjectName JavaDoc> deploy(URL JavaDoc url, boolean temporary) throws Exception JavaDoc
76    {
77       long start = System.currentTimeMillis();
78       
79       List JavaDoc<ObjectName JavaDoc> result = doInstall(url, temporary);
80
81       try
82       {
83          create(result);
84          try
85          {
86             start(result);
87          }
88          catch (Throwable JavaDoc t)
89          {
90             stop(result);
91             throw t;
92          }
93       }
94       catch (Throwable JavaDoc t)
95       {
96          destroy(result);
97          remove(result);
98          DeploymentException.rethrowAsDeploymentException("Error", t);
99       }
100       
101       log.debug("Deployed " + url + " took " + (System.currentTimeMillis() - start) + "ms");
102       return result;
103    }
104    
105    public void uninstall()
106    {
107       undeploy(deployed);
108    }
109    
110    public void uninstallTemporary()
111    {
112       undeploy(tempDeployed);
113    }
114    
115    public void undeploy(List JavaDoc<ObjectName JavaDoc> objectNames)
116    {
117       stop(objectNames);
118       destroy(objectNames);
119       remove(objectNames);
120    }
121    
122    public void create(List JavaDoc<ObjectName JavaDoc> services) throws Exception JavaDoc
123    {
124       for (ObjectName JavaDoc name: services)
125          serviceController.create(name);
126    }
127    
128    public void start(List JavaDoc<ObjectName JavaDoc> services) throws Exception JavaDoc
129    {
130       for (ObjectName JavaDoc name: services)
131          serviceController.start(name);
132    }
133    
134    public void stop(List JavaDoc<ObjectName JavaDoc> services)
135    {
136       for (ListIterator JavaDoc<ObjectName JavaDoc> i = services.listIterator(services.size()); i.hasPrevious();)
137       {
138          ObjectName JavaDoc name = i.previous();
139          try
140          {
141             serviceController.stop(name);
142          }
143          catch (Throwable JavaDoc ignored)
144          {
145             log.debug("Ignored", ignored);
146          }
147       }
148    }
149    
150    public void destroy(List JavaDoc<ObjectName JavaDoc> services)
151    {
152       for (ListIterator JavaDoc<ObjectName JavaDoc> i = services.listIterator(services.size()); i.hasPrevious();)
153       {
154          ObjectName JavaDoc name = i.previous();
155          try
156          {
157             serviceController.destroy(name);
158          }
159          catch (Throwable JavaDoc ignored)
160          {
161             log.debug("Ignored", ignored);
162          }
163       }
164    }
165    
166    public void remove(List JavaDoc<ObjectName JavaDoc> services)
167    {
168       for (ObjectName JavaDoc name: services)
169       {
170          deployed.remove(name);
171          tempDeployed.remove(name);
172          try
173          {
174             serviceController.remove(name);
175          }
176          catch (Throwable JavaDoc ignored)
177          {
178             log.debug("Ignored", ignored);
179          }
180       }
181    }
182    
183    public List JavaDoc<ObjectName JavaDoc> install(URL JavaDoc url) throws Exception JavaDoc
184    {
185       long start = System.currentTimeMillis();
186       
187       List JavaDoc<ObjectName JavaDoc> result = doInstall(url, true);
188       
189       log.debug("Deployed " + url + " took " + (System.currentTimeMillis() - start) + "ms");
190       return result;
191    }
192    
193    public void uninstall(List JavaDoc<ObjectName JavaDoc> services)
194    {
195       remove(services);
196    }
197    
198    protected List JavaDoc<ObjectName JavaDoc> doInstall(URL JavaDoc url, boolean temporary) throws Exception JavaDoc
199    {
200       List JavaDoc<ObjectName JavaDoc> result = null;
201       
202       Element JavaDoc element = null;
203
204       try
205       {
206          InputStream JavaDoc stream = url.openStream();
207          try
208          {
209             InputSource JavaDoc is = new InputSource JavaDoc(stream);
210             is.setSystemId(url.toString());
211             parser.setEntityResolver(new JBossEntityResolver());
212
213             Document JavaDoc document = parser.parse(is);
214             element = document.getDocumentElement();
215          }
216          finally
217          {
218             stream.close();
219          }
220
221          result = serviceController.install(element, classLoaderObjectName);
222
223          deployed.addAll(result);
224          if (temporary)
225             tempDeployed.addAll(result);
226       }
227       catch (Exception JavaDoc e)
228       {
229          log.debug("Error deploying: " + url + ": " + e);
230          throw e;
231       }
232       
233       return result;
234    }
235 }
236
Popular Tags