Source Code Cross Referenced for JonasAdmin.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » adm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » JOnAS 4.8.6 » org.objectweb.jonas.adm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * JOnAS: Java(TM) Open Application Server
0003:         * Copyright (C) 1999-2006 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: JonasAdmin.java 9299 2006-08-01 16:10:46Z durieuxp $
0023:         * --------------------------------------------------------------------------
0024:         */package org.objectweb.jonas.adm;
0025:
0026:        import java.io.BufferedReader;
0027:        import java.io.IOException;
0028:        import java.io.InputStreamReader;
0029:        import java.rmi.RemoteException;
0030:        import java.util.Enumeration;
0031:        import java.util.Properties;
0032:        import java.util.StringTokenizer;
0033:        import java.util.Vector;
0034:
0035:        import javax.naming.InitialContext;
0036:        import javax.rmi.PortableRemoteObject;
0037:
0038:        import org.objectweb.jonas.ear.EarServiceException;
0039:        import org.objectweb.jonas.resource.ResourceServiceException;
0040:        import org.objectweb.jonas.web.JWebContainerServiceException;
0041:
0042:        /**
0043:         * This class implements a rmi client to administrate the server. JonasAdmin may
0044:         * be run either in interactive mode or command mode.
0045:         * @author Philippe Coq
0046:         * Contributor(s): Florent Benoit & Ludovic Bert : Methods for wars and ear
0047:         * files
0048:         */
0049:        public class JonasAdmin {
0050:
0051:            private static final String SERVERNAME = "JOnAS server ";
0052:
0053:            private static String jonasName = null;
0054:
0055:            private static AdmInterface admI = null;
0056:
0057:            private static int state = -1;
0058:
0059:            private static boolean reachable = false;
0060:
0061:            private static boolean isNotEJB = false;
0062:
0063:            private static boolean isError = false;
0064:            /**
0065:             * Default timeout (100 seconds)
0066:             */
0067:            private static final int DEFAULT_TIMEOUT = 100;
0068:
0069:            /**
0070:             * Define half a second by loop (sleep value) in ms
0071:             */
0072:            private static final long WAIT_LOOP_MSEC = 500;
0073:
0074:            /**
0075:             * List of servers where deployment will be done.
0076:             */
0077:            private static String[] target = null;
0078:
0079:            /** Static states
0080:             */
0081:            public final static int ADDFILE = 101;
0082:            public final static int REMOVEFILE = 102;
0083:            public final static int LISTBEAN = 103;
0084:            public final static int LISTJNDI = 104;
0085:            public final static int LISTENV = 105;
0086:            public final static int LISTTOPICS = 106;
0087:            public final static int DEBUGOPTION = 107;
0088:            public final static int TTOPTION = 108;
0089:            public final static int SYNCOPTION = 109;
0090:            public final static int CUSTOMOPTION = 110;
0091:            public final static int PASSIVATEOPTION = 111;
0092:            public final static int GCOPTION = 112;
0093:            public final static int ISDEPLOYOPTION = 113;
0094:            public final static int STARTOPTION = 114;
0095:            public final static int STOPOPTION = 115;
0096:
0097:            private static boolean qOption = false;
0098:
0099:            private static void checkAdm() {
0100:
0101:                // Gets reference on Adm object
0102:                String admName = jonasName + Adm.ADMNAME_SUFFIX;
0103:                try {
0104:                    InitialContext initialContext = new InitialContext();
0105:                    admI = (AdmInterface) PortableRemoteObject.narrow(
0106:                            initialContext.lookup(admName), AdmInterface.class);
0107:                    state = admI.getServerState();
0108:                    reachable = (state == Adm.READY);
0109:                    if (reachable) {
0110:                        isNotEJB = !admI.isEJBContainer();
0111:                    }
0112:                } catch (Exception e) {
0113:                    reachable = false;
0114:                }
0115:            }
0116:
0117:            /**
0118:             * check every second if server is ready and return when it's OK. don't wait
0119:             * more than given timeout (DEFAULT_TIMEOUT if not set) sec.
0120:             * @param pingTimeout value to wait
0121:             */
0122:            private static int waitServer(int pingTimeout) {
0123:                if (pingTimeout <= 0) {
0124:                    throw new IllegalArgumentException(
0125:                            "Timeout should be a value greater than 0");
0126:                }
0127:                // define the number of loops (WAIT_LOOP_MSEC milliseconds by loop)
0128:                double loopValue = pingTimeout
0129:                        / (WAIT_LOOP_MSEC / (double) 1000);
0130:                for (int i = 0; i < loopValue; i++) {
0131:                    try {
0132:                        // wait WAIT_LOOP_SEC second
0133:                        Thread.sleep(WAIT_LOOP_MSEC);
0134:                    } catch (InterruptedException e) {
0135:                        return 1;
0136:                    }
0137:                    // test if server is ready
0138:                    if (reachable) {
0139:                        return 0;
0140:                    }
0141:                    checkAdm();
0142:                    if (state == Adm.STOPPED) {
0143:                        return 1;
0144:                    }
0145:                }
0146:                System.out.println(SERVERNAME + jonasName + " unreachable");
0147:                return 1;
0148:            }
0149:
0150:            /**
0151:             * This method can add beans, war file ear or rar file depending on the
0152:             * extension of the file if it's a .jar file : -> Create a new JOnAS
0153:             * container for Beans relative to the file FileName if it's a .war file : ->
0154:             * deploy the war with the webcontainer service if it's a .ear file : ->
0155:             * Deploy the ear if it's a .rar file : -> Deploy the rar
0156:             * @param fileName name of the .jar file (Case of ejb-jar file) name of the
0157:             *        .war fle (Case of WAR file) name of the .ear file (Case of EAR
0158:             *        file) name of the .rar file (Case of RAR file)
0159:             */
0160:            private static void addFile(String fileName) {
0161:                if (!qOption) {
0162:                    System.out.println("Add File:");
0163:                }
0164:                if (!reachable) {
0165:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0166:                    isError = true;
0167:                    return;
0168:                }
0169:                try {
0170:                    if (fileName.toLowerCase().endsWith(".jar")
0171:                            || fileName.toLowerCase().endsWith(".xml")) {
0172:                        //jar file
0173:                        if (isNotEJB) {
0174:                            System.err
0175:                                    .println("This server doesn't implement an EJB Container");
0176:                            isError = true;
0177:                            return;
0178:                        }
0179:                        if (target == null) {
0180:                            admI.addBeans(fileName);
0181:                        } else {
0182:                            admI.deployFileOn(fileName, target);
0183:                        }
0184:                    } else if (fileName.toLowerCase().endsWith(".war")) {
0185:                        //War file
0186:                        if (target == null) {
0187:                            admI.addWar(fileName);
0188:                        } else {
0189:                            admI.deployFileOn(fileName, target);
0190:                        }
0191:                    } else if (fileName.toLowerCase().endsWith(".ear")) {
0192:                        //ear file
0193:                        if (target == null) {
0194:                            admI.addEar(fileName);
0195:                        } else {
0196:                            admI.deployFileOn(fileName, target);
0197:                        }
0198:                    } else if (fileName.toLowerCase().endsWith(".rar")) {
0199:                        //rar file
0200:                        if (target == null) {
0201:                            admI.addRar(fileName);
0202:                        } else {
0203:                            admI.deployFileOn(fileName, target);
0204:                        }
0205:                    } else {
0206:                        //bad format
0207:                        System.err
0208:                                .println("Valid file extensions are : .jar, .xml, .war, .ear, .rar");
0209:                        return;
0210:                    }
0211:                } catch (JWebContainerServiceException e) {
0212:                    System.err.println("admI.addWar: " + e);
0213:                    isError = true;
0214:                    return;
0215:                } catch (EarServiceException e) {
0216:                    System.err.println("admI.addEar: " + e);
0217:                    isError = true;
0218:                    return;
0219:                } catch (ResourceServiceException e) {
0220:                    System.err.println("admI.addRar: " + e);
0221:                    isError = true;
0222:                    return;
0223:                } catch (RemoteException e) {
0224:                    System.err.println("Cannot Deploy " + fileName + ": "
0225:                            + e.getCause().getMessage());
0226:                    return;
0227:                }
0228:            }
0229:
0230:            /**
0231:             * This method can remove beans, war file or ear file depending on the
0232:             * extension of the file if it's a .jar file : -> Remove a JOnAS container
0233:             * for Beans relative to the file FileName if it's a .war file : -> undeploy
0234:             * the war with the webcontainer service if it's a .ear file : -> uneploy
0235:             * the ear if it's a .rar file : -> uneploy the rar
0236:             * @param fileName name of the .jar file (Case of ejb-jar file) name of the
0237:             *        .war fle (Case of WAR file) name of the .ear file (Case of EAR
0238:             *        file) name of the .rar file (Case of RAR file)
0239:             */
0240:            private static void removeFile(String fileName) {
0241:                if (!qOption) {
0242:                    System.out.println("Remove File:");
0243:                }
0244:                if (!reachable) {
0245:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0246:                    isError = true;
0247:                    return;
0248:                }
0249:                //Remove a file (based upon the extension file
0250:                try {
0251:                    if (fileName.toLowerCase().endsWith(".jar")
0252:                            || fileName.toLowerCase().endsWith(".xml")) {
0253:                        //jar file
0254:                        if (isNotEJB) {
0255:                            System.err.println("no beans on TMServer");
0256:                            isError = true;
0257:                            return;
0258:                        }
0259:                        admI.removeBeans(fileName);
0260:                    } else if (fileName.toLowerCase().endsWith(".war")) {
0261:                        //War file
0262:                        admI.removeWar(fileName);
0263:                    } else if (fileName.toLowerCase().endsWith(".ear")) {
0264:                        //ear file
0265:                        admI.removeEar(fileName);
0266:                    } else if (fileName.toLowerCase().endsWith(".rar")) {
0267:                        //rar file
0268:                        admI.removeRar(fileName);
0269:                    } else {
0270:                        //bad format
0271:                        System.err
0272:                                .println("Valid file extensions are : .jar, xml, .war, .ear, *.rar");
0273:                        return;
0274:                    }
0275:                } catch (JWebContainerServiceException e) {
0276:                    System.err.println("admI.removeWar: " + e.getMessage());
0277:                    isError = true;
0278:                    return;
0279:                } catch (EarServiceException e) {
0280:                    System.err.println("admI.removeEar: " + e.getMessage());
0281:                    isError = true;
0282:                    return;
0283:                } catch (ResourceServiceException e) {
0284:                    System.err.println("admI.removeRar: " + e);
0285:                    isError = true;
0286:                    return;
0287:                } catch (RemoteException e) {
0288:                    System.err.println("RemoteException : " + e.getMessage());
0289:                    isError = true;
0290:                    return;
0291:                }
0292:                System.out.println("");
0293:            }
0294:
0295:            /**
0296:             * This method tells if the given file is deployed or not.
0297:             * @param fileName name of the .jar file (Case of ejb-jar file) name of the
0298:             *        .war file (Case of WAR file) name of the .ear file (Case of EAR
0299:             *        file) name of the .rar file (Case of RAR file)
0300:             */
0301:            private static void isDeployedFile(String fileName) {
0302:                if (!qOption) {
0303:                    System.out.println("Is File Deployable:");
0304:                }
0305:                if (!reachable) {
0306:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0307:                    isError = true;
0308:                    return;
0309:                }
0310:                //Remove a file (based upon the extension file
0311:                try {
0312:                    boolean isDeployed = false;
0313:                    if (fileName.toLowerCase().endsWith(".jar")
0314:                            || fileName.toLowerCase().endsWith(".xml")) {
0315:                        //jar file
0316:                        if (isNotEJB) {
0317:                            System.err.println("no beans on TMServer");
0318:                            isError = true;
0319:                            return;
0320:                        }
0321:                        isDeployed = admI.isLoaded(fileName);
0322:                    } else if (fileName.toLowerCase().endsWith(".war")) {
0323:                        //War file
0324:                        isDeployed = admI.isWarLoaded(fileName);
0325:                    } else if (fileName.toLowerCase().endsWith(".ear")) {
0326:                        //ear file
0327:                        isDeployed = admI.isEarLoaded(fileName);
0328:                    } else if (fileName.toLowerCase().endsWith(".rar")) {
0329:                        //rar file
0330:                        isDeployed = admI.isRarLoaded(fileName);
0331:                    } else {
0332:                        //bad format
0333:                        System.err
0334:                                .println("Valid file extensions are : .jar, xml, .war, .ear, .rar");
0335:                        return;
0336:                    }
0337:                    if (isDeployed) {
0338:                        System.out.println(fileName + " deployed in "
0339:                                + jonasName);
0340:                    } else {
0341:                        System.out.println(fileName + " NOT deployed in "
0342:                                + jonasName);
0343:                    }
0344:                } catch (JWebContainerServiceException e) {
0345:                    System.err.println("admI.removeWar: " + e.getMessage());
0346:                    isError = true;
0347:                    return;
0348:                } catch (EarServiceException e) {
0349:                    System.err.println("admI.removeEar: " + e.getMessage());
0350:                    isError = true;
0351:                    return;
0352:                } catch (ResourceServiceException e) {
0353:                    System.err.println("admI.removeRar: " + e);
0354:                    isError = true;
0355:                    return;
0356:                } catch (RemoteException e) {
0357:                    System.err.println("RemoteException : " + e.getMessage());
0358:                    isError = true;
0359:                    return;
0360:                }
0361:                System.out.println("");
0362:            }
0363:
0364:            private static void custom() {
0365:                if (!qOption) {
0366:                    System.out.println("Custom:");
0367:                }
0368:                if (isNotEJB) {
0369:                    System.err.println("no beans on TMServer");
0370:                    isError = true;
0371:                    return;
0372:                }
0373:                if (!reachable) {
0374:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0375:                    isError = true;
0376:                    return;
0377:                }
0378:                try {
0379:                    System.out.println(admI.dumpCustom());
0380:                } catch (RemoteException e) {
0381:                    System.err.println("admI.dumpCustom() : " + e);
0382:                    isError = true;
0383:                    return;
0384:                }
0385:                System.out.println("");
0386:            }
0387:
0388:            private static void listbeans() {
0389:                if (!qOption) {
0390:                    System.out.println("ListBeans:");
0391:                }
0392:                if (isNotEJB) {
0393:                    System.err.println("no beans on TMServer");
0394:                    isError = true;
0395:                    return;
0396:                }
0397:                if (!reachable) {
0398:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0399:                    isError = true;
0400:                    return;
0401:                }
0402:                try {
0403:                    String[] result = admI.listBeans();
0404:                    if (result.length == 0) {
0405:                        System.out.println("No bean in " + jonasName);
0406:                    }
0407:                    for (int i = 0; i < result.length; i++) {
0408:                        System.out.println(result[i]);
0409:                    }
0410:                } catch (RemoteException e) {
0411:                    System.err.println("admI.listBeans() : " + e);
0412:                    isError = true;
0413:                    return;
0414:                }
0415:                System.out.println("");
0416:            }
0417:
0418:            private static void listnames() {
0419:                if (!qOption) {
0420:                    System.out.println("List JndiNames:");
0421:                }
0422:                if (!reachable) {
0423:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0424:                    isError = true;
0425:                    return;
0426:                }
0427:                Vector result = new Vector();
0428:                try {
0429:                    result = admI.listContext();
0430:                } catch (RemoteException e) {
0431:                    System.err.println("admI.listContext() : " + e);
0432:                    isError = true;
0433:                    return;
0434:                }
0435:
0436:                if (result.size() == 0) {
0437:                    System.out.println("No name in JNDI context.");
0438:                }
0439:                for (int i = 0; i < result.size(); i++) {
0440:                    System.out.println(result.elementAt(i));
0441:                }
0442:                System.out.println("");
0443:            }
0444:
0445:            private static void listproperties() {
0446:                if (!qOption) {
0447:                    System.out.println("ListProperties:");
0448:                }
0449:                if (!reachable) {
0450:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0451:                    isError = true;
0452:                    return;
0453:                }
0454:                Properties p = new Properties();
0455:                try {
0456:                    p = admI.listEnv();
0457:                } catch (RemoteException e) {
0458:                    System.err.println("admI.listEnv() : " + e);
0459:                    isError = true;
0460:                    return;
0461:                }
0462:                for (Enumeration e = p.keys(); e.hasMoreElements();) {
0463:                    Object key = e.nextElement();
0464:                    Object value = p.get(key);
0465:                    System.out.println(key.toString() + "=" + value.toString());
0466:                }
0467:                System.out.println("");
0468:            }
0469:
0470:            private static void sync(boolean passivate) {
0471:                if (!qOption) {
0472:                    System.out.println("Sync:");
0473:                }
0474:                if (isNotEJB) {
0475:                    System.err.println("no beans on TMServer");
0476:                    isError = true;
0477:                    return;
0478:                }
0479:                if (!reachable) {
0480:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0481:                    isError = true;
0482:                    return;
0483:                }
0484:                try {
0485:                    admI.syncAllEntities(passivate);
0486:                } catch (RemoteException e) {
0487:                    System.err.println("admI.syncAllEntities : " + e);
0488:                    isError = true;
0489:                    return;
0490:                }
0491:                System.out.println("");
0492:            }
0493:
0494:            private static void runGC() {
0495:                if (!qOption) {
0496:                    System.out.println("Run GC:");
0497:                }
0498:                if (!reachable) {
0499:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0500:                    isError = true;
0501:                    return;
0502:                }
0503:                try {
0504:                    admI.runGC();
0505:                } catch (RemoteException e) {
0506:                    System.err.println("admI.runGC : " + e);
0507:                    isError = true;
0508:                    return;
0509:                }
0510:                System.out.println("");
0511:            }
0512:
0513:            private static void setTTimeout(String tstr) {
0514:                if (!qOption) {
0515:                    System.out.println("Set TransactionTimeout:");
0516:                }
0517:                if (!reachable) {
0518:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0519:                    isError = true;
0520:                    return;
0521:                }
0522:                Integer i = new Integer(tstr);
0523:                try {
0524:                    admI.setTransactionTimeout(i.intValue());
0525:                } catch (RemoteException e) {
0526:                    System.err.println("admI.setTransactionTimeout : " + e);
0527:                    isError = true;
0528:                    return;
0529:                }
0530:                System.out.println("");
0531:            }
0532:
0533:            private static void listTopics() {
0534:                if (!qOption) {
0535:                    System.out.println("List Monolog Topics:");
0536:                }
0537:                if (!reachable) {
0538:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0539:                    isError = true;
0540:                    return;
0541:                }
0542:                try {
0543:                    String[] result = admI.getTopics();
0544:                    if (result.length == 0) {
0545:                        System.out.println("No topics in " + jonasName);
0546:                    }
0547:                    for (int i = 0; i < result.length; i++) {
0548:                        String level = admI.getTopicLevel(result[i]);
0549:                        System.out.println(level + "\t" + result[i]);
0550:                    }
0551:                } catch (RemoteException e) {
0552:                    System.err.println("admI.getTopics : " + e);
0553:                    isError = true;
0554:                    return;
0555:                }
0556:                System.out.println("");
0557:            }
0558:
0559:            private static void setTopic(String t, String l) {
0560:                if (!qOption) {
0561:                    System.out.println("Set Monolog Topic:");
0562:                }
0563:                try {
0564:                    admI.setTopicLevel(t, l.toUpperCase());
0565:                } catch (RemoteException e) {
0566:                    System.err.println("admI.setTopics : " + e);
0567:                    isError = true;
0568:                    return;
0569:                }
0570:                System.out.println("");
0571:            }
0572:
0573:            private static void startserver() {
0574:                if (!reachable) {
0575:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0576:                    isError = true;
0577:                    return;
0578:                }
0579:                if (target == null) {
0580:                    System.out.println("ERROR: Current Server Already started");
0581:                    return;
0582:                }
0583:                try {
0584:                    admI.startRemoteServers(target);
0585:                } catch (RemoteException e) {
0586:                    System.err.println("Error while starting servers:"
0587:                            + e.getCause().getMessage());
0588:                }
0589:            }
0590:
0591:            private static void stopserver() {
0592:                if (!reachable) {
0593:                    System.err.println(SERVERNAME + jonasName + " unreachable");
0594:                    isError = true;
0595:                    return;
0596:                }
0597:                try {
0598:                    if (target == null) {
0599:                        admI.killServer();
0600:                    } else {
0601:                        admI.stopRemoteServers(target);
0602:                    }
0603:                } catch (RemoteException e) {
0604:                    if (target != null) {
0605:                        System.err.println("Error while stopping servers:"
0606:                                + e.getCause().getMessage());
0607:                    }
0608:                }
0609:                if (target == null) {
0610:                    System.out.println(SERVERNAME + jonasName + " stopped");
0611:                }
0612:            }
0613:
0614:            private static void help() {
0615:                System.out
0616:                        .println("addbeans    adds beans in a new JOnAS container");
0617:                System.out
0618:                        .println("addfile     adds beans/servlets/j2ee app/rars based upon the file extension");
0619:                System.out.println("custom      dump jonas customization");
0620:                System.out
0621:                        .println("env         JOnAS properties used by the server");
0622:                System.out.println("gc          run the garbage collector");
0623:                System.out.println("help        help");
0624:                System.out.println("jndinames   lists registered JNDI names");
0625:                System.out.println("listbeans   lists beans");
0626:                System.out
0627:                        .println("name        to identify a current JOnAS server");
0628:                System.out.println("quit        quit JonasAdmin");
0629:                System.out
0630:                        .println("removebeans remove beans in a new JOnAS container");
0631:                System.out
0632:                        .println("removefile  remove beans/servlets/j2ee app/rars (based upon the file extension)");
0633:                System.out.println("start       stop target servers");
0634:                System.out.println("stop        stop target servers");
0635:                System.out.println("sync        synchronize all entities");
0636:                System.out.println("passivate   passivate all entities");
0637:                System.out.println("trace       get/set monolog topics");
0638:                System.out
0639:                        .println("ttimeout    set default transaction timeout");
0640:                System.out
0641:                        .println("target      set list of servers where command must be applied");
0642:            }
0643:
0644:            private static void usage() {
0645:                System.out.println("usage : jonas admin <options>");
0646:                System.out
0647:                        .println("if no option(except -n), mode is interactive.");
0648:                System.out.println("list of available options:");
0649:                System.out
0650:                        .println("    -n JonasName : to identify an JOnAS Server");
0651:                System.out.println("    -s : stops the EJB server.");
0652:                System.out
0653:                        .println("    -l : lists beans currently in the JOnAS Server.");
0654:                System.out.println("    -j : lists registered JNDI names.");
0655:                System.out
0656:                        .println("    -e : lists JOnAS properties currently used by the JOnAS Server.");
0657:                System.out
0658:                        .println("    -a fileName : dynamically adds   : - beans from fileName in a new container");
0659:                System.out
0660:                        .println("                                     : - servlets from a WAR file");
0661:                System.out
0662:                        .println("                                     : - j2ee application from an EAR file");
0663:                System.out
0664:                        .println("                                     : - resource adapter from a RAR file");
0665:                System.out
0666:                        .println("    -r fileName : dynamically remove : - beans from container fileName");
0667:                System.out
0668:                        .println("                                     : - servlets of a WAR file");
0669:                System.out
0670:                        .println("                                     : - j2ee application of an EAR file");
0671:                System.out
0672:                        .println("                                     : - resource adapter from a RAR file");
0673:                System.out
0674:                        .println("    -isdeployed fileName : tells if the file is deployed or not");
0675:                System.out.println("    -sync: synchronize all entities");
0676:                System.out.println("    -passivate: passivate all entities");
0677:                System.out.println("    -gc: run the garbage collector");
0678:                System.out
0679:                        .println("    -tt timeout: set default transaction timeout");
0680:                System.out
0681:                        .println("    -start: Start servers designed by '-target' arg");
0682:                System.out
0683:                        .println("    -target target: set target for commands (default is local server)");
0684:                System.out
0685:                        .println("    -ping [-timeout <val (sec)>]: ping server for a given time.(default=100s)");
0686:                System.out.println("    -t list monolog topics");
0687:                System.out
0688:                        .println("    --debug-level topic : set DEBUG for a monolog topic");
0689:                System.out
0690:                        .println("    -q : quiet mode, no processing header information.");
0691:                System.out.println("    -h : help message.");
0692:                System.out.println("    -? : help message.");
0693:            }
0694:
0695:            /*
0696:             * interactive mode
0697:             */
0698:            public static void menu() throws IOException {
0699:                // First name the jonas server
0700:                if (!reachable) {
0701:                    System.out
0702:                            .println("You must first choose a jonas server. (command `name`)");
0703:                    System.out
0704:                            .println("Type `help` to get the list of available commands");
0705:                }
0706:
0707:                // User interface
0708:                BufferedReader inbuf = new BufferedReader(
0709:                        new InputStreamReader(System.in));
0710:                while (true) {
0711:                    System.out.print("Admin (" + jonasName + ") > ");
0712:                    String command = inbuf.readLine();
0713:                    if (command.length() == 0) {
0714:                        continue;
0715:                    }
0716:                    if ("addbeans".startsWith(command)
0717:                            || "addfile".startsWith(command)) {
0718:                        String fName = null;
0719:                        System.out.print("file name ? > ");
0720:                        if ((fName = inbuf.readLine()).length() != 0) {
0721:                            addFile(fName);
0722:                        }
0723:                        continue;
0724:                    }
0725:                    if ("start".startsWith(command)) {
0726:                        startserver();
0727:                        continue;
0728:                    }
0729:                    if ("env".startsWith(command)) {
0730:                        listproperties();
0731:                        continue;
0732:                    }
0733:                    if ("gc".startsWith(command)) {
0734:                        runGC();
0735:                        continue;
0736:                    }
0737:                    if ("help".startsWith(command) || command.equals("?")) {
0738:                        help();
0739:                        continue;
0740:                    }
0741:                    if ("jndinames".startsWith(command)) {
0742:                        listnames();
0743:                        continue;
0744:                    }
0745:                    if ("listbeans".startsWith(command)) {
0746:                        listbeans();
0747:                        continue;
0748:                    }
0749:                    if ("custom".startsWith(command)) {
0750:                        custom();
0751:                        continue;
0752:                    }
0753:                    if ("name".startsWith(command)) {
0754:                        System.out.print("Enter the " + SERVERNAME
0755:                                + "'s name (jonas.name property) : ");
0756:                        jonasName = new String(inbuf.readLine());
0757:                        checkAdm();
0758:                        continue;
0759:                    }
0760:                    if ("quit".startsWith(command)
0761:                            || "exit".startsWith(command)) {
0762:                        return;
0763:                    }
0764:                    if ("removebeans".startsWith(command)
0765:                            || "removefile".startsWith(command)) {
0766:                        String fName = null;
0767:                        System.out.print("file name ? > ");
0768:                        if ((fName = inbuf.readLine()).length() != 0) {
0769:                            removeFile(fName);
0770:                        }
0771:                        continue;
0772:                    }
0773:                    if ("trace".startsWith(command)) {
0774:                        while (true) {
0775:                            listTopics();
0776:                            System.out.print("topic name ? > ");
0777:                            String tname = inbuf.readLine().trim();
0778:                            if (tname.length() == 0) {
0779:                                break;
0780:                            }
0781:                            System.out
0782:                                    .print("topic level ? (DEBUG | WARN | INFO | ERROR | INHERIT) > ");
0783:                            String levstr = inbuf.readLine().trim();
0784:                            setTopic(tname, levstr);
0785:                        }
0786:                        continue;
0787:                    }
0788:                    if ("stop".startsWith(command)) {
0789:                        stopserver();
0790:                        continue;
0791:                    }
0792:                    if ("sync".startsWith(command)) {
0793:                        sync(false);
0794:                        continue;
0795:                    }
0796:                    if ("passivate".startsWith(command)) {
0797:                        sync(true);
0798:                        continue;
0799:                    }
0800:                    if ("ttimeout".startsWith(command)) {
0801:                        String tstr = null;
0802:                        System.out.print("transaction timeout in seconds ? > ");
0803:                        if ((tstr = inbuf.readLine()).length() != 0) {
0804:                            setTTimeout(tstr);
0805:                        }
0806:                        continue;
0807:                    }
0808:                    if ("target".startsWith(command)) {
0809:                        System.out
0810:                                .print("Server or Cluster where to deploy the beans ? > ");
0811:                        target = makeArrayFrom(inbuf.readLine());
0812:                        continue;
0813:                    }
0814:                    System.out
0815:                            .println("Unknown command. Type help to get for the list of commands.");
0816:                }
0817:            }
0818:
0819:            /**
0820:             * Make an Array of OBJECT_NAMEs
0821:             * @param line A comma separated list of names
0822:             * @return Array of Stringified objectnames
0823:             */
0824:            private static String[] makeArrayFrom(String line) {
0825:                StringTokenizer stk = new StringTokenizer(line, ",");
0826:                int nb = stk.countTokens();
0827:                String[] ret = new String[nb];
0828:                for (int i = 0; i < nb; i++) {
0829:                    ret[i] = stk.nextToken();
0830:                }
0831:                return ret;
0832:            }
0833:
0834:            public static void main(String[] args) throws IOException {
0835:
0836:                String fileName = null;
0837:                String timeout = null;
0838:                String topic = null;
0839:                String pingTimeoutValue = null;
0840:
0841:                boolean pingOption = false;
0842:                boolean sOption = false;
0843:
0844:                boolean interactive = true;
0845:                boolean namedServer = false;
0846:                Vector lstArgs = new Vector();
0847:                // Get command args
0848:                for (int argn = 0; argn < args.length; argn++) {
0849:                    String arg = args[argn];
0850:                    boolean nextArgument = argn < args.length - 1;
0851:
0852:                    if (arg.equals("-a") && nextArgument) {
0853:                        fileName = args[++argn];
0854:                        JonasArg ja = new JonasArg(ADDFILE, fileName);
0855:                        lstArgs.add(ja);
0856:                        interactive = false;
0857:                        continue;
0858:                    }
0859:                    if (arg.equals("-ping")) {
0860:                        pingOption = true;
0861:                        continue;
0862:                    }
0863:                    if (arg.equals("-custom")) {
0864:                        JonasArg ja = new JonasArg(CUSTOMOPTION);
0865:                        lstArgs.add(ja);
0866:                        interactive = false;
0867:                        continue;
0868:                    }
0869:                    if (arg.equals("-e")) {
0870:                        JonasArg ja = new JonasArg(LISTENV);
0871:                        lstArgs.add(ja);
0872:                        interactive = false;
0873:                        continue;
0874:                    }
0875:                    if (arg.equals("-h") || arg.equals("-?")) {
0876:                        usage();
0877:                        System.exit(0);
0878:                    }
0879:                    if (arg.equals("-gc")) {
0880:                        JonasArg ja = new JonasArg(GCOPTION);
0881:                        lstArgs.add(ja);
0882:                        interactive = false;
0883:                        continue;
0884:                    }
0885:                    if (arg.equals("-j")) {
0886:                        JonasArg ja = new JonasArg(LISTJNDI);
0887:                        lstArgs.add(ja);
0888:                        interactive = false;
0889:                        continue;
0890:                    }
0891:                    if (arg.equals("-l")) {
0892:                        JonasArg ja = new JonasArg(LISTBEAN);
0893:                        lstArgs.add(ja);
0894:                        interactive = false;
0895:                        continue;
0896:                    }
0897:                    if (arg.equals("-n") && nextArgument) {
0898:                        jonasName = args[++argn];
0899:                        namedServer = true;
0900:                        continue;
0901:                    }
0902:                    if (arg.equals("-timeout") && nextArgument) {
0903:                        pingTimeoutValue = args[++argn];
0904:                        continue;
0905:                    }
0906:                    if (arg.equals("-target") && nextArgument) {
0907:                        target = makeArrayFrom(args[++argn]);
0908:                        continue;
0909:                    }
0910:                    if (arg.equals("-r") && nextArgument) {
0911:                        fileName = args[++argn];
0912:                        JonasArg ja = new JonasArg(REMOVEFILE, fileName);
0913:                        lstArgs.add(ja);
0914:                        interactive = false;
0915:                        continue;
0916:                    }
0917:                    if (arg.equals("-s")) {
0918:                        sOption = true;
0919:                        interactive = false;
0920:                        continue;
0921:                    }
0922:                    if (arg.equals("-stop")) {
0923:                        JonasArg ja = new JonasArg(STOPOPTION);
0924:                        lstArgs.add(ja);
0925:                        interactive = false;
0926:                        continue;
0927:                    }
0928:                    if (arg.equals("-start")) {
0929:                        JonasArg ja = new JonasArg(STARTOPTION);
0930:                        lstArgs.add(ja);
0931:                        interactive = false;
0932:                        continue;
0933:                    }
0934:                    if (arg.equals("-sync")) {
0935:                        JonasArg ja = new JonasArg(SYNCOPTION);
0936:                        lstArgs.add(ja);
0937:                        interactive = false;
0938:                        continue;
0939:                    }
0940:                    if (arg.equals("-passivate")) {
0941:                        JonasArg ja = new JonasArg(PASSIVATEOPTION);
0942:                        lstArgs.add(ja);
0943:                        interactive = false;
0944:                        continue;
0945:                    }
0946:                    if (arg.equals("--debug-level") && nextArgument) {
0947:                        topic = args[++argn];
0948:                        JonasArg ja = new JonasArg(DEBUGOPTION, topic);
0949:                        lstArgs.add(ja);
0950:                        interactive = false;
0951:                        continue;
0952:                    }
0953:                    if (arg.equals("-t")) {
0954:                        JonasArg ja = new JonasArg(LISTTOPICS);
0955:                        lstArgs.add(ja);
0956:                        interactive = false;
0957:                        continue;
0958:                    }
0959:                    if (arg.equals("-tt") && nextArgument) {
0960:                        timeout = args[++argn];
0961:                        JonasArg ja = new JonasArg(TTOPTION, timeout);
0962:                        lstArgs.add(ja);
0963:                        interactive = false;
0964:                        continue;
0965:                    }
0966:                    if (arg.equals("-isdeployed") && nextArgument) {
0967:                        fileName = args[++argn];
0968:                        JonasArg ja = new JonasArg(ISDEPLOYOPTION, fileName);
0969:                        lstArgs.add(ja);
0970:                        interactive = false;
0971:                        continue;
0972:                    }
0973:                    if (arg.equals("-q")) {
0974:                        qOption = true;
0975:                        interactive = false;
0976:                        continue;
0977:                    }
0978:                    System.out.println("Bad option: " + arg);
0979:                    usage();
0980:                    System.exit(2);
0981:                }
0982:
0983:                if (!namedServer) {
0984:                    jonasName = "jonas"; // default value
0985:                }
0986:
0987:                if (pingOption) {
0988:                    //
0989:                    int pingTimeout = DEFAULT_TIMEOUT;
0990:                    // if a timeout is set by user, use it.
0991:                    if (pingTimeoutValue != null) {
0992:                        try {
0993:                            pingTimeout = Integer.parseInt(pingTimeoutValue);
0994:                        } catch (NumberFormatException nfe) {
0995:                            System.err
0996:                                    .println("Incorrect timeout value for ping. Value is '"
0997:                                            + pingTimeoutValue + "'.");
0998:                        }
0999:                    }
1000:                    // Wait EJB server is ready
1001:                    System.exit(waitServer(pingTimeout));
1002:                }
1003:
1004:                // for all other options, first check server is reachable
1005:                checkAdm();
1006:
1007:                // Stopserver is special, don't process anything else
1008:                if (sOption) {
1009:                    // Stops Server
1010:                    stopserver();
1011:                    if (isError) {
1012:                        System.exit(2);
1013:                    } else {
1014:                        return;
1015:                    }
1016:
1017:                }
1018:
1019:                // No option => interactive mode.
1020:                if (interactive) {
1021:                    menu();
1022:                } else {
1023:                    boolean first = true;
1024:                    boolean forQuiet = qOption;
1025:                    qOption = true;
1026:                    for (Enumeration e = lstArgs.elements(); e
1027:                            .hasMoreElements();) {
1028:                        JonasArg ai = (JonasArg) e.nextElement();
1029:                        switch (ai.argType) {
1030:                        case ADDFILE:
1031:                            // Adds file into JOnAS container
1032:                            addFile(ai.argName);
1033:                            break;
1034:                        case REMOVEFILE:
1035:                            // Remove file from JOnAS container
1036:                            removeFile(ai.argName);
1037:                            break;
1038:                        case STARTOPTION:
1039:                            startserver();
1040:                            break;
1041:                        case STOPOPTION:
1042:                            stopserver();
1043:                            break;
1044:                        case LISTBEAN:
1045:                            // List beans in JOnAS container
1046:                            listbeans();
1047:                            break;
1048:                        case LISTJNDI:
1049:                            // List jndi names in JOnAS container
1050:                            listnames();
1051:                            break;
1052:                        case LISTENV:
1053:                            // List JOnAS environment
1054:                            listproperties();
1055:                            break;
1056:                        case LISTTOPICS:
1057:                            // List Monolog topics
1058:                            listTopics();
1059:                            break;
1060:                        case DEBUGOPTION:
1061:                            // set debug for a topic
1062:                            setTopic(ai.argName, "DEBUG");
1063:                            break;
1064:                        case TTOPTION:
1065:                            // set the default value for transaction timeout
1066:                            setTTimeout(ai.argName);
1067:                            break;
1068:                        case SYNCOPTION:
1069:                            // sync all entity instances outside transactions
1070:                            sync(false);
1071:                            break;
1072:                        case CUSTOMOPTION:
1073:                            // Custom option
1074:                            custom();
1075:                            break;
1076:                        case PASSIVATEOPTION:
1077:                            // passivate all entity instances outside transactions
1078:                            sync(true);
1079:                            break;
1080:                        case GCOPTION:
1081:                            // run the garbage collector
1082:                            runGC();
1083:                            break;
1084:                        case ISDEPLOYOPTION:
1085:                            // is the file deployed ?
1086:                            isDeployedFile(ai.argName);
1087:                            break;
1088:                        }
1089:                        // Keep original functionality w/ first option then add header
1090:                        // lines for each additional command if -q not set.
1091:                        if (first) {
1092:                            first = false;
1093:                            qOption = forQuiet;
1094:                        }
1095:                        // Exit if error
1096:                        if (isError) {
1097:                            System.exit(2);
1098:                        }
1099:                    }
1100:                }
1101:            }
1102:
1103:            // Class to hold the arguments for processing
1104:            public static class JonasArg {
1105:                public int argType;
1106:                public String argName = "";
1107:
1108:                public JonasArg(int type) {
1109:                    argType = type;
1110:                }
1111:
1112:                public JonasArg(int type, String name) {
1113:                    argType = type;
1114:                    argName = name;
1115:                }
1116:            }
1117:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.