1 22 package org.jboss.deployment.services; 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.net.URL ; 31 import java.net.URLConnection ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 import javax.enterprise.deploy.shared.ModuleType ; 40 import javax.enterprise.deploy.spi.exceptions.TargetException ; 41 42 import org.jboss.deployers.plugins.structure.AbstractDeploymentContext; 43 import org.jboss.deployers.spi.attachments.Attachments; 44 import org.jboss.deployers.spi.deployment.MainDeployer; 45 import org.jboss.deployers.spi.structure.DeploymentContext; 46 import org.jboss.deployers.spi.structure.DeploymentState; 47 import org.jboss.deployment.spi.SerializableTargetModuleID; 48 import org.jboss.logging.Logger; 49 import org.jboss.util.file.Files; 50 import org.jboss.virtual.VFS; 51 import org.jboss.virtual.VirtualFile; 52 53 59 public class DeploymentManagerService 60 implements DeploymentManagerServiceMBean 61 { 62 private static Logger log = Logger.getLogger(DeploymentManagerService.class); 63 64 67 private Map <String , SerializableTargetModuleID> moduleMap = 68 Collections.synchronizedMap(new HashMap <String , SerializableTargetModuleID>()); 69 70 73 private Class carDeployerType; 74 77 private Class earDeployerType; 78 81 private Class ejbDeployerType; 82 85 private Class rarDeployerType; 86 89 private Class warDeployerType; 90 93 private MainDeployer mainDeployer; 94 95 98 private File uploadDir; 99 private boolean failOnCollision; 100 private boolean deleteOnUndeploy; 101 102 public MainDeployer getMainDeployer() 103 { 104 return mainDeployer; 105 } 106 public void setMainDeployer(MainDeployer mainDeployer) 107 { 108 this.mainDeployer = mainDeployer; 109 } 110 111 public Class getCarDeployerType() 112 { 113 return carDeployerType; 114 } 115 public void setCarDeployerType(Class carDeployerType) 116 { 117 this.carDeployerType = carDeployerType; 118 } 119 public Class getEarDeployerType() 120 { 121 return earDeployerType; 122 } 123 public void setEarDeployerType(Class earDeployerType) 124 { 125 this.earDeployerType = earDeployerType; 126 } 127 public Class getEjbDeployerType() 128 { 129 return ejbDeployerType; 130 } 131 public void setEjbDeployerType(Class ejbDeployerType) 132 { 133 this.ejbDeployerType = ejbDeployerType; 134 } 135 public Class getRarDeployerType() 136 { 137 return rarDeployerType; 138 } 139 public void setRarDeployerType(Class rarDeployerType) 140 { 141 this.rarDeployerType = rarDeployerType; 142 } 143 public Class getWarDeployerType() 144 { 145 return warDeployerType; 146 } 147 public void setWarDeployerType(Class warDeployerType) 148 { 149 this.warDeployerType = warDeployerType; 150 } 151 152 public void setModuleMap(Map <String , SerializableTargetModuleID> moduleMap) 153 { 154 this.moduleMap = moduleMap; 155 } 156 public File getUploadDir() 157 { 158 return this.uploadDir; 159 } 160 161 public void setUploadDir(File uploadDir) 162 { 163 this.uploadDir = uploadDir; 164 } 165 166 public boolean isDeleteOnUndeploy() 167 { 168 return deleteOnUndeploy; 169 } 170 171 public void setDeleteOnUndeploy(boolean deleteOnUndeploy) 172 { 173 this.deleteOnUndeploy = deleteOnUndeploy; 174 } 175 176 public boolean isFailOnCollision() 177 { 178 return failOnCollision; 179 } 180 181 public void setFailOnCollision(boolean failOnCollision) 182 { 183 this.failOnCollision = failOnCollision; 184 } 185 186 public Map getModuleMap() 187 { 188 return Collections.unmodifiableMap(moduleMap); 189 } 190 191 public void deploy(SerializableTargetModuleID moduleID) throws Exception 192 { 193 String url = moduleID.getModuleID(); 194 195 URL deployURL = new URL (url); 197 URLConnection conn = deployURL.openConnection(); 198 int contentLength = conn.getContentLength(); 199 log.debug("Begin deploy, url: " + deployURL + ", contentLength: " + contentLength); 200 201 File path = new File (deployURL.getFile()); 203 String archive = path.getName(); 204 File deployFile = new File (uploadDir, archive); 205 if (failOnCollision && deployFile.exists()) 206 throw new IOException ("deployURL(" + deployURL + ") collides with: " + deployFile.getPath()); 207 208 if (deployFile.exists()) 209 Files.delete(deployFile); 210 211 File parentFile = deployFile.getParentFile(); 212 if (parentFile.exists() == false) 213 { 214 if (parentFile.mkdirs() == false) 215 throw new IOException ("Failed to create local path: " + parentFile); 216 } 217 218 InputStream is = conn.getInputStream(); 219 BufferedInputStream bis = new BufferedInputStream (is); 220 byte[] buffer = new byte[4096]; 221 FileOutputStream fos = new FileOutputStream (deployFile); 222 BufferedOutputStream bos = new BufferedOutputStream (fos); 223 int count = 0; 224 while ((count = bis.read(buffer)) > 0) 225 { 226 bos.write(buffer, 0, count); 227 } 228 bis.close(); 229 bos.close(); 230 231 moduleMap.put(url, moduleID); 232 } 233 234 public void start(String url) throws Exception 235 { 236 SerializableTargetModuleID moduleID = (SerializableTargetModuleID)moduleMap.get(url); 237 if (moduleID == null) 238 throw new IOException ("deployURL(" + url + ") has not been distributed"); 239 240 URL deployURL = new URL (url); 241 File path = new File (deployURL.getFile()); 243 String archive = path.getName(); 244 File deployFile = new File (uploadDir, archive); 245 if (deployFile.exists() == false) 246 throw new IOException ("deployURL(" + url + ") has no local archive"); 247 248 VirtualFile root = VFS.getRoot(deployFile.toURI()); 249 DeploymentContext context = new AbstractDeploymentContext(root); 250 mainDeployer.addDeploymentContext(context); 251 mainDeployer.process(); 252 moduleID.setRunning(true); 253 moduleID.clearChildModuleIDs(); 254 fillChildrenTargetModuleID(moduleID, context); 256 } 257 258 public void stop(String url) throws Exception 259 { 260 SerializableTargetModuleID moduleID = (SerializableTargetModuleID)moduleMap.get(url); 261 if (moduleID == null) 262 throw new IOException ("deployURL(" + url + ") has not been distributed"); 263 264 URL deployURL = new URL (url); 265 File path = new File (deployURL.getFile()); 267 String archive = path.getName(); 268 File deployFile = new File (uploadDir, archive); 269 if (deployFile.exists() == false) 270 throw new IOException ("deployURL(" + url + ") has no local archive"); 271 272 VirtualFile root = VFS.getRoot(deployFile.toURI()); 273 mainDeployer.removeDeploymentContext(AbstractDeploymentContext.getDeploymentName(root)); 274 mainDeployer.process(); 275 moduleID.setRunning(false); 276 } 277 278 public void undeploy(String url) throws Exception 279 { 280 SerializableTargetModuleID moduleID = (SerializableTargetModuleID)moduleMap.get(url); 281 if (moduleID == null) 282 return; 283 284 if (moduleID.isRunning()) 286 stop(url); 287 288 URL deployURL = new URL (url); 289 File path = new File (deployURL.getFile()); 291 String archive = path.getName(); 292 File deployFile = new File (uploadDir, archive); 293 if (deployFile.exists() == false) 294 throw new IOException ("deployURL(" + url + ") has not been distributed"); 295 296 if (deleteOnUndeploy) 297 Files.delete(deployFile); 298 299 moduleMap.remove(url); 300 } 301 302 public SerializableTargetModuleID[] getAvailableModules(int moduleType) 303 throws TargetException 304 { 305 ArrayList <SerializableTargetModuleID> matches = new ArrayList <SerializableTargetModuleID>(); 306 Iterator <SerializableTargetModuleID> modules = moduleMap.values().iterator(); 307 while (modules.hasNext()) 308 { 309 SerializableTargetModuleID module = modules.next(); 310 if (module.getModuleType() == moduleType) 311 matches.add(module); 312 } 313 314 SerializableTargetModuleID[] ids = new SerializableTargetModuleID[matches.size()]; 315 matches.toArray(ids); 316 return ids; 317 } 318 319 private void fillChildrenTargetModuleID(SerializableTargetModuleID moduleID, 320 DeploymentContext info) 321 { 322 Set <DeploymentContext> children = info.getChildren(); 323 for(DeploymentContext ctx : children) 324 { 325 try 326 { 327 ModuleType type = getModuleType(ctx); 328 if (type != null) 330 { 331 String module = ctx.getName(); 332 boolean isRunning = ctx.getState() == DeploymentState.DEPLOYED; 334 SerializableTargetModuleID child = new SerializableTargetModuleID(moduleID, module, type.getValue(), isRunning); 335 moduleID.addChildTargetModuleID(child); 336 fillChildrenTargetModuleID(child, ctx); 337 } 338 } 339 catch (UnsupportedOperationException e) 340 { 341 if (log.isTraceEnabled()) 342 log.trace("Ignoring", e); 343 } 344 } 345 } 346 347 354 private ModuleType getModuleType(DeploymentContext info) 355 { 356 ModuleType type = null; 357 358 Attachments attachments = info.getTransientAttachments(); 359 if (attachments.getAttachment(carDeployerType) != null) 360 { 361 type = ModuleType.CAR; 362 } 363 else if (attachments.getAttachment(ejbDeployerType) != null) 364 { 365 type = ModuleType.EJB; 366 } 367 else if (attachments.getAttachment(earDeployerType) != null) 368 { 369 type = ModuleType.EAR; 370 } 371 else if (attachments.getAttachment(rarDeployerType) != null) 372 { 373 type = ModuleType.RAR; 374 } 375 else if (attachments.getAttachment(warDeployerType) != null) 376 { 377 type = ModuleType.WAR; 378 } 379 380 return type; 381 } 382 383 } 384 | Popular Tags |