1 25 26 package org.objectweb.jonas.ear; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.net.URLClassLoader ; 33 import java.security.Policy ; 34 import java.util.ArrayList ; 35 import java.util.Date ; 36 import java.util.Enumeration ; 37 import java.util.HashSet ; 38 import java.util.Hashtable ; 39 import java.util.Iterator ; 40 import java.util.LinkedList ; 41 import java.util.List ; 42 import java.util.Map ; 43 import java.util.Set ; 44 import java.util.StringTokenizer ; 45 import java.util.Vector ; 46 47 import javax.management.InstanceAlreadyExistsException ; 48 import javax.management.MBeanRegistrationException ; 49 import javax.management.MBeanServer ; 50 import javax.management.NotCompliantMBeanException ; 51 import javax.management.ObjectName ; 52 import javax.management.modelmbean.ModelMBean ; 53 import javax.naming.Context ; 54 import javax.naming.NamingException ; 55 import javax.security.jacc.PolicyConfiguration ; 56 import javax.security.jacc.PolicyConfigurationFactory ; 57 import javax.security.jacc.PolicyContextException ; 58 59 import org.apache.commons.modeler.ManagedBean; 60 import org.apache.commons.modeler.Registry; 61 import org.objectweb.jonas.client.AppClientModule; 62 import org.objectweb.jonas.common.JModule; 63 import org.objectweb.jonas.common.JProp; 64 import org.objectweb.jonas.common.Log; 65 import org.objectweb.jonas.container.EJBService; 66 import org.objectweb.jonas.container.EJBServiceImpl; 67 import org.objectweb.jonas.ear.lib.EarClassPathManager; 68 import org.objectweb.jonas.ear.lib.EarClassPathManagerException; 69 import org.objectweb.jonas.ear.lib.JarList; 70 import org.objectweb.jonas.ear.lib.JarListException; 71 import org.objectweb.jonas.jmx.J2eeObjectName; 72 import org.objectweb.jonas.jmx.JmxService; 73 import org.objectweb.jonas.jmx.JonasObjectName; 74 import org.objectweb.jonas.management.JonasMBeanTools; 75 import org.objectweb.jonas.naming.CompNamingContext; 76 import org.objectweb.jonas.resource.ResourceService; 77 import org.objectweb.jonas.resource.ResourceServiceException; 78 import org.objectweb.jonas.security.jacc.JPolicyUserRoleMapping; 79 import org.objectweb.jonas.server.JClassLoader; 80 import org.objectweb.jonas.server.LoaderManager; 81 import org.objectweb.jonas.service.AbsServiceImpl; 82 import org.objectweb.jonas.service.ServiceException; 83 import org.objectweb.jonas.service.ServiceManager; 84 import org.objectweb.jonas.web.JWebContainerService; 85 import org.objectweb.jonas.web.JWebContainerServiceException; 86 import org.objectweb.jonas.ws.AbsWebServicesServiceImpl; 87 import org.objectweb.jonas.ws.WSServiceException; 88 import org.objectweb.jonas.ws.WebServicesService; 89 import org.objectweb.jonas_client.deployment.api.ClientContainerDeploymentDesc; 90 import org.objectweb.jonas_client.deployment.api.ClientContainerDeploymentDescException; 91 import org.objectweb.jonas_client.deployment.lib.wrapper.ClientManagerWrapper; 92 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc; 93 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException; 94 import org.objectweb.jonas_ear.deployment.lib.wrapper.EarManagerWrapper; 95 import org.objectweb.jonas_ear.deployment.xml.Web; 96 97 import org.objectweb.jonas_ejb.deployment.lib.wrapper.EjbManagerWrapper; 98 import org.objectweb.jonas_lib.deployment.work.CleanerException; 99 import org.objectweb.jonas_lib.deployment.work.DeployerLog; 100 import org.objectweb.jonas_lib.deployment.work.DeployerLogException; 101 import org.objectweb.jonas_lib.deployment.work.EarCleanTask; 102 import org.objectweb.jonas_lib.deployment.work.EarFileManager; 103 import org.objectweb.jonas_lib.deployment.work.FileManagerException; 104 import org.objectweb.jonas_lib.deployment.work.WorkCleaner; 105 import org.objectweb.jonas_lib.loader.ClientClassLoader; 106 import org.objectweb.jonas_lib.loader.EjbJarClassLoader; 107 import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper; 108 import org.objectweb.util.monolog.api.BasicLevel; 109 import org.objectweb.util.monolog.api.Logger; 110 111 119 public class EarServiceImpl extends AbsServiceImpl implements EarService, EarServiceImplMBean { 120 121 124 protected static final String JONAS_BASE = JProp.getJonasBase(); 125 126 129 protected static final String APPS_DIR = JONAS_BASE + File.separator + "apps"; 130 131 134 protected static final String WORK_DIR = JProp.getWorkDir(); 135 136 139 protected static final String WORK_APPS_DIR = WORK_DIR + File.separator + "apps"; 140 141 144 protected static final String DESCRIPTORS = "jonas.service.ear.descriptors"; 145 146 149 protected static final String AUTOLOADDIR = "jonas.service.ear.autoloaddir"; 150 151 154 protected static final String PARSINGWITHVALIDATION = "jonas.service.ear.parsingwithvalidation"; 155 156 159 protected static final String CLASS = "jonas.service.ear.class"; 160 161 164 private static Logger logger = null; 165 166 169 private static WorkCleaner workCleaner = null; 170 171 174 private MBeanServer mbeanServer = null; 175 176 179 private Vector earNames = new Vector (); 180 181 184 private EJBService ejbService = null; 185 186 189 private JWebContainerService webContainerService = null; 190 191 194 private WebServicesService wsService = null; 195 196 199 private ResourceService resourceService = null; 200 201 204 private Hashtable ears = null; 205 206 209 private ArrayList autoloadDirectories = new ArrayList (); 210 211 215 private DeployerLog earDeployerLog = null; 216 217 220 private ClassLoader appsClassLoader; 221 222 227 protected void doInit(Context ctx) throws ServiceException { 228 229 logger = Log.getLogger(Log.JONAS_EAR_PREFIX); 231 232 try { 234 LoaderManager lm = LoaderManager.getInstance(); 235 appsClassLoader = lm.getAppsLoader(); 236 } catch (Exception e) { 237 logger.log(BasicLevel.ERROR, "Cannot get the Applications ClassLoader from EAR Container Service: " + e); 238 throw new ServiceException("Cannot get the Applications ClassLoader from EAR Container Service", e); 239 } 240 241 ServiceManager sm = null; 242 243 try { 244 sm = ServiceManager.getInstance(); 245 } catch (Exception e) { 246 String err = "Cannot get ServiceManager instance"; 247 logger.log(BasicLevel.ERROR, err); 248 throw new ServiceException(err, e); 249 } 250 251 try { 253 mbeanServer = ((JmxService) sm.getJmxService()).getJmxServer(); 254 } catch (ServiceException e) { 255 mbeanServer = null; 257 } 258 259 try { 261 ejbService = (EJBService) sm.getEjbService(); 262 } catch (ServiceException e) { 263 ejbService = null; 265 } 266 267 try { 269 webContainerService = (JWebContainerService) sm.getWebContainerService(); 270 } catch (ServiceException e) { 271 webContainerService = null; 273 } 274 275 try { 277 wsService = (WebServicesService) sm.getWebServicesService(); 278 } catch (ServiceException e) { 279 wsService = null; 281 } 282 283 try { 285 resourceService = (ResourceService) sm.getResourceService(); 286 } catch (ServiceException e) { 287 resourceService = null; 289 } 290 291 String parsingMode = "false"; 293 try { 294 parsingMode = (String ) ctx.lookup(PARSINGWITHVALIDATION); 295 } catch (NamingException e) { 296 300 if (logger.isLoggable(BasicLevel.DEBUG)) { 301 logger.log(BasicLevel.DEBUG, "No XML parsing validation property, use default"); 302 } 303 } 304 EarManagerWrapper.setParsingWithValidation("true".equalsIgnoreCase(parsingMode)); 305 if ("false".equalsIgnoreCase(parsingMode)) { 306 if (logger.isLoggable(BasicLevel.DEBUG)) { 307 logger.log(BasicLevel.DEBUG, "Ear XML parsing without validation"); 308 } 309 } else { 310 if (logger.isLoggable(BasicLevel.DEBUG)) { 311 logger.log(BasicLevel.DEBUG, "Ear XML parsing with validation"); 312 } 313 } 314 315 String descsValue = null; 317 try { 318 descsValue = (String ) ctx.lookup(DESCRIPTORS); 319 } catch (NamingException e) { 320 if (logger.isLoggable(BasicLevel.DEBUG)) { 321 logger.log(BasicLevel.DEBUG, "No property DESCRIPTORS"); 322 } 323 } 326 if (descsValue != null) { 327 StringTokenizer st = new StringTokenizer (descsValue, ","); 328 while (st.hasMoreTokens()) { 329 String fileName = st.nextToken().trim(); 330 earNames.add(fileName); 331 } 332 } 333 334 String dirValue = null; 336 ArrayList autoDirs = new ArrayList (); 337 try { 338 dirValue = (String ) ctx.lookup(AUTOLOADDIR); 339 } catch (NamingException e) { 340 if (logger.isLoggable(BasicLevel.DEBUG)) { 341 logger.log(BasicLevel.DEBUG, "No AUTOLOADDIR property"); 342 } 343 } 346 if (dirValue != null) { 347 StringTokenizer st = new StringTokenizer (dirValue, ","); 348 while (st.hasMoreTokens()) { 349 String dirName = st.nextToken().trim(); 350 addEars(dirName); 351 autoDirs.add(dirName); 352 } 353 } 354 File oFile; 356 Iterator it = autoDirs.iterator(); 357 while (it.hasNext()) { 358 String dirName = (String ) it.next(); 359 try { 360 oFile = new File (APPS_DIR, dirName); 361 if (!oFile.exists()) { 362 oFile = new File (dirName); 363 } 364 if (oFile.exists()) { 365 autoloadDirectories.add(oFile.getCanonicalPath()); 366 } 367 } catch (Exception e) { 368 String err = "Error when trying to verify Application EAR autoload directory : " + dirName; 369 logger.log(BasicLevel.ERROR, err, e); 370 } 371 } 372 373 ears = new Hashtable (); 375 376 } 379 380 384 protected void doStop() throws ServiceException { 385 386 URL earFileName = null; 388 for (Enumeration earEntries = ears.keys(); earEntries.hasMoreElements();) { 391 earFileName = (URL ) earEntries.nextElement(); 392 try { 394 Context ctx = new CompNamingContext(earFileName.getFile()); 395 ctx.rebind("filename", earFileName); 396 unDeployEar(ctx); 397 } catch (Exception e) { 398 String err = "Error when undeploying the ear :" + earFileName; 401 logger.log(BasicLevel.ERROR, err, e); 402 } 403 } 404 405 if (mbeanServer != null) { 406 try { 407 mbeanServer.unregisterMBean(JonasObjectName.earService()); 409 } catch (Exception e) { 410 logger.log(BasicLevel.ERROR, "Cannot stop the EarService", e); 411 } 412 } 413 414 if (logger.isLoggable(BasicLevel.DEBUG)) { 416 logger.log(BasicLevel.DEBUG, "EarService stopped"); 417 } 418 419 } 420 421 430 public String deployEar(Context ctx) throws EarServiceException { 431 432 if ((webContainerService == null) && (ejbService == null)) { 434 throw new EarServiceException( 435 "The ear service requires that at least the service ejb or web is launched for deploying an ear file."); 436 } 437 438 String fileName; 441 try { 442 fileName = (String ) ctx.lookup("filename"); 443 } catch (NamingException e) { 444 throw new EarServiceException("Error during performing lookup a fileName", e); 445 } 446 447 File f = null; 448 try { 449 f = new File (fileName).getCanonicalFile(); 451 452 if (!f.exists()) { 453 boolean found = false; 454 String earFileName = null; 455 if (fileName.toLowerCase().endsWith(".ear")) { 456 earFileName = APPS_DIR + File.separator + fileName; 459 f = new File (earFileName).getCanonicalFile(); 460 found = f.exists(); 461 } 462 if (found) { 463 fileName = earFileName; 464 } else { 465 String err = "deployEar: The file " + fileName 466 + " was not found neither in the current directory nor in the " + APPS_DIR + " directory"; 467 logger.log(BasicLevel.ERROR, err); 468 throw new EarServiceException(err); 469 } 470 } 471 } catch (IOException e) { 472 String err = "Error when trying to get the canonical file from " + fileName; 473 logger.log(BasicLevel.ERROR, err + " " + e.getMessage()); 474 throw new EarServiceException(err, e); 475 } 476 477 URL [] earUrl = new URL [1]; 479 URL earRootUrl = null; 480 481 try { 482 earUrl[0] = f.toURL(); 483 earRootUrl = (new File (WORK_APPS_DIR + File.separator + getJonasServerName())).toURL(); 484 } catch (MalformedURLException e) { 485 String err = "Invalid ear file name '" + fileName; 486 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage()); 487 throw new EarServiceException(err, e); 488 } 489 490 if (ears.get(earUrl[0]) != null) { 492 String err = "The ear file : " + f.getName() + " is already deployed ('" + earUrl[0].getFile() 493 + "'). You must undeploy the application " + "before a new deployment."; 494 logger.log(BasicLevel.ERROR, err); 495 throw new EarServiceException(err); 496 } 497 498 URLClassLoader loaderCls = new URLClassLoader (earUrl, appsClassLoader); 501 502 EarDeploymentDesc earDD = null; 503 if (logger.isLoggable(BasicLevel.DEBUG)) { 504 logger.log(BasicLevel.DEBUG, "Getting the deployment descriptor of the file" + f.getName()); 505 } 506 try { 507 earDD = EarManagerWrapper.getDeploymentDesc(earUrl[0].getFile(), loaderCls); 508 } catch (EarDeploymentDescException e) { 509 String err = "Error in the Deployment descriptor '" + fileName + "': " + e; 510 logger.log(BasicLevel.ERROR, err); 511 throw new EarServiceException(err, e); 512 } 513 514 Web[] webTags = earDD.getWebTags(); 516 String [] ejbTags = earDD.getEjbTags(); 517 String [] connectorTags = earDD.getConnectorTags(); 518 String [] clientTags = earDD.getClientTags(); 519 String [] altDDClients = earDD.getAltDDClients(); 520 String [] altDDEjbs = earDD.getAltDDEjbs(); 521 String [] altDDWebs = earDD.getAltDDWebs(); 522 String [] altDDConnectors = earDD.getAltDDConnectors(); 523 String [] securityRoles = earDD.getSecurityRolesNames(); 524 525 528 File fEar = new File (earUrl[0].getFile()); 529 File tmpFile = null; 530 531 try { 532 File fCanonicEar = fEar.getCanonicalFile(); 533 for (int i = 0; i < ejbTags.length; i++) { 534 tmpFile = new File (fEar, ejbTags[i]); 535 tmpFile = tmpFile.getCanonicalFile(); 536 if (!tmpFile.getPath().startsWith(fCanonicEar.getPath())) { 537 String err = "Error : The ejb-jar file " + ejbTags[i] + " is not inside the ear file " + fEar; 538 logger.log(BasicLevel.ERROR, err); 539 throw new EarServiceException(err); 540 } 541 } 542 543 for (int i = 0; i < webTags.length; i++) { 544 tmpFile = new File (fEar, webTags[i].getWebUri()); 545 tmpFile = tmpFile.getCanonicalFile(); 546 if (!tmpFile.getPath().startsWith(fCanonicEar.getPath())) { 547 String err = "Error : The war file " + webTags[i].getWebUri() + " is not inside the ear file " 548 + fEar; 549 logger.log(BasicLevel.ERROR, err); 550 throw new EarServiceException(err); 551 } 552 } 553 554 for (int i = 0; i < connectorTags.length; i++) { 555 tmpFile = new File (fEar, connectorTags[i]); 556 tmpFile = tmpFile.getCanonicalFile(); 557 if (!tmpFile.getPath().startsWith(fCanonicEar.getPath())) { 558 String err = "Error : The rar file " + connectorTags[i] + " is not inside the ear file " + fEar; 559 logger.log(BasicLevel.ERROR, err); 560 throw new EarServiceException(err); 561 } 562 } 563 564 565 for (int i = 0; i < clientTags.length; i++) { 566 tmpFile = new File (fEar, clientTags[i]); 567 tmpFile = tmpFile.getCanonicalFile(); 568 569 if (!tmpFile.getPath().startsWith(fCanonicEar.getPath())) { 570 String err = "Error : The client jar file " + clientTags[i] + " is not inside the ear file " + fEar; 571 throw new EarServiceException(err); 572 } 573 } 574 } catch (IOException ioe) { 575 String err = "Error while trying to get the canonical file of " + tmpFile; 576 logger.log(BasicLevel.ERROR, err + " : " + ioe.getMessage()); 577 throw new EarServiceException(err, ioe); 578 } 579 580 JarList ejbsList = new JarList(ejbTags); 582 JarList warsList = new JarList(webTags); 583 JarList connectorsList = new JarList(connectorTags); 584 JarList clientsList = new JarList(clientTags); 585 586 URL dirUnpackURL = null; 588 try { 589 dirUnpackURL = EarFileManager.unpackEar(earUrl[0], earRootUrl); 590 } catch (FileManagerException e) { 591 String err = "Error while unpacking the file '" + earUrl[0] + "'"; 592 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage()); 593 throw new EarServiceException(err, e); 594 } 595 596 if (new File (earUrl[0].getFile()).isFile()) { 599 try { 600 earDeployerLog.addEntry(new File (earUrl[0].getFile()), new File (dirUnpackURL.getFile())); 601 } catch (DeployerLogException e) { 602 String err = "Error while adding the " + earUrl[0] + " entry in the log file"; 603 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage()); 604 throw new EarServiceException(err, e); 605 } 606 } 607 608 EarClassPathManager earCPManager = null; 611 try { 612 earCPManager = new EarClassPathManager(ejbsList, warsList, dirUnpackURL); 613 } catch (EarClassPathManagerException e) { 614 String err = "Error while creating the Ear class path manager of the ear : '" + earUrl[0] + "'"; 615 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage()); 616 throw new EarServiceException(err, e); 617 } 618 619 URL [] classpathURLs = null; 620 try { 622 classpathURLs = earCPManager.getResolvedClassPath(); 623 } catch (EarClassPathManagerException e) { 624 String err = "Error while trying to resolve the classpath of the ejbjars and wars of the ear : '" 625 + earUrl[0] + "'"; 626 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage()); 627 throw new EarServiceException(err, e); 628 } 629 630 634 if (logger.isLoggable(BasicLevel.DEBUG)) { 635 logger.log(BasicLevel.DEBUG, "Creating the EAR classLoader"); 636 } 637 JClassLoader earClassLoader = new JClassLoader(earUrl[0].toExternalForm() , new URL [0], appsClassLoader); 638 639 URL [] jarUrls = null; 641 URL [] warUrls = null; 642 URL [] connectorUrls = null; 643 URL [] clientUrls = null; 644 try { 645 jarUrls = ejbsList.getURLs(dirUnpackURL.toExternalForm()); 646 warUrls = warsList.getURLs(dirUnpackURL.toExternalForm()); 647 connectorUrls = connectorsList.getURLs(dirUnpackURL.toExternalForm()); 648 clientUrls = clientsList.getURLs(dirUnpackURL.toExternalForm()); 649 } catch (JarListException e) { 650 String err = "Error while geting the Urls from jarlist of the ear : '" + earUrl[0] + "'"; 651 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage()); 652 throw new EarServiceException(err, e); 653 } 654 655 String [] warsContextRoots = new String [webTags.length]; 658 String ctxRoot = null; 659 for (int i = 0; i < webTags.length; i++) { 660 ctxRoot = webTags[i].getContextRoot(); 661 if (ctxRoot != null) { 662 warsContextRoots[i] = ctxRoot; 663 } 664 } 665 666 String altdd = null; 668 File fAltDD = null; 669 670 URL [] warsAltDDs = new URL [altDDWebs.length]; 673 for (int i = 0; i < altDDWebs.length; i++) { 674 if (altDDWebs[i] != null) { 675 altdd = altDDWebs[i]; 676 if (altdd != null) { 677 try { 678 fAltDD = new File (new URL (dirUnpackURL.toExternalForm() + File.separator + altdd).getFile()); 679 warsAltDDs[i] = fAltDD.getCanonicalFile().toURL(); 680 } catch (MalformedURLException e) { 681 String err = "Can't build URL for alt-dd '" + altdd; 682 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage()); 683 throw new EarServiceException(err, e); 684 } catch (IOException ioe) { 685 String err = "Can't get canonicalFile() for the file '" + fAltDD; 686 logger.log(BasicLevel.ERROR, err + "': " + ioe.getMessage()); 687 throw new EarServiceException(err, ioe); 688 } 689 } 690 } 691 } 692 693 URL [] ejbsAltDDs = new URL [altDDEjbs.length]; 694 for (int i = 0; i < altDDEjbs.length; i++) { 695 if (altDDEjbs[i] != null) { 696 altdd = altDDEjbs[i]; 697 if (altdd != null) { 698 try { 699 fAltDD = new File (new URL (dirUnpackURL.toExternalForm() + File.separator + altdd).getFile()); 700 ejbsAltDDs[i] = fAltDD.getCanonicalFile().toURL(); 701 } catch (MalformedURLException e) { 702 String err = "Can't build URL for alt-dd '" + altdd; 703 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage()); 704 throw new EarServiceException(err, e); 705 } catch (IOException ioe) { 706 String err = "Can't get canonicalFile() for the file '" + fAltDD; 707 logger.log(BasicLevel.ERROR, err + "': " + ioe.getMessage()); 708 throw new EarServiceException(err, ioe); 709 } 710 } 711 } 712 } 713 714 URL [] connectorsAltDDs = new URL [altDDConnectors.length]; 715 for (int i = 0; i < altDDConnectors.length; i++) { 716 if (altDDConnectors[i] != null) { 717 altdd = altDDConnectors[i]; 718 if (altdd != null) { 719 try { 720 fAltDD = new File (new URL (dirUnpackURL.toExternalForm() + File.separator + altdd).getFile()); 721 connectorsAltDDs[i] = fAltDD.getCanonicalFile().toURL(); 722 } catch (MalformedURLException e) { 723 String err = "Can't build URL for alt-dd '" + altdd; 724 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage()); 725 throw new EarServiceException(err, e); 726 } catch (IOException ioe) { 727 String err = "Can't get canonicalFile() for the file '" + fAltDD; 728 logger.log(BasicLevel.ERROR, err + "': " + ioe.getMessage()); 729 throw new EarServiceException(err, ioe); 730 } 731 } 732 } 733 } 734 735 URL [] clientAltDDs = new URL [altDDClients.length]; 736 for (int i = 0; i < altDDClients.length; i++) { 737 if (altDDClients[i] != null) { 738 altdd = altDDClients[i]; 739 if (altdd != null) { 740 try { 741 fAltDD = new File (new URL (dirUnpackURL.toExternalForm() + File.separator + altdd).getFile()); 742 clientAltDDs[i] = fAltDD.getCanonicalFile().toURL(); 743 } catch (MalformedURLException e) { 744 String err = "Can't build URL for alt-dd '" + altdd; 745 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage()); 746 throw new EarServiceException(err, e); 747 } catch (IOException ioe) { 748 String err = "Can't get canonicalFile() for the file '" + fAltDD; 749 logger.log(BasicLevel.ERROR, err + "': " + ioe.getMessage()); 750 throw new EarServiceException(err, ioe); 751 } 752 } 753 } 754 } 755 String [] roleNames = new String [securityRoles.length]; 757 String affRoleNames = ""; 758 for (int i = 0; i < securityRoles.length; i++) { 759 roleNames[i] = securityRoles[i]; 760 affRoleNames += roleNames[i] + ";"; 761 } 762 763 if (logger.isLoggable(BasicLevel.DEBUG)) { 764 logger.log(BasicLevel.DEBUG, "role names = " + affRoleNames); 765 } 766 767 774 775 780 if (jarUrls.length > 0 && ejbService != null) { 781 for (int i = 0; i < jarUrls.length; i++) { 783 ejbService.checkGenIC(jarUrls[i].getFile()); 784 } 785 } 786 787 788 789 793 EjbManagerWrapper.setAvailableEjbJarsAndAltDDs(earClassLoader, jarUrls, ejbsAltDDs); 797 WebManagerWrapper.setAltDD(earClassLoader, warUrls, warsAltDDs); 798 ClientManagerWrapper.setAltDD(earClassLoader, clientUrls, clientAltDDs); 799 800 URL [] urls = new URL [jarUrls.length + classpathURLs.length]; 804 System.arraycopy(jarUrls, 0, urls, 0, jarUrls.length); 805 System.arraycopy(classpathURLs, 0, urls, jarUrls.length, classpathURLs.length); 806 URLClassLoader ejbClassLoader; 807 try { 808 ejbClassLoader = new EjbJarClassLoader(urls, earClassLoader); 809 } catch (IOException ioe) { 810 String err = "Cannot Create EJB ClassLoader for EAR '" + fileName + "'"; 811 logger.log(BasicLevel.ERROR, err + " : " + ioe.getMessage()); 812 throw new EarServiceException(err, ioe); 813 } 814 815 819 822 if (resourceService != null && (connectorUrls.length > 0)) { 825 try { 826 CompNamingContext contctx = null; 827 try { 828 contctx = new CompNamingContext(dirUnpackURL.getFile()); 829 contctx.rebind("earUrl", earUrl[0]); 830 contctx.rebind("urls", connectorUrls); 831 contctx.rebind("earClassLoader", earClassLoader); 832 contctx.rebind("altDDs", connectorsAltDDs); 833 } catch (NamingException e) { 834 String err = "Can not bind params for the resource service, Can't deploy rars "; 835 throw new ResourceServiceException(err, e); 836 } 837 resourceService.deployRars(contctx); 838 } catch (ServiceException e) { 839 String err = "Error during the deployment of the rars files of the Ear file " + fileName; 840 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage()); 841 throw new EarServiceException(err, e); 844 } 845 } 846 847 if (wsService != null) { 849 try { 850 Hashtable ctxRoots = new Hashtable (); 851 for (int w = 0; w < webTags.length; w++) { 852 ctxRoots.put(warUrls[w], webTags[w].getContextRoot()); 853 } 854 CompNamingContext contctx = null; 855 try { 856 contctx = new CompNamingContext(dirUnpackURL.getFile()); 857 contctx.rebind("unpackDir", dirUnpackURL.toExternalForm()); 858 contctx.rebind("jarUrls", jarUrls); 859 contctx.rebind("warUrls", warUrls); 860 contctx.rebind("earURL", earUrl[0]); 861 contctx.rebind("ejbClassLoader", ejbClassLoader); 862 contctx.rebind("earClassLoader", earClassLoader); 863 contctx.rebind("warCtxRootMapping", ctxRoots); 864 } catch (NamingException e) { 865 String err = "Cannot bind params for the WebServices service, Can't deploy Web Services Endpoints "; 866 throw new WSServiceException(err, e); 867 } 868 wsService.deployWebServices(contctx); 869 } catch (ServiceException se) { 870 String err = "Error during the deployment of the WebServices of the Ear file " + fileName; 871 logger.log(BasicLevel.ERROR, err + " : " + se.getMessage()); 872 wsService.removeCache(earClassLoader); 873 try { 874 if (logger.isLoggable(BasicLevel.DEBUG)) { 875 logger.log(BasicLevel.DEBUG, "Undeploying Rars of the ear " + fileName); 876 } 877 if (resourceService != null && connectorUrls.length > 0) { 878 resourceService.unDeployRars(connectorUrls, earUrl[0]); 880 } 881 } catch (ServiceException se2) { 882 err = "Error during the undeployment of the rars files of the Ear file " + fileName; 883 logger.log(BasicLevel.ERROR, err + "': " + se2.getMessage()); 884 } 885 886 throw new EarServiceException(err, se); 887 } 888 } 889 890 892 if (ejbService != null && (jarUrls.length > 0)) { 895 try { 896 CompNamingContext contctx = null; 897 try { 898 contctx = new CompNamingContext(dirUnpackURL.getFile()); 899 contctx.rebind("earRootUrl", dirUnpackURL); 900 contctx.rebind("earUrl", earUrl[0]); 901 contctx.rebind("jarURLs", jarUrls); 902 contctx.rebind("earClassLoader", earClassLoader); 903 contctx.rebind("ejbClassLoader", ejbClassLoader); 904 contctx.rebind("roleNames", roleNames); 905 } catch (NamingException e) { 906 String err = "Can not bind params for the ejb service, Can't deploy jars "; 907 throw new EarServiceException(err, e); 908 } 909 ejbService.deployJars(contctx); 910 } catch (ServiceException e) { 911 String err = "Error during the deployment of the jars files of the Ear file " + fileName; 912 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage()); 913 ejbService.removeCache(earClassLoader); 915 if (resourceService != null && connectorUrls.length > 0) { 917 resourceService.unDeployRars(connectorUrls, earUrl[0]); 919 } 920 throw new EarServiceException(err, e); 921 } 922 } 923 924 928 929 934 935 ClassLoader parentWarClassLoader = ejbClassLoader; 936 937 if (webContainerService != null && (warUrls.length > 0)) { 940 try { 941 CompNamingContext contctx = null; 942 try { 943 contctx = new CompNamingContext(dirUnpackURL.getFile()); 944 contctx.rebind("earURL", earUrl[0]); 945 contctx.rebind("urls", warUrls); 946 contctx.rebind("parentClassLoader", parentWarClassLoader); 947 contctx.rebind("earClassLoader", earClassLoader); 948 contctx.rebind("altDDs", warsAltDDs); 949 contctx.rebind("contextRoots", warsContextRoots); 950 } catch (NamingException e) { 951 String err = "Can not bind params for the web container service, Can't deploy wars "; 952 throw new EarServiceException(err, e); 953 } 954 webContainerService.deployWars(contctx); 955 956 } catch (JWebContainerServiceException e) { 957 String err = "Error during the deployment of the wars file of the Ear file " + fileName; 958 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage()); 959 webContainerService.removeCache(earClassLoader); 961 err = "Undeploy the jars loaded from this ear"; 962 if (logger.isLoggable(BasicLevel.DEBUG)) { 963 logger.log(BasicLevel.DEBUG, err); 964 } 965 try { 966 if (ejbService != null && (jarUrls.length > 0)) { 967 ejbService.removeCache(earClassLoader); 969 ejbService.unDeployJars(jarUrls); 971 } 972 if (resourceService != null && connectorUrls.length > 0) { 974 resourceService.unDeployRars(connectorUrls, earUrl[0]); 976 } 977 } catch (ServiceException se) { 978 err = "Error during the undeployment of the jars file of the Ear file " + fileName; 979 logger.log(BasicLevel.ERROR, err + "': " + se.getMessage()); 980 throw new EarServiceException(err, e); 981 } 982 983 throw new EarServiceException(err, e); 984 } 985 } 986 987 linkPolicyObjects(earDD.getUserToRoleMapping(), jarUrls, warUrls); 989 990 if (logger.isLoggable(BasicLevel.DEBUG)) { 991 logger.log(BasicLevel.DEBUG, "We store the rars/wars/jars associated to the url :" + earUrl[0]); 992 } 993 994 if (wsService != null) { 997 try { 998 CompNamingContext contctx = null; 999 try { 1000 contctx = new CompNamingContext(earRootUrl.getFile()); 1001 contctx.rebind(AbsWebServicesServiceImpl.CLASSLOADER_CTX_PARAM, earClassLoader); 1002 ObjectName name = J2eeObjectName.J2EEApplication(getDomainName(), getJonasServerName(), buildJ2eeApplicationName(earUrl[0])); 1003 contctx.rebind(WebServicesService.PARENT_OBJECTNAME_CTX_PARAM , name); 1004 contctx.rebind(WebServicesService.ISINEAR_CTX_PARAM, Boolean.TRUE); 1005 1006 } catch (NamingException e) { 1007 String err = "Can not bind params for the WebServices service, " 1008 + "can't complete deployment of Web Services Endpoints"; 1009 throw new JWebContainerServiceException(err, e); 1010 } 1011 wsService.completeWSDeployment(contctx); 1012 } catch (ServiceException se) { 1013 String err = "Error during the deployment of the WebServices of the Ear file '" + earRootUrl + "'"; 1014 logger.log(BasicLevel.ERROR, err + " : " + se.getMessage()); 1015 try { 1016 if (logger.isLoggable(BasicLevel.DEBUG)) { 1017 logger.log(BasicLevel.DEBUG, "Undeploy the WebApps loaded from this ear"); 1018 } 1019 if (webContainerService != null && (warUrls.length > 0)) { 1020 webContainerService.removeCache(earClassLoader); 1022 webContainerService.unDeployWars(warUrls); 1024 } 1025 if (logger.isLoggable(BasicLevel.DEBUG)) { 1026 logger.log(BasicLevel.DEBUG, "Undeploy the EjbJars loaded from this ear"); 1027 } 1028 if (ejbService != null && (jarUrls.length > 0)) { 1029 ejbService.removeCache(earClassLoader); 1031 ejbService.unDeployJars(jarUrls); 1033 } 1034 if (logger.isLoggable(BasicLevel.DEBUG)) { 1036 logger.log(BasicLevel.DEBUG, "Undeploy the Resources loaded from this ear"); 1037 } 1038 if (resourceService != null && connectorUrls.length > 0) { 1039 resourceService.unDeployRars(connectorUrls, earUrl[0]); 1041 } 1042 } catch (ServiceException se2) { 1043 err = "Error during the undeployment of the webapps/ejbjars/rars file of the Ear file " + fileName; 1044 logger.log(BasicLevel.ERROR, err + "': " + se2.getMessage()); 1045 } 1046 throw new JWebContainerServiceException(err, se); 1047 } 1048 } 1049 1050 1051 1054 String sJ2EEApplicationName = buildJ2eeApplicationName(earUrl[0]); 1056 1057 AppClientModule[] appClientModules = null; 1059 ObjectName [] onAppClientModules = null; 1060 if (clientUrls.length > 0) { 1061 appClientModules = new AppClientModule[clientUrls.length]; 1062 onAppClientModules = new ObjectName [clientUrls.length]; 1063 } 1064 1065 String moduleName = null; 1066 String moduleFileName = null; 1067 String moduleDD = null; 1068 String jonasModuleDD = null; 1069 ClientContainerDeploymentDesc clientContainerDD = null; 1070 for (int i = 0; i < clientUrls.length; i++) { 1071 ClassLoader moduleCL = null; 1072 try { 1073 moduleCL = new ClientClassLoader(clientUrls[i], ejbClassLoader); 1074 } catch (IOException ioe) { 1075 moduleCL = ejbClassLoader; 1078 } 1080 moduleName = EJBServiceImpl.buildEJBModuleName(clientUrls[i]); 1081 moduleFileName = clientUrls[i].getPath(); 1082 try { 1083 clientContainerDD = ClientManagerWrapper.getDeploymentDesc(clientUrls[i], moduleCL, earClassLoader); 1084 moduleDD = clientContainerDD.getXmlContent(); 1085 jonasModuleDD = clientContainerDD.getJOnASXmlContent(); 1086 } catch (ClientContainerDeploymentDescException e) { 1087 String err = "Cannot read the deployment descriptors ': " + clientUrls[i] + "'" + e.toString(); 1089 logger.log(BasicLevel.ERROR, err, e); 1090 } 1091 onAppClientModules[i] = J2eeObjectName.getAppClientModule(getDomainName(), getJonasServerName(), sJ2EEApplicationName, moduleName); 1092 appClientModules[i] = new AppClientModule(onAppClientModules[i].toString() 1093 , moduleFileName 1094 , moduleDD 1095 , jonasModuleDD); 1096 } 1097 URL clientUrl = null; 1098 try { 1099 if (mbeanServer != null) { 1100 Registry oRegistry = JonasMBeanTools.getRegistry(); 1101 ManagedBean oManaged = null; 1102 ModelMBean oMBean = null; 1103 if (appClientModules != null) { 1105 oManaged = oRegistry.findManagedBean("AppClientModule"); 1106 for (int i = 0; i < appClientModules.length; i++) { 1107 clientUrl = clientUrls[i]; 1108 oMBean = oManaged.createMBean(appClientModules[i]); 1109 mbeanServer.registerMBean(oMBean, onAppClientModules[i]); 1110 } 1111 } 1112 } 1113 } catch (Exception eare) { 1114 logger.log(BasicLevel.ERROR, "Can not register the MBean for the client jar " + clientUrl, eare); 1115 } 1116 1117 if (ejbService != null) { 1119 ejbService.removeCache(earClassLoader); 1120 } 1121 1122 if (webContainerService != null) { 1123 webContainerService.removeCache(earClassLoader); 1124 } 1125 1126 if (wsService != null) { 1127 wsService.removeCache(earClassLoader); 1128 } 1129 1130 ObjectName onEar = J2eeObjectName.J2EEApplication(getDomainName(), getJonasServerName(), sJ2EEApplicationName); 1132 Ear ear = new Ear(onEar.toString(), sJ2EEApplicationName, dirUnpackURL.getFile(), earUrl[0], earDD.getXmlContent(), jarUrls, warUrls, 1135 connectorUrls); 1136 1137 try { 1138 if (mbeanServer != null) { 1139 Registry oRegistry = JonasMBeanTools.getRegistry(); 1140 ManagedBean oManaged = oRegistry.findManagedBean("J2EEApplication"); 1141 ModelMBean oMBean = oManaged.createMBean(ear); 1142 mbeanServer.registerMBean(oMBean, onEar); 1144 } 1145 } catch (Exception eare) { 1146 logger.log(BasicLevel.ERROR, "Can not register the MBean for the ear " + earUrl[0], eare); 1147 } 1148 1149 1150 ears.put(earUrl[0], ear); 1151 1152 logger.log(BasicLevel.INFO, "Ear " + earUrl[0] + " available."); 1153 1154 File dirUnpackFile = new File (dirUnpackURL.getFile()); 1156 dirUnpackFile.setLastModified((new Date ()).getTime()); 1157 1158 return onEar.toString(); 1160 } 1161 1162 1170 public String deployEar(String fileName) throws Exception { 1171 return deployEarMBean(fileName); 1172 } 1173 1174 1182 public String deployEarMBean(String fileName) throws Exception { 1183 Context ctx = null; 1184 String mbeanOn = null; 1185 try { 1186 ctx = new CompNamingContext(fileName); 1187 ctx.rebind("filename", fileName); 1188 } catch (NamingException e) { 1189 String err = "Error when deploying the ear file "; 1190 logger.log(BasicLevel.ERROR, err, e); 1191 throw e; 1192 } 1193 try { 1194 mbeanOn = deployEar(ctx); 1195 } catch (ServiceException e) { 1196 logger.log(BasicLevel.ERROR, "Cannot deploy file '" + fileName + "'"); 1197 throw new Exception ("Cannot deploy file '" + fileName + "'", e); 1198 } 1199 return mbeanOn; 1200 } 1201 1202 1206 protected void doStart() throws ServiceException { 1207 1208 URL earApps = null; 1210 try { 1211 earApps = new File (WORK_APPS_DIR + File.separator + getJonasServerName()).toURL(); 1212 } catch (MalformedURLException mue) { 1213 throw new ServiceException("Error when trying to get the URL of the jonasroot/apps directory", mue); 1214 } 1215 1216 File fLog = new File (earApps.getFile() + File.separator + getJonasServerName() + ".log"); 1217 if (!fLog.exists()) { 1218 try { 1219 fLog.getParentFile().mkdirs(); 1221 fLog.createNewFile(); 1222 } catch (IOException e) { 1223 throw new ServiceException("cannot create the log file" + fLog, e); 1224 } 1225 } 1226 1227 try { 1229 earDeployerLog = new DeployerLog(fLog); 1230 } catch (DeployerLogException e) { 1231 throw new ServiceException("Can not get an EarDeployerLog", e); 1232 } 1233 1234 EarCleanTask earCleanTask = new EarCleanTask(earApps, earDeployerLog); 1236 1237 workCleaner = WorkCleaner.getInstance(); 1239 1240 try { 1242 workCleaner.registerTask(earCleanTask); 1243 } catch (CleanerException ce) { 1244 throw new ServiceException("Cannot register the EAR clean task", ce); 1245 } 1246 1247 workCleaner.executeTasks(); 1249 1250 Iterator it = earNames.iterator(); 1252 while (it.hasNext()) { 1253 String fileName = (String ) it.next(); 1254 Context contctx = null; 1255 try { 1256 contctx = new CompNamingContext(fileName); 1257 contctx.rebind("filename", fileName); 1258 } catch (NamingException e) { 1259 throw new ServiceException("Cannot start the EarService", e); 1260 } 1261 deployEar(contctx); 1262 } 1263 1264 try { 1265 if (mbeanServer != null) { 1267 mbeanServer.registerMBean(this, JonasObjectName.earService()); 1268 } 1269 } catch (InstanceAlreadyExistsException iae) { 1270 throw new ServiceException("Cannot start the EarService Already Exists", iae); 1271 } catch (MBeanRegistrationException mbre) { 1272 throw new ServiceException("Cannot start the EarService (MBean registration error)", mbre); 1273 } catch (NotCompliantMBeanException ncmbe) { 1274 throw new ServiceException("Cannot start the EarService (MBean Not compliant error)", ncmbe); 1275 } 1276 } 1277 1278 1285 public void unDeployEar(Context ctx) throws EarServiceException { 1286 1287 URL earUrl = null; 1289 try { 1290 earUrl = (URL ) ctx.lookup("filename"); 1291 } catch (NamingException e) { 1292 throw new EarServiceException("Trying to remove the ear file but there is no filename specified", e); 1293 } 1294 1295 Ear ear = (Ear) ears.get(earUrl); 1297 1298 if (ear == null) { 1300 throw new EarServiceException("Trying to remove the ear file " + earUrl.getFile() 1301 + " but this file was not found in the loaded Ear files"); 1302 } 1303 1304 URL [] warsToUndeploy = ear.getWars(); 1306 if (webContainerService != null && (warsToUndeploy.length > 0)) { 1307 webContainerService.unDeployWars(warsToUndeploy); 1308 } 1309 1310 URL [] jarsToUndeploy = ear.getEjbJars(); 1312 if (ejbService != null && (jarsToUndeploy.length > 0)) { 1313 ejbService.unDeployJars(jarsToUndeploy); 1314 } 1315 1316 URL [] rarsToUndeploy = ear.getRars(); 1318 if (resourceService != null && (rarsToUndeploy.length > 0)) { 1319 resourceService.unDeployRars(rarsToUndeploy, earUrl); 1320 } 1321 1322 File f = new File (earUrl.getFile()); 1323 1325 ears.remove(earUrl); 1327 if (mbeanServer != null) { 1329 try { 1336 ObjectName onJ2EEApplication = ObjectName.getInstance(ear.getObjectName()); 1338 String appName = onJ2EEApplication.getKeyProperty("name"); 1339 String domainName = onJ2EEApplication.getDomain(); 1340 String serverName = onJ2EEApplication.getKeyProperty("J2EEServer"); 1341 mbeanServer.unregisterMBean(onJ2EEApplication); 1342 ObjectName onAppClientModules = J2eeObjectName.getAppClientModules(domainName, serverName, appName); 1344 List onList = J2eeObjectName.queryObjectNames(onAppClientModules); 1345 for (int i = 0; i < onList.size(); i++) { 1346 mbeanServer.unregisterMBean((ObjectName ) onList.get(i)); 1347 } 1348 } catch (Exception e) { 1349 logger.log(BasicLevel.ERROR, "Cannot remove the MBean for the ear " + earUrl.getFile(), e); 1350 } 1351 } 1352 1353 logger.log(BasicLevel.INFO, "Ear " + f.getName() + " no longer available."); 1354 1355 } 1356 1357 1364 public void unDeployEar(String fileName) throws Exception { 1365 unDeployEarMBean(fileName); 1366 } 1367 1368 1374 public void unDeployEarMBean(String fileName) throws Exception { 1375 1376 1380 1381 URL url = null; 1384 URL earUrl = null; 1385 boolean found = false; 1386 try { 1387 Enumeration e = ears.keys(); 1388 url = new File (fileName).getCanonicalFile().toURL(); 1389 while (e.hasMoreElements() && !found) { 1390 earUrl = (URL ) e.nextElement(); 1391 if (earUrl.equals(url)) { 1392 found = true; 1394 } 1395 } 1396 1397 if (fileName.toLowerCase().endsWith(".ear") && !found) { 1398 String earFileName = APPS_DIR + File.separator + fileName; 1401 e = ears.keys(); 1402 url = new File (earFileName).getCanonicalFile().toURL(); 1403 while (e.hasMoreElements() && !found) { 1404 earUrl = (URL ) e.nextElement(); 1405 if (earUrl.equals(url)) { 1406 found = true; 1408 } 1409 } 1410 } 1411 if (!found) { 1412 String err = "Cannot undeploy the ear '" + fileName + "', it is not deployed."; 1413 logger.log(BasicLevel.ERROR, err); 1414 throw new Exception (err); 1415 } 1416 } catch (MalformedURLException mue) { 1417 String err = "Error when trying to get the url from" + fileName; 1418 logger.log(BasicLevel.ERROR, err); 1419 throw new Exception (err, mue); 1420 } catch (IOException ioe) { 1421 String err = "Error when trying to get the canonical file from " + fileName; 1422 logger.log(BasicLevel.ERROR, err + ioe.getMessage()); 1423 throw new Exception (err, ioe); 1424 } 1425 1426 Context ctx = null; 1428 try { 1429 ctx = new CompNamingContext(fileName); 1430 ctx.rebind("filename", url); 1432 } catch (NamingException e) { 1433 String err = "Error when binding parameters"; 1434 logger.log(BasicLevel.ERROR, err + e.getMessage()); 1435 throw new Exception (err, e); 1436 } 1437 1438 try { 1440 unDeployEar(ctx); 1441 } catch (EarServiceException e) { 1442 throw new Exception (e); 1443 } 1444 1445 } 1446 1447 1450 public Integer getCurrentNumberOfEars() { 1451 return new Integer (ears.size()); 1452 } 1453 1454 1462 public List getInstalledEars() throws Exception { 1463 ArrayList al = JModule.getInstalledContainersInDir(APPS_DIR, JModule.EAR_EXTENSION, JModule.EAR_CHILD_DIR, 1465 JModule.EAR_CONFIRM_FILE); 1466 Iterator it = autoloadDirectories.iterator(); 1468 while (it.hasNext()) { 1469 String name = (String ) it.next(); 1470 al.addAll(JModule.getInstalledContainersInDir(name, JModule.EAR_EXTENSION, JModule.EAR_CHILD_DIR, 1471 JModule.EAR_CONFIRM_FILE)); 1472 } 1473 return al; 1474 } 1475 1476 1481 public Set getEarNames() { 1482 HashSet names = new HashSet (); 1483 URL earUrl = null; 1484 for (Enumeration e = ears.keys(); e.hasMoreElements();) { 1485 earUrl = (URL ) e.nextElement(); 1486 names.add(earUrl.getFile()); 1487 } 1488 return names; 1489 } 1490 1491 1498 private void addEars(String dirPath) { 1499 boolean found = false; 1500 1501 File dir = new File (APPS_DIR + File.separator + dirPath); 1503 found = dir.isDirectory(); 1504 1505 if (found) { 1506 addEarsFrom(dir); 1507 } else { 1508 String err = "Warning: Cannot load dir: '" + dirPath + "' "; 1509 err += "is not a directory or directory doesn't exist"; 1510 logger.log(BasicLevel.WARN, err); 1511 } 1512 } 1513 1514 1519 private void addEarsFrom(File dir) throws EarServiceException { 1520 try { 1521 if (dir.isDirectory()) { 1522 File [] files = dir.listFiles(); 1523 for (int i = 0; i < files.length; i++) { 1524 if (files[i].getPath().toLowerCase().endsWith(".ear")) { 1525 earNames.add(files[i].getCanonicalPath()); 1526 } else { 1527 if (files[i].isDirectory()) { 1528 addEarsFrom(files[i]); 1529 } 1530 } 1531 } 1532 } else { 1533 String err = "Cannot load dir: '" + dir.getPath(); 1534 err += "' is not a directory"; 1535 logger.log(BasicLevel.ERROR, err); 1536 throw new EarServiceException(err); 1537 } 1538 } catch (IOException e) { 1539 String err = "Invalid file name '" + dir.getPath(); 1540 logger.log(BasicLevel.ERROR, err); 1541 throw new EarServiceException(err, e); 1542 } 1543 } 1544 1545 1551 public boolean isEarLoaded(String fileName) { 1552 1553 URL url = null; 1554 boolean isLoaded = false; 1555 try { 1556 try { 1558 url = new File (fileName).getCanonicalFile().toURL(); 1559 if (ears.get(url) != null) { 1561 isLoaded = true; 1562 } else { 1563 url = null; 1565 } 1566 } catch (Exception e) { 1567 url = null; 1568 } 1569 if (url == null) { 1571 url = new File (APPS_DIR + File.separator + fileName).getCanonicalFile().toURL(); 1572 if (ears.get(url) != null) { 1574 isLoaded = true; 1575 } 1576 } 1577 } catch (Exception e) { 1578 String err = "Can not found if the ear is deployed or not"; 1579 logger.log(BasicLevel.ERROR, err); 1580 return false; 1581 } 1582 1583 return isLoaded; 1584 } 1585 1586 1592 public Boolean isEarDeployed(String fileName) { 1593 return new Boolean (isEarLoaded(fileName)); 1594 } 1595 1596 1602 public boolean isEarDeployedByUnpackName(String unpackName) { 1603 if (logger.isLoggable(BasicLevel.DEBUG)) { 1604 logger.log(BasicLevel.DEBUG, "entering for unpackName= " + unpackName); 1605 } 1606 Enumeration lc = ears.elements(); 1608 while (lc.hasMoreElements()) { 1609 Ear ear = (Ear) lc.nextElement(); 1610 1611 String deployedUnpackName = new File (ear.getUnpackName()).getName(); 1613 if (logger.isLoggable(BasicLevel.DEBUG)) { 1614 logger.log(BasicLevel.DEBUG, "deployedUnpackName=" + deployedUnpackName); 1615 } 1616 1617 if (deployedUnpackName.equals(unpackName)) { 1618 return true; 1619 } 1620 } 1622 return false; 1624 } 1625 1626 1627 1631 public List getDeployedEars() { 1632 ArrayList al = new ArrayList (); 1633 Ear oEar; 1634 URL oURL; 1635 for (Enumeration enumEars = ears.elements(); enumEars.hasMoreElements();) { 1636 oEar = (Ear) enumEars.nextElement(); 1637 oURL = oEar.getEarUrl(); 1638 al.add(oURL.getFile()); 1639 } 1640 return al; 1641 } 1642 1643 1648 public List getDeployableEars() throws Exception { 1649 List al = getInstalledEars(); 1650 al.removeAll(getDeployedEars()); 1651 return al; 1652 } 1653 1654 1658 public List getAutoloadDirectories() { 1659 ArrayList al = new ArrayList (); 1660 Iterator it = autoloadDirectories.iterator(); 1661 while (it.hasNext()) { 1662 String fileName = (String ) it.next(); 1663 try { 1664 al.add(new File (fileName).toURL().getPath()); 1665 } catch (Exception e) { 1666 if (logger.isLoggable(BasicLevel.DEBUG)) { 1667 logger.log(BasicLevel.DEBUG, "Can't get autoload directories : " + e.getMessage()); 1668 } 1669 } 1670 } 1671 return al; 1672 } 1673 1674 1678 public String getAppsDirectory() { 1679 String sRet = null; 1680 try { 1681 sRet = (new File (APPS_DIR)).toURL().getPath(); 1682 } catch (Exception e) { 1683 throw new RuntimeException ("Cannot get the APPS directory", e); 1684 } 1685 return sRet; 1686 } 1687 1688 1693 public static String buildJ2eeApplicationName(URL pUrl) { 1694 String sName = null; 1695 try { 1696 sName = new File (pUrl.getFile()).getName(); 1697 if ("file".equals(pUrl.getProtocol())) { 1698 sName = buildJ2eeApplicationName(sName); 1699 } 1700 } catch (NullPointerException e) { 1701 if (logger != null) { 1702 if (logger.isLoggable(BasicLevel.DEBUG)) { 1703 logger.log(BasicLevel.DEBUG, "Can't build j2ee application", e); 1704 } 1705 } 1706 } 1707 return sName; 1708 } 1709 1710 1715 public static String buildJ2eeApplicationName(String pFilename) { 1716 String sName = null; 1717 try { 1718 sName = new File (pFilename).getName(); 1719 int iPos = sName.lastIndexOf('.'); 1720 if (iPos > -1) { 1721 sName = sName.substring(0, iPos); 1722 } 1723 } catch (NullPointerException e) { 1724 if (logger != null) { 1725 if (logger.isLoggable(BasicLevel.DEBUG)) { 1726 logger.log(BasicLevel.DEBUG, "Can't build j2ee application : " + e.getMessage()); 1727 } 1728 } 1729 } 1730 return sName; 1731 } 1732 1733 1740 private void linkPolicyObjects(Map userToRoleMapping, URL [] jarUrls, URL [] warUrls) throws EarServiceException { 1741 List ctxIDs = new LinkedList (); 1742 if (ejbService != null) { 1744 for (int u = 0; u < jarUrls.length; u++) { 1745 ctxIDs.add(ejbService.getContainer(jarUrls[u].getFile()).getContextId()); 1746 } 1747 } 1748 if (webContainerService != null) { 1750 for (int u = 0; u < warUrls.length; u++) { 1751 ctxIDs.add(webContainerService.getWar(warUrls[u]).getContextId()); 1752 } 1753 } 1754 1755 PolicyConfigurationFactory pcFactory = null; 1756 try { 1757 pcFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory(); 1758 } catch (Exception cnfe) { 1759 String err = "Cannot retrieve current policy configuration factory"; 1760 logger.log(BasicLevel.ERROR, err + "': " + cnfe.getMessage()); 1761 throw new EarServiceException(err, cnfe); 1762 } 1763 1764 try { 1765 for (Iterator itCtxId = ctxIDs.iterator(); itCtxId.hasNext();) { 1767 String toBeLinkedCtxId = (String ) itCtxId.next(); 1768 PolicyConfiguration toBeLinkedPC = pcFactory.getPolicyConfiguration(toBeLinkedCtxId, false); 1769 for (Iterator linkCId = ctxIDs.iterator(); linkCId.hasNext();) { 1770 String linkedCtxId = (String ) linkCId.next(); 1771 if (!toBeLinkedCtxId.equals(linkedCtxId)) { 1772 PolicyConfiguration linkedPC = pcFactory.getPolicyConfiguration(linkedCtxId, false); 1773 toBeLinkedPC.linkConfiguration(linkedPC); 1774 } 1775 } 1776 } 1777 } catch (PolicyContextException pce) { 1778 String err = "Cannot retrieve a policy configuration"; 1779 logger.log(BasicLevel.ERROR, err + "': " + pce.getMessage()); 1780 throw new EarServiceException(err, pce); 1781 } 1782 1783 if (userToRoleMapping != null) { 1785 for (Iterator itCtxId = ctxIDs.iterator(); itCtxId.hasNext();) { 1786 String contextId = (String ) itCtxId.next(); 1787 for (Iterator itMapping = userToRoleMapping.keySet().iterator(); itMapping.hasNext();) { 1788 String principalName = (String ) itMapping.next(); 1789 List roles = (List ) userToRoleMapping.get(principalName); 1790 String [] roleNames = (String []) roles.toArray(new String [roles.size()]); 1791 JPolicyUserRoleMapping.addUserToRoleMapping(contextId, principalName, roleNames); 1792 } 1793 } 1794 1795 } 1796 1797 1798 String ctxId = null; 1799 try { 1800 for (Iterator itCtxId = ctxIDs.iterator(); itCtxId.hasNext();) { 1802 ctxId = (String ) itCtxId.next(); 1803 PolicyConfiguration pc = pcFactory.getPolicyConfiguration(ctxId, false); 1804 pc.commit(); 1805 } 1806 } catch (PolicyContextException pce) { 1807 String err = "Cannot commit policy configuration with Id '" + ctxId + "'"; 1808 logger.log(BasicLevel.ERROR, err + "': " + pce.getMessage()); 1809 throw new EarServiceException(err, pce); 1810 } 1811 1812 Policy.getPolicy().refresh(); 1814 } 1815 1816} | Popular Tags |