Adm.java

00001 
00027 package org.objectweb.jonas.adm;
00028 
00029 
00030 import java.io.File;
00031 import java.io.FileOutputStream;
00032 import java.io.IOException;
00033 import java.net.MalformedURLException;
00034 import java.net.URL;
00035 import java.net.URLClassLoader;
00036 import java.rmi.RemoteException;
00037 import java.util.ArrayList;
00038 import java.util.Enumeration;
00039 import java.util.List;
00040 import java.util.Properties;
00041 import java.util.Vector;
00042 
00043 import javax.naming.InitialContext;
00044 import javax.naming.NameClassPair;
00045 import javax.naming.NamingEnumeration;
00046 import javax.naming.NamingException;
00047 import javax.rmi.PortableRemoteObject;
00048 
00049 import org.objectweb.common.Cmd;
00050 import org.objectweb.jonas.common.JProp;
00051 import org.objectweb.jonas.common.LogManagement;
00052 import org.objectweb.jonas.container.EJBServiceImpl;
00053 import org.objectweb.jonas.ear.EarServiceException;
00054 import org.objectweb.jonas.ear.EarServiceImplMBean;
00055 import org.objectweb.jonas.jtm.TransactionServiceImpl;
00056 import org.objectweb.jonas.mail.MailServiceImplMBean;
00057 import org.objectweb.jonas.naming.NamingManager;
00058 import org.objectweb.jonas.resource.ResourceServiceException;
00059 import org.objectweb.jonas.resource.ResourceServiceImplMBean;
00060 import org.objectweb.jonas.service.ServiceException;
00061 import org.objectweb.jonas.service.ServiceManager;
00062 import org.objectweb.jonas.web.AbsJWebContainerServiceImpl;
00063 import org.objectweb.jonas.web.AbsJWebContainerServiceImplMBean;
00064 import org.objectweb.jonas.web.JWebContainerServiceException;
00065 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
00066 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException;
00067 import org.objectweb.jonas_ejb.container.Container;
00068 import org.objectweb.jonas_ejb.genic.GenIC;
00069 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc;
00070 import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper;
00071 import org.objectweb.jonas_ear.deployment.lib.wrapper.EarManagerWrapper;
00072 
00082 public class Adm extends PortableRemoteObject implements AdmInterface {
00083 
00084     public static final String ADMNAME_SUFFIX = "_Adm";
00085 
00086     private ServiceManager sm = null;
00087     private EJBServiceImpl ejbserv  = null;
00088     private TransactionServiceImpl tm = null;
00089     private JProp jonasProperties = null;
00090     private InitialContext ictx = null;
00091     private boolean isEJBContainer;
00092 
00093     // Server State
00094     public static final int NOT_READY = 0;
00095     public static final int READY = 1;
00096     public static final int STOPPED = 2;
00097     private int serverState = NOT_READY;
00098 
00102     private AbsJWebContainerServiceImplMBean webContainerService = null;
00103 
00107     private EarServiceImplMBean earService = null;
00108 
00112     private ResourceServiceImplMBean resourceService = null;
00113 
00117     private MailServiceImplMBean mailService = null;
00118 
00123     public Adm(JProp jp) throws RemoteException, NamingException, ServiceException, Exception {
00124 
00125         jonasProperties = jp;
00126 
00127         // Get Service references
00128         sm = ServiceManager.getInstance();
00129 
00130         // Get an Initial Context and
00131         // registers the JOnAS Server Name to JNDI
00132         String name = null;
00133         name = jonasProperties.getValue("jonas.name", "jonas") + ADMNAME_SUFFIX;
00134         try {
00135             ictx = NamingManager.getInstance().getInitialContext();
00136             ictx.rebind(name, this);
00137         } catch (NamingException e) {
00138             throw e;
00139         }
00140     }
00141 
00142     private TransactionServiceImpl getTM() {
00143         if (tm == null) {
00144             try {
00145                 tm = (TransactionServiceImpl) sm.getTransactionService();
00146             } catch (ServiceException e) {
00147             }
00148         }
00149         return tm;
00150     }
00151 
00156     private AbsJWebContainerServiceImplMBean getWebContainerService() {
00157         if (webContainerService == null) {
00158             try {
00159                 webContainerService = (AbsJWebContainerServiceImplMBean) sm.getWebContainerService();
00160             } catch (ServiceException e) {
00161                 //not started
00162             }
00163         }
00164         return webContainerService;
00165     }
00166 
00171     private MailServiceImplMBean getMailService() {
00172         if (mailService == null) {
00173             try {
00174                 mailService = (MailServiceImplMBean) sm.getMailService();
00175             } catch (Exception e) {
00176                 //not started
00177             }
00178         }
00179         return mailService;
00180     }
00181 
00186     private EarServiceImplMBean getEarService() {
00187         if (earService == null) {
00188             try {
00189                 earService = (EarServiceImplMBean) sm.getEarService();
00190             } catch (Exception e) {
00191                 //not started
00192             }
00193         }
00194         return earService;
00195     }
00196 
00201     private ResourceServiceImplMBean getResourceService() {
00202         if (resourceService == null) {
00203             try {
00204                 resourceService = (ResourceServiceImplMBean) sm.getResourceService();
00205             } catch (Exception e) {
00206                 //not started
00207             }
00208         }
00209         return resourceService;
00210     }
00211 
00212     // ------------------------------------------------------------------
00213     //  AdmInterface implementation
00214     // ------------------------------------------------------------------
00215 
00220     public String [] getTopics() throws RemoteException {
00221         return LogManagement.getInstance().getTopics();
00222     }
00223 
00227     public String getTopicLevel(String topic) throws RemoteException {
00228         return LogManagement.getInstance().getTopicLevel(topic);
00229     }
00230 
00234     public void setTopicLevel(String topic, String l) throws RemoteException {
00235         LogManagement.getInstance().setTopicLevel(topic, l);
00236     }
00237 
00242     public void addBeans(String fileName) throws RemoteException {
00243         if (!isEJBContainer) {
00244             return;
00245         }
00246         try {
00247             ejbserv.createContainer(fileName);
00248         } catch (Exception e) {
00249             throw new RemoteException(e.toString());
00250         }
00251     }
00252 
00253 
00260     public void addEar(String fileName) throws RemoteException, EarServiceException {
00261         if (getEarService() != null) {
00262             try {
00263                 getEarService().deployEarMBean(fileName);
00264             } catch (EarServiceException ear) {
00265                 throw ear;
00266             } catch (Exception e) {
00267                 throw new RemoteException(e.toString());
00268             }
00269         } else {
00270             throw new EarServiceException("Can't add the Ear file " + fileName + ". The ear service is not started");
00271         }
00272     }
00273 
00280     public boolean isEarLoaded(String fileName) throws RemoteException, EarServiceException {
00281         boolean isLoaded = false;
00282         if (getEarService() != null) {
00283             isLoaded = getEarService().isEarLoaded(fileName);
00284         } else {
00285             throw new EarServiceException("Can't test if the ear file " + fileName + " is deployed or not. The ear service is not started");
00286         }
00287         return isLoaded;
00288     }
00289 
00296     public void addRar(String fileName) throws RemoteException, ResourceServiceException {
00297         if (getResourceService() != null) {
00298             getResourceService().deployRarMBean(fileName);
00299         } else {
00300             throw new ResourceServiceException("Can't add the Rar file " + fileName + ". The resource service is not started");
00301         }
00302     }
00303 
00311     public boolean isRarLoaded(String fileName) throws RemoteException, ResourceServiceException {
00312         boolean isLoaded = false;
00313         if (getResourceService() != null) {
00314             isLoaded = getResourceService().isRarLoaded(fileName);
00315         } else {
00316             throw new ResourceServiceException("Can't test if the rar file " + fileName + " is deployed or not. The rar service is not started");
00317         }
00318         return isLoaded;
00319     }
00320 
00327     public void addWar(String fileName) throws RemoteException, JWebContainerServiceException {
00328         if (getWebContainerService() != null) {
00329             getWebContainerService().registerWarMBean(fileName);
00330         } else {
00331             throw new JWebContainerServiceException("Can't add the war file " + fileName + ". The web container service is not started");
00332         }
00333     }
00334 
00341     public boolean isWarLoaded(String fileName) throws RemoteException, JWebContainerServiceException {
00342         boolean isLoaded = false;
00343         if (getWebContainerService() != null) {
00344             isLoaded = getWebContainerService().isWarLoaded(fileName);
00345         } else {
00346             throw new JWebContainerServiceException("Can't test if the war file " + fileName + " is deployed or not. The war service is not started");
00347         }
00348         return isLoaded;
00349     }
00350 
00357     public void removeEar(String fileName) throws RemoteException, EarServiceException {
00358         if (getEarService() != null) {
00359             try {
00360                 getEarService().unDeployEarMBean(fileName);
00361                 // needed here to run also the distributed garbage collector
00362                 runGC();
00363             } catch (EarServiceException ear) {
00364                 throw ear;
00365             } catch (Exception e) {
00366                 throw new RemoteException(e.toString());
00367             }
00368         } else {
00369             throw new EarServiceException("Can't remove of the Ear file " + fileName + ". The ear service is not started");
00370         }
00371     }
00372 
00379     public void removeRar(String fileName) throws RemoteException, ResourceServiceException {
00380         if (getResourceService() != null) {
00381             getResourceService().unDeployRarMBean(fileName);
00382             // needed here to run also the distributed garbage collector
00383             runGC();
00384         } else {
00385             throw new ResourceServiceException("Can't remove of the Rar file " + fileName + ". The resource service is not started");
00386         }
00387     }
00388 
00395     public void removeWar(String fileName) throws RemoteException, JWebContainerServiceException {
00396         if (getWebContainerService() != null) {
00397             getWebContainerService().unRegisterWarMBean(fileName);
00398             // needed here to run also the distributed garbage collector
00399             runGC();
00400         } else {
00401             throw new JWebContainerServiceException("Can't remove of the war file " + fileName + ". The web container service is not started");
00402         }
00403     }
00404 
00405 
00410     public void removeBeans(String fileName) throws RemoteException {
00411         if (!isEJBContainer) {
00412             return;
00413         }
00414         try {
00415             ejbserv.removeContainer(fileName);
00416         } catch (Exception e) {
00417             throw new RemoteException(e.getMessage());
00418         }
00419         // needed here to run also the distributed garbage collector
00420         runGC();
00421     }
00422 
00427     public boolean isLoaded(String fileName) throws RemoteException {
00428         return (ejbserv.getContainer(fileName) != null);
00429     }
00430 
00434     public String [] listBeans() throws RemoteException {
00435 
00436         if (!isEJBContainer) {
00437             return new String[0];
00438         }
00439 
00440         Container[] lcont = ejbserv.listContainers();
00441         Vector lbeans = new Vector();
00442         for (int j = 0; j < lcont.length; j++) {
00443             String[] lbn = lcont[j].listBeanNames();
00444             String contname = lcont[j].getName();
00445             for (int k = 0; k < lbn.length; k++) {
00446                 String s = new String(contname + ": " + lbn[k]);
00447                 lbeans.addElement(s);
00448             }
00449         }
00450         String[] ret = new String[lbeans.size()];
00451         lbeans.copyInto(ret);
00452 
00453         return ret;
00454     }
00455 
00459     public Vector listContext() throws RemoteException {
00460         String name = null;
00461         Vector ret  = new Vector();
00462         try {
00463             NamingEnumeration ne;
00464             ne = NamingManager.getInstance().getInitialContext().list("");
00465             for (Enumeration e = ne; e.hasMoreElements(); ) {
00466                 name = ((NameClassPair) e.nextElement()).getName();
00467                 ret.addElement(name);
00468             }
00469         } catch (Exception ex) {
00470             throw new RemoteException("Cannot list JNDI context", ex);
00471         }
00472         return ret;
00473     }
00474 
00478     public Properties listEnv() {
00479         return jonasProperties.getConfigFileEnv();
00480     }
00481 
00485     public void stopServer() throws RemoteException {
00486         try {
00487             serverState = STOPPED;
00488             // keep registry alive a little while (for jonas admin) in case of error at launch
00489             Thread.sleep(5000);
00490             sm.stopServices();
00491         } catch (ServiceException e) {
00492             throw new RemoteException("Cannot stop services: ", e);
00493         } catch (InterruptedException e) {
00494         }
00495     }
00496 
00497 
00501     public void killServer() throws RemoteException {
00502         stopServer();
00503         System.exit(0);
00504     }
00505 
00510     public int getServerState() throws RemoteException {
00511         return serverState;
00512     }
00513 
00517     public boolean isEJBContainer() throws RemoteException {
00518         return isEJBContainer;
00519     }
00520 
00524     public void setTransactionTimeout(int timeout) throws RemoteException {
00525         getTM().setTimeout(timeout);
00526     }
00527 
00531     public void runGC() throws RemoteException {
00532         Runtime.getRuntime().gc();
00533     }
00534 
00539     public void syncAllEntities(boolean passivate) throws RemoteException {
00540         if (!isEJBContainer) {
00541             return;
00542         }
00543         ejbserv.syncAllEntities(passivate);
00544     }
00545 
00546 
00547     // ------------------------------------------------------------------
00548     //  other public methods
00549     // ------------------------------------------------------------------
00550 
00554     public void serverReady(boolean isEJB) {
00555         serverState = READY;
00556         isEJBContainer = isEJB;
00557         if (isEJBContainer) {
00558             ejbserv = (EJBServiceImpl) sm.getEjbService();
00559         }
00560     }
00561 
00574     public String deployFile(int type, byte[] bfile, String filename) throws RemoteException , EarServiceException, JWebContainerServiceException {
00575         try {
00576             String directory = "";
00577 
00578             if (type == TYPE_EJB) {
00579                 directory = ejbserv.getEjbjarsDirectory();
00580             } else if (type == TYPE_EAR) {
00581                 directory = getEarService().getAppsDirectory();
00582             } else if (type == TYPE_WAR) {
00583                 directory = getWebContainerService().getWebappsDirectory();
00584             } else if (type == TYPE_RAR) {
00585                 directory = getResourceService().getRarsDirectory();
00586             } else if (type == TYPE_CAR) {
00587                 throw new UnsupportedOperationException("Not Supported yet");
00588             }
00589 
00590             File file = new File(directory + filename);
00591 
00592             FileOutputStream out = new FileOutputStream(file);
00593             out.write(bfile);
00594             out.close();
00595             if (type == TYPE_EJB) {
00596                 String args[] = new String[1];
00597                 args[0] = directory + filename;
00598                 GenIC.main(args);
00599             } else if (type == TYPE_EAR) {
00600                 unpackAndCompileEar(file);
00601             }
00602 
00603             return file.getAbsolutePath();
00604 
00605         } catch (Exception e) {
00606             e.printStackTrace();
00607             throw new RemoteException(e.toString(), e);
00608         }
00609     }
00610 
00611     private void unpackAndCompileEar(File file) throws EarServiceException, IOException, EarDeploymentDescException {
00612         URL earUrl[] = new URL[1];
00613 
00614         try {
00615             earUrl[0] = file.toURL();
00616         } catch (MalformedURLException e) {
00617             String err = "Invalid ear file name '" + file;
00618             throw new EarServiceException(err, e);
00619         }
00620 
00621         //Create classLoader
00622         //parent classloader is the current classloader
00623         ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
00624         URLClassLoader loaderCls = new URLClassLoader(earUrl, currentLoader);
00625 
00626         EarDeploymentDesc desc = EarManagerWrapper.getDeploymentDesc(earUrl[0].getFile(), loaderCls);
00627 
00628         String[] ejbTags = desc.getEjbTags();
00629         /*
00630          * Example of a jar command:
00631          *   jar -uf ../output/ejbjars/sb.jar -C /tmp/genic1547.tmp a/b/C1.class
00632          *                                    -C /tmp/genic1547.tmp a/b/C2.class
00633          *                                    .....
00634          */
00635         String javaHomeBin = System.getProperty("java.home", "");
00636         if (!("".equals(javaHomeBin))) {
00637             javaHomeBin = javaHomeBin + File.separator + ".."
00638                 + File.separator + "bin" + File.separator;
00639         }
00640 
00641         Cmd cmd = null;
00642         cmd = new Cmd(javaHomeBin + "jar");
00643         cmd.addArgument("-xf");
00644         cmd.addArgument(file.getAbsolutePath());
00645 
00646         for (int i = 0; i < ejbTags.length; i++) {
00647             cmd.addArgument(ejbTags[i]);
00648         }
00649 
00650         boolean exitCmd = cmd.run();
00651         // Analyse the result of the jar command
00652         if (!exitCmd) {
00653             throw new EarServiceException("Failed when extracting the the ejb jar "
00654                                           + "in the given jar file '" + file + "'.");
00655         }
00656 
00657         for (int i = 0; i < ejbTags.length; i++) {
00658             String args[] = new String[1];
00659             args[0] = ejbTags[i];
00660             GenIC.main(args);
00661         }
00662 
00663         cmd = new Cmd(javaHomeBin + "jar");
00664         cmd.addArgument("-uf");
00665         cmd.addArgument(file.getAbsolutePath());
00666 
00667         for (int i = 0; i < ejbTags.length; i++) {
00668             cmd.addArgument(ejbTags[i]);
00669         }
00670         exitCmd = cmd.run();
00671         // Analyse the result of the jar command
00672         if (!exitCmd) {
00673             throw new EarServiceException("Failed when extracting the the ejb jar "
00674                                           + "in the given jar file '" + file + "'.");
00675         }
00676 
00677         for (int i = 0; i < ejbTags.length; i++) {
00678             File del = new File(ejbTags[i]);
00679             del.delete();
00680         }
00681 
00682     }
00683 
00694     public List listModules(int type, int state) throws RemoteException {
00695         try {
00696 
00697             //Should look at refactoring this logic into the services instead.
00698             List modules = new ArrayList();
00699             if (type == TYPE_EJB) {
00700                 // Will need to do parent element in future
00701                 List jars = null;
00702                 if (state == STATUS_RUNNING || state == STATUS_ALL) {
00703                     jars = ejbserv.getDeployedJars();
00704                     for (int i = 0; i < jars.size(); i++) {
00705                         ModuleDesc desc = new ModuleDesc((String)jars.get(i), STATUS_RUNNING, null);
00706                         modules.add(desc);
00707                     }
00708                 } else if (state == STATUS_STOPPED || state == STATUS_ALL) {
00709                     jars = ejbserv.getDeployableJars();
00710                     for (int i = 0; i < jars.size(); i++) {
00711                         ModuleDesc desc = new ModuleDesc((String)jars.get(i), STATUS_STOPPED, null);
00712                         modules.add(desc);
00713                     }
00714                 }
00715             } else if (type == TYPE_EAR) {
00716                 // Will need to do sub elements in future
00717                 List ears = null;
00718                 if (state == STATUS_RUNNING || state == STATUS_ALL) {
00719                     ears = getEarService().getDeployedEars();
00720                     for (int i = 0; i < ears.size(); i++) {
00721                         ModuleDesc desc = new ModuleDesc((String)ears.get(i), STATUS_RUNNING, null);
00722                         modules.add(desc);
00723                     }
00724                 } else if (state == STATUS_STOPPED || state == STATUS_ALL) {
00725                     ears = getEarService().getDeployableEars();
00726                     for (int i = 0; i < ears.size(); i++) {
00727                         ModuleDesc desc = new ModuleDesc((String)ears.get(i), STATUS_STOPPED, null);
00728                         modules.add(desc);
00729                     }
00730                 }
00731             } else if (type == TYPE_WAR) {
00732                 // Will need to do parent element in future
00733                 List wars = null;
00734                 AbsJWebContainerServiceImpl webService = (AbsJWebContainerServiceImpl)getWebContainerService(); // Very evil.
00735                 if (state == STATUS_RUNNING || state == STATUS_ALL) {
00736                     wars = webService.getDeployedWars();
00737                     for (int i = 0; i < wars.size(); i++) {
00738                         // Need to get the Context root of the web app as well.
00739                         String warName = (String)wars.get(i);
00740                         ClassLoader cl = webService.getClassLoader(new File(warName).toURL(), null, null);
00741                         WebContainerDeploymentDesc wdesc = WebManagerWrapper.getDeploymentDesc(warName, cl);
00742                         int start = warName.lastIndexOf("/");
00743                         if (start < 0) {
00744                             start = 0;
00745                         }
00746                         String url = wdesc.getContextRoot() != null ? wdesc.getContextRoot(): warName.substring(start, warName.indexOf(".war"));
00747                         ModuleDesc desc = new ModuleDesc(warName, STATUS_RUNNING, url);
00748                         modules.add(desc);
00749                     }
00750                 } else if (state == STATUS_STOPPED || state == STATUS_ALL) {
00751                     wars = webService.getDeployableWars();
00752                     for (int i = 0; i < wars.size(); i++) {
00753                         // Need to get the Context root of the web app as well.
00754                         String warName = (String)wars.get(i);
00755                         ClassLoader cl = webService.getClassLoader(new File(warName).toURL(), null, null);
00756                         WebContainerDeploymentDesc wdesc = WebManagerWrapper.getDeploymentDesc(warName, cl);
00757                         int start = warName.lastIndexOf("/");
00758                         if (start < 0) {
00759                             start = 0;
00760                         }
00761 
00762                         String url = wdesc.getContextRoot() != null ? wdesc.getContextRoot(): warName.substring(start, warName.indexOf(".war"));
00763                         ModuleDesc desc = new ModuleDesc(warName, STATUS_STOPPED, url);
00764                         modules.add(desc);
00765                     }
00766                 }
00767             } else if (type == TYPE_RAR) {
00768                 // Will need to do parent element in future
00769                 List rars = getResourceService().getInstalledRars();
00770                 for (int i = 0; i < rars.size(); i++) {
00771                     ModuleDesc desc = new ModuleDesc((String)rars.get(i), STATUS_RUNNING, null);
00772                     modules.add(desc);
00773                 }
00774             } else if (type == TYPE_CAR) {
00775                 throw new UnsupportedOperationException("Not Supported yet");
00776             }
00777 
00778             return modules;
00779         } catch (Exception e) {
00780             //Ugly
00781             throw new RemoteException("Failed to list modules", e);
00782         }
00783 
00784 
00785     }
00786 
00794     public void undeployFile(String filename) throws RemoteException {
00795         try {
00796             File f = new File(filename);
00797             f.delete();
00798         } catch (Exception ioe) {
00799             throw new RemoteException("Failed to remove the module " + filename + ":" + ioe.getMessage(), ioe);
00800         }
00801 
00802     }
00803 
00804 
00805 }

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