1 26 27 package com.rift.coad.lib.deployment; 29 30 import java.lang.ClassLoader ; 32 import java.io.*; 33 import java.io.File ; 34 import java.io.InputStream ; 35 import java.io.OutputStream ; 36 import java.io.FileInputStream ; 37 import java.io.FileOutputStream ; 38 import java.net.URL ; 39 import java.net.URLClassLoader ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 44 import org.apache.log4j.Logger; 46 47 import com.rift.coad.lib.common.ClassUtil; 49 import com.rift.coad.lib.common.FileUtil; 50 import com.rift.coad.lib.common.JarUtil; 51 import com.rift.coad.lib.common.RandomGuid; 52 import com.rift.coad.lib.common.ResourceReader; 53 import com.rift.coad.lib.configuration.Configuration; 54 import com.rift.coad.lib.configuration.ConfigurationFactory; 55 import com.rift.coad.lib.deployment.rmi.TieGenerator; 56 import com.rift.coad.lib.loader.MasterClassLoader; 57 import com.rift.coad.lib.thirdparty.ant.CopyFile; 58 import com.rift.coad.lib.thirdparty.ant.JAR; 59 import com.rift.coad.lib.thirdparty.ant.JavaC; 60 import com.rift.coad.lib.thirdparty.ant.RMIC; 61 62 66 public class DeploymentLoader { 67 68 72 public static class ClassLoaderLookup { 73 private static ClassLoaderLookup singleton = null; 75 76 private Map lookupMap = new HashMap (); 78 79 82 private ClassLoaderLookup() { 83 84 } 85 86 87 93 public static synchronized ClassLoaderLookup getInstance() { 94 if (singleton == null) { 95 singleton = new ClassLoaderLookup(); 96 } 97 return singleton; 98 } 99 100 101 106 protected void addClassLoader(DeploymentLoader loader) { 107 lookupMap.put(loader.getClassLoader(), 108 loader.getClientStubCodeName()); 109 } 110 111 112 117 protected void removeClassLoader(DeploymentLoader loader) { 118 lookupMap.remove(loader.getClassLoader()); 119 } 120 121 122 128 public String getStubCodeForLoader(ClassLoader loader) { 129 return (String )lookupMap.get(loader); 130 } 131 132 } 133 134 public static final String META_FILE = "META-INF/coadunation.xml"; 136 public static final String TEMP_DIRECTORY = "temp_dir"; 137 private static final String PREFIX = "tmp"; 138 private static final String SUFFIX = ".jar"; 139 private static final String CLIENT_STUB = "Client_Stub"; 140 private static final String SUB_SUFFIX = "_Stub" + SUFFIX; 141 142 protected Logger log = 144 Logger.getLogger(DeploymentLoader.class.getName()); 145 146 private Configuration config = null; 148 private File file = null; 149 private File tmpDir = null; 150 private URLClassLoader classLoader = null; 151 private DeploymentInfo deploymentInfo = null; 152 private long lastModified = 0; 153 private boolean successful = false; 154 private String clientStubCodeName = null; 155 156 162 public DeploymentLoader(File file) throws DeploymentException { 163 try { 164 this.file = file; 166 lastModified = file.lastModified(); 167 168 config = ConfigurationFactory.getInstance(). 170 getConfig(this.getClass()); 171 172 tmpDir = createTmpDir(file); 174 175 createStubs(tmpDir); 177 178 File [] jars = FileUtil.filter(tmpDir.listFiles(),".jar"); 180 181 URL [] urlList = new URL [jars.length + 1]; 183 urlList[0] = tmpDir.toURL(); 184 log.info("Load url :" + tmpDir.toURL()); 185 for (int index = 0; index < jars.length; index++) { 186 log.info("Load url :" + jars[index].toURL().toString()); 187 urlList[index + 1] = jars[index].toURL(); 188 } 189 190 classLoader = new URLClassLoader (urlList, 192 MasterClassLoader.getInstance().getLoader()); 193 194 String xmlSource = new ResourceReader(META_FILE,classLoader) 196 .getDocument(); 197 198 CoadunationParser xmlParser = new CoadunationParser(xmlSource); 200 deploymentInfo = xmlParser.getDeploymentInfo(); 201 202 successful = true; 204 205 ClassLoaderLookup.getInstance().addClassLoader(this); 207 } catch (Exception ex) { 208 log.error("Failed to load the file [" + 209 file.toString() + "] :" + ex.getMessage(),ex); 210 } 211 } 212 213 214 219 public File getFile() { 220 return file; 221 } 222 223 224 229 public File getTmpDir() { 230 return tmpDir; 231 } 232 233 234 240 public long getLastModified() { 241 return lastModified; 242 } 243 244 245 250 public DeploymentInfo getDeploymentInfo() { 251 return deploymentInfo; 252 } 253 254 255 262 public Class getClass(String className) throws DeploymentException { 263 try { 264 return classLoader.loadClass(className); 265 } catch (Exception ex) { 266 throw new DeploymentException("Failed to load the class : " + 267 className,ex); 268 } 269 } 270 271 272 277 public ClassLoader getClassLoader() { 278 return classLoader; 279 } 280 281 282 287 public boolean getSuccessful() { 288 return successful; 289 } 290 291 296 public String getClientStubCodeName() { 297 return clientStubCodeName; 298 } 299 300 301 307 public int hashCode() { 308 return file.getPath().hashCode(); 309 } 310 311 312 318 public boolean equals(Object obj) { 319 if (!(obj instanceof DeploymentLoader)) { 320 return false; 321 } 322 DeploymentLoader loader = (DeploymentLoader)obj; 323 if (null == loader) { 324 return false; 325 } 326 if (loader.getFile().getPath().equals(file.getPath())) { 327 return true; 328 } 329 return false; 330 } 331 332 333 338 private File createTmpDir(File source) throws DeploymentException { 339 try { 340 File tmpDir = new File (config.getString(TEMP_DIRECTORY), 341 RandomGuid.getInstance().getGuid()); 342 if (tmpDir.mkdirs() == false) { 343 throw new DeploymentException("Failed to create the director [" 344 + tmpDir.getAbsolutePath() + "]"); 345 } 346 JarUtil.extract(source,tmpDir); 347 return tmpDir; 348 } catch (DeploymentException ex) { 349 throw ex; 350 } catch (Exception ex) { 351 log.error("Failed to create the temporary directory : " + 352 ex.getMessage(),ex); 353 throw new DeploymentException( 354 "Failed to create the temporary directory : " + 355 ex.getMessage(), ex); 356 } 357 } 358 359 360 367 private void createStubs(File tmpDir) throws DeploymentException { 368 try { 369 File stubDir = new File (tmpDir, 370 RandomGuid.getInstance().getGuid()); 371 if (stubDir.mkdirs() == false) { 372 throw new DeploymentException("Failed to create the stub " + 373 "directory [" + stubDir.getAbsolutePath() + "]"); 374 } 375 376 File [] jars = FileUtil.filter(tmpDir.listFiles(),".jar"); 378 379 File [] classPath = new File [jars.length + 2]; 381 classPath[0] = tmpDir; 382 for (int index = 0; index < jars.length; index++) { 383 classPath[index + 1] = jars[index]; 384 } 385 classPath[classPath.length - 1] = stubDir; 386 387 createTies(tmpDir,tmpDir,stubDir); 388 389 395 396 JavaC javaC = new JavaC(classPath,stubDir,stubDir); 398 javaC.compileClasses(); 399 400 RMIC rmic = new RMIC(classPath,stubDir,"**/*.class",stubDir); 402 rmic.parse(); 403 404 String clientFileName = file.getName(); 406 clientFileName = clientFileName.substring(0, 407 clientFileName.indexOf('.')) + SUB_SUFFIX; 408 File stubJar = new File (tmpDir.getPath() + 409 File.separator + clientFileName); 410 JAR jar = new JAR(stubDir,stubJar); 411 jar.archive(); 412 413 File clientStubJar = new File (config.getString(CLIENT_STUB) + 414 File.separator + clientFileName); 415 CopyFile copyFile = new CopyFile(stubJar,clientStubJar); 416 copyFile.copy(); 417 clientStubCodeName = clientFileName; 418 } catch (Exception ex) { 419 log.error("Failed to create the stub : " + ex.getMessage(), 420 ex); 421 throw new DeploymentException( 422 "Failed to create the stub : " + ex.getMessage(), 423 ex); 424 } 425 } 426 427 428 436 private void createTies(File tmpDir, File path, 437 File stubDir) throws DeploymentException { 438 try { 439 URLClassLoader classLoader = new URLClassLoader (new URL [] 441 {path.toURL()},MasterClassLoader.getInstance().getLoader()); 442 443 if (classLoader.getResource(META_FILE) == null) { 445 return; 447 } 448 449 String xmlSource = new ResourceReader(META_FILE,classLoader). 451 getDocument(); 452 453 CoadunationParser xmlParser = new CoadunationParser(xmlSource); 455 DeploymentInfo deploymentInfo = xmlParser.getDeploymentInfo(); 456 457 Map beans = deploymentInfo.getBeans(); 459 Iterator iter = beans.keySet().iterator(); 460 while(iter.hasNext()) { 461 BeanInfo beanInfo = (BeanInfo)beans.get(iter.next()); 462 TieGenerator tieGenerator = new TieGenerator(tmpDir,stubDir, 463 beanInfo); 464 tieGenerator.generate(); 465 } 466 467 beans = deploymentInfo.getJmxBeans(); 468 iter = beans.keySet().iterator(); 469 while(iter.hasNext()) { 470 JMXBeanInfo beanInfo = (JMXBeanInfo)beans.get(iter.next()); 471 TieGenerator tieGenerator = new TieGenerator(tmpDir,stubDir, 472 beanInfo); 473 tieGenerator.generate(); 474 } 475 476 } catch (Exception ex) { 477 log.error("Failed to create the ties for [" + path.getPath() 478 + "] because : " + ex.getMessage(), ex); 479 throw new DeploymentException( 480 "Failed to create the ties for [" + path.getPath() 481 + "] because : " + ex.getMessage(), ex); 482 } 483 } 484 485 } 486 | Popular Tags |