1 23 24 package com.sun.enterprise.appclient; 25 26 import com.sun.enterprise.appclient.jws.Util; 27 import com.sun.enterprise.appclient.jws.boot.JWSACCMain; 28 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException; 29 import com.sun.enterprise.deployment.ApplicationClientDescriptor; 30 import com.sun.enterprise.deployment.archivist.Archivist; 31 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 32 import com.sun.enterprise.deployment.RootDeploymentDescriptor; 33 import com.sun.enterprise.deployment.ServiceReferenceDescriptor; 34 import com.sun.enterprise.deployment.util.AnnotationDetector; 35 import com.sun.enterprise.deployment.util.AppClientPersistenceDependencyAnnotationDetector; 36 import com.sun.enterprise.loader.EJBClassLoader; 37 import com.sun.enterprise.loader.InstrumentableClassLoader; 38 import com.sun.enterprise.util.FileUtil; 39 import com.sun.enterprise.util.i18n.StringManager; 40 import java.io.File ; 41 import java.io.FileNotFoundException ; 42 import java.io.IOException ; 43 import java.io.PrintStream ; 44 import java.net.MalformedURLException ; 45 import java.net.URI ; 46 import java.net.URISyntaxException ; 47 import java.net.URL ; 48 import java.util.ArrayList ; 49 import java.util.Iterator ; 50 import java.util.List ; 51 import java.util.Properties ; 52 import java.util.logging.Level ; 53 import java.util.logging.Logger ; 54 import org.xml.sax.SAXParseException ; 55 56 63 64 public abstract class AppClientInfo { 65 66 public static final String USER_CODE_IS_SIGNED_PROPERTYNAME = "com.sun.aas.user.code.signed"; 67 68 private static final String SIGNED_USER_CODE_PERMISSION_TEMPLATE_NAME = "jwsclientSigned.policy"; 69 70 private static final String UNSIGNED_USER_CODE_PERMISSION_TEMPLATE_NAME = "jwsclientUnsigned.policy"; 71 72 private static final String CODE_BASE_PLACEHOLDER_NAME = "com.sun.aas.jws.client.codeBase"; 73 74 75 protected Logger _logger; 76 77 78 private AbstractArchive appClientArchive = null; 79 80 81 private File appClientFile = null; 82 83 85 private Archivist archivist = null; 86 87 88 protected String mainClassFromCommandLine; 89 90 94 protected String mainClassNameToRun = null; 95 96 97 private ClassLoader classLoader = null; 98 99 100 protected boolean isJWS = false; 101 102 103 protected PersistenceUnitLoaderImpl puLoader = null; 104 105 106 protected StringManager localStrings = 107 StringManager.getManager(MainWithModuleSupport.class); 108 109 125 public AppClientInfo(boolean isJWS, Logger logger, File appClientFile, 126 Archivist archivist, String mainClassFromCommandLine) { 127 this.isJWS = isJWS; 128 _logger = logger; 129 this.appClientFile = appClientFile; 130 this.archivist = archivist; 131 this.mainClassFromCommandLine = mainClassFromCommandLine; 132 } 133 134 145 protected void completeInit() 146 throws IOException , SAXParseException , ClassNotFoundException , 147 URISyntaxException , AnnotationProcessorException, Exception { 148 149 appClientArchive = expand(appClientFile); 151 152 classLoader = createClassLoader(appClientArchive); 155 156 RootDeploymentDescriptor descriptor = 160 populateDescriptor(appClientArchive, archivist, classLoader); 161 162 if (appClientDependsOnPersistenceUnit(getAppClient())) { 165 handlePersistenceUnitDependency(); 173 } 174 175 archivist.validate(classLoader); 178 179 fixupWSDLEntries(); 180 181 if (isJWS) { 182 grantRequestedPermissionsToUserCode(); 183 } 184 } 185 186 190 protected ApplicationClientDescriptor getAppClient() { 191 return getAppClient(archivist); 192 } 193 194 protected ClassLoader getClassLoader() { 195 return classLoader; 196 } 197 198 protected File createTmpArchiveDir(File forArchive) 199 throws IOException { 200 204 String name = forArchive.getName(); 205 206 File tmpDir = File.createTempFile("acc-" + name, ""); 207 tmpDir.delete(); 208 209 tmpDir.mkdirs(); 210 211 215 if (!_keepExplodedDir) { 216 tmpDir.deleteOnExit(); 217 } 218 219 return tmpDir; 220 } 221 222 227 protected void close() throws IOException { 228 try { 229 if (puLoader != null) { 230 puLoader.unload(); 231 puLoader = null; 232 } 233 if (appClientArchive != null) { 234 appClientArchive.close(); 235 } 236 if (classLoader != null && 237 classLoader instanceof EJBClassLoader) { 238 ((EJBClassLoader) classLoader).done(); 239 } 240 } finally { 241 if (deleteAppClientDir()) { 242 if (appClientArchive != null) { 243 deleteFile(new File (appClientArchive.getArchiveUri())); 244 } 245 } 246 appClientArchive = null; 247 } 248 } 249 250 protected boolean deleteAppClientDir() { 251 return !_keepExplodedDir; 252 } 253 254 255 258 private void handlePersistenceUnitDependency() 259 throws URISyntaxException , MalformedURLException , 260 ClassNotFoundException , IOException { 261 InstrumentableClassLoader classLoader = 262 (InstrumentableClassLoader) getClassLoader(); 263 puLoader = new PersistenceUnitLoaderImpl( 264 appClientArchive.getArchiveUri(), classLoader, getAppClient()); 265 puLoader.load(); 266 } 267 268 private ClassLoader createClassLoader(AbstractArchive archive) 269 throws IOException { 270 List <String > paths = getClassPaths(archive); 271 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 272 EJBClassLoader loader = new EJBClassLoader(parent); 273 274 final int LIST_SZ = paths.size(); 275 for (int i=0; i<LIST_SZ; i++) { 276 String path = paths.get(i); 277 loader.appendURL(new File (path)); 278 } 279 280 if (_logger.isLoggable(Level.FINE)) { 281 for (int i = 0; i < paths.size(); i++) { 282 _logger.fine("Added path to classloader ==> " + paths.get(i)); 283 } 284 } 285 286 return loader; 287 } 288 289 private void deleteFile(File f) { 290 if (f.isDirectory()) { 291 for (File subFile : f.listFiles()) { 292 deleteFile(subFile); 293 } 294 } 295 f.delete(); 296 } 297 298 303 private boolean appClientDependsOnPersistenceUnit( 304 ApplicationClientDescriptor acDescr) 305 throws MalformedURLException , ClassNotFoundException , 306 IOException , URISyntaxException { 307 311 return descriptorContainsPURefcs(acDescr) 312 || mainClassContainsPURefcAnnotations(acDescr); 313 } 314 315 321 private boolean descriptorContainsPURefcs( 322 ApplicationClientDescriptor descr) { 323 return ! descr.getEntityManagerFactoryReferenceDescriptors().isEmpty(); 324 } 325 326 331 private boolean mainClassContainsPURefcAnnotations( 332 ApplicationClientDescriptor acDescr) 333 throws MalformedURLException , ClassNotFoundException , 334 IOException , URISyntaxException { 335 AppClientPersistenceDependencyAnnotationDetector annoDetector = 336 new AppClientPersistenceDependencyAnnotationDetector(); 337 338 String mainClassEntryName = 340 acDescr.getMainClassName().replace('.', '/') + ".class"; 341 342 return classContainsAnnotation 343 (mainClassEntryName, annoDetector, appClientArchive, acDescr); 344 } 345 346 350 private void fixupWSDLEntries() 351 throws URISyntaxException , MalformedURLException , IOException , 352 AnnotationProcessorException { 353 ApplicationClientDescriptor ac = getAppClient(); 354 URI uri = (new File (getAppClientRoot(appClientArchive, ac))).toURI(); 355 File moduleFile = new File (uri); 356 for (Iterator itr = ac.getServiceReferenceDescriptors().iterator(); 357 itr.hasNext();) { 358 ServiceReferenceDescriptor serviceRef = 359 (ServiceReferenceDescriptor) itr.next(); 360 if (serviceRef.getWsdlFileUri()!=null) { 361 URI wsdlURI = new URI (serviceRef.getWsdlFileUri()); 367 File wsdlFile = new File (serviceRef.getWsdlFileUri()); 368 if(wsdlFile.isAbsolute()) { 369 serviceRef.setWsdlFileUrl(wsdlFile.toURI().toURL()); 370 } else { 371 serviceRef.setWsdlFileUrl(FileUtil.getEntryAsUrl( 375 moduleFile, serviceRef.getWsdlFileUri())); 376 } 377 } 378 } 379 } 380 381 private RootDeploymentDescriptor populateDescriptor( 382 AbstractArchive archive, Archivist theArchivist, ClassLoader loader) 383 throws IOException , SAXParseException , Exception { 384 385 389 theArchivist.setAnnotationProcessingRequested(true); 390 391 theArchivist.setClassLoader(loader); 400 401 RootDeploymentDescriptor d = null; 405 try { 406 d = theArchivist.open(archive); 407 } catch (Exception ex) { 408 close(); throw ex; 410 } 411 412 messageDescriptor(d, theArchivist, archive); 415 416 theArchivist.setDescriptor(d); 417 return d; 418 } 419 420 432 private void grantRequestedPermissionsToUserCode() throws IOException , URISyntaxException { 433 442 boolean userJarIsSigned = Boolean.getBoolean(USER_CODE_IS_SIGNED_PROPERTYNAME); 443 444 boolean retainTempFiles = Boolean.getBoolean(MainWithModuleSupport.APPCLIENT_RETAIN_TEMP_FILES_PROPERTYNAME); 445 446 450 String templateName = (userJarIsSigned ? SIGNED_USER_CODE_PERMISSION_TEMPLATE_NAME : UNSIGNED_USER_CODE_PERMISSION_TEMPLATE_NAME); 451 String template = Util.loadResource(JWSACCMain.class, templateName); 452 453 456 File policyFile = File.createTempFile("accjws-user", ".policy"); 457 if ( ! retainTempFiles) { 458 policyFile.deleteOnExit(); 459 } 460 461 PrintStream ps = new PrintStream (policyFile); 462 463 Properties p = new Properties (); 464 465 470 EJBClassLoader loader = (EJBClassLoader) getClassLoader(); 471 for (URL classPathElement : loader.getURLs()) { 472 476 String codeBase = Util.URLtoCodeBase(classPathElement); 477 if (codeBase != null) { 478 p.setProperty(CODE_BASE_PLACEHOLDER_NAME, codeBase); 479 String policyPart = Util.replaceTokens(template, p); 480 481 ps.println(policyPart); 482 } 483 } 484 485 488 ps.close(); 489 490 JWSACCMain.refreshPolicy(policyFile); 491 492 if ( ! retainTempFiles) { 493 policyFile.delete(); 494 } 495 } 496 497 502 509 protected abstract AbstractArchive expand(File file) 510 throws IOException , Exception ; 511 512 protected ApplicationClientDescriptor getAppClient( 513 Archivist archivist) { 514 return ApplicationClientDescriptor.class.cast( 515 archivist.getDescriptor()); 516 } 517 518 protected String getAppClientRoot( 519 AbstractArchive archive, ApplicationClientDescriptor descriptor) { 520 return archive.getArchiveUri(); 521 } 522 523 protected void messageDescriptor(RootDeploymentDescriptor d, 524 Archivist archivist, AbstractArchive archive) 525 throws IOException , AnnotationProcessorException { 526 } 528 529 protected List <String > getClassPaths(AbstractArchive archive) { 530 List <String > paths = new ArrayList (); 531 paths.add(archive.getArchiveUri()); 532 return paths; 533 } 534 535 539 protected String getMainClassNameToRun(ApplicationClientDescriptor acDescr) { 540 if (mainClassNameToRun == null) { 541 if (mainClassFromCommandLine != null) { 542 mainClassNameToRun = mainClassFromCommandLine; 543 _logger.fine("Main class is " + mainClassNameToRun + " from command line"); 544 } else { 545 548 mainClassNameToRun = getAppClient().getMainClassName(); 549 _logger.fine("Main class is " + mainClassNameToRun + " from descriptor"); 550 } 551 } 552 return mainClassNameToRun; 553 } 554 555 protected boolean classContainsAnnotation( 556 String entry, AnnotationDetector detector, 557 AbstractArchive archive, ApplicationClientDescriptor descriptor) 558 throws FileNotFoundException , IOException { 559 String acRoot = getAppClientRoot(archive, descriptor); 560 String entryLocation = acRoot + File.separator + entry; 561 File entryFile = new File (entryLocation); 562 return detector.containsAnnotation(entryFile); 563 } 564 565 public String toString() { 566 String lineSep = System.getProperty("line.separator"); 567 StringBuilder result = new StringBuilder (); 568 result.append(this.getClass().getName() + ": " + lineSep); 569 result.append(" isJWS: " + isJWS); 570 result.append(" archive file: " + appClientFile.getAbsolutePath() + lineSep); 571 result.append(" archive type: " + appClientArchive.getClass().getName() + lineSep); 572 result.append(" archivist type: " + archivist.getClass().getName() + lineSep); 573 result.append(" main class to be run: " + mainClassNameToRun + lineSep); 574 result.append(" temporary archive directory: " + appClientArchive.getArchiveUri() + lineSep); 575 result.append(" class loader type: " + classLoader.getClass().getName() + lineSep); 576 577 return result.toString(); 578 } 579 580 protected static final boolean _keepExplodedDir = 582 Boolean.getBoolean("appclient.keep.exploded.dir"); 583 } 584 | Popular Tags |