1 22 package org.jboss.varia.deployment; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 31 import java.net.URL ; 32 import java.net.MalformedURLException ; 33 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.ListIterator ; 39 import java.util.Enumeration ; 40 import java.util.HashMap ; 41 import java.util.Map ; 42 import java.util.jar.JarFile ; 43 import java.util.jar.JarEntry ; 44 45 import javax.management.MBeanServer ; 46 import javax.management.MalformedObjectNameException ; 47 import javax.management.ObjectName ; 48 49 import org.jboss.deployment.DeploymentException; 50 import org.jboss.deployment.DeploymentInfo; 51 import org.jboss.deployment.MainDeployerMBean; 52 import org.jboss.deployment.SubDeployer; 53 import org.jboss.deployment.SubDeployerSupport; 54 55 import org.jboss.system.ServiceControllerMBean; 56 import org.jboss.system.server.ServerConfig; 57 import org.jboss.system.server.ServerConfigLocator; 58 59 import org.jboss.util.Counter; 60 import org.jboss.util.file.Files; 61 import org.jboss.util.file.JarUtils; 62 import org.jboss.mx.util.MBeanProxyExt; 63 64 import org.jboss.varia.deployment.convertor.Convertor; 65 66 80 public class FoeDeployer 81 extends SubDeployerSupport 82 implements SubDeployer, FoeDeployerMBean 83 { 84 86 private ServiceControllerMBean serviceController; 87 88 89 private File scratchDirectory; 90 91 92 private List converterList = new ArrayList (); 93 94 95 private final Counter id = Counter.makeSynchronized(new Counter(0)); 96 97 98 private ThreadLocal destinationByDI = new ThreadLocal () { 99 protected Object initialValue() 100 { 101 return new HashMap (); 102 } 103 }; 104 105 112 public boolean accepts(DeploymentInfo di) 113 { 114 Iterator i = converterList.iterator(); 116 while(i.hasNext()) 117 { 118 Convertor converter = (Convertor)i.next(); 119 if(converter.accepts(di.url)) 120 { 121 return true; 122 } 123 } 124 return false; 125 } 126 127 131 public boolean accepts(URL url) 132 { 133 Iterator i = converterList.iterator(); 135 while(i.hasNext()) 136 { 137 Convertor converter = (Convertor)i.next(); 138 if(converter.accepts(url)) 139 { 140 return true; 141 } 142 } 143 return false; 144 } 145 146 150 public void init(DeploymentInfo di) 151 throws DeploymentException 152 { 153 Map destinations = (Map )destinationByDI.get(); 155 File destination = (File )destinations.get(di.parent); 156 if(destination == null) 157 { 158 while(destination == null || destination.exists()) 160 destination = new File (scratchDirectory, id.increment() + "." + di.shortName); 161 } 162 else 163 { 164 destination = new File (destination, di.shortName); 165 } 166 destinations.put(di, destination); 167 destinationByDI.set(destinations); 168 169 try 170 { 171 log.debug("unpacking to " + destination); 172 inflateJar(di.localUrl, destination); 173 } 174 catch(Exception e) 175 { 176 throw new DeploymentException("Unpacking failed: ", e); 177 } 178 179 super.init(di); 181 } 182 183 187 public void create(DeploymentInfo di) 188 throws DeploymentException 189 { 190 try 191 { 192 Map destinations = (Map )destinationByDI.get(); 194 File inflateDest = (File )destinations.get(di); 195 196 Iterator i = converterList.iterator(); 199 while(i.hasNext()) 200 { 201 Convertor converter = (Convertor)i.next(); 202 if(converter.accepts(di.url)) 203 { 204 converter.convert(di, inflateDest); 206 break; 208 } 209 } 210 211 File deflateDest = (File )destinations.get(di.parent); 213 if(deflateDest == null) 214 deflateDest = scratchDirectory; 215 String validName = null; 216 if(di.shortName.endsWith(".wl")) 217 validName = di.shortName.substring(0, di.shortName.length()-3); 218 else 219 validName = di.shortName.substring( 0, di.shortName.length() - 4 ) + "jar"; 220 File convertedUnit = new File (deflateDest, validName); 221 log.debug("deflating to " + convertedUnit); 222 deflateJar(convertedUnit, inflateDest); 223 224 Files.delete(inflateDest); 226 227 if(di.parent == null) 229 copyFile(convertedUnit, new File (di.url.getFile()).getParentFile()); 230 } 231 catch(Exception e) 232 { 233 log.error("Conversion error: ", e); 234 } 235 } 236 237 242 public void start(DeploymentInfo di) 243 throws DeploymentException 244 { 245 stop(di); 246 destroy(di); 247 } 248 249 252 public void stop(DeploymentInfo di) 253 { 254 log.debug("undeploying application: " + di.url); 255 } 256 257 260 public void destroy(DeploymentInfo di) 261 { 262 List services = di.mbeans; 263 int lastService = services.size(); 264 for(ListIterator i = services.listIterator(lastService); i.hasPrevious();) 265 { 266 ObjectName name = (ObjectName )i.previous(); 267 log.debug( "destroying mbean " + name ); 268 try 269 { 270 serviceController.destroy(name); 271 } 272 catch(Exception e) 273 { 274 log.error("Could not destroy mbean: " + name, e); 275 } 276 } 277 278 for(ListIterator i = services.listIterator(lastService); i.hasPrevious();) 279 { 280 ObjectName name = (ObjectName )i.previous(); 281 log.debug("removing mbean " + name); 282 try 283 { 284 serviceController.remove( name ); 285 } 286 catch(Exception e) 287 { 288 log.error("Could not remove mbean: " + name, e); 289 } 290 } 291 } 292 293 297 protected void addDeployableJar(DeploymentInfo di, JarFile jarFile) 298 throws DeploymentException 299 { 300 String urlPrefix = "jar:" + di.localUrl.toString() + "!/"; 301 for(Enumeration e = jarFile.entries(); e.hasMoreElements();) 302 { 303 JarEntry entry = (JarEntry )e.nextElement(); 304 String name = entry.getName(); 305 try 306 { 307 URL url = new URL (urlPrefix + name); 308 if(isDeployable(name, url)) 309 { 310 URL nestedURL = JarUtils.extractNestedJar(url, this.tempDeployDir); 314 File file = new File (nestedURL.getFile()); 315 File wlFile = new File (nestedURL.getFile() + ".wl"); 316 file.renameTo(wlFile); 317 318 if(accepts(wlFile.toURL())) 319 { 320 deployUrl(di, wlFile.toURL(), name + ".wl"); 321 } 322 else 323 { 324 wlFile.renameTo(new File (nestedURL.getFile())); 326 } 327 } 328 } 329 catch(MalformedURLException mue) 330 { 331 log.warn("Jar entry invalid; ignoring: " + name, mue); 332 } 333 catch(IOException ex) 334 { 335 log.warn("Failed to extract nested jar; ignoring: " + name, ex); 336 } 337 } 338 } 339 340 347 protected void startService() 348 throws Exception 349 { 350 mainDeployer = (MainDeployerMBean) MBeanProxyExt.create( 351 MainDeployerMBean.class, 352 MainDeployerMBean.OBJECT_NAME, 353 server 354 ); 355 356 serviceController = (ServiceControllerMBean) MBeanProxyExt.create( 358 ServiceControllerMBean.class, 359 ServiceControllerMBean.OBJECT_NAME, 360 server 361 ); 362 363 ServerConfig config = ServerConfigLocator.locate(); 364 365 File tempDirectory = config.getServerTempDir(); 367 scratchDirectory = new File (tempDirectory, "foe"); 368 if(!scratchDirectory.exists()) 369 scratchDirectory.mkdirs(); 370 371 super.startService(); 374 } 375 376 379 protected ObjectName getObjectName(MBeanServer server, ObjectName name) 380 throws MalformedObjectNameException 381 { 382 return name == null ? OBJECT_NAME : name; 383 } 384 385 396 public void addConvertor(Convertor converter) 397 { 398 converterList.add(converter); 399 400 Collection waitingDeployments = mainDeployer.listWaitingForDeployer(); 404 if((waitingDeployments != null) && (waitingDeployments.size() > 0)) 405 { 406 for( Iterator iter = waitingDeployments.iterator(); iter.hasNext(); ) 407 { 408 DeploymentInfo di = (DeploymentInfo)iter.next(); 409 410 if(!converter.accepts(di.url)) 412 continue; 413 414 log.debug("trying to deploy with new converter: " + di.shortName); 415 try 416 { 417 mainDeployer.redeploy(di); 418 } 419 catch (DeploymentException e) 420 { 421 log.error("DeploymentException while trying to deploy a package with new converter", e); 422 } 423 } 424 } 425 } 426 427 437 public void removeConvertor(Convertor converter) 438 { 439 converterList.remove(converter); 440 } 441 442 453 protected void inflateJar( URL fileURL, File destinationDirectory ) 454 throws DeploymentException, IOException 455 { 456 File destFile = new File (fileURL.getFile()); 457 InputStream input = new FileInputStream (fileURL.getFile()); 458 JarUtils.unjar(input, destinationDirectory); 459 } 461 462 469 private void deflateJar( File jarFile, File root ) 470 throws Exception 471 { 472 OutputStream output = new FileOutputStream (jarFile); 473 JarUtils.jar(output, root.listFiles(), null, null, null); 474 output.close(); 475 } 476 477 483 private void copyFile(File source, File destinationDirectory) 484 throws Exception 485 { 486 File target = new File ( destinationDirectory, source.getName() ); 487 Files.copy(source, target); 490 } 491 } 492 | Popular Tags |