KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > adm > Adm


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Adm.java,v 1.57 2005/07/28 07:52:27 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.adm;
27
28 import java.io.File JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.net.MalformedURLException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.net.URLClassLoader JavaDoc;
34 import java.rmi.RemoteException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Properties JavaDoc;
41 import java.util.Vector JavaDoc;
42
43 import javax.naming.InitialContext JavaDoc;
44 import javax.naming.NameClassPair JavaDoc;
45 import javax.naming.NamingEnumeration JavaDoc;
46 import javax.naming.NamingException JavaDoc;
47 import javax.rmi.PortableRemoteObject JavaDoc;
48
49 import org.objectweb.common.Cmd;
50 import org.objectweb.jonas.common.JProp;
51 import org.objectweb.jonas.common.LogManagement;
52 import org.objectweb.jonas.container.EJBServiceImpl;
53 import org.objectweb.jonas.dbm.ConnectionManager;
54 import org.objectweb.jonas.dbm.DataBaseServiceImpl;
55 import org.objectweb.jonas.dbm.Pool;
56 import org.objectweb.jonas.ear.EarServiceException;
57 import org.objectweb.jonas.ear.EarServiceImplMBean;
58 import org.objectweb.jonas.jtm.TransactionServiceImpl;
59 import org.objectweb.jonas.mail.MailServiceImplMBean;
60 import org.objectweb.jonas.naming.NamingManager;
61 import org.objectweb.jonas.resource.ResourceServiceException;
62 import org.objectweb.jonas.resource.ResourceServiceImplMBean;
63 import org.objectweb.jonas.server.Server;
64 import org.objectweb.jonas.service.ServiceException;
65 import org.objectweb.jonas.service.ServiceManager;
66 import org.objectweb.jonas.web.AbsJWebContainerServiceImpl;
67 import org.objectweb.jonas.web.AbsJWebContainerServiceImplMBean;
68 import org.objectweb.jonas.web.JWebContainerServiceException;
69 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
70 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException;
71 import org.objectweb.jonas_ear.deployment.lib.wrapper.EarManagerWrapper;
72 import org.objectweb.jonas_ejb.container.Container;
73 import org.objectweb.jonas_ejb.container.JFactory;
74 import org.objectweb.jonas_ejb.genic.GenIC;
75 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc;
76 import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper;
77
78 /**
79  * This class implements a remote interface used for administering the server.
80  * @author Philippe Coq
81  * @author Philippe Durieux
82  * @author Marc Dutoo, JOnAS 2.4
83  * @author Murad Meghani (Murad.Meghani@compuware.com) killServer and stopServer
84  * @author Florent Benoit & Ludovic Bert : Methods for wars and ear files JOnAS
85  * 2.5 2002.06
86  * @author Dean Jennings : add deployFile
87  */

88 public class Adm extends PortableRemoteObject JavaDoc implements AdmInterface {
89
90     public static final String JavaDoc ADMNAME_SUFFIX = "_Adm";
91
92     private ServiceManager sm = null;
93
94     private EJBServiceImpl ejbserv = null;
95
96     private DataBaseServiceImpl dbmserv = null;
97
98     private TransactionServiceImpl tm = null;
99
100     private JProp jonasProperties = null;
101
102     private InitialContext JavaDoc ictx = null;
103
104     private boolean isEJBContainer;
105
106     // Server State
107
public static final int NOT_READY = 0;
108
109     public static final int READY = 1;
110
111     public static final int STOPPED = 2;
112
113     private int serverState = NOT_READY;
114
115     /**
116      * Sleep before exit of JVM
117      */

118     public static final int SLEEP_DELAY = 2000;
119
120     /**
121      * Reference to the web container service
122      */

123     private AbsJWebContainerServiceImplMBean webContainerService = null;
124
125     /**
126      * Reference to the ear service
127      */

128     private EarServiceImplMBean earService = null;
129
130     /**
131      * Reference to the resource service
132      */

133     private ResourceServiceImplMBean resourceService = null;
134
135     /**
136      * Reference to the mail service
137      */

138     private MailServiceImplMBean mailService = null;
139
140     /**
141      * Adm constructor
142      * @throws RemoteException if problem
143      */

144     public Adm(JProp jp) throws RemoteException JavaDoc, NamingException JavaDoc, ServiceException, Exception JavaDoc {
145
146         jonasProperties = jp;
147
148         // Get Service references
149
sm = ServiceManager.getInstance();
150
151         // Get an Initial Context andorg.objectweb.jonas_ejb.container;
152
// registers the JOnAS Server Name to JNDI
153
String JavaDoc name = null;
154         name = jonasProperties.getValue("jonas.name", "jonas") + ADMNAME_SUFFIX;
155         ictx = NamingManager.getInstance().getInitialContext();
156         ictx.rebind(name, this);
157     }
158
159     private TransactionServiceImpl getTM() {
160         if (tm == null) {
161             try {
162                 tm = (TransactionServiceImpl) sm.getTransactionService();
163             } catch (ServiceException e) {
164             }
165         }
166         return tm;
167     }
168
169     private DataBaseServiceImpl getDBM() {
170         if (dbmserv == null) {
171             try {
172                 dbmserv = (DataBaseServiceImpl) sm.getDataBaseService();
173             }catch (ServiceException e) {
174             }
175         }
176         return dbmserv;
177     }
178
179     /**
180      * Get the web container service We can use methods provided by the
181      * interface for MBean
182      */

183     private AbsJWebContainerServiceImplMBean getWebContainerService() {
184         if (webContainerService == null) {
185             try {
186                 webContainerService = (AbsJWebContainerServiceImplMBean) sm.getWebContainerService();
187             } catch (ServiceException e) {
188                 // not started
189
}
190         }
191         return webContainerService;
192     }
193
194     /**
195      * Get the mail service We can use methods provided by the interface for
196      * MBean
197      */

198     private MailServiceImplMBean getMailService() {
199         if (mailService == null) {
200             try {
201                 mailService = (MailServiceImplMBean) sm.getMailService();
202             } catch (Exception JavaDoc e) {
203                 // not started
204
}
205         }
206         return mailService;
207     }
208
209     /**
210      * Get the ear service We can use methods provided by the interface for
211      * MBean
212      */

213     private EarServiceImplMBean getEarService() {
214         if (earService == null) {
215             try {
216                 earService = (EarServiceImplMBean) sm.getEarService();
217             } catch (Exception JavaDoc e) {
218                 // not started
219
}
220         }
221         return earService;
222     }
223
224     /**
225      * Get the resource service We can use methods provided by the interface for
226      * MBean
227      */

228     private ResourceServiceImplMBean getResourceService() {
229         if (resourceService == null) {
230             try {
231                 resourceService = (ResourceServiceImplMBean) sm.getResourceService();
232             } catch (Exception JavaDoc e) {
233                 // not started
234
}
235         }
236         return resourceService;
237     }
238
239     // ------------------------------------------------------------------
240
// AdmInterface implementation
241
// ------------------------------------------------------------------
242

243     /**
244      * get Topics. Assumes that all Loggers are TopicalLoggers.
245      */

246     public String JavaDoc[] getTopics() throws RemoteException JavaDoc {
247         return LogManagement.getInstance().getTopics();
248     }
249
250     /**
251      * get Topic Level
252      */

253     public String JavaDoc getTopicLevel(String JavaDoc topic) throws RemoteException JavaDoc {
254         return LogManagement.getInstance().getTopicLevel(topic);
255     }
256
257     /**
258      * set Topic Level
259      */

260     public void setTopicLevel(String JavaDoc topic, String JavaDoc l) throws RemoteException JavaDoc {
261         LogManagement.getInstance().setTopicLevel(topic, l);
262     }
263
264     /**
265      * Create a container and load beans in it
266      * @param fileName name of the ejb-jar or xml file
267      */

268     public void addBeans(String JavaDoc fileName) throws RemoteException JavaDoc {
269         if (!isEJBContainer) {
270             return;
271         }
272         try {
273             ejbserv.createContainer(fileName);
274         } catch (Exception JavaDoc e) {
275             throw new RemoteException JavaDoc("Cannot add beans", e);
276         }
277     }
278
279     /**
280      * Deploy a given ear file with the help of the ear service.
281      * @param fileName the name of the ear file.
282      * @throws RemoteException if rmi call failed.
283      * @throws EarServiceException if the deployment failed.
284      */

285     public void addEar(String JavaDoc fileName) throws RemoteException JavaDoc, EarServiceException {
286         if (getEarService() != null) {
287             try {
288                 getEarService().deployEarMBean(fileName);
289             } catch (EarServiceException ear) {
290                 throw ear;
291             } catch (Exception JavaDoc e) {
292                 throw new RemoteException JavaDoc("Unable to deploy the EAR '" + fileName + "'.", e);
293             }
294         } else {
295             throw new EarServiceException("Can't add the Ear file " + fileName + ". The ear service is not started");
296         }
297     }
298
299     /**
300      * Test if the specified filename is already deployed or not
301      * @param fileName the name of the ear file.
302      * @return true if the ear is deployed, else false.
303      * @throws RemoteException if rmi call failed.
304      */

305     public boolean isEarLoaded(String JavaDoc fileName) throws RemoteException JavaDoc, EarServiceException {
306         boolean isLoaded = false;
307         if (getEarService() != null) {
308             isLoaded = getEarService().isEarLoaded(fileName);
309         } else {
310             throw new EarServiceException("Can't test if the ear file " + fileName
311                     + " is deployed or not. The ear service is not started");
312         }
313         return isLoaded;
314     }
315
316     /**
317      * Deploy a given rar file with the help of the resource service.
318      * @param fileName the name of the rar file.
319      * @throws RemoteException if rmi call failed.
320      * @throws ResourceServiceException if the deployment failed.
321      */

322     public void addRar(String JavaDoc fileName) throws RemoteException JavaDoc, ResourceServiceException {
323         if (getResourceService() != null) {
324             getResourceService().deployRarMBean(fileName);
325         } else {
326             throw new ResourceServiceException("Can't add the Rar file " + fileName
327                     + ". The resource service is not started");
328         }
329     }
330
331     /**
332      * Test if the specified filename is already deployed or not
333      * @param fileName the name of the rar file.
334      * @return true if the rar is deployed, else false.
335      * @throws RemoteException if rmi call failed.
336      * @throws ResourceServiceException if unable to get resource service
337      */

338     public boolean isRarLoaded(String JavaDoc fileName) throws RemoteException JavaDoc, ResourceServiceException {
339         boolean isLoaded = false;
340         if (getResourceService() != null) {
341             isLoaded = getResourceService().isRarLoaded(fileName);
342         } else {
343             throw new ResourceServiceException("Can't test if the rar file " + fileName
344                     + " is deployed or not. The rar service is not started");
345         }
346         return isLoaded;
347     }
348
349     /**
350      * Deploy a given war file with the help of the web container service.
351      * @param fileName the name of the war file.
352      * @throws RemoteException if rmi call failed.
353      * @throws JWebContainerServiceException if the deployment failed.
354      */

355     public void addWar(String JavaDoc fileName) throws RemoteException JavaDoc, JWebContainerServiceException {
356         if (getWebContainerService() != null) {
357             getWebContainerService().registerWarMBean(fileName);
358         } else {
359             throw new JWebContainerServiceException("Can't add the war file " + fileName
360                     + ". The web container service is not started");
361         }
362     }
363
364     /**
365      * Test if the specified filename is already deployed or not
366      * @param fileName the name of the war file.
367      * @return true if the war is deployed, else false.
368      * @throws RemoteException if rmi call failed.
369      */

370     public boolean isWarLoaded(String JavaDoc fileName) throws RemoteException JavaDoc, JWebContainerServiceException {
371         boolean isLoaded = false;
372         if (getWebContainerService() != null) {
373             isLoaded = getWebContainerService().isWarLoaded(fileName);
374         } else {
375             throw new JWebContainerServiceException("Can't test if the war file " + fileName
376                     + " is deployed or not. The war service is not started");
377         }
378         return isLoaded;
379     }
380
381     /**
382      * UnDeploy a given ear file with the help of the ear service.
383      * @param fileName the name of the ear file.
384      * @throws RemoteException if rmi call failed.
385      * @throws EarServiceException if the undeployment failed.
386      */

387     public void removeEar(String JavaDoc fileName) throws RemoteException JavaDoc, EarServiceException {
388         if (getEarService() != null) {
389             try {
390                 getEarService().unDeployEarMBean(fileName);
391                 // needed here to run also the distributed garbage collector
392
runGC();
393             } catch (EarServiceException ear) {
394                 throw ear;
395             } catch (Exception JavaDoc e) {
396                 throw new RemoteException JavaDoc("Cannot remove ear", e);
397             }
398         } else {
399             throw new EarServiceException("Can't remove of the Ear file " + fileName
400                     + ". The ear service is not started");
401         }
402     }
403
404     /**
405      * UnDeploy a given rar file with the help of the resource service.
406      * @param fileName the name of the rar file.
407      * @throws RemoteException if rmi call failed.
408      * @throws ResourceServiceException if the undeployment failed.
409      */

410     public void removeRar(String JavaDoc fileName) throws RemoteException JavaDoc, ResourceServiceException {
411         if (getResourceService() != null) {
412             getResourceService().unDeployRarMBean(fileName);
413             // needed here to run also the distributed garbage collector
414
runGC();
415         } else {
416             throw new ResourceServiceException("Can't remove of the Rar file " + fileName
417                     + ". The resource service is not started");
418         }
419     }
420
421     /**
422      * UnDeploy a given war file with the help of the web container service.
423      * @param fileName the name of the war file.
424      * @throws RemoteException if rmi call failed.
425      * @throws JWebContainerServiceException if the undeployment failed.
426      */

427     public void removeWar(String JavaDoc fileName) throws RemoteException JavaDoc, JWebContainerServiceException {
428         if (getWebContainerService() != null) {
429             getWebContainerService().unRegisterWarMBean(fileName);
430             // needed here to run also the distributed garbage collector
431
runGC();
432         } else {
433             throw new JWebContainerServiceException("Can't remove of the war file " + fileName
434                     + ". The web container service is not started");
435         }
436     }
437
438     /**
439      * Remove the container identified by fileName and remove all beans in it
440      * @param fileName name of the ejb-jar or xml file
441      */

442     public void removeBeans(String JavaDoc fileName) throws RemoteException JavaDoc {
443         if (!isEJBContainer) {
444             return;
445         }
446         try {
447             ejbserv.removeContainer(fileName);
448         } catch (Exception JavaDoc e) {
449             throw new RemoteException JavaDoc("Cannot remove bean", e);
450         }
451         // needed here to run also the distributed garbage collector
452
runGC();
453     }
454
455     /**
456      * returns true if beans are already loaded in server.
457      * @param fileName name of the ejb-jar or xml file
458      */

459     public boolean isLoaded(String JavaDoc fileName) throws RemoteException JavaDoc {
460         return (ejbserv.getContainer(fileName) != null);
461     }
462
463     public String JavaDoc dumpCustom() throws RemoteException JavaDoc {
464         String JavaDoc ret = "";
465         if (!isEJBContainer) {
466             return ret;
467         }
468         // JVM
469
ret += Server.jvmInfos();
470
471         // Transaction Service
472
if (getTM() != null) {
473             ret += "TM timeout=" + getTM().getTimeout();
474             ret += "\n";
475         }
476
477         // datasources
478
if (getDBM() != null) {
479             Collection JavaDoc dslist = getDBM().getDSList();
480             for (Iterator JavaDoc i = dslist.iterator(); i.hasNext(); ) {
481                 ConnectionManager cm = (ConnectionManager) i.next();
482                 ret += cm.getDSName() + ":lockPolicy=" + cm.getTransactionIsolation();
483                 Pool pool = cm.getPool();
484                 ret += ":minPoolSize=" + pool.getPoolMin();
485                 ret += ":maxPoolSize=" + pool.getPoolMax();
486                 ret += ":maxOpenTime=" + pool.getMaxOpenTime();
487                 ret += ":maxWaitTime=" + pool.getMaxWaitTime();
488                 ret += ":maxWaiters=" + pool.getMaxWaiters();
489                 ret += "\n";
490             }
491         }
492
493         // EJB Containers
494
Container[] lcont = (Container[]) ejbserv.listContainers();
495         for (int j = 0; j < lcont.length; j++) {
496             Container cont = lcont[j];
497             String JavaDoc contname = cont.getName();
498             String JavaDoc[] lbn = cont.listBeanNames();
499             for (int k = 0; k < lbn.length; k++) {
500                 JFactory bf = (JFactory) cont.getBeanFactory(lbn[k]);
501                 ret += contname + ":=" + lbn[k];
502                 ret += ":minPoolSize=" + bf.getMinPoolSize();
503                 ret += ":maxCacheSize=" + bf.getMaxCacheSize();
504                 ret += "\n";
505             }
506         }
507         return ret;
508     }
509
510     /**
511      * List beans of all JOnAS containers
512      */

513     public String JavaDoc[] listBeans() throws RemoteException JavaDoc {
514
515         if (!isEJBContainer) {
516             return new String JavaDoc[0];
517         }
518
519         Container[] lcont = ejbserv.listContainers();
520         Vector JavaDoc lbeans = new Vector JavaDoc();
521         for (int j = 0; j < lcont.length; j++) {
522             String JavaDoc[] lbn = lcont[j].listBeanNames();
523             String JavaDoc contname = lcont[j].getName();
524             for (int k = 0; k < lbn.length; k++) {
525                 String JavaDoc s = new String JavaDoc(contname + ": " + lbn[k]);
526                 lbeans.addElement(s);
527             }
528         }
529         String JavaDoc[] ret = new String JavaDoc[lbeans.size()];
530         lbeans.copyInto(ret);
531
532         return ret;
533     }
534
535     /**
536      * List JNDI context
537      */

538     public Vector JavaDoc listContext() throws RemoteException JavaDoc {
539         String JavaDoc name = null;
540         Vector JavaDoc ret = new Vector JavaDoc();
541         try {
542             NamingEnumeration JavaDoc ne;
543             ne = NamingManager.getInstance().getInitialContext().list("");
544             for (Enumeration JavaDoc e = ne; e.hasMoreElements();) {
545                 name = ((NameClassPair JavaDoc) e.nextElement()).getName();
546                 ret.addElement(name);
547             }
548         } catch (Exception JavaDoc ex) {
549             throw new RemoteException JavaDoc("Cannot list JNDI context", ex);
550         }
551         return ret;
552     }
553
554     /**
555      * List Environment (configuration properties provided by the configuration
556      * file).
557      */

558     public Properties JavaDoc listEnv() {
559         return jonasProperties.getConfigFileEnv();
560     }
561
562     /**
563      * Stop the Server without stopping the JVM
564      */

565     public void stopServer() throws RemoteException JavaDoc {
566         try {
567             serverState = STOPPED;
568             // keep registry alive a little while (for jonas admin) in case of
569
// error at launch
570
Thread.sleep(5000);
571             sm.stopServices();
572         } catch (ServiceException e) {
573             throw new RemoteException JavaDoc("Cannot stop services: ", e);
574         } catch (InterruptedException JavaDoc e) {
575         }
576     }
577
578     /**
579      * Stop the Server and stop the JVM
580      */

581     public void killServer() throws RemoteException JavaDoc {
582         stopServer();
583         // Delay the exit of the JVM so the link client/server is not broken.
584
// client call will return before the end of the JVM on server side.
585
new Thread JavaDoc(new Runnable JavaDoc() {
586
587             public void run() {
588                 try {
589                     // Wait before exit
590
Thread.sleep(SLEEP_DELAY);
591                 } catch (InterruptedException JavaDoc ie) {
592                     ie.printStackTrace();
593                     throw new IllegalStateException JavaDoc("Cannot wait: " + ie.getMessage());
594                 }
595                 System.exit(0);
596             }
597         }).start();
598
599     }
600
601     /**
602      * To test if the server is ready
603      * @return int 0=not ready, 1=ready, 2=stopped
604      */

605     public int getServerState() throws RemoteException JavaDoc {
606         return serverState;
607     }
608
609     /**
610      * To test if the server is an EJB container
611      */

612     public boolean isEJBContainer() throws RemoteException JavaDoc {
613         return isEJBContainer;
614     }
615
616     /**
617      * set the default value for transaction timeout
618      */

619     public void setTransactionTimeout(int timeout) throws RemoteException JavaDoc {
620         getTM().setTimeout(timeout);
621     }
622
623     /**
624      * run the garbage collector
625      */

626     public void runGC() throws RemoteException JavaDoc {
627         Runtime.getRuntime().gc();
628     }
629
630     /**
631      * sync all entity instances outside transactions
632      * @param passivate passivate instances after synchronization.
633      */

634     public void syncAllEntities(boolean passivate) throws RemoteException JavaDoc {
635         if (!isEJBContainer) {
636             return;
637         }
638         ejbserv.syncAllEntities(passivate);
639     }
640
641     // ------------------------------------------------------------------
642
// other public methods
643
// ------------------------------------------------------------------
644

645     /**
646      * server is ready
647      */

648     public void serverReady(boolean isEJB) {
649         serverState = READY;
650         isEJBContainer = isEJB;
651         if (isEJBContainer) {
652             ejbserv = (EJBServiceImpl) sm.getEjbService();
653         }
654     }
655
656     /**
657      * Deploy file (GenIC), needed for Ishmael to work. The file is in the
658      * directory specified by the property "jonas.service.deployment.directory"
659      * @param type type of the file (EJB, WAR, EAR, RAR, CAR)
660      * @param bfile
661      * @param filename basename of the file to be deployed
662      * @throws RemoteException
663      * @throws EarServiceException
664      * @throws ResourceServiceException
665      * @throws JWebContainerServiceException
666      * @author Dean Jennings. <da_jennings@junta.com.au>
667      */

668     public String JavaDoc deployFile(int type, byte[] bfile, String JavaDoc filename) throws RemoteException JavaDoc, EarServiceException,
669             JWebContainerServiceException {
670         try {
671             String JavaDoc directory = "";
672
673             if (type == TYPE_EJB) {
674                 directory = ejbserv.getEjbjarsDirectory();
675             } else if (type == TYPE_EAR) {
676                 directory = getEarService().getAppsDirectory();
677             } else if (type == TYPE_WAR) {
678                 directory = getWebContainerService().getWebappsDirectory();
679             } else if (type == TYPE_RAR) {
680                 directory = getResourceService().getRarsDirectory();
681             } else if (type == TYPE_CAR) {
682                 throw new UnsupportedOperationException JavaDoc("Not Supported yet");
683             }
684
685             File JavaDoc file = new File JavaDoc(directory + filename);
686
687             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
688             out.write(bfile);
689             out.close();
690             if (type == TYPE_EJB) {
691                 String JavaDoc args[] = new String JavaDoc[1];
692                 args[0] = directory + filename;
693                 GenIC.main(args);
694             } else if (type == TYPE_EAR) {
695                 unpackAndCompileEar(file);
696             }
697
698             return file.getAbsolutePath();
699
700         } catch (Exception JavaDoc e) {
701             e.printStackTrace();
702             throw new RemoteException JavaDoc(e.toString(), e);
703         }
704     }
705
706     private void unpackAndCompileEar(File JavaDoc file) throws EarServiceException, IOException JavaDoc, EarDeploymentDescException {
707         URL JavaDoc earUrl[] = new URL JavaDoc[1];
708
709         try {
710             earUrl[0] = file.toURL();
711         } catch (MalformedURLException JavaDoc e) {
712             String JavaDoc err = "Invalid ear file name '" + file;
713             throw new EarServiceException(err, e);
714         }
715
716         // Create classLoader
717
// parent classloader is the current classloader
718
ClassLoader JavaDoc currentLoader = Thread.currentThread().getContextClassLoader();
719         URLClassLoader JavaDoc loaderCls = new URLClassLoader JavaDoc(earUrl, currentLoader);
720
721         EarDeploymentDesc desc = EarManagerWrapper.getDeploymentDesc(earUrl[0].getFile(), loaderCls);
722
723         String JavaDoc[] ejbTags = desc.getEjbTags();
724         /*
725          * Example of a jar command: jar -uf ../output/ejbjars/sb.jar -C
726          * /tmp/genic1547.tmp a/b/C1.class -C /tmp/genic1547.tmp a/b/C2.class
727          * .....
728          */

729         String JavaDoc javaHomeBin = System.getProperty("java.home", "");
730         if (!("".equals(javaHomeBin))) {
731             javaHomeBin = javaHomeBin + File.separator + ".." + File.separator + "bin" + File.separator;
732         }
733
734         Cmd cmd = null;
735         cmd = new Cmd(javaHomeBin + "jar");
736         cmd.addArgument("-xf");
737         cmd.addArgument(file.getAbsolutePath());
738
739         for (int i = 0; i < ejbTags.length; i++) {
740             cmd.addArgument(ejbTags[i]);
741         }
742
743         boolean exitCmd = cmd.run();
744         // Analyse the result of the jar command
745
if (!exitCmd) {
746             throw new EarServiceException("Failed when extracting the the ejb jar " + "in the given jar file '" + file
747                     + "'.");
748         }
749
750         for (int i = 0; i < ejbTags.length; i++) {
751             String JavaDoc[] args = new String JavaDoc[1];
752             args[0] = ejbTags[i];
753             GenIC.main(args);
754         }
755
756         cmd = new Cmd(javaHomeBin + "jar");
757         cmd.addArgument("-uf");
758         cmd.addArgument(file.getAbsolutePath());
759
760         for (int i = 0; i < ejbTags.length; i++) {
761             cmd.addArgument(ejbTags[i]);
762         }
763         exitCmd = cmd.run();
764         // Analyse the result of the jar command
765
if (!exitCmd) {
766             throw new EarServiceException("Failed when extracting the the ejb jar " + "in the given jar file '" + file
767                     + "'.");
768         }
769
770         for (int i = 0; i < ejbTags.length; i++) {
771             File JavaDoc del = new File JavaDoc(ejbTags[i]);
772             del.delete();
773         }
774
775     }
776
777     /**
778      * List modules, needed for Ishmael to work. Currently only lists top level
779      * modules. It does need to do sub elements of ears in the future.
780      * @param type type of the file (EJB, WAR, EAR, RAR, CAR)
781      * @param state status of the modules to be returned (RUNNING, STOPPED, ALL)
782      * @throws RemoteException
783      * @author Dean Jennings. <deanjennings@junta.com.au>
784      */

785
786     public List JavaDoc listModules(int type, int state) throws RemoteException JavaDoc {
787         try {
788
789             // Should look at refactoring this logic into the services instead.
790
List JavaDoc modules = new ArrayList JavaDoc();
791             if (type == TYPE_EJB) {
792                 // Will need to do parent element in future
793
List JavaDoc jars = null;
794                 if (state == STATUS_RUNNING || state == STATUS_ALL) {
795                     jars = ejbserv.getDeployedJars();
796                     for (int i = 0; i < jars.size(); i++) {
797                         ModuleDesc desc = new ModuleDesc((String JavaDoc) jars.get(i), STATUS_RUNNING, null);
798                         modules.add(desc);
799                     }
800                 } else if (state == STATUS_STOPPED || state == STATUS_ALL) {
801                     jars = ejbserv.getDeployableJars();
802                     for (int i = 0; i < jars.size(); i++) {
803                         ModuleDesc desc = new ModuleDesc((String JavaDoc) jars.get(i), STATUS_STOPPED, null);
804                         modules.add(desc);
805                     }
806                 }
807             } else if (type == TYPE_EAR) {
808                 // Will need to do sub elements in future
809
List JavaDoc ears = null;
810                 if (state == STATUS_RUNNING || state == STATUS_ALL) {
811                     ears = getEarService().getDeployedEars();
812                     for (int i = 0; i < ears.size(); i++) {
813                         ModuleDesc desc = new ModuleDesc((String JavaDoc) ears.get(i), STATUS_RUNNING, null);
814                         modules.add(desc);
815                     }
816                 } else if (state == STATUS_STOPPED || state == STATUS_ALL) {
817                     ears = getEarService().getDeployableEars();
818                     for (int i = 0; i < ears.size(); i++) {
819                         ModuleDesc desc = new ModuleDesc((String JavaDoc) ears.get(i), STATUS_STOPPED, null);
820                         modules.add(desc);
821                     }
822                 }
823             } else if (type == TYPE_WAR) {
824                 // Will need to do parent element in future
825
List JavaDoc wars = null;
826                 AbsJWebContainerServiceImpl webService = (AbsJWebContainerServiceImpl) getWebContainerService(); // Very
827
// evil.
828
if (state == STATUS_RUNNING || state == STATUS_ALL) {
829                     wars = webService.getDeployedWars();
830                     for (int i = 0; i < wars.size(); i++) {
831                         // Need to get the Context root of the web app as well.
832
String JavaDoc warName = (String JavaDoc) wars.get(i);
833                         ClassLoader JavaDoc cl = webService.getClassLoader(new File JavaDoc(warName).toURL(), null, null);
834                         WebContainerDeploymentDesc wdesc = WebManagerWrapper.getDeploymentDesc(warName, cl);
835                         int start = warName.lastIndexOf("/");
836                         if (start < 0) {
837                             start = 0;
838                         }
839                         String JavaDoc url = wdesc.getContextRoot() != null ? wdesc.getContextRoot() : warName.substring(start,
840                                 warName.indexOf(".war"));
841                         ModuleDesc desc = new ModuleDesc(warName, STATUS_RUNNING, url);
842                         modules.add(desc);
843                     }
844                 } else if (state == STATUS_STOPPED || state == STATUS_ALL) {
845                     wars = webService.getDeployableWars();
846                     for (int i = 0; i < wars.size(); i++) {
847                         // Need to get the Context root of the web app as well.
848
String JavaDoc warName = (String JavaDoc) wars.get(i);
849                         ClassLoader JavaDoc cl = webService.getClassLoader(new File JavaDoc(warName).toURL(), null, null);
850                         WebContainerDeploymentDesc wdesc = WebManagerWrapper.getDeploymentDesc(warName, cl);
851                         int start = warName.lastIndexOf("/");
852                         if (start < 0) {
853                             start = 0;
854                         }
855
856                         String JavaDoc url = wdesc.getContextRoot() != null ? wdesc.getContextRoot() : warName.substring(start,
857                                 warName.indexOf(".war"));
858                         ModuleDesc desc = new ModuleDesc(warName, STATUS_STOPPED, url);
859                         modules.add(desc);
860                     }
861                 }
862             } else if (type == TYPE_RAR) {
863                 // Will need to do parent element in future
864
List JavaDoc rars = getResourceService().getInstalledRars();
865                 for (int i = 0; i < rars.size(); i++) {
866                     ModuleDesc desc = new ModuleDesc((String JavaDoc) rars.get(i), STATUS_RUNNING, null);
867                     modules.add(desc);
868                 }
869             } else if (type == TYPE_CAR) {
870                 throw new UnsupportedOperationException JavaDoc("Not Supported yet");
871             }
872
873             return modules;
874         } catch (Exception JavaDoc e) {
875             // Ugly
876
throw new RemoteException JavaDoc("Failed to list modules", e);
877         }
878
879     }
880
881     /**
882      * Physically remove the module from the server
883      * @param filename the filename of the module to be removed
884      * @throws RemoteException
885      * @author Dean Jennings. <deanjennings@junta.com.au>
886      */

887     public void undeployFile(String JavaDoc filename) throws RemoteException JavaDoc {
888         try {
889             File JavaDoc f = new File JavaDoc(filename);
890             f.delete();
891         } catch (Exception JavaDoc ioe) {
892             throw new RemoteException JavaDoc("Failed to remove the module " + filename + ":", ioe);
893         }
894
895     }
896
897 }
898
Popular Tags