EarServiceImpl.java

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         //we init the logger
00230         logger = Log.getLogger(Log.JONAS_EAR_PREFIX);
00231 
00232         // get apps ClassLoader
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         // Get the JMX Server via JMX Service
00252         try {
00253             mbeanServer = ((JmxService) sm.getJmxService()).getJmxServer();
00254         } catch (ServiceException e) {
00255             // the JMX service may not be started
00256             mbeanServer = null;
00257         }
00258 
00259         // Get the EJB service
00260         try {
00261             ejbService = (EJBService) sm.getEjbService();
00262         } catch (ServiceException e) {
00263             //not started
00264             ejbService = null;
00265         }
00266 
00267         // Get the Web Container service
00268         try {
00269             webContainerService = (JWebContainerService) sm.getWebContainerService();
00270         } catch (ServiceException e) {
00271             //not started
00272             webContainerService = null;
00273         }
00274 
00275         // Get the WebServices service
00276         try {
00277             wsService = (WebServicesService) sm.getWebServicesService();
00278         } catch (ServiceException e) {
00279             //not started
00280             wsService = null;
00281         }
00282 
00283         // Get the Resource service
00284         try {
00285             resourceService = (ResourceService) sm.getResourceService();
00286         } catch (ServiceException e) {
00287             //not started
00288             resourceService = null;
00289         }
00290 
00291         // Set the XML parsing mode to no validation
00292         String parsingMode = "false";
00293         try {
00294             parsingMode = (String) ctx.lookup(PARSINGWITHVALIDATION);
00295         } catch (NamingException e) {
00296             /*
00297              * No problem if there is no value for 'parsingwithvalidation'
00298              * (false by default)
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         // Init the ear names to be loaded when starting
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             // No problem if there is no value for 'descriptors'
00316             //(no ear to load)
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         // Add the ears of the jonas.service.ear.autoloaddir property
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             // No problem if there is no value for 'autoloaddir'
00334             //(no ear to load)
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         // Build autoload directories
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         //create hashtable of the ears ( Url of an ear ---> Ear structure)
00364         ears = new Hashtable();
00365 
00366         // Init the rar deployment desc manager.
00367         //rarDDManager = RarDeploymentDescManager.getInstance();
00368     }
00369 
00374     protected void doStop() throws ServiceException {
00375 
00376         //undeploy the loaded ears files
00377         URL earFileName = null;
00378         //For each ear file, we delegate the operation to the
00379         //unDeployEar method
00380         for (Enumeration earEntries = ears.keys(); earEntries.hasMoreElements();) {
00381             earFileName = (URL) earEntries.nextElement();
00382             //Try to undeploy the ear
00383             try {
00384                 Context ctx = new CompNamingContext(earFileName.getFile());
00385                 ctx.rebind("filename", earFileName);
00386                 unDeployEar(ctx);
00387             } catch (Exception e) {
00388                 //We don't stop the process of undeploying the ears.
00389                 //We only display an error in the logger.
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                 // unregister EarService MBean : EarServiceImplMBean
00398                 mbeanServer.unregisterMBean(JonasObjectName.earService());
00399             } catch (Exception e) {
00400                 logger.log(BasicLevel.ERROR, "Cannot stop the EarService: " + e.getMessage());
00401             }
00402         }
00403 
00404         //The service is stopped
00405         logger.log(BasicLevel.DEBUG, "EarService stopped");
00406 
00407     }
00408 
00418     public String deployEar(Context ctx) throws EarServiceException {
00419 
00420         //We have one of the ejb or web services launched
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         //We get the fileName
00427         // Get the file name and check if file exists
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             //We search the file
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                     // In case of the name is a ear file name, check also in
00445                     // the JONAS_BASE/apps directory
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         //Make the url for the specified fileName and the earRootUrl
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         //Check if the ear is already deployed or not
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         //Create classLoader
00487         //parent classloader is the current classloader
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         //We get the tags from the Deployment descriptor
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         //Check if all modules are inside the EAR file
00512         //no relatives mode like ../../file1.jar
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         //Changing array into JarList
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         //Unpack the ear file and get the unpacked dir
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         //the file is unpacked. Log it only if the ear is a file and not a
00583         // directory
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         //We have successfully unpacked the ear file, now we analyze manifest
00595         // Class-path:
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         //Get the urls of the ear class path manager
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          * We have the urls to load at our level We can now create our
00618          * classLoader
00619          */
00620         logger.log(BasicLevel.DEBUG, "Creating the EAR classLoader");
00621         JClassLoader earClassLoader = new JClassLoader(earUrl[0].toExternalForm() , new URL[0], appsClassLoader);
00622 
00623         //Extract the urls of the jarList
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         //Extract context roots for wars
00640         //They are in webTags. Dump into warsContextRoots
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         //Fill Alt-DD for Ejbs and Wars and Rars
00651         String altdd = null;
00652         File fAltDD = null;
00653 
00654         //Transorm the array altDDWebs into an array with the absolute URL to
00655         // the file
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         //Extract roles name from the securityRole array
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         // Set the list of the ejb-jar, war and clients that can do an ejb-link in this
00762         // ear application. This array can be empty.
00763         // The alternate urls are given
00764         EjbManagerWrapper.setAvailableEjbJarsAndAltDDs(earClassLoader, jarUrls, ejbsAltDDs);
00765         WebManagerWrapper.setAltDD(earClassLoader, warUrls, warsAltDDs);
00766         ClientManagerWrapper.setAltDD(earClassLoader, clientUrls, clientAltDDs);
00767 
00768         // Construct the ejb classloader for all the ejb-jars of the same
00769         // ear application. Because there is one ejb classloader for all
00770         // the ejb-jars of the same ear application.
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         // Set the list of the rars that can be referenced in this
00784         // ear application. This array can be empty.
00785         // The alternate urls are given
00786 
00787         //rarDDManager.setAvailableRarsAndAltDDs(earClassLoader, connectorUrls,
00788         // connectorsAltDDs);
00789 
00790         //Only if the connectorUrls is not empty
00791         //And if the service is started
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                 //remove DDesc
00810                 //resourceService.removeCache(earClassLoader);
00811                 throw new EarServiceException(err, e);
00812             }
00813         }
00814 
00815         // Only if the WebServicesService is started
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                         // remove Resource
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         // ejbClassLoader created above
00857 
00858         //Only if the jarUrls is not empty
00859         //And if the service is started
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                 //remove DDesc
00880                 ejbService.removeCache(earClassLoader);
00881                 // Remove the deployed rars
00882                 if (resourceService != null && connectorUrls.length > 0) {
00883                     // remove Resource
00884                     resourceService.unDeployRars(connectorUrls, earUrl[0]);
00885                 }
00886                 throw new EarServiceException(err, e);
00887             }
00888         }
00889 
00890         /*
00891          * We have deploy the ejb-jar files if the service is active And we have
00892          * the ejbClassLoader
00893          */
00894 
00895         /*
00896          * We're going to deploy wars Parent ClassLoader for web container is
00897          * ejbclassloader if ejbService is active, and a classloader with the
00898          * visibility of jar file if ejbservice is not active.
00899          */
00900 
00901         ClassLoader parentWarClassLoader = ejbClassLoader;
00902 
00903         //Only if the warUrls is not empty
00904         //And if the service is started
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                 //remove DDesc
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                         //remove DDesc
00932                         ejbService.removeCache(earClassLoader);
00933                         //remove the ejbs
00934                         ejbService.unDeployJars(jarUrls);
00935                     }
00936                     // Remove the deployed rars
00937                     if (resourceService != null && connectorUrls.length > 0) {
00938                         // remove Resource
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         // Link the JACC policy configuration objects of EJB and WebApp together and commit them to be available.
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         // Complete the Deployment of the WebServices
00957         // Only if the WebServicesService is started
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                         //remove DDesc
00977                         webContainerService.removeCache(earClassLoader);
00978                         //remove the webapps
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                         //remove DDesc
00984                         ejbService.removeCache(earClassLoader);
00985                         //remove the ejbs
00986                         ejbService.unDeployJars(jarUrls);
00987                     }
00988                     // Remove the deployed rars
00989                     logger.log(BasicLevel.DEBUG, "Undeploy the Resources loaded from this ear");
00990                     if (resourceService != null && connectorUrls.length > 0) {
00991                         // remove Resource
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         // Management support
01004         // ------------------
01005 
01006         // Build the 'name' used to construct the J2EEApplication ObjectName
01007         String sJ2EEApplicationName = buildJ2eeApplicationName(earUrl[0]);
01008 
01009         // Create AppClientModule MBeans first
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                 // fail case :
01028                 // we use the ejbjar classloader, but the descriptor loading will fail
01029                 moduleCL = ejbClassLoader;
01030                 // TODO Find a better Exception handling ?
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                 // make debug trace
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                 // Register AppClientModule MBeans
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         // Remove the Deployment descriptors
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         // Create J2EEApplication MBeans
01084         ObjectName onEar = J2eeObjectName.J2EEApplication(getDomainName(), getJonasServerName(), sJ2EEApplicationName);
01085         // Store the ejb-jar, war and the rar urls associated with the ear URL
01086         // in case of success deploy
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                 // Register Ear MBean (all the J2EEModule MBeans are already registered
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         //set the timestamp dir
01109         File dirUnpackFile = new File(dirUnpackURL.getFile());
01110         dirUnpackFile.setLastModified((new Date()).getTime());
01111 
01112         // return ObjectName of the J2EEApplication MBean
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         //url
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                 //create
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         //get the logger
01182         try {
01183             earDeployerLog = new DeployerLog(fLog);
01184         } catch (DeployerLogException e) {
01185             throw new ServiceException("Can not get an EarDeployerLog", e);
01186         }
01187 
01188         // create the ear deployment work file control task
01189         EarCleanTask earCleanTask = new EarCleanTask(earApps, earDeployerLog);
01190 
01191         //get the cleaner ref
01192         workCleaner = WorkCleaner.getInstance();
01193 
01194         // add the ear deployment work file control task
01195         try {
01196             workCleaner.registerTask(earCleanTask);
01197         } catch (CleanerException ce) {
01198             throw new ServiceException("Cannot register the EAR clean task", ce);
01199         }
01200 
01201         // force the cleaning now
01202         workCleaner.executeTasks();
01203 
01204         // Deploy all ear which are in descriptors section
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             // Register EarService MBean : EarServiceImplMBean
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         //Get the ear url param which we want to undeploy
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         //This ear is in loaded EARs files ?
01250         Ear ear = (Ear) ears.get(earUrl);
01251 
01252         //Not loaded
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         //undeploy wars if the web container service is started
01259         URL[] warsToUndeploy = ear.getWars();
01260         if (webContainerService != null && (warsToUndeploy.length > 0)) {
01261             webContainerService.unDeployWars(warsToUndeploy);
01262         }
01263 
01264         //undeploy jars if the ejb service is started
01265         URL[] jarsToUndeploy = ear.getEjbJars();
01266         if (ejbService != null && (jarsToUndeploy.length > 0)) {
01267             ejbService.unDeployJars(jarsToUndeploy);
01268         }
01269 
01270         //undeploy rars if the resource service is started
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         //Undeploy successful, we remove the ear
01278 
01279         //Only if there is no Exception !
01280         ears.remove(earUrl);
01281         //unregister mbean
01282         if (mbeanServer != null) {
01283             // Build the ObjectName if the ear's MBea
01284             //String sJ2EEApplicationName = buildJ2eeApplicationName(earUrl);
01285             //ObjectName onEar =
01286             // J2eeObjectName.J2EEApplication(getDomainName(),
01287             // getJonasServerName(), sJ2EEApplicationName);
01288             // Un-regsiter the MBean
01289             try {
01290                 // unregister J2EEApplication
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                 // ubregister the AppClientModules, if any
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          * We have only the name of the file, not its associated path, so we
01333          * look in the current directory and in the ear applications directory
01334          */
01335 
01336         //We check if the url exists in the hashtable
01337         //compare: First in the current dir, after in the apps directory
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                     //We have found
01348                     found = true;
01349                 }
01350             }
01351 
01352             if (fileName.toLowerCase().endsWith(".ear") && !found) {
01353                 // In case of the name is an ear check also in
01354                 // the JONAS_BASE/apps directory
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                         //We have found
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         //We've got the file, now we bind the params
01382         Context ctx = null;
01383         try {
01384             ctx = new CompNamingContext(fileName);
01385             //The fileName is an url
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         //Call the function
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         // get EAR files found in JONAS_BASE/apps
01419         ArrayList al = JModule.getInstalledContainersInDir(APPS_DIR, JModule.EAR_EXTENSION, JModule.EAR_CHILD_DIR,
01420                 JModule.EAR_CONFIRM_FILE);
01421         // get EAR files found in all autoload directories
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         // Look the directory relative to where the JOnAS server is launch or
01457         // if the path is absolute.
01458         File dir = new File(dirPath);
01459         found = dir.isDirectory();
01460 
01461         if (!found) {
01462             // Look the directory relative to the $JONAS_BASE/apps directory
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             // Absolute filename
01519             try {
01520                 url = new File(fileName).getCanonicalFile().toURL();
01521                 //Check if the ear is already deployed or not
01522                 if (ears.get(url) != null) {
01523                     isLoaded = true;
01524                 } else {
01525                     // Not found force to test in relative Apps directory
01526                     url = null;
01527                 }
01528             } catch (Exception e) {
01529                 url = null;
01530             }
01531             // Relative filename
01532             if (url == null) {
01533                 url = new File(APPS_DIR + File.separator + fileName).getCanonicalFile().toURL();
01534                 //Check if the ear is already deployed or not
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         // for each ear loaded
01567         Enumeration lc = ears.elements();
01568         while (lc.hasMoreElements()) {
01569             Ear ear = (Ear) lc.nextElement();
01570 
01571             // get unpack name of the ear
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             // else, go to the next loop
01579         }
01580         // not found
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         // Get contextID of EJB
01695         for (int u = 0; u < jarUrls.length; u++) {
01696             ctxIDs[u] = ejbService.getContainer(jarUrls[u].getFile()).getContextId();
01697         }
01698         // Now for WebApp
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             // Now link the policy configuration objects
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         // Do user-to-role mapping
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             // And to finish, commit the policy configuration objects
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         // refresh policy
01760         Policy.getPolicy().refresh();
01761     }
01762 
01763 }

Generated on Tue Feb 15 15:05:15 2005 for JOnAS by  doxygen 1.3.9.1