00001
00027 package org.objectweb.jonas.ear;
00028
00029 import java.io.File;
00030 import java.io.IOException;
00031 import java.net.MalformedURLException;
00032 import java.net.URL;
00033 import java.net.URLClassLoader;
00034 import java.security.Policy;
00035 import java.util.ArrayList;
00036 import java.util.Date;
00037 import java.util.Enumeration;
00038 import java.util.HashSet;
00039 import java.util.Hashtable;
00040 import java.util.Iterator;
00041 import java.util.List;
00042 import java.util.Map;
00043 import java.util.Set;
00044 import java.util.StringTokenizer;
00045 import java.util.Vector;
00046
00047 import javax.management.InstanceAlreadyExistsException;
00048 import javax.management.MBeanRegistrationException;
00049 import javax.management.MBeanServer;
00050 import javax.management.NotCompliantMBeanException;
00051 import javax.management.ObjectName;
00052 import javax.management.modelmbean.ModelMBean;
00053 import javax.naming.Context;
00054 import javax.naming.NamingException;
00055 import javax.security.jacc.PolicyConfiguration;
00056 import javax.security.jacc.PolicyConfigurationFactory;
00057 import javax.security.jacc.PolicyContextException;
00058
00059 import org.apache.commons.modeler.ManagedBean;
00060 import org.apache.commons.modeler.Registry;
00061 import org.objectweb.jonas.client.AppClientModule;
00062 import org.objectweb.jonas.common.JModule;
00063 import org.objectweb.jonas.common.JProp;
00064 import org.objectweb.jonas.common.Log;
00065 import org.objectweb.jonas.container.EJBService;
00066 import org.objectweb.jonas.container.EJBServiceImpl;
00067 import org.objectweb.jonas.ear.lib.EarClassPathManager;
00068 import org.objectweb.jonas.ear.lib.EarClassPathManagerException;
00069 import org.objectweb.jonas.ear.lib.JarList;
00070 import org.objectweb.jonas.ear.lib.JarListException;
00071 import org.objectweb.jonas.jmx.J2eeObjectName;
00072 import org.objectweb.jonas.jmx.JmxService;
00073 import org.objectweb.jonas.jmx.JonasObjectName;
00074 import org.objectweb.jonas.management.JonasMBeanTools;
00075 import org.objectweb.jonas.naming.CompNamingContext;
00076 import org.objectweb.jonas.resource.ResourceService;
00077 import org.objectweb.jonas.resource.ResourceServiceException;
00078 import org.objectweb.jonas.security.jacc.JPolicyUserRoleMapping;
00079 import org.objectweb.jonas.server.JClassLoader;
00080 import org.objectweb.jonas.server.LoaderManager;
00081 import org.objectweb.jonas.service.AbsServiceImpl;
00082 import org.objectweb.jonas.service.ServiceException;
00083 import org.objectweb.jonas.service.ServiceManager;
00084 import org.objectweb.jonas.web.JWebContainerService;
00085 import org.objectweb.jonas.web.JWebContainerServiceException;
00086 import org.objectweb.jonas.ws.AbsWebServicesServiceImpl;
00087 import org.objectweb.jonas.ws.WSServiceException;
00088 import org.objectweb.jonas.ws.WebServicesService;
00089 import org.objectweb.jonas_client.deployment.api.ClientContainerDeploymentDesc;
00090 import org.objectweb.jonas_client.deployment.api.ClientContainerDeploymentDescException;
00091 import org.objectweb.jonas_client.deployment.lib.wrapper.ClientManagerWrapper;
00092 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
00093 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException;
00094 import org.objectweb.jonas_ear.deployment.lib.wrapper.EarManagerWrapper;
00095 import org.objectweb.jonas_ear.deployment.xml.Web;
00096
00097 import org.objectweb.jonas_ejb.deployment.lib.wrapper.EjbManagerWrapper;
00098 import org.objectweb.jonas_lib.deployment.work.CleanerException;
00099 import org.objectweb.jonas_lib.deployment.work.DeployerLog;
00100 import org.objectweb.jonas_lib.deployment.work.DeployerLogException;
00101 import org.objectweb.jonas_lib.deployment.work.EarCleanTask;
00102 import org.objectweb.jonas_lib.deployment.work.EarFileManager;
00103 import org.objectweb.jonas_lib.deployment.work.FileManagerException;
00104 import org.objectweb.jonas_lib.deployment.work.WorkCleaner;
00105 import org.objectweb.jonas_lib.loader.ClientClassLoader;
00106 import org.objectweb.jonas_lib.loader.EjbJarClassLoader;
00107 import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper;
00108 import org.objectweb.util.monolog.api.BasicLevel;
00109 import org.objectweb.util.monolog.api.Logger;
00110
00119 public class EarServiceImpl extends AbsServiceImpl implements EarService, EarServiceImplMBean {
00120
00124 protected static final String JONAS_BASE = JProp.getJonasBase();
00125
00129 protected static final String APPS_DIR = JONAS_BASE + File.separator + "apps";
00130
00134 protected static final String WORK_DIR = JProp.getWorkDir();
00135
00139 protected static final String WORK_APPS_DIR = WORK_DIR + File.separator + "apps";
00140
00144 protected static final String DESCRIPTORS = "jonas.service.ear.descriptors";
00145
00149 protected static final String AUTOLOADDIR = "jonas.service.ear.autoloaddir";
00150
00154 protected static final String PARSINGWITHVALIDATION = "jonas.service.ear.parsingwithvalidation";
00155
00159 protected static final String CLASS = "jonas.service.ear.class";
00160
00164 private static Logger logger = null;
00165
00169 private static WorkCleaner workCleaner = null;
00170
00174 private MBeanServer mbeanServer = null;
00175
00179 private Vector earNames = new Vector();
00180
00184 private EJBService ejbService = null;
00185
00189 private JWebContainerService webContainerService = null;
00190
00194 private WebServicesService wsService = null;
00195
00199 private ResourceService resourceService = null;
00200
00204 private Hashtable ears = null;
00205
00209 private ArrayList autoloadDirectories = new ArrayList();
00210
00215 private DeployerLog earDeployerLog = null;
00216
00220 private ClassLoader appsClassLoader;
00221
00227 protected void doInit(Context ctx) throws ServiceException {
00228
00229
00230 logger = Log.getLogger(Log.JONAS_EAR_PREFIX);
00231
00232
00233 try {
00234 LoaderManager lm = LoaderManager.getInstance();
00235 appsClassLoader = lm.getAppsLoader();
00236 } catch (Exception e) {
00237 logger.log(BasicLevel.ERROR, "Cannot get the Applications ClassLoader from EAR Container Service: " + e);
00238 throw new ServiceException("Cannot get the Applications ClassLoader from EAR Container Service", e);
00239 }
00240
00241 ServiceManager sm = null;
00242
00243 try {
00244 sm = ServiceManager.getInstance();
00245 } catch (Exception e) {
00246 String err = "Cannot get ServiceManager instance";
00247 logger.log(BasicLevel.ERROR, err);
00248 throw new ServiceException(err, e);
00249 }
00250
00251
00252 try {
00253 mbeanServer = ((JmxService) sm.getJmxService()).getJmxServer();
00254 } catch (ServiceException e) {
00255
00256 mbeanServer = null;
00257 }
00258
00259
00260 try {
00261 ejbService = (EJBService) sm.getEjbService();
00262 } catch (ServiceException e) {
00263
00264 ejbService = null;
00265 }
00266
00267
00268 try {
00269 webContainerService = (JWebContainerService) sm.getWebContainerService();
00270 } catch (ServiceException e) {
00271
00272 webContainerService = null;
00273 }
00274
00275
00276 try {
00277 wsService = (WebServicesService) sm.getWebServicesService();
00278 } catch (ServiceException e) {
00279
00280 wsService = null;
00281 }
00282
00283
00284 try {
00285 resourceService = (ResourceService) sm.getResourceService();
00286 } catch (ServiceException e) {
00287
00288 resourceService = null;
00289 }
00290
00291
00292 String parsingMode = "false";
00293 try {
00294 parsingMode = (String) ctx.lookup(PARSINGWITHVALIDATION);
00295 } catch (NamingException e) {
00296
00297
00298
00299
00300 logger.log(BasicLevel.DEBUG, "No XML parsing validation property, use default");
00301 }
00302 EarManagerWrapper.setParsingWithValidation("true".equalsIgnoreCase(parsingMode));
00303 if ("false".equalsIgnoreCase(parsingMode)) {
00304 logger.log(BasicLevel.DEBUG, "Ear XML parsing without validation");
00305 } else {
00306 logger.log(BasicLevel.DEBUG, "Ear XML parsing with validation");
00307 }
00308
00309
00310 String descsValue = null;
00311 try {
00312 descsValue = (String) ctx.lookup(DESCRIPTORS);
00313 } catch (NamingException e) {
00314 logger.log(BasicLevel.DEBUG, "No property DESCRIPTORS");
00315
00316
00317 }
00318 if (descsValue != null) {
00319 StringTokenizer st = new StringTokenizer(descsValue, ",");
00320 while (st.hasMoreTokens()) {
00321 String fileName = st.nextToken().trim();
00322 earNames.add(fileName);
00323 }
00324 }
00325
00326
00327 String dirValue = null;
00328 ArrayList autoDirs = new ArrayList();
00329 try {
00330 dirValue = (String) ctx.lookup(AUTOLOADDIR);
00331 } catch (NamingException e) {
00332 logger.log(BasicLevel.DEBUG, "No AUTOLOADDIR property");
00333
00334
00335 }
00336 if (dirValue != null) {
00337 StringTokenizer st = new StringTokenizer(dirValue, ",");
00338 while (st.hasMoreTokens()) {
00339 String dirName = st.nextToken().trim();
00340 addEars(dirName);
00341 autoDirs.add(dirName);
00342 }
00343 }
00344
00345 File oFile;
00346 Iterator it = autoDirs.iterator();
00347 while (it.hasNext()) {
00348 String dirName = (String) it.next();
00349 try {
00350 oFile = new File(APPS_DIR, dirName);
00351 if (!oFile.exists()) {
00352 oFile = new File(dirName);
00353 }
00354 if (oFile.exists()) {
00355 autoloadDirectories.add(oFile.getCanonicalPath());
00356 }
00357 } catch (Exception e) {
00358 String err = "Error when trying to verify Application EAR autoload directory : " + dirName;
00359 logger.log(BasicLevel.ERROR, err + " " + e.getMessage());
00360 }
00361 }
00362
00363
00364 ears = new Hashtable();
00365
00366
00367
00368 }
00369
00374 protected void doStop() throws ServiceException {
00375
00376
00377 URL earFileName = null;
00378
00379
00380 for (Enumeration earEntries = ears.keys(); earEntries.hasMoreElements();) {
00381 earFileName = (URL) earEntries.nextElement();
00382
00383 try {
00384 Context ctx = new CompNamingContext(earFileName.getFile());
00385 ctx.rebind("filename", earFileName);
00386 unDeployEar(ctx);
00387 } catch (Exception e) {
00388
00389
00390 String err = "Error when undeploying the ear :" + earFileName;
00391 logger.log(BasicLevel.ERROR, err + e.getMessage());
00392 }
00393 }
00394
00395 if (mbeanServer != null) {
00396 try {
00397
00398 mbeanServer.unregisterMBean(JonasObjectName.earService());
00399 } catch (Exception e) {
00400 logger.log(BasicLevel.ERROR, "Cannot stop the EarService: " + e.getMessage());
00401 }
00402 }
00403
00404
00405 logger.log(BasicLevel.DEBUG, "EarService stopped");
00406
00407 }
00408
00418 public String deployEar(Context ctx) throws EarServiceException {
00419
00420
00421 if ((webContainerService == null) && (ejbService == null)) {
00422 throw new EarServiceException(
00423 "The ear service requires that at least the service ejb or web is launched for deploying an ear file.");
00424 }
00425
00426
00427
00428 String fileName;
00429 try {
00430 fileName = (String) ctx.lookup("filename");
00431 } catch (NamingException e) {
00432 throw new EarServiceException("Error during performing lookup a fileName", e);
00433 }
00434
00435 File f = null;
00436 try {
00437
00438 f = new File(fileName).getCanonicalFile();
00439
00440 if (!f.exists()) {
00441 boolean found = false;
00442 String earFileName = null;
00443 if (fileName.toLowerCase().endsWith(".ear")) {
00444
00445
00446 earFileName = APPS_DIR + File.separator + fileName;
00447 f = new File(earFileName).getCanonicalFile();
00448 found = f.exists();
00449 }
00450 if (found) {
00451 fileName = earFileName;
00452 } else {
00453 String err = "deployEar: The file " + fileName
00454 + " was not found neither in the current directory nor in the " + APPS_DIR + " directory";
00455 logger.log(BasicLevel.ERROR, err);
00456 throw new EarServiceException(err);
00457 }
00458 }
00459 } catch (IOException e) {
00460 String err = "Error when trying to get the canonical file from " + fileName;
00461 logger.log(BasicLevel.ERROR, err + " " + e.getMessage());
00462 throw new EarServiceException(err, e);
00463 }
00464
00465
00466 URL[] earUrl = new URL[1];
00467 URL earRootUrl = null;
00468
00469 try {
00470 earUrl[0] = f.toURL();
00471 earRootUrl = (new File(WORK_APPS_DIR + File.separator + getJonasServerName())).toURL();
00472 } catch (MalformedURLException e) {
00473 String err = "Invalid ear file name '" + fileName;
00474 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage());
00475 throw new EarServiceException(err, e);
00476 }
00477
00478
00479 if (ears.get(earUrl[0]) != null) {
00480 String err = "The ear file : " + f.getName() + " is already deployed ('" + earUrl[0].getFile()
00481 + "'). You must undeploy the application " + "before a new deployment.";
00482 logger.log(BasicLevel.ERROR, err);
00483 throw new EarServiceException(err);
00484 }
00485
00486
00487
00488 URLClassLoader loaderCls = new URLClassLoader(earUrl, appsClassLoader);
00489
00490 EarDeploymentDesc earDD = null;
00491 logger.log(BasicLevel.DEBUG, "Getting the deployment descriptor of the file" + f.getName());
00492 try {
00493 earDD = EarManagerWrapper.getDeploymentDesc(earUrl[0].getFile(), loaderCls);
00494 } catch (EarDeploymentDescException e) {
00495 String err = "Error in the Deployment descriptor '" + fileName + "': " + e;
00496 logger.log(BasicLevel.ERROR, err);
00497 throw new EarServiceException(err, e);
00498 }
00499
00500
00501 Web[] webTags = earDD.getWebTags();
00502 String[] ejbTags = earDD.getEjbTags();
00503 String[] connectorTags = earDD.getConnectorTags();
00504 String[] clientTags = earDD.getClientTags();
00505 String[] altDDClients = earDD.getAltDDClients();
00506 String[] altDDEjbs = earDD.getAltDDEjbs();
00507 String[] altDDWebs = earDD.getAltDDWebs();
00508 String[] altDDConnectors = earDD.getAltDDConnectors();
00509 String[] securityRoles = earDD.getSecurityRolesNames();
00510
00511
00512
00513
00514 File fEar = new File(earUrl[0].getFile());
00515 File tmpFile = null;
00516
00517 try {
00518 File fCanonicEar = fEar.getCanonicalFile();
00519 for (int i = 0; i < ejbTags.length; i++) {
00520 tmpFile = new File(fEar, ejbTags[i]);
00521 tmpFile = tmpFile.getCanonicalFile();
00522 if (!tmpFile.getPath().startsWith(fCanonicEar.getPath())) {
00523 String err = "Error : The ejb-jar file " + ejbTags[i] + " is not inside the ear file " + fEar;
00524 logger.log(BasicLevel.ERROR, err);
00525 throw new EarServiceException(err);
00526 }
00527 }
00528
00529 for (int i = 0; i < webTags.length; i++) {
00530 tmpFile = new File(fEar, webTags[i].getWebUri());
00531 tmpFile = tmpFile.getCanonicalFile();
00532 if (!tmpFile.getPath().startsWith(fCanonicEar.getPath())) {
00533 String err = "Error : The war file " + webTags[i].getWebUri() + " is not inside the ear file "
00534 + fEar;
00535 logger.log(BasicLevel.ERROR, err);
00536 throw new EarServiceException(err);
00537 }
00538 }
00539
00540 for (int i = 0; i < connectorTags.length; i++) {
00541 tmpFile = new File(fEar, connectorTags[i]);
00542 tmpFile = tmpFile.getCanonicalFile();
00543 if (!tmpFile.getPath().startsWith(fCanonicEar.getPath())) {
00544 String err = "Error : The rar file " + connectorTags[i] + " is not inside the ear file " + fEar;
00545 logger.log(BasicLevel.ERROR, err);
00546 throw new EarServiceException(err);
00547 }
00548 }
00549
00550
00551 for (int i = 0; i < clientTags.length; i++) {
00552 tmpFile = new File(fEar, clientTags[i]);
00553 tmpFile = tmpFile.getCanonicalFile();
00554
00555 if (!tmpFile.getPath().startsWith(fCanonicEar.getPath())) {
00556 String err = "Error : The client jar file " + clientTags[i] + " is not inside the ear file " + fEar;
00557 throw new EarServiceException(err);
00558 }
00559 }
00560 } catch (IOException ioe) {
00561 String err = "Error while trying to get the canonical file of " + tmpFile;
00562 logger.log(BasicLevel.ERROR, err + " : " + ioe.getMessage());
00563 throw new EarServiceException(err, ioe);
00564 }
00565
00566
00567 JarList ejbsList = new JarList(ejbTags);
00568 JarList warsList = new JarList(webTags);
00569 JarList connectorsList = new JarList(connectorTags);
00570 JarList clientsList = new JarList(clientTags);
00571
00572
00573 URL dirUnpackURL = null;
00574 try {
00575 dirUnpackURL = EarFileManager.unpackEar(earUrl[0], earRootUrl);
00576 } catch (FileManagerException e) {
00577 String err = "Error while unpacking the file '" + earUrl[0] + "'";
00578 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage());
00579 throw new EarServiceException(err, e);
00580 }
00581
00582
00583
00584 if (new File(earUrl[0].getFile()).isFile()) {
00585 try {
00586 earDeployerLog.addEntry(new File(earUrl[0].getFile()), new File(dirUnpackURL.getFile()));
00587 } catch (DeployerLogException e) {
00588 String err = "Error while adding the " + earUrl[0] + " entry in the log file";
00589 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage());
00590 throw new EarServiceException(err, e);
00591 }
00592 }
00593
00594
00595
00596 EarClassPathManager earCPManager = null;
00597 try {
00598 earCPManager = new EarClassPathManager(ejbsList, warsList, dirUnpackURL);
00599 } catch (EarClassPathManagerException e) {
00600 String err = "Error while creating the Ear class path manager of the ear : '" + earUrl[0] + "'";
00601 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage());
00602 throw new EarServiceException(err, e);
00603 }
00604
00605 URL[] classpathURLs = null;
00606
00607 try {
00608 classpathURLs = earCPManager.getResolvedClassPath();
00609 } catch (EarClassPathManagerException e) {
00610 String err = "Error while trying to resolve the classpath of the ejbjars and wars of the ear : '"
00611 + earUrl[0] + "'";
00612 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage());
00613 throw new EarServiceException(err, e);
00614 }
00615
00616
00617
00618
00619
00620 logger.log(BasicLevel.DEBUG, "Creating the EAR classLoader");
00621 JClassLoader earClassLoader = new JClassLoader(earUrl[0].toExternalForm() , new URL[0], appsClassLoader);
00622
00623
00624 URL[] jarUrls = null;
00625 URL[] warUrls = null;
00626 URL[] connectorUrls = null;
00627 URL[] clientUrls = null;
00628 try {
00629 jarUrls = ejbsList.getURLs(dirUnpackURL.toExternalForm());
00630 warUrls = warsList.getURLs(dirUnpackURL.toExternalForm());
00631 connectorUrls = connectorsList.getURLs(dirUnpackURL.toExternalForm());
00632 clientUrls = clientsList.getURLs(dirUnpackURL.toExternalForm());
00633 } catch (JarListException e) {
00634 String err = "Error while geting the Urls from jarlist of the ear : '" + earUrl[0] + "'";
00635 logger.log(BasicLevel.ERROR, err + " : " + e.getMessage());
00636 throw new EarServiceException(err, e);
00637 }
00638
00639
00640
00641 String[] warsContextRoots = new String[webTags.length];
00642 String ctxRoot = null;
00643 for (int i = 0; i < webTags.length; i++) {
00644 ctxRoot = webTags[i].getContextRoot();
00645 if (ctxRoot != null) {
00646 warsContextRoots[i] = ctxRoot;
00647 }
00648 }
00649
00650
00651 String altdd = null;
00652 File fAltDD = null;
00653
00654
00655
00656 URL[] warsAltDDs = new URL[altDDWebs.length];
00657 for (int i = 0; i < altDDWebs.length; i++) {
00658 if (altDDWebs[i] != null) {
00659 altdd = altDDWebs[i];
00660 if (altdd != null) {
00661 try {
00662 fAltDD = new File(new URL(dirUnpackURL.toExternalForm() + File.separator + altdd).getFile());
00663 warsAltDDs[i] = fAltDD.getCanonicalFile().toURL();
00664 } catch (MalformedURLException e) {
00665 String err = "Can't build URL for alt-dd '" + altdd;
00666 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage());
00667 throw new EarServiceException(err, e);
00668 } catch (IOException ioe) {
00669 String err = "Can't get canonicalFile() for the file '" + fAltDD;
00670 logger.log(BasicLevel.ERROR, err + "': " + ioe.getMessage());
00671 throw new EarServiceException(err, ioe);
00672 }
00673 }
00674 }
00675 }
00676
00677 URL[] ejbsAltDDs = new URL[altDDEjbs.length];
00678 for (int i = 0; i < altDDEjbs.length; i++) {
00679 if (altDDEjbs[i] != null) {
00680 altdd = altDDEjbs[i];
00681 if (altdd != null) {
00682 try {
00683 fAltDD = new File(new URL(dirUnpackURL.toExternalForm() + File.separator + altdd).getFile());
00684 ejbsAltDDs[i] = fAltDD.getCanonicalFile().toURL();
00685 } catch (MalformedURLException e) {
00686 String err = "Can't build URL for alt-dd '" + altdd;
00687 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage());
00688 throw new EarServiceException(err, e);
00689 } catch (IOException ioe) {
00690 String err = "Can't get canonicalFile() for the file '" + fAltDD;
00691 logger.log(BasicLevel.ERROR, err + "': " + ioe.getMessage());
00692 throw new EarServiceException(err, ioe);
00693 }
00694 }
00695 }
00696 }
00697
00698 URL[] connectorsAltDDs = new URL[altDDConnectors.length];
00699 for (int i = 0; i < altDDConnectors.length; i++) {
00700 if (altDDConnectors[i] != null) {
00701 altdd = altDDConnectors[i];
00702 if (altdd != null) {
00703 try {
00704 fAltDD = new File(new URL(dirUnpackURL.toExternalForm() + File.separator + altdd).getFile());
00705 connectorsAltDDs[i] = fAltDD.getCanonicalFile().toURL();
00706 } catch (MalformedURLException e) {
00707 String err = "Can't build URL for alt-dd '" + altdd;
00708 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage());
00709 throw new EarServiceException(err, e);
00710 } catch (IOException ioe) {
00711 String err = "Can't get canonicalFile() for the file '" + fAltDD;
00712 logger.log(BasicLevel.ERROR, err + "': " + ioe.getMessage());
00713 throw new EarServiceException(err, ioe);
00714 }
00715 }
00716 }
00717 }
00718
00719 URL[] clientAltDDs = new URL[altDDClients.length];
00720 for (int i = 0; i < altDDClients.length; i++) {
00721 if (altDDClients[i] != null) {
00722 altdd = altDDClients[i];
00723 if (altdd != null) {
00724 try {
00725 fAltDD = new File(new URL(dirUnpackURL.toExternalForm() + File.separator + altdd).getFile());
00726 clientAltDDs[i] = fAltDD.getCanonicalFile().toURL();
00727 } catch (MalformedURLException e) {
00728 String err = "Can't build URL for alt-dd '" + altdd;
00729 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage());
00730 throw new EarServiceException(err, e);
00731 } catch (IOException ioe) {
00732 String err = "Can't get canonicalFile() for the file '" + fAltDD;
00733 logger.log(BasicLevel.ERROR, err + "': " + ioe.getMessage());
00734 throw new EarServiceException(err, ioe);
00735 }
00736 }
00737 }
00738 }
00739
00740 String[] roleNames = new String[securityRoles.length];
00741 String affRoleNames = "";
00742 for (int i = 0; i < securityRoles.length; i++) {
00743 roleNames[i] = securityRoles[i];
00744 affRoleNames += roleNames[i] + ";";
00745 }
00746
00747 logger.log(BasicLevel.DEBUG, "role names = " + affRoleNames);
00748
00761
00762
00763
00764 EjbManagerWrapper.setAvailableEjbJarsAndAltDDs(earClassLoader, jarUrls, ejbsAltDDs);
00765 WebManagerWrapper.setAltDD(earClassLoader, warUrls, warsAltDDs);
00766 ClientManagerWrapper.setAltDD(earClassLoader, clientUrls, clientAltDDs);
00767
00768
00769
00770
00771 URL[] urls = new URL[jarUrls.length + classpathURLs.length];
00772 System.arraycopy(jarUrls, 0, urls, 0, jarUrls.length);
00773 System.arraycopy(classpathURLs, 0, urls, jarUrls.length, classpathURLs.length);
00774 URLClassLoader ejbClassLoader;
00775 try {
00776 ejbClassLoader = new EjbJarClassLoader(urls, earClassLoader);
00777 } catch (IOException ioe) {
00778 String err = "Cannot Create EJB ClassLoader for EAR '" + fileName + "'";
00779 logger.log(BasicLevel.ERROR, err + " : " + ioe.getMessage());
00780 throw new EarServiceException(err, ioe);
00781 }
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792 if (resourceService != null && (connectorUrls.length > 0)) {
00793 try {
00794 CompNamingContext contctx = null;
00795 try {
00796 contctx = new CompNamingContext(dirUnpackURL.getFile());
00797 contctx.rebind("earUrl", earUrl[0]);
00798 contctx.rebind("urls", connectorUrls);
00799 contctx.rebind("earClassLoader", earClassLoader);
00800 contctx.rebind("altDDs", connectorsAltDDs);
00801 } catch (NamingException e) {
00802 String err = "Can not bind params for the resource service, Can't deploy rars ";
00803 throw new ResourceServiceException(err, e);
00804 }
00805 resourceService.deployRars(contctx);
00806 } catch (ServiceException e) {
00807 String err = "Error during the deployment of the rars files of the Ear file " + fileName;
00808 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage());
00809
00810
00811 throw new EarServiceException(err, e);
00812 }
00813 }
00814
00815
00816 if (wsService != null) {
00817 try {
00818 Hashtable ctxRoots = new Hashtable();
00819 for (int w = 0; w < webTags.length; w++) {
00820 ctxRoots.put(warUrls[w], webTags[w].getContextRoot());
00821 }
00822 CompNamingContext contctx = null;
00823 try {
00824 contctx = new CompNamingContext(dirUnpackURL.getFile());
00825 contctx.rebind("unpackDir", dirUnpackURL.toExternalForm());
00826 contctx.rebind("jarUrls", jarUrls);
00827 contctx.rebind("warUrls", warUrls);
00828 contctx.rebind("earURL", earUrl[0]);
00829 contctx.rebind("ejbClassLoader", ejbClassLoader);
00830 contctx.rebind("earClassLoader", earClassLoader);
00831 contctx.rebind("warCtxRootMapping", ctxRoots);
00832 } catch (NamingException e) {
00833 String err = "Cannot bind params for the WebServices service, Can't deploy Web Services Endpoints ";
00834 throw new WSServiceException(err, e);
00835 }
00836 wsService.deployWebServices(contctx);
00837 } catch (ServiceException se) {
00838 String err = "Error during the deployment of the WebServices of the Ear file " + fileName;
00839 logger.log(BasicLevel.ERROR, err + " : " + se.getMessage());
00840 wsService.removeCache(earClassLoader);
00841 try {
00842 logger.log(BasicLevel.DEBUG, "Undeploying Rars of the ear " + fileName);
00843 if (resourceService != null && connectorUrls.length > 0) {
00844
00845 resourceService.unDeployRars(connectorUrls, earUrl[0]);
00846 }
00847 } catch (ServiceException se2) {
00848 err = "Error during the undeployment of the rars files of the Ear file " + fileName;
00849 logger.log(BasicLevel.ERROR, err + "': " + se2.getMessage());
00850 }
00851
00852 throw new EarServiceException(err, se);
00853 }
00854 }
00855
00856
00857
00858
00859
00860 if (ejbService != null && (jarUrls.length > 0)) {
00861 try {
00862 CompNamingContext contctx = null;
00863 try {
00864 contctx = new CompNamingContext(dirUnpackURL.getFile());
00865 contctx.rebind("earRootUrl", dirUnpackURL);
00866 contctx.rebind("earUrl", earUrl[0]);
00867 contctx.rebind("jarURLs", jarUrls);
00868 contctx.rebind("earClassLoader", earClassLoader);
00869 contctx.rebind("ejbClassLoader", ejbClassLoader);
00870 contctx.rebind("roleNames", roleNames);
00871 } catch (NamingException e) {
00872 String err = "Can not bind params for the ejb service, Can't deploy jars ";
00873 throw new EarServiceException(err, e);
00874 }
00875 ejbService.deployJars(contctx);
00876 } catch (ServiceException e) {
00877 String err = "Error during the deployment of the jars files of the Ear file " + fileName;
00878 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage());
00879
00880 ejbService.removeCache(earClassLoader);
00881
00882 if (resourceService != null && connectorUrls.length > 0) {
00883
00884 resourceService.unDeployRars(connectorUrls, earUrl[0]);
00885 }
00886 throw new EarServiceException(err, e);
00887 }
00888 }
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901 ClassLoader parentWarClassLoader = ejbClassLoader;
00902
00903
00904
00905 if (webContainerService != null && (warUrls.length > 0)) {
00906 try {
00907 CompNamingContext contctx = null;
00908 try {
00909 contctx = new CompNamingContext(dirUnpackURL.getFile());
00910 contctx.rebind("earURL", earUrl[0]);
00911 contctx.rebind("urls", warUrls);
00912 contctx.rebind("parentClassLoader", parentWarClassLoader);
00913 contctx.rebind("earClassLoader", earClassLoader);
00914 contctx.rebind("altDDs", warsAltDDs);
00915 contctx.rebind("contextRoots", warsContextRoots);
00916 } catch (NamingException e) {
00917 String err = "Can not bind params for the web container service, Can't deploy wars ";
00918 throw new EarServiceException(err, e);
00919 }
00920 webContainerService.deployWars(contctx);
00921
00922 } catch (JWebContainerServiceException e) {
00923 String err = "Error during the deployment of the wars file of the Ear file " + fileName;
00924 logger.log(BasicLevel.ERROR, err + "': " + e.getMessage());
00925
00926 webContainerService.removeCache(earClassLoader);
00927 err = "Undeploy the jars loaded from this ear";
00928 logger.log(BasicLevel.DEBUG, err);
00929 try {
00930 if (ejbService != null && (jarUrls.length > 0)) {
00931
00932 ejbService.removeCache(earClassLoader);
00933
00934 ejbService.unDeployJars(jarUrls);
00935 }
00936
00937 if (resourceService != null && connectorUrls.length > 0) {
00938
00939 resourceService.unDeployRars(connectorUrls, earUrl[0]);
00940 }
00941 } catch (ServiceException se) {
00942 err = "Error during the undeployment of the jars file of the Ear file " + fileName;
00943 logger.log(BasicLevel.ERROR, err + "': " + se.getMessage());
00944 throw new EarServiceException(err, e);
00945 }
00946
00947 throw new EarServiceException(err, e);
00948 }
00949 }
00950
00951
00952 linkPolicyObjects(earDD.getUserToRoleMapping(), jarUrls, warUrls);
00953
00954 logger.log(BasicLevel.DEBUG, "We store the rars/wars/jars associated to the url :" + earUrl[0]);
00955
00956
00957
00958 if (wsService != null) {
00959 try {
00960 CompNamingContext contctx = null;
00961 try {
00962 contctx = new CompNamingContext(earRootUrl.getFile());
00963 contctx.rebind(AbsWebServicesServiceImpl.CLASSLOADER_CTX_PARAM, earClassLoader);
00964 } catch (NamingException e) {
00965 String err = "Can not bind params for the WebServices service, "
00966 + "can't complete deployment of Web Services Endpoints";
00967 throw new JWebContainerServiceException(err, e);
00968 }
00969 wsService.completeWSDeployment(contctx);
00970 } catch (ServiceException se) {
00971 String err = "Error during the deployment of the WebServices of the Ear file '" + earRootUrl + "'";
00972 logger.log(BasicLevel.ERROR, err + " : " + se.getMessage());
00973 try {
00974 logger.log(BasicLevel.DEBUG, "Undeploy the WebApps loaded from this ear");
00975 if (webContainerService != null && (warUrls.length > 0)) {
00976
00977 webContainerService.removeCache(earClassLoader);
00978
00979 webContainerService.unDeployWars(warUrls);
00980 }
00981 logger.log(BasicLevel.DEBUG, "Undeploy the EjbJars loaded from this ear");
00982 if (ejbService != null && (jarUrls.length > 0)) {
00983
00984 ejbService.removeCache(earClassLoader);
00985
00986 ejbService.unDeployJars(jarUrls);
00987 }
00988
00989 logger.log(BasicLevel.DEBUG, "Undeploy the Resources loaded from this ear");
00990 if (resourceService != null && connectorUrls.length > 0) {
00991
00992 resourceService.unDeployRars(connectorUrls, earUrl[0]);
00993 }
00994 } catch (ServiceException se2) {
00995 err = "Error during the undeployment of the webapps/ejbjars/rars file of the Ear file " + fileName;
00996 logger.log(BasicLevel.ERROR, err + "': " + se2.getMessage());
00997 }
00998 throw new JWebContainerServiceException(err, se);
00999 }
01000 }
01001
01002
01003
01004
01005
01006
01007 String sJ2EEApplicationName = buildJ2eeApplicationName(earUrl[0]);
01008
01009
01010 AppClientModule[] appClientModules = null;
01011 ObjectName[] onAppClientModules = null;
01012 if (clientUrls.length > 0) {
01013 appClientModules = new AppClientModule[clientUrls.length];
01014 onAppClientModules = new ObjectName[clientUrls.length];
01015 }
01016
01017 String moduleName = null;
01018 String moduleFileName = null;
01019 String moduleDD = null;
01020 String jonasModuleDD = null;
01021 ClientContainerDeploymentDesc clientContainerDD = null;
01022 for (int i = 0; i < clientUrls.length; i++) {
01023 ClassLoader moduleCL = null;
01024 try {
01025 moduleCL = new ClientClassLoader(clientUrls[i], ejbClassLoader);
01026 } catch (IOException ioe) {
01027
01028
01029 moduleCL = ejbClassLoader;
01030
01031 }
01032 moduleName = EJBServiceImpl.buildEJBModuleName(clientUrls[i]);
01033 moduleFileName = clientUrls[i].getPath();
01034 try {
01035 clientContainerDD = ClientManagerWrapper.getDeploymentDesc(clientUrls[i], moduleCL, earClassLoader);
01036 moduleDD = clientContainerDD.getXmlContent();
01037 jonasModuleDD = clientContainerDD.getJOnASXmlContent();
01038 } catch (ClientContainerDeploymentDescException e) {
01039
01040 String err = "Cannot read the deployment descriptors ': " + clientUrls[i] + "'" + e.toString();
01041 logger.log(BasicLevel.ERROR, err);
01042 }
01043 onAppClientModules[i] = J2eeObjectName.getAppClientModule(getDomainName(), getJonasServerName(), sJ2EEApplicationName, moduleName);
01044 appClientModules[i] = new AppClientModule(onAppClientModules[i].toString()
01045 , moduleFileName
01046 , moduleDD
01047 , jonasModuleDD);
01048 }
01049 URL clientUrl = null;
01050 try {
01051 if (mbeanServer != null) {
01052 Registry oRegistry = JonasMBeanTools.getRegistry();
01053 ManagedBean oManaged = null;
01054 ModelMBean oMBean = null;
01055
01056 if (appClientModules != null) {
01057 oManaged = oRegistry.findManagedBean("AppClientModule");
01058 for (int i = 0; i < appClientModules.length; i++) {
01059 clientUrl = clientUrls[i];
01060 oMBean = oManaged.createMBean(appClientModules[i]);
01061 mbeanServer.registerMBean(oMBean, onAppClientModules[i]);
01062 }
01063 }
01064 }
01065 } catch (Exception eare) {
01066 logger.log(BasicLevel.ERROR, "Can not register the MBean for the client jar " + clientUrl + " :"
01067 + eare.getMessage());
01068 }
01069
01070
01071 if (ejbService != null) {
01072 ejbService.removeCache(earClassLoader);
01073 }
01074
01075 if (webContainerService != null) {
01076 webContainerService.removeCache(earClassLoader);
01077 }
01078
01079 if (wsService != null) {
01080 wsService.removeCache(earClassLoader);
01081 }
01082
01083
01084 ObjectName onEar = J2eeObjectName.J2EEApplication(getDomainName(), getJonasServerName(), sJ2EEApplicationName);
01085
01086
01087 Ear ear = new Ear(onEar.toString(), sJ2EEApplicationName, dirUnpackURL.getFile(), earUrl[0], earDD.getXmlContent(), jarUrls, warUrls,
01088 connectorUrls);
01089
01090 try {
01091 if (mbeanServer != null) {
01092 Registry oRegistry = JonasMBeanTools.getRegistry();
01093 ManagedBean oManaged = oRegistry.findManagedBean("J2EEApplication");
01094 ModelMBean oMBean = oManaged.createMBean(ear);
01095
01096 mbeanServer.registerMBean(oMBean, onEar);
01097 }
01098 } catch (Exception eare) {
01099 logger.log(BasicLevel.ERROR, "Can not register the MBean for the ear " + earUrl[0] + " :"
01100 + eare.getMessage());
01101 }
01102
01103
01104 ears.put(earUrl[0], ear);
01105
01106 logger.log(BasicLevel.INFO, "Ear " + earUrl[0] + " available.");
01107
01108
01109 File dirUnpackFile = new File(dirUnpackURL.getFile());
01110 dirUnpackFile.setLastModified((new Date()).getTime());
01111
01112
01113 return onEar.toString();
01114 }
01115
01124 public String deployEar(String fileName) throws Exception {
01125 return deployEarMBean(fileName);
01126 }
01127
01136 public String deployEarMBean(String fileName) throws Exception {
01137 Context ctx = null;
01138 String mbeanOn = null;
01139 try {
01140 ctx = new CompNamingContext(fileName);
01141 ctx.rebind("filename", fileName);
01142 } catch (NamingException e) {
01143 String err = "Error when deploying the ear file ";
01144 logger.log(BasicLevel.ERROR, err + e.getMessage());
01145 throw e;
01146 }
01147 try {
01148 mbeanOn = deployEar(ctx);
01149 } catch (ServiceException e) {
01150 System.out.println("in ServiceException: " + e.getMessage());
01151 throw new Exception("in ServiceException: " + e.getMessage(), e);
01152 }
01153 return mbeanOn;
01154 }
01155
01160 protected void doStart() throws ServiceException {
01161
01162
01163 URL earApps = null;
01164 try {
01165 earApps = new File(WORK_APPS_DIR + File.separator + getJonasServerName()).toURL();
01166 } catch (MalformedURLException mue) {
01167 throw new ServiceException("Error when trying to get the URL of the jonasroot/apps directory", mue);
01168 }
01169
01170 File fLog = new File(earApps.getFile() + File.separator + getJonasServerName() + ".log");
01171 if (!fLog.exists()) {
01172 try {
01173
01174 fLog.getParentFile().mkdirs();
01175 fLog.createNewFile();
01176 } catch (IOException e) {
01177 throw new ServiceException("cannot create the log file" + fLog, e);
01178 }
01179 }
01180
01181
01182 try {
01183 earDeployerLog = new DeployerLog(fLog);
01184 } catch (DeployerLogException e) {
01185 throw new ServiceException("Can not get an EarDeployerLog", e);
01186 }
01187
01188
01189 EarCleanTask earCleanTask = new EarCleanTask(earApps, earDeployerLog);
01190
01191
01192 workCleaner = WorkCleaner.getInstance();
01193
01194
01195 try {
01196 workCleaner.registerTask(earCleanTask);
01197 } catch (CleanerException ce) {
01198 throw new ServiceException("Cannot register the EAR clean task", ce);
01199 }
01200
01201
01202 workCleaner.executeTasks();
01203
01204
01205 Iterator it = earNames.iterator();
01206 while (it.hasNext()) {
01207 String fileName = (String) it.next();
01208 Context contctx = null;
01209 try {
01210 contctx = new CompNamingContext(fileName);
01211 contctx.rebind("filename", fileName);
01212 } catch (NamingException e) {
01213 throw new ServiceException("Cannot start the EarService", e);
01214 }
01215 deployEar(contctx);
01216 }
01217
01218 try {
01219
01220 if (mbeanServer != null) {
01221 mbeanServer.registerMBean(this, JonasObjectName.earService());
01222 }
01223 } catch (InstanceAlreadyExistsException iae) {
01224 throw new ServiceException("Cannot start the EarService Already Exists", iae);
01225 } catch (MBeanRegistrationException mbre) {
01226 throw new ServiceException("Cannot start the EarService (MBean registration error)", mbre);
01227 } catch (NotCompliantMBeanException ncmbe) {
01228 throw new ServiceException("Cannot start the EarService (MBean Not compliant error)", ncmbe);
01229 }
01230 }
01231
01239 public void unDeployEar(Context ctx) throws EarServiceException {
01240
01241
01242 URL earUrl = null;
01243 try {
01244 earUrl = (URL) ctx.lookup("filename");
01245 } catch (NamingException e) {
01246 throw new EarServiceException("Trying to remove the ear file but there is no filename specified", e);
01247 }
01248
01249
01250 Ear ear = (Ear) ears.get(earUrl);
01251
01252
01253 if (ear == null) {
01254 throw new EarServiceException("Trying to remove the ear file " + earUrl.getFile()
01255 + " but this file was not found in the loaded Ear files");
01256 }
01257
01258
01259 URL[] warsToUndeploy = ear.getWars();
01260 if (webContainerService != null && (warsToUndeploy.length > 0)) {
01261 webContainerService.unDeployWars(warsToUndeploy);
01262 }
01263
01264
01265 URL[] jarsToUndeploy = ear.getEjbJars();
01266 if (ejbService != null && (jarsToUndeploy.length > 0)) {
01267 ejbService.unDeployJars(jarsToUndeploy);
01268 }
01269
01270
01271 URL[] rarsToUndeploy = ear.getRars();
01272 if (resourceService != null && (rarsToUndeploy.length > 0)) {
01273 resourceService.unDeployRars(rarsToUndeploy, earUrl);
01274 }
01275
01276 File f = new File(earUrl.getFile());
01277
01278
01279
01280 ears.remove(earUrl);
01281
01282 if (mbeanServer != null) {
01283
01284
01285
01286
01287
01288
01289 try {
01290
01291 ObjectName onJ2EEApplication = ObjectName.getInstance(ear.getObjectName());
01292 String appName = onJ2EEApplication.getKeyProperty("name");
01293 String domainName = onJ2EEApplication.getDomain();
01294 String serverName = onJ2EEApplication.getKeyProperty("J2EEServer");
01295 mbeanServer.unregisterMBean(onJ2EEApplication);
01296
01297 ObjectName onAppClientModules = J2eeObjectName.getAppClientModules(domainName, serverName, appName);
01298 ArrayList onList = (ArrayList) J2eeObjectName.queryObjectNames(onAppClientModules);
01299 for (int i = 0; i < onList.size(); i++) {
01300 mbeanServer.unregisterMBean((ObjectName) onList.get(i));
01301 }
01302 } catch (Exception e) {
01303 logger.log(BasicLevel.ERROR, "Cannot remove the MBean for the ear " + earUrl.getFile() + " : "
01304 + e.getMessage());
01305 }
01306 }
01307
01308 logger.log(BasicLevel.INFO, "Ear " + f.getName() + " no longer available.");
01309
01310 }
01311
01319 public void unDeployEar(String fileName) throws Exception {
01320 unDeployEarMBean(fileName);
01321 }
01322
01329 public void unDeployEarMBean(String fileName) throws Exception {
01330
01331
01332
01333
01334
01335
01336
01337
01338 URL url = null;
01339 URL earUrl = null;
01340 boolean found = false;
01341 try {
01342 Enumeration e = ears.keys();
01343 url = new File(fileName).getCanonicalFile().toURL();
01344 while (e.hasMoreElements() && !found) {
01345 earUrl = (URL) e.nextElement();
01346 if (earUrl.equals(url)) {
01347
01348 found = true;
01349 }
01350 }
01351
01352 if (fileName.toLowerCase().endsWith(".ear") && !found) {
01353
01354
01355 String earFileName = APPS_DIR + File.separator + fileName;
01356 e = ears.keys();
01357 url = new File(earFileName).getCanonicalFile().toURL();
01358 while (e.hasMoreElements() && !found) {
01359 earUrl = (URL) e.nextElement();
01360 if (earUrl.equals(url)) {
01361
01362 found = true;
01363 }
01364 }
01365 }
01366 if (!found) {
01367 String err = "Cannot undeploy the ear '" + fileName + "', it is not deployed.";
01368 logger.log(BasicLevel.ERROR, err);
01369 throw new Exception(err);
01370 }
01371 } catch (MalformedURLException mue) {
01372 String err = "Error when trying to get the url from" + fileName;
01373 logger.log(BasicLevel.ERROR, err + mue.getMessage());
01374 throw new Exception(err, mue);
01375 } catch (IOException ioe) {
01376 String err = "Error when trying to get the canonical file from " + fileName;
01377 logger.log(BasicLevel.ERROR, err + ioe.getMessage());
01378 throw new Exception(err, ioe);
01379 }
01380
01381
01382 Context ctx = null;
01383 try {
01384 ctx = new CompNamingContext(fileName);
01385
01386 ctx.rebind("filename", url);
01387 } catch (NamingException e) {
01388 String err = "Error when binding parameters";
01389 logger.log(BasicLevel.ERROR, err + e.getMessage());
01390 throw new Exception(err, e);
01391 }
01392
01393
01394 try {
01395 unDeployEar(ctx);
01396 } catch (EarServiceException e) {
01397 throw new Exception(e);
01398 }
01399
01400 }
01401
01405 public Integer getCurrentNumberOfEars() {
01406 return new Integer(ears.size());
01407 }
01408
01417 public List getInstalledEars() throws Exception {
01418
01419 ArrayList al = JModule.getInstalledContainersInDir(APPS_DIR, JModule.EAR_EXTENSION, JModule.EAR_CHILD_DIR,
01420 JModule.EAR_CONFIRM_FILE);
01421
01422 Iterator it = autoloadDirectories.iterator();
01423 while (it.hasNext()) {
01424 String name = (String) it.next();
01425 al.addAll(JModule.getInstalledContainersInDir(name, JModule.EAR_EXTENSION, JModule.EAR_CHILD_DIR,
01426 JModule.EAR_CONFIRM_FILE));
01427 }
01428 return al;
01429 }
01430
01436 public Set getEarNames() {
01437 HashSet names = new HashSet();
01438 URL earUrl = null;
01439 for (Enumeration e = ears.keys(); e.hasMoreElements();) {
01440 earUrl = (URL) e.nextElement();
01441 names.add(earUrl.getFile());
01442 }
01443 return names;
01444 }
01445
01453 private void addEars(String dirPath) {
01454 boolean found = false;
01455
01456
01457
01458 File dir = new File(dirPath);
01459 found = dir.isDirectory();
01460
01461 if (!found) {
01462
01463 dir = new File(APPS_DIR + File.separator + dirPath);
01464 found = dir.isDirectory();
01465 }
01466
01467 if (found) {
01468 addEarsFrom(dir);
01469 } else {
01470 String err = "Warning: Cannot load dir: '" + dirPath + "' ";
01471 err += "is not a directory or directory doesn't exist";
01472 logger.log(BasicLevel.WARN, err);
01473 }
01474 }
01475
01481 private void addEarsFrom(File dir) throws EarServiceException {
01482 try {
01483 if (dir.isDirectory()) {
01484 File[] files = dir.listFiles();
01485 for (int i = 0; i < files.length; i++) {
01486 if (files[i].getPath().toLowerCase().endsWith(".ear")) {
01487 earNames.add(files[i].getCanonicalPath());
01488 } else {
01489 if (files[i].isDirectory()) {
01490 addEarsFrom(files[i]);
01491 }
01492 }
01493 }
01494 } else {
01495 String err = "Cannot load dir: '" + dir.getPath();
01496 err += "' is not a directory";
01497 logger.log(BasicLevel.ERROR, err);
01498 throw new EarServiceException(err);
01499 }
01500 } catch (IOException e) {
01501 String err = "Invalid file name '" + dir.getPath();
01502 logger.log(BasicLevel.ERROR, err);
01503 throw new EarServiceException(err, e);
01504 }
01505 }
01506
01513 public boolean isEarLoaded(String fileName) {
01514
01515 URL url = null;
01516 boolean isLoaded = false;
01517 try {
01518
01519 try {
01520 url = new File(fileName).getCanonicalFile().toURL();
01521
01522 if (ears.get(url) != null) {
01523 isLoaded = true;
01524 } else {
01525
01526 url = null;
01527 }
01528 } catch (Exception e) {
01529 url = null;
01530 }
01531
01532 if (url == null) {
01533 url = new File(APPS_DIR + File.separator + fileName).getCanonicalFile().toURL();
01534
01535 if (ears.get(url) != null) {
01536 isLoaded = true;
01537 }
01538 }
01539 } catch (Exception e) {
01540 String err = "Can not found if the ear is deployed or not";
01541 logger.log(BasicLevel.ERROR, err);
01542 return false;
01543 }
01544
01545 return isLoaded;
01546 }
01547
01554 public Boolean isEarDeployed(String fileName) {
01555 return new Boolean(isEarLoaded(fileName));
01556 }
01557
01564 public boolean isEarDeployedByUnpackName(String unpackName) {
01565 logger.log(BasicLevel.DEBUG, "entering for unpackName= " + unpackName);
01566
01567 Enumeration lc = ears.elements();
01568 while (lc.hasMoreElements()) {
01569 Ear ear = (Ear) lc.nextElement();
01570
01571
01572 String deployedUnpackName = new File(ear.getUnpackName()).getName();
01573 logger.log(BasicLevel.DEBUG, "deployedUnpackName=" + deployedUnpackName);
01574
01575 if (deployedUnpackName.equals(unpackName)) {
01576 return true;
01577 }
01578
01579 }
01580
01581 return false;
01582 }
01583
01584
01589 public List getDeployedEars() {
01590 ArrayList al = new ArrayList();
01591 Ear oEar;
01592 URL oURL;
01593 for (Enumeration enumEars = ears.elements(); enumEars.hasMoreElements();) {
01594 oEar = (Ear) enumEars.nextElement();
01595 oURL = oEar.getEarUrl();
01596 al.add(oURL.getFile());
01597 }
01598 return al;
01599 }
01600
01606 public List getDeployableEars() throws Exception {
01607 List al = getInstalledEars();
01608 al.removeAll(getDeployedEars());
01609 return al;
01610 }
01611
01616 public List getAutoloadDirectories() {
01617 ArrayList al = new ArrayList();
01618 Iterator it = autoloadDirectories.iterator();
01619 while (it.hasNext()) {
01620 String fileName = (String) it.next();
01621 try {
01622 al.add(new File(fileName).toURL().getPath());
01623 } catch (Exception e) {
01624 logger.log(BasicLevel.DEBUG, "Can't get autoload directories : " + e.getMessage());
01625 }
01626 }
01627 return al;
01628 }
01629
01634 public String getAppsDirectory() {
01635 String sRet = null;
01636 try {
01637 sRet = (new File(APPS_DIR)).toURL().getPath();
01638 } catch (Exception e) {
01639 throw new RuntimeException("Cannot get the APPS directory", e);
01640 }
01641 return sRet;
01642 }
01643
01649 public static String buildJ2eeApplicationName(URL pUrl) {
01650 String sName = null;
01651 try {
01652 sName = new File(pUrl.getFile()).getName();
01653 if ("file".equals(pUrl.getProtocol())) {
01654 sName = buildJ2eeApplicationName(sName);
01655 }
01656 } catch (NullPointerException e) {
01657 if (logger != null) {
01658 logger.log(BasicLevel.DEBUG, "Can't build j2ee application : " + e.getMessage());
01659 }
01660 }
01661 return sName;
01662 }
01663
01669 public static String buildJ2eeApplicationName(String pFilename) {
01670 String sName = null;
01671 try {
01672 sName = new File(pFilename).getName();
01673 int iPos = sName.lastIndexOf('.');
01674 if (iPos > -1) {
01675 sName = sName.substring(0, iPos);
01676 }
01677 } catch (NullPointerException e) {
01678 if (logger != null) {
01679 logger.log(BasicLevel.DEBUG, "Can't build j2ee application : " + e.getMessage());
01680 }
01681 }
01682 return sName;
01683 }
01684
01692 private void linkPolicyObjects(Map userToRoleMapping, URL[] jarUrls, URL[] warUrls) throws EarServiceException {
01693 String[] ctxIDs = new String[jarUrls.length + warUrls.length];
01694
01695 for (int u = 0; u < jarUrls.length; u++) {
01696 ctxIDs[u] = ejbService.getContainer(jarUrls[u].getFile()).getContextId();
01697 }
01698
01699 for (int u = 0; u < warUrls.length; u++) {
01700 ctxIDs[u + jarUrls.length] = webContainerService.getWar(warUrls[u]).getContextId();
01701 }
01702
01703 PolicyConfigurationFactory pcFactory = null;
01704 try {
01705 pcFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory();
01706 } catch (Exception cnfe) {
01707 String err = "Cannot retrieve current policy configuration factory";
01708 logger.log(BasicLevel.ERROR, err + "': " + cnfe.getMessage());
01709 throw new EarServiceException(err, cnfe);
01710 }
01711
01712 try {
01713
01714 for (int cId = 0; cId < ctxIDs.length; cId++) {
01715 String toBeLinkedCtxId = ctxIDs[cId];
01716 PolicyConfiguration toBeLinkedPC = pcFactory.getPolicyConfiguration(toBeLinkedCtxId, false);
01717 for (int linkCId = 0; linkCId < ctxIDs.length; linkCId++) {
01718 String linkedCtxId = ctxIDs[linkCId];
01719 if (!toBeLinkedCtxId.equals(linkedCtxId)) {
01720 PolicyConfiguration linkedPC = pcFactory.getPolicyConfiguration(linkedCtxId, false);
01721 toBeLinkedPC.linkConfiguration(linkedPC);
01722 }
01723 }
01724 }
01725 } catch (PolicyContextException pce) {
01726 String err = "Cannot retrieve a policy configuration";
01727 logger.log(BasicLevel.ERROR, err + "': " + pce.getMessage());
01728 throw new EarServiceException(err, pce);
01729 }
01730
01731
01732 if (userToRoleMapping != null) {
01733 for (int cId = 0; cId < ctxIDs.length; cId++) {
01734 for (Iterator itMapping = userToRoleMapping.keySet().iterator(); itMapping.hasNext();) {
01735 String principalName = (String) itMapping.next();
01736 List roles = (List) userToRoleMapping.get(principalName);
01737 String[] roleNames = (String[]) roles.toArray(new String[roles.size()]);
01738 JPolicyUserRoleMapping.addUserToRoleMapping(ctxIDs[cId], principalName, roleNames);
01739 }
01740 }
01741
01742 }
01743
01744
01745 String ctxId = null;
01746 try {
01747
01748 for (int cId = 0; cId < ctxIDs.length; cId++) {
01749 ctxId = ctxIDs[cId];
01750 PolicyConfiguration pc = pcFactory.getPolicyConfiguration(ctxId, false);
01751 pc.commit();
01752 }
01753 } catch (PolicyContextException pce) {
01754 String err = "Cannot commit policy configuration with Id '" + ctxId + "'";
01755 logger.log(BasicLevel.ERROR, err + "': " + pce.getMessage());
01756 throw new EarServiceException(err, pce);
01757 }
01758
01759
01760 Policy.getPolicy().refresh();
01761 }
01762
01763 }