1 22 package org.jboss.test.system.controller; 23 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.util.List ; 27 import java.util.ListIterator ; 28 import java.util.concurrent.CopyOnWriteArrayList ; 29 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectName ; 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 41 import org.w3c.dom.Element ; 42 import org.xml.sax.InputSource ; 43 44 50 public class SimpleSARDeployer 51 { 52 private static Logger log = Logger.getLogger(SimpleSARDeployer.class); 53 54 public static ObjectName classLoaderObjectName = ObjectNameFactory.create("test:classloader=test"); 55 56 private ServiceControllerMBean serviceController; 57 58 private List <ObjectName > deployed = new CopyOnWriteArrayList <ObjectName >(); 59 60 private List <ObjectName > tempDeployed = new CopyOnWriteArrayList <ObjectName >(); 61 62 private DocumentBuilder parser; 63 64 public SimpleSARDeployer(MBeanServer server, ServiceControllerMBean serviceController) throws Exception 65 { 66 this.serviceController = serviceController; 67 68 TestClassLoader classLoader = new TestClassLoader(); 69 server.registerMBean(classLoader, classLoaderObjectName); 70 71 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 72 parser = factory.newDocumentBuilder(); 73 } 74 75 public List <ObjectName > deploy(URL url, boolean temporary) throws Exception 76 { 77 long start = System.currentTimeMillis(); 78 79 List <ObjectName > result = doInstall(url, temporary); 80 81 try 82 { 83 create(result); 84 try 85 { 86 start(result); 87 } 88 catch (Throwable t) 89 { 90 stop(result); 91 throw t; 92 } 93 } 94 catch (Throwable 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 <ObjectName > objectNames) 116 { 117 stop(objectNames); 118 destroy(objectNames); 119 remove(objectNames); 120 } 121 122 public void create(List <ObjectName > services) throws Exception 123 { 124 for (ObjectName name: services) 125 serviceController.create(name); 126 } 127 128 public void start(List <ObjectName > services) throws Exception 129 { 130 for (ObjectName name: services) 131 serviceController.start(name); 132 } 133 134 public void stop(List <ObjectName > services) 135 { 136 for (ListIterator <ObjectName > i = services.listIterator(services.size()); i.hasPrevious();) 137 { 138 ObjectName name = i.previous(); 139 try 140 { 141 serviceController.stop(name); 142 } 143 catch (Throwable ignored) 144 { 145 log.debug("Ignored", ignored); 146 } 147 } 148 } 149 150 public void destroy(List <ObjectName > services) 151 { 152 for (ListIterator <ObjectName > i = services.listIterator(services.size()); i.hasPrevious();) 153 { 154 ObjectName name = i.previous(); 155 try 156 { 157 serviceController.destroy(name); 158 } 159 catch (Throwable ignored) 160 { 161 log.debug("Ignored", ignored); 162 } 163 } 164 } 165 166 public void remove(List <ObjectName > services) 167 { 168 for (ObjectName name: services) 169 { 170 deployed.remove(name); 171 tempDeployed.remove(name); 172 try 173 { 174 serviceController.remove(name); 175 } 176 catch (Throwable ignored) 177 { 178 log.debug("Ignored", ignored); 179 } 180 } 181 } 182 183 public List <ObjectName > install(URL url) throws Exception 184 { 185 long start = System.currentTimeMillis(); 186 187 List <ObjectName > 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 <ObjectName > services) 194 { 195 remove(services); 196 } 197 198 protected List <ObjectName > doInstall(URL url, boolean temporary) throws Exception 199 { 200 List <ObjectName > result = null; 201 202 Element element = null; 203 204 try 205 { 206 InputStream stream = url.openStream(); 207 try 208 { 209 InputSource is = new InputSource (stream); 210 is.setSystemId(url.toString()); 211 parser.setEntityResolver(new JBossEntityResolver()); 212 213 Document 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 e) 228 { 229 log.debug("Error deploying: " + url + ": " + e); 230 throw e; 231 } 232 233 return result; 234 } 235 } 236 | Popular Tags |