0001: /**
0002: * JOnAS: Java(TM) Open Application Server
0003: * Copyright (C) 1999-2005 Bull S.A.
0004: * Contact: jonas-team@objectweb.org
0005: *
0006: * This library is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU Lesser General Public
0008: * License as published by the Free Software Foundation; either
0009: * version 2.1 of the License, or any later version.
0010: *
0011: * This library is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * Lesser General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU Lesser General Public
0017: * License along with this library; if not, write to the Free Software
0018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
0019: * USA
0020: *
0021: * --------------------------------------------------------------------------
0022: * $Id: Adm.java 9870 2006-11-27 08:42:49Z danesa $
0023: * --------------------------------------------------------------------------
0024: */package org.objectweb.jonas.adm;
0025:
0026: import java.io.File;
0027: import java.io.FileOutputStream;
0028: import java.io.IOException;
0029: import java.net.MalformedURLException;
0030: import java.net.URL;
0031: import java.net.URLClassLoader;
0032: import java.rmi.RemoteException;
0033: import java.util.ArrayList;
0034: import java.util.Collection;
0035: import java.util.Enumeration;
0036: import java.util.HashMap;
0037: import java.util.Iterator;
0038: import java.util.List;
0039: import java.util.Map;
0040: import java.util.Properties;
0041: import java.util.Vector;
0042:
0043: import javax.management.JMException;
0044: import javax.naming.InitialContext;
0045: import javax.naming.NameClassPair;
0046: import javax.naming.NamingEnumeration;
0047: import javax.naming.NamingException;
0048: import javax.rmi.PortableRemoteObject;
0049:
0050: import org.objectweb.common.Cmd;
0051: import org.objectweb.jonas.common.JProp;
0052: import org.objectweb.jonas.common.LogManagement;
0053: import org.objectweb.jonas.container.EJBServiceImpl;
0054: import org.objectweb.jonas.dbm.ConnectionManager;
0055: import org.objectweb.jonas.dbm.DataBaseServiceImpl;
0056: import org.objectweb.jonas.dbm.Pool;
0057: import org.objectweb.jonas.ear.EarServiceException;
0058: import org.objectweb.jonas.ear.EarServiceImplMBean;
0059: import org.objectweb.jonas.jtm.TransactionServiceImpl;
0060: import org.objectweb.jonas.mail.MailServiceImplMBean;
0061: import org.objectweb.jonas.management.cluster.BaseCluster;
0062: import org.objectweb.jonas.management.j2eemanagement.J2EEDomain;
0063: import org.objectweb.jonas.management.monitoring.ServerProxy;
0064: import org.objectweb.jonas.naming.NamingManager;
0065: import org.objectweb.jonas.resource.ResourceServiceException;
0066: import org.objectweb.jonas.resource.ResourceServiceImplMBean;
0067: import org.objectweb.jonas.server.Server;
0068: import org.objectweb.jonas.service.ServiceException;
0069: import org.objectweb.jonas.service.ServiceManager;
0070: import org.objectweb.jonas.web.AbsJWebContainerServiceImpl;
0071: import org.objectweb.jonas.web.AbsJWebContainerServiceImplMBean;
0072: import org.objectweb.jonas.web.JWebContainerServiceException;
0073: import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
0074: import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException;
0075: import org.objectweb.jonas_ear.deployment.lib.wrapper.EarManagerWrapper;
0076: import org.objectweb.jonas_ejb.container.Container;
0077: import org.objectweb.jonas_ejb.container.JFactory;
0078: import org.objectweb.jonas_ejb.genic.GenIC;
0079: import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc;
0080: import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper;
0081:
0082: /**
0083: * This class implements a remote interface used for administering the server.
0084: * @author Philippe Coq
0085: * @author Philippe Durieux
0086: * @author Marc Dutoo, JOnAS 2.4
0087: * @author Murad Meghani (Murad.Meghani@compuware.com) killServer and stopServer
0088: * @author Florent Benoit & Ludovic Bert : Methods for wars and ear files JOnAS
0089: * 2.5 2002.06
0090: * @author Dean Jennings : add deployFile
0091: */
0092: public class Adm extends PortableRemoteObject implements AdmInterface {
0093:
0094: public static final String ADMNAME_SUFFIX = "_Adm";
0095:
0096: private ServiceManager sm = null;
0097:
0098: private EJBServiceImpl ejbserv = null;
0099:
0100: private DataBaseServiceImpl dbmserv = null;
0101:
0102: private TransactionServiceImpl tm = null;
0103:
0104: private JProp jonasProperties = null;
0105:
0106: private InitialContext ictx = null;
0107:
0108: private boolean isEJBContainer;
0109:
0110: // Server State
0111: public static final int NOT_READY = 0;
0112:
0113: public static final int READY = 1;
0114:
0115: public static final int STOPPED = 2;
0116:
0117: private int serverState = NOT_READY;
0118:
0119: /**
0120: * Sleep before exit of JVM
0121: */
0122: public static final int SLEEP_DELAY = 2000;
0123:
0124: /**
0125: * Reference to the web container service
0126: */
0127: private AbsJWebContainerServiceImplMBean webContainerService = null;
0128:
0129: /**
0130: * Reference to the ear service
0131: */
0132: private EarServiceImplMBean earService = null;
0133:
0134: /**
0135: * Reference to the resource service
0136: */
0137: private ResourceServiceImplMBean resourceService = null;
0138:
0139: /**
0140: * Reference to the mail service
0141: */
0142: private MailServiceImplMBean mailService = null;
0143:
0144: /**
0145: * Reference on the J2EEDomain
0146: * Used to access other servers and clusters in the domain.
0147: */
0148: J2EEDomain mydomain = null;
0149:
0150: /**
0151: * Adm constructor
0152: * @throws RemoteException if problem
0153: */
0154: public Adm(JProp jp) throws RemoteException, NamingException,
0155: ServiceException, Exception {
0156:
0157: jonasProperties = jp;
0158:
0159: // Get Service references
0160: sm = ServiceManager.getInstance();
0161:
0162: // Get an Initial Context andorg.objectweb.jonas_ejb.container;
0163: // registers the JOnAS Server Name to JNDI
0164: String name = null;
0165: name = jonasProperties.getValue("jonas.name", "jonas")
0166: + ADMNAME_SUFFIX;
0167: ictx = NamingManager.getInstance().getInitialContext();
0168: ictx.rebind(name, this );
0169:
0170: // Keep a reference on the J2EEDomain
0171: mydomain = Server.getInstance().getJ2EEDomain();
0172: }
0173:
0174: private TransactionServiceImpl getTM() {
0175: if (tm == null) {
0176: try {
0177: tm = (TransactionServiceImpl) sm
0178: .getTransactionService();
0179: } catch (ServiceException e) {
0180: }
0181: }
0182: return tm;
0183: }
0184:
0185: private DataBaseServiceImpl getDBM() {
0186: if (dbmserv == null) {
0187: try {
0188: dbmserv = (DataBaseServiceImpl) sm.getDataBaseService();
0189: } catch (ServiceException e) {
0190: }
0191: }
0192: return dbmserv;
0193: }
0194:
0195: /**
0196: * Get the web container service We can use methods provided by the
0197: * interface for MBean
0198: */
0199: private AbsJWebContainerServiceImplMBean getWebContainerService() {
0200: if (webContainerService == null) {
0201: try {
0202: webContainerService = (AbsJWebContainerServiceImplMBean) sm
0203: .getWebContainerService();
0204: } catch (ServiceException e) {
0205: // not started
0206: }
0207: }
0208: return webContainerService;
0209: }
0210:
0211: /**
0212: * Get the ear service We can use methods provided by the interface for
0213: * MBean
0214: */
0215: private EarServiceImplMBean getEarService() {
0216: if (earService == null) {
0217: try {
0218: earService = (EarServiceImplMBean) sm.getEarService();
0219: } catch (Exception e) {
0220: // not started
0221: }
0222: }
0223: return earService;
0224: }
0225:
0226: /**
0227: * Get the resource service We can use methods provided by the interface for
0228: * MBean
0229: */
0230: private ResourceServiceImplMBean getResourceService() {
0231: if (resourceService == null) {
0232: try {
0233: resourceService = (ResourceServiceImplMBean) sm
0234: .getResourceService();
0235: } catch (Exception e) {
0236: // not started
0237: }
0238: }
0239: return resourceService;
0240: }
0241:
0242: // ------------------------------------------------------------------
0243: // AdmInterface implementation
0244: // ------------------------------------------------------------------
0245:
0246: /**
0247: * get Topics. Assumes that all Loggers are TopicalLoggers.
0248: */
0249: public String[] getTopics() throws RemoteException {
0250: return LogManagement.getInstance().getTopics();
0251: }
0252:
0253: /**
0254: * get Topic Level
0255: */
0256: public String getTopicLevel(String topic) throws RemoteException {
0257: return LogManagement.getInstance().getTopicLevel(topic);
0258: }
0259:
0260: /**
0261: * set Topic Level
0262: */
0263: public void setTopicLevel(String topic, String l)
0264: throws RemoteException {
0265: LogManagement.getInstance().setTopicLevel(topic, l);
0266: }
0267:
0268: /**
0269: * Create a container and load beans in it
0270: * @param fileName name of the ejb-jar or xml file
0271: */
0272: public void addBeans(String fileName) throws RemoteException {
0273: if (!isEJBContainer) {
0274: return;
0275: }
0276: try {
0277: ejbserv.createContainer(fileName);
0278: } catch (Exception e) {
0279: throw new RemoteException("Cannot add beans", e);
0280: }
0281: }
0282:
0283: /**
0284: * Deploy a given ear file with the help of the ear service.
0285: * @param fileName the name of the ear file.
0286: * @throws RemoteException if rmi call failed.
0287: * @throws EarServiceException if the deployment failed.
0288: */
0289: public void addEar(String fileName) throws RemoteException,
0290: EarServiceException {
0291: if (getEarService() != null) {
0292: try {
0293: getEarService().deployEarMBean(fileName);
0294: } catch (EarServiceException ear) {
0295: throw ear;
0296: } catch (Exception e) {
0297: throw new RemoteException("Unable to deploy the EAR '"
0298: + fileName + "'.", e);
0299: }
0300: } else {
0301: throw new EarServiceException("Can't add the Ear file "
0302: + fileName + ". The ear service is not started");
0303: }
0304: }
0305:
0306: /**
0307: * Test if the specified filename is already deployed or not
0308: * @param fileName the name of the ear file.
0309: * @return true if the ear is deployed, else false.
0310: * @throws RemoteException if rmi call failed.
0311: */
0312: public boolean isEarLoaded(String fileName) throws RemoteException,
0313: EarServiceException {
0314: boolean isLoaded = false;
0315: if (getEarService() != null) {
0316: isLoaded = getEarService().isEarLoaded(fileName);
0317: } else {
0318: throw new EarServiceException(
0319: "Can't test if the ear file "
0320: + fileName
0321: + " is deployed or not. The ear service is not started");
0322: }
0323: return isLoaded;
0324: }
0325:
0326: /**
0327: * Deploy a given rar file with the help of the resource service.
0328: * @param fileName the name of the rar file.
0329: * @throws RemoteException if rmi call failed.
0330: * @throws ResourceServiceException if the deployment failed.
0331: */
0332: public void addRar(String fileName) throws RemoteException,
0333: ResourceServiceException {
0334: if (getResourceService() != null) {
0335: getResourceService().deployRarMBean(fileName);
0336: } else {
0337: throw new ResourceServiceException(
0338: "Can't add the Rar file " + fileName
0339: + ". The resource service is not started");
0340: }
0341: }
0342:
0343: /**
0344: * Test if the specified filename is already deployed or not
0345: * @param fileName the name of the rar file.
0346: * @return true if the rar is deployed, else false.
0347: * @throws RemoteException if rmi call failed.
0348: * @throws ResourceServiceException if unable to get resource service
0349: */
0350: public boolean isRarLoaded(String fileName) throws RemoteException,
0351: ResourceServiceException {
0352: boolean isLoaded = false;
0353: if (getResourceService() != null) {
0354: isLoaded = getResourceService().isRarLoaded(fileName);
0355: } else {
0356: throw new ResourceServiceException(
0357: "Can't test if the rar file "
0358: + fileName
0359: + " is deployed or not. The rar service is not started");
0360: }
0361: return isLoaded;
0362: }
0363:
0364: /**
0365: * Deploy a given war file with the help of the web container service.
0366: * @param fileName the name of the war file.
0367: * @throws RemoteException if rmi call failed.
0368: * @throws JWebContainerServiceException if the deployment failed.
0369: */
0370: public void addWar(String fileName) throws RemoteException,
0371: JWebContainerServiceException {
0372: if (getWebContainerService() != null) {
0373: getWebContainerService().registerWarMBean(fileName);
0374: } else {
0375: throw new JWebContainerServiceException(
0376: "Can't add the war file "
0377: + fileName
0378: + ". The web container service is not started");
0379: }
0380: }
0381:
0382: /**
0383: * Test if the specified filename is already deployed or not
0384: * @param fileName the name of the war file.
0385: * @return true if the war is deployed, else false.
0386: * @throws RemoteException if rmi call failed.
0387: */
0388: public boolean isWarLoaded(String fileName) throws RemoteException,
0389: JWebContainerServiceException {
0390: boolean isLoaded = false;
0391: if (getWebContainerService() != null) {
0392: isLoaded = getWebContainerService().isWarLoaded(fileName);
0393: } else {
0394: throw new JWebContainerServiceException(
0395: "Can't test if the war file "
0396: + fileName
0397: + " is deployed or not. The war service is not started");
0398: }
0399: return isLoaded;
0400: }
0401:
0402: /**
0403: * UnDeploy a given ear file with the help of the ear service.
0404: * @param fileName the name of the ear file.
0405: * @throws RemoteException if rmi call failed.
0406: * @throws EarServiceException if the undeployment failed.
0407: */
0408: public void removeEar(String fileName) throws RemoteException,
0409: EarServiceException {
0410: if (getEarService() != null) {
0411: try {
0412: getEarService().unDeployEarMBean(fileName);
0413: // needed here to run also the distributed garbage collector
0414: runGC();
0415: } catch (EarServiceException ear) {
0416: throw ear;
0417: } catch (Exception e) {
0418: throw new RemoteException("Cannot remove ear", e);
0419: }
0420: } else {
0421: throw new EarServiceException(
0422: "Can't remove of the Ear file " + fileName
0423: + ". The ear service is not started");
0424: }
0425: }
0426:
0427: /**
0428: * UnDeploy a given rar file with the help of the resource service.
0429: * @param fileName the name of the rar file.
0430: * @throws RemoteException if rmi call failed.
0431: * @throws ResourceServiceException if the undeployment failed.
0432: */
0433: public void removeRar(String fileName) throws RemoteException,
0434: ResourceServiceException {
0435: if (getResourceService() != null) {
0436: getResourceService().unDeployRarMBean(fileName);
0437: // needed here to run also the distributed garbage collector
0438: runGC();
0439: } else {
0440: throw new ResourceServiceException(
0441: "Can't remove of the Rar file " + fileName
0442: + ". The resource service is not started");
0443: }
0444: }
0445:
0446: /**
0447: * UnDeploy a given war file with the help of the web container service.
0448: * @param fileName the name of the war file.
0449: * @throws RemoteException if rmi call failed.
0450: * @throws JWebContainerServiceException if the undeployment failed.
0451: */
0452: public void removeWar(String fileName) throws RemoteException,
0453: JWebContainerServiceException {
0454: if (getWebContainerService() != null) {
0455: getWebContainerService().unRegisterWarMBean(fileName);
0456: // needed here to run also the distributed garbage collector
0457: runGC();
0458: } else {
0459: throw new JWebContainerServiceException(
0460: "Can't remove of the war file "
0461: + fileName
0462: + ". The web container service is not started");
0463: }
0464: }
0465:
0466: /**
0467: * Remove the container identified by fileName and remove all beans in it
0468: * @param fileName name of the ejb-jar or xml file
0469: */
0470: public void removeBeans(String fileName) throws RemoteException {
0471: if (!isEJBContainer) {
0472: return;
0473: }
0474: try {
0475: ejbserv.removeContainer(fileName);
0476: } catch (Exception e) {
0477: throw new RemoteException("Cannot remove bean", e);
0478: }
0479: // needed here to run also the distributed garbage collector
0480: runGC();
0481: }
0482:
0483: /**
0484: * returns true if beans are already loaded in server.
0485: * @param fileName name of the ejb-jar or xml file
0486: */
0487: public boolean isLoaded(String fileName) throws RemoteException {
0488: return (ejbserv.getContainer(fileName) != null);
0489: }
0490:
0491: public String dumpCustom() throws RemoteException {
0492: String ret = "";
0493: if (!isEJBContainer) {
0494: return ret;
0495: }
0496: // JVM
0497: ret += Server.jvmInfos();
0498:
0499: // Transaction Service
0500: if (getTM() != null) {
0501: ret += "TM timeout=" + getTM().getTimeout();
0502: ret += "\n";
0503: }
0504:
0505: // datasources
0506: if (getDBM() != null) {
0507: Collection dslist = getDBM().getDSList();
0508: for (Iterator i = dslist.iterator(); i.hasNext();) {
0509: ConnectionManager cm = (ConnectionManager) i.next();
0510: ret += cm.getDSName() + ":lockPolicy="
0511: + cm.getTransactionIsolation();
0512: Pool pool = cm.getPool();
0513: ret += ":minPoolSize=" + pool.getPoolMin();
0514: ret += ":maxPoolSize=" + pool.getPoolMax();
0515: ret += ":maxOpenTime=" + pool.getMaxOpenTime();
0516: ret += ":maxWaitTime=" + pool.getMaxWaitTime();
0517: ret += ":maxWaiters=" + pool.getMaxWaiters();
0518: ret += ":pstmtMax=" + cm.getPstmtMax();
0519: ret += "\n";
0520: }
0521: }
0522:
0523: // EJB Containers
0524: Container[] lcont = (Container[]) ejbserv.listContainers();
0525: for (int j = 0; j < lcont.length; j++) {
0526: Container cont = lcont[j];
0527: String contname = cont.getName();
0528: String[] lbn = cont.listBeanNames();
0529: for (int k = 0; k < lbn.length; k++) {
0530: JFactory bf = (JFactory) cont.getBeanFactory(lbn[k]);
0531: ret += contname + ":=" + lbn[k];
0532: ret += ":minPoolSize=" + bf.getMinPoolSize();
0533: ret += ":maxCacheSize=" + bf.getMaxCacheSize();
0534: ret += "\n";
0535: }
0536: }
0537: return ret;
0538: }
0539:
0540: /**
0541: * List beans of all JOnAS containers
0542: */
0543: public String[] listBeans() throws RemoteException {
0544:
0545: if (!isEJBContainer) {
0546: return new String[0];
0547: }
0548:
0549: Container[] lcont = ejbserv.listContainers();
0550: Vector lbeans = new Vector();
0551: for (int j = 0; j < lcont.length; j++) {
0552: String[] lbn = lcont[j].listBeanNames();
0553: String contname = lcont[j].getName();
0554: for (int k = 0; k < lbn.length; k++) {
0555: String s = new String(contname + ": " + lbn[k]);
0556: lbeans.addElement(s);
0557: }
0558: }
0559: String[] ret = new String[lbeans.size()];
0560: lbeans.copyInto(ret);
0561:
0562: return ret;
0563: }
0564:
0565: /**
0566: * List JNDI context
0567: */
0568: public Vector listContext() throws RemoteException {
0569: String name = null;
0570: Vector ret = new Vector();
0571: try {
0572: NamingEnumeration ne;
0573: ne = NamingManager.getInstance().getInitialContext().list(
0574: "");
0575: for (Enumeration e = ne; e.hasMoreElements();) {
0576: name = ((NameClassPair) e.nextElement()).getName();
0577: ret.addElement(name);
0578: }
0579: } catch (Exception ex) {
0580: throw new RemoteException("Cannot list JNDI context", ex);
0581: }
0582: return ret;
0583: }
0584:
0585: /**
0586: * List Environment (configuration properties provided by the configuration
0587: * file).
0588: */
0589: public Properties listEnv() {
0590: return jonasProperties.getConfigFileEnv();
0591: }
0592:
0593: /**
0594: * Stop the Server without stopping the JVM
0595: */
0596: public void stopServer() throws RemoteException {
0597: try {
0598: serverState = STOPPED;
0599: // keep registry alive a little while (for jonas admin) in case of
0600: // error at launch
0601: Thread.sleep(5000);
0602: sm.stopServices();
0603: } catch (ServiceException e) {
0604: throw new RemoteException("Cannot stop services: ", e);
0605: } catch (InterruptedException e) {
0606: }
0607: }
0608:
0609: /**
0610: * Stop the Server and stop the JVM
0611: */
0612: public void killServer() throws RemoteException {
0613: // Stops the JOnAS's services
0614: stopServer();
0615:
0616: // Unregisters the JOnAS Server Name in the JNDI if the registry is still up (should
0617: // be the case whether it is shared.
0618: String name = null;
0619: try {
0620: name = jonasProperties.getValue("jonas.name", "jonas")
0621: + ADMNAME_SUFFIX;
0622: ictx = NamingManager.getInstance().getInitialContext();
0623: ictx.unbind(name);
0624: } catch (Exception e) {
0625: //e.printStackTrace();
0626: }
0627:
0628: // Delay the exit of the JVM so the link client/server is not broken.
0629: // client call will return before the end of the JVM on server side.
0630: new Thread(new Runnable() {
0631:
0632: public void run() {
0633: try {
0634: // Wait before exit
0635: Thread.sleep(SLEEP_DELAY);
0636: } catch (InterruptedException ie) {
0637: ie.printStackTrace();
0638: throw new IllegalStateException("Cannot wait: "
0639: + ie.getMessage());
0640: }
0641: System.exit(0);
0642: }
0643: }).start();
0644:
0645: }
0646:
0647: /**
0648: * To test if the server is ready
0649: * @return int 0=not ready, 1=ready, 2=stopped
0650: */
0651: public int getServerState() throws RemoteException {
0652: return serverState;
0653: }
0654:
0655: /**
0656: * To test if the server is an EJB container
0657: */
0658: public boolean isEJBContainer() throws RemoteException {
0659: return isEJBContainer;
0660: }
0661:
0662: /**
0663: * set the default value for transaction timeout
0664: */
0665: public void setTransactionTimeout(int timeout)
0666: throws RemoteException {
0667: getTM().setTimeout(timeout);
0668: }
0669:
0670: /**
0671: * run the garbage collector
0672: */
0673: public void runGC() throws RemoteException {
0674: Runtime.getRuntime().gc();
0675: }
0676:
0677: /**
0678: * sync all entity instances outside transactions
0679: * @param passivate passivate instances after synchronization.
0680: */
0681: public void syncAllEntities(boolean passivate)
0682: throws RemoteException {
0683: if (!isEJBContainer) {
0684: return;
0685: }
0686: ejbserv.syncAllEntities(passivate);
0687: }
0688:
0689: // ------------------------------------------------------------------
0690: // other public methods
0691: // ------------------------------------------------------------------
0692:
0693: /**
0694: * server is ready
0695: */
0696: public void serverReady(boolean isEJB) {
0697: serverState = READY;
0698: isEJBContainer = isEJB;
0699: if (isEJBContainer) {
0700: ejbserv = (EJBServiceImpl) sm.getEjbService();
0701: }
0702: }
0703:
0704: /**
0705: * Deploy file (GenIC), needed for Ishmael to work. The file is in the
0706: * directory specified by the property "jonas.service.deployment.directory"
0707: * @param type type of the file (EJB, WAR, EAR, RAR, CAR)
0708: * @param bfile
0709: * @param filename basename of the file to be deployed
0710: * @throws RemoteException
0711: * @throws EarServiceException
0712: * @throws ResourceServiceException
0713: * @throws JWebContainerServiceException
0714: * @author Dean Jennings. <da_jennings@junta.com.au>
0715: */
0716: public String deployFile(int type, byte[] bfile, String filename)
0717: throws RemoteException, EarServiceException,
0718: JWebContainerServiceException {
0719: try {
0720: String directory = "";
0721:
0722: if (type == TYPE_EJB) {
0723: directory = ejbserv.getEjbjarsDirectory();
0724: } else if (type == TYPE_EAR) {
0725: directory = getEarService().getAppsDirectory();
0726: } else if (type == TYPE_WAR) {
0727: directory = getWebContainerService()
0728: .getWebappsDirectory();
0729: } else if (type == TYPE_RAR) {
0730: directory = getResourceService().getRarsDirectory();
0731: } else if (type == TYPE_CAR) {
0732: throw new UnsupportedOperationException(
0733: "Not Supported yet");
0734: }
0735:
0736: File file = new File(directory + filename);
0737:
0738: FileOutputStream out = new FileOutputStream(file);
0739: out.write(bfile);
0740: out.close();
0741: if (type == TYPE_EJB) {
0742: String args[] = new String[1];
0743: args[0] = directory + filename;
0744: GenIC.main(args);
0745: } else if (type == TYPE_EAR) {
0746: unpackAndCompileEar(file);
0747: }
0748:
0749: return file.getAbsolutePath();
0750:
0751: } catch (Exception e) {
0752: e.printStackTrace();
0753: throw new RemoteException(e.toString(), e);
0754: }
0755: }
0756:
0757: private void unpackAndCompileEar(File file)
0758: throws EarServiceException, IOException,
0759: EarDeploymentDescException {
0760: URL earUrl[] = new URL[1];
0761:
0762: try {
0763: earUrl[0] = file.toURL();
0764: } catch (MalformedURLException e) {
0765: String err = "Invalid ear file name '" + file;
0766: throw new EarServiceException(err, e);
0767: }
0768:
0769: // Create classLoader
0770: // parent classloader is the current classloader
0771: ClassLoader currentLoader = Thread.currentThread()
0772: .getContextClassLoader();
0773: URLClassLoader loaderCls = new URLClassLoader(earUrl,
0774: currentLoader);
0775:
0776: EarDeploymentDesc desc = EarManagerWrapper.getDeploymentDesc(
0777: earUrl[0].getFile(), loaderCls);
0778:
0779: String[] ejbTags = desc.getEjbTags();
0780: /*
0781: * Example of a jar command: jar -uf ../output/ejbjars/sb.jar -C
0782: * /tmp/genic1547.tmp a/b/C1.class -C /tmp/genic1547.tmp a/b/C2.class
0783: * .....
0784: */
0785: String javaHomeBin = System.getProperty("java.home", "");
0786: if (!("".equals(javaHomeBin))) {
0787: javaHomeBin = javaHomeBin + File.separator + ".."
0788: + File.separator + "bin" + File.separator;
0789: }
0790:
0791: Cmd cmd = null;
0792: cmd = new Cmd(javaHomeBin + "jar");
0793: cmd.addArgument("-xf");
0794: cmd.addArgument(file.getAbsolutePath());
0795:
0796: for (int i = 0; i < ejbTags.length; i++) {
0797: cmd.addArgument(ejbTags[i]);
0798: }
0799:
0800: boolean exitCmd = cmd.run();
0801: // Analyse the result of the jar command
0802: if (!exitCmd) {
0803: throw new EarServiceException(
0804: "Failed when extracting the the ejb jar "
0805: + "in the given jar file '" + file + "'.");
0806: }
0807:
0808: for (int i = 0; i < ejbTags.length; i++) {
0809: String[] args = new String[1];
0810: args[0] = ejbTags[i];
0811: GenIC.main(args);
0812: }
0813:
0814: cmd = new Cmd(javaHomeBin + "jar");
0815: cmd.addArgument("-uf");
0816: cmd.addArgument(file.getAbsolutePath());
0817:
0818: for (int i = 0; i < ejbTags.length; i++) {
0819: cmd.addArgument(ejbTags[i]);
0820: }
0821: exitCmd = cmd.run();
0822: // Analyse the result of the jar command
0823: if (!exitCmd) {
0824: throw new EarServiceException(
0825: "Failed when extracting the the ejb jar "
0826: + "in the given jar file '" + file + "'.");
0827: }
0828:
0829: for (int i = 0; i < ejbTags.length; i++) {
0830: File del = new File(ejbTags[i]);
0831: del.delete();
0832: }
0833:
0834: }
0835:
0836: /**
0837: * List modules, needed for Ishmael to work. Currently only lists top level
0838: * modules. It does need to do sub elements of ears in the future.
0839: * @param type type of the file (EJB, WAR, EAR, RAR, CAR)
0840: * @param state status of the modules to be returned (RUNNING, STOPPED, ALL)
0841: * @throws RemoteException
0842: * @author Dean Jennings. <deanjennings@junta.com.au>
0843: */
0844:
0845: public List listModules(int type, int state) throws RemoteException {
0846: try {
0847:
0848: // Should look at refactoring this logic into the services instead.
0849: List modules = new ArrayList();
0850: if (type == TYPE_EJB) {
0851: // Will need to do parent element in future
0852: List jars = null;
0853: if (state == STATUS_RUNNING || state == STATUS_ALL) {
0854: jars = ejbserv.getDeployedJars();
0855: for (int i = 0; i < jars.size(); i++) {
0856: ModuleDesc desc = new ModuleDesc((String) jars
0857: .get(i), STATUS_RUNNING, null);
0858: modules.add(desc);
0859: }
0860: } else if (state == STATUS_STOPPED
0861: || state == STATUS_ALL) {
0862: jars = ejbserv.getDeployableJars();
0863: for (int i = 0; i < jars.size(); i++) {
0864: ModuleDesc desc = new ModuleDesc((String) jars
0865: .get(i), STATUS_STOPPED, null);
0866: modules.add(desc);
0867: }
0868: }
0869: } else if (type == TYPE_EAR) {
0870: // Will need to do sub elements in future
0871: List ears = null;
0872: if (state == STATUS_RUNNING || state == STATUS_ALL) {
0873: ears = getEarService().getDeployedEars();
0874: for (int i = 0; i < ears.size(); i++) {
0875: ModuleDesc desc = new ModuleDesc((String) ears
0876: .get(i), STATUS_RUNNING, null);
0877: modules.add(desc);
0878: }
0879: } else if (state == STATUS_STOPPED
0880: || state == STATUS_ALL) {
0881: ears = getEarService().getDeployableEars();
0882: for (int i = 0; i < ears.size(); i++) {
0883: ModuleDesc desc = new ModuleDesc((String) ears
0884: .get(i), STATUS_STOPPED, null);
0885: modules.add(desc);
0886: }
0887: }
0888: } else if (type == TYPE_WAR) {
0889: // Will need to do parent element in future
0890: List wars = null;
0891: AbsJWebContainerServiceImpl webService = (AbsJWebContainerServiceImpl) getWebContainerService(); // Very
0892: // evil.
0893: if (state == STATUS_RUNNING || state == STATUS_ALL) {
0894: wars = webService.getDeployedWars();
0895: for (int i = 0; i < wars.size(); i++) {
0896: // Need to get the Context root of the web app as well.
0897: String warName = (String) wars.get(i);
0898: ClassLoader cl = webService.getClassLoader(
0899: new File(warName).toURL(), null, null);
0900: WebContainerDeploymentDesc wdesc = WebManagerWrapper
0901: .getDeploymentDesc(warName, cl);
0902: int start = warName.lastIndexOf("/");
0903: if (start < 0) {
0904: start = 0;
0905: }
0906: String url = wdesc.getContextRoot() != null ? wdesc
0907: .getContextRoot()
0908: : warName.substring(start, warName
0909: .indexOf(".war"));
0910: ModuleDesc desc = new ModuleDesc(warName,
0911: STATUS_RUNNING, url);
0912: modules.add(desc);
0913: }
0914: } else if (state == STATUS_STOPPED
0915: || state == STATUS_ALL) {
0916: wars = webService.getDeployableWars();
0917: for (int i = 0; i < wars.size(); i++) {
0918: // Need to get the Context root of the web app as well.
0919: String warName = (String) wars.get(i);
0920: ClassLoader cl = webService.getClassLoader(
0921: new File(warName).toURL(), null, null);
0922: WebContainerDeploymentDesc wdesc = WebManagerWrapper
0923: .getDeploymentDesc(warName, cl);
0924: int start = warName.lastIndexOf("/");
0925: if (start < 0) {
0926: start = 0;
0927: }
0928:
0929: String url = wdesc.getContextRoot() != null ? wdesc
0930: .getContextRoot()
0931: : warName.substring(start, warName
0932: .indexOf(".war"));
0933: ModuleDesc desc = new ModuleDesc(warName,
0934: STATUS_STOPPED, url);
0935: modules.add(desc);
0936: }
0937: }
0938: } else if (type == TYPE_RAR) {
0939: // Will need to do parent element in future
0940: List rars = getResourceService().getInstalledRars();
0941: for (int i = 0; i < rars.size(); i++) {
0942: ModuleDesc desc = new ModuleDesc((String) rars
0943: .get(i), STATUS_RUNNING, null);
0944: modules.add(desc);
0945: }
0946: } else if (type == TYPE_CAR) {
0947: throw new UnsupportedOperationException(
0948: "Not Supported yet");
0949: }
0950:
0951: return modules;
0952: } catch (Exception e) {
0953: // Ugly
0954: throw new RemoteException("Failed to list modules", e);
0955: }
0956:
0957: }
0958:
0959: /**
0960: * Physically remove the module from the server
0961: * @param filename the filename of the module to be removed
0962: * @throws RemoteException
0963: * @author Dean Jennings. <deanjennings@junta.com.au>
0964: */
0965: public void undeployFile(String filename) throws RemoteException {
0966: try {
0967: File f = new File(filename);
0968: f.delete();
0969: } catch (Exception ioe) {
0970: throw new RemoteException("Failed to remove the module "
0971: + filename + ":", ioe);
0972: }
0973:
0974: }
0975:
0976: /**
0977: * Start remote servers or clusters
0978: * @param target List of clusters or servers to start
0979: * @throws RemoteException
0980: */
0981: public void startRemoteServers(String[] target)
0982: throws RemoteException {
0983: for (int i = 0; i < target.length; i++) {
0984: String mytarget = target[i];
0985: BaseCluster mycluster = mydomain.findCluster(mytarget);
0986: if (mycluster != null) {
0987: // this target is a cluster
0988: try {
0989: mycluster.startit();
0990: } catch (JMException e) {
0991: throw new RemoteException("Cannot start cluster: "
0992: + mytarget, e);
0993: }
0994: continue;
0995: }
0996: ServerProxy myserver = mydomain.findServerProxy(mytarget);
0997: if (myserver != null) {
0998: // this target is a server
0999: myserver.startit();
1000: continue;
1001: }
1002: throw new RemoteException("unknown target: " + mytarget);
1003: }
1004: }
1005:
1006: /**
1007: * Stop remote servers or clusters
1008: * @param target List of clusters or servers to stop
1009: * @throws RemoteException
1010: */
1011: public void stopRemoteServers(String[] target)
1012: throws RemoteException {
1013: for (int i = 0; i < target.length; i++) {
1014: String mytarget = target[i];
1015: BaseCluster mycluster = mydomain.findCluster(mytarget);
1016: if (mycluster != null) {
1017: // this target is a cluster
1018: try {
1019: mycluster.stopit();
1020: } catch (JMException e) {
1021: throw new RemoteException("Cannot stop cluster: "
1022: + mytarget, e);
1023: }
1024: continue;
1025: }
1026: ServerProxy myserver = mydomain.findServerProxy(mytarget);
1027: if (myserver != null) {
1028: // this target is a server
1029: myserver.stopit();
1030: continue;
1031: }
1032: throw new RemoteException("unknown target: " + mytarget);
1033: }
1034: }
1035:
1036: /**
1037: * Deploy file on another server or a cluster.
1038: * @param filename Name of the file
1039: * @param target List of clusters or servers where to deploy the file.
1040: * @throws RemoteException
1041: */
1042: public void deployFileOn(String filename, String[] target)
1043: throws RemoteException {
1044:
1045: for (int i = 0; i < target.length; i++) {
1046: String mytarget = target[i];
1047: BaseCluster mycluster = mydomain.findCluster(mytarget);
1048: if (mycluster != null) {
1049: // this target is a cluster
1050: mycluster.uploadDeployModule(filename, true);
1051: continue;
1052: }
1053: ServerProxy myserver = mydomain.findServerProxy(mytarget);
1054: if (myserver != null) {
1055: // this target is a server
1056: myserver.uploadDeployModule(filename, true);
1057: continue;
1058: }
1059: throw new RemoteException("unknown target: " + mytarget);
1060: }
1061: }
1062:
1063: }
|