Source Code Cross Referenced for JonasAdminTestCase.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » jonasadmin » test » util » 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.jonasadmin.test.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * JOnAS: Java(TM) Open Application Server
0003:         * Copyright (C) 1999-2005 Bull S.A.
0004:         * Contact: jonas-team@objectweb.org
0005:         *
0006:         * This library is free software; you can redistribute it and/or
0007:         * modify it under the terms of the GNU Lesser General Public
0008:         * License as published by the Free Software Foundation; either
0009:         * version 2.1 of the License, or 1any 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: JonasAdminTestCase.java 8188 2006-03-30 09:05:39Z danesa $
0023:         * --------------------------------------------------------------------------
0024:         */package org.objectweb.jonas.jonasadmin.test.util;
0025:
0026:        import java.io.File;
0027:        import java.lang.reflect.Method;
0028:        import java.net.MalformedURLException;
0029:        import java.net.URI;
0030:        import java.net.URL;
0031:        import java.net.URLClassLoader;
0032:        import java.text.SimpleDateFormat;
0033:        import java.util.Calendar;
0034:        import java.util.Date;
0035:        import java.util.HashMap;
0036:        import java.util.List;
0037:        import java.util.Map;
0038:        import java.util.Properties;
0039:
0040:        import javax.management.MBeanServerConnection;
0041:        import javax.management.ObjectName;
0042:        import javax.management.remote.JMXConnector;
0043:        import javax.management.remote.JMXConnectorFactory;
0044:        import javax.management.remote.JMXServiceURL;
0045:        import javax.naming.Context;
0046:        import javax.naming.InitialContext;
0047:        import javax.naming.NamingException;
0048:        import javax.rmi.PortableRemoteObject;
0049:
0050:        import junit.framework.TestCase;
0051:
0052:        import org.objectweb.jonas.adm.AdmInterface;
0053:        import org.objectweb.jonas.jmx.J2eeObjectName;
0054:        import org.omg.CORBA.ORB;
0055:
0056:        import com.meterware.httpunit.HttpUnitOptions;
0057:        import com.meterware.httpunit.TableCell;
0058:        import com.meterware.httpunit.WebConversation;
0059:        import com.meterware.httpunit.WebForm;
0060:        import com.meterware.httpunit.WebResponse;
0061:        import com.meterware.httpunit.WebTable;
0062:
0063:        /**
0064:         * Define a class to add useful methods for test the examples
0065:         *  - Deploy ear, war and beans
0066:         *  - Retrieve initial context
0067:         * @author Florent Benoit
0068:         */
0069:        public class JonasAdminTestCase extends TestCase {
0070:
0071:            /**
0072:             * URL of the login page
0073:             *
0074:             */
0075:            protected static final String URL_JONASADMIN = "/jonasAdmin/";
0076:
0077:            /**
0078:             * NAME of FRAME content
0079:             */
0080:            protected static final String FRAME_CONTENT = "content";
0081:
0082:            /**
0083:             * NAME of FRAME tree
0084:             */
0085:            protected static final String FRAME_TREE = "tree";
0086:
0087:            /**
0088:             * NAME of FRAME top
0089:             */
0090:            protected static final String FRAME_TOP = "top";
0091:
0092:            /**
0093:             * URL of the deployment of EARs
0094:             */
0095:            protected static final String URL_JONASADMIN_DEPLOYEAR = "/jonasAdmin/EditDeploy.do?type=ear";
0096:
0097:            /**
0098:             * URL of the deployment of JARs
0099:             */
0100:            protected static final String URL_JONASADMIN_DEPLOYJAR = "/jonasAdmin/EditDeploy.do?type=jar";
0101:
0102:            /**
0103:             * Name of the property file of jonasAdmin tests
0104:             */
0105:            private static final String JONASADMINTEST_PROPERTY_NAME = "jonasAdminTests.properties";
0106:            /**
0107:             * JoramAdmin MBean's OBJECT_NAME
0108:             */
0109:            private static final String JORAM_ADMIN_ON = "joramClient:type=JoramAdmin";
0110:            /**
0111:             * Name of the JOnAS server used for tests
0112:             */
0113:            private static String jonasName = "jonas";
0114:            /**
0115:             * Name of the domain
0116:             */
0117:            private static String domainName = "jonas";
0118:            /**
0119:             * Initial context used for lookup
0120:             */
0121:            private static Context ictx = null;
0122:
0123:            /**
0124:             * JOnAS admin used for communicate via JMX to JOnAS
0125:             */
0126:            private static AdmInterface admI = null;
0127:
0128:            /**
0129:             * JMX connector
0130:             */
0131:            private static JMXConnector connector = null;
0132:
0133:            /**
0134:             * MBean Server Connection
0135:             */
0136:            private static MBeanServerConnection currentServerConnection = null;
0137:
0138:            /**
0139:             * Client ORB
0140:             */
0141:            private static ORB orb = null;
0142:
0143:            /**
0144:             * Conversation used for HttpUnit
0145:             */
0146:            protected static WebConversation wc = null;
0147:
0148:            /**
0149:             * URL used for the constructor
0150:             */
0151:            protected String url = null;
0152:
0153:            /**
0154:             * URL used to return to Welcome.jsp
0155:             */
0156:            protected String urlWelcome = null;
0157:
0158:            /**
0159:             * URL used to log out
0160:             */
0161:            protected String urlLogOut = null;
0162:
0163:            /**
0164:             * Prefix for build URLs
0165:             */
0166:            protected String prefixUrl = null;
0167:
0168:            /**
0169:             * JOnAS and jonasAdmin properties
0170:             */
0171:            protected JProperties jProp = null;
0172:
0173:            /**
0174:             * jonasAdmin test properties
0175:             */
0176:            protected Properties configFile;
0177:
0178:            /**
0179:             * jonasAdmin port
0180:             */
0181:            protected String port;
0182:
0183:            /**
0184:             * Add to the specified url the prefix
0185:             * @param url relative URL
0186:             * @return absolute path of URL
0187:             */
0188:            protected String getAbsoluteUrl(String url) {
0189:                return (this .prefixUrl + url);
0190:            }
0191:
0192:            /**
0193:             * Initialize the port used by tests and the prefix
0194:             */
0195:            private void init() {
0196:                // Port
0197:                port = System.getProperty("http.port");
0198:                if (port == null) {
0199:                    port = "9000";
0200:                }
0201:
0202:                // urls
0203:                prefixUrl = "http://localhost:" + port;
0204:                urlWelcome = prefixUrl + "/jonasAdmin/Welcome.do";
0205:                urlLogOut = prefixUrl + "/jonasAdmin/logOut.do";
0206:
0207:                // Properties
0208:                jProp = new JProperties();
0209:
0210:                try {
0211:                    configFile = jProp
0212:                            .getProperties(JONASADMINTEST_PROPERTY_NAME);
0213:                } catch (Exception e) {
0214:                    System.err.println("Cannot find file : "
0215:                            + JONASADMINTEST_PROPERTY_NAME + ". " + e);
0216:                    e.printStackTrace();
0217:                }
0218:
0219:            }
0220:
0221:            /**
0222:             * Constructor with a specified name
0223:             * @param s the name
0224:             */
0225:            public JonasAdminTestCase(String s) {
0226:                super (s);
0227:                init();
0228:            }
0229:
0230:            /**
0231:             * Constructor with a specified name and url
0232:             * @param s the name
0233:             * @param url the url which can be used
0234:             */
0235:            public JonasAdminTestCase(String s, String url) {
0236:                super (s);
0237:                init();
0238:                this .url = getAbsoluteUrl(url);
0239:            }
0240:
0241:            /**
0242:             * Get initialContext
0243:             * @return the initialContext
0244:             * @throws NamingException if the initial context can't be retrieved
0245:             */
0246:            private Context getInitialContext() throws NamingException {
0247:                return new InitialContext();
0248:            }
0249:
0250:            /**
0251:             * Common setUp routine, used for every test.
0252:             * @throws Exception if an error occurs
0253:             */
0254:            protected void setUp() throws Exception {
0255:
0256:                // WebConversation to jonasAdmin
0257:                wc = JonasAdminConnexion.getWc();
0258:                if (wc == null) {
0259:                    wc = JonasAdminConnexion.createWc();
0260:                }
0261:
0262:                try {
0263:                    // get InitialContext
0264:                    if (ictx == null) {
0265:                        ictx = getInitialContext();
0266:                    }
0267:                    if (admI == null) {
0268:                        admI = (AdmInterface) PortableRemoteObject
0269:                                .narrow(ictx.lookup(jonasName + "_Adm"),
0270:                                        AdmInterface.class);
0271:                    }
0272:
0273:                } catch (NamingException e) {
0274:                    System.err.println("Cannot setup test: " + e);
0275:                    e.printStackTrace();
0276:                }
0277:
0278:                // Get a MBean Server connection and set currentServerConnection variable
0279:                initMBeanServer();
0280:            }
0281:
0282:            /**
0283:             * Load an ear file in the jonas server
0284:             * @param filename ear file, without ".ear" extension
0285:             * @throws Exception if an error occurs
0286:             */
0287:            public void useEar(String filename) throws Exception {
0288:
0289:                try {
0290:                    // Load ear in JOnAS if not already loaded.
0291:                    if (ictx == null) {
0292:                        ictx = getInitialContext();
0293:                    }
0294:
0295:                    if (admI == null) {
0296:                        admI = (AdmInterface) PortableRemoteObject
0297:                                .narrow(ictx.lookup(jonasName + "_Adm"),
0298:                                        AdmInterface.class);
0299:                    }
0300:
0301:                    //Test in both directories (apps/ and apps/autoload)
0302:                    String appsFileName = filename + ".ear";
0303:                    String autoloadAppsFileName = "autoload" + File.separator
0304:                            + filename + ".ear";
0305:                    if (!admI.isEarLoaded(appsFileName)
0306:                            && !admI.isEarLoaded(autoloadAppsFileName)) {
0307:                        //if the file was in autoload, it was loaded
0308:                        admI.addEar(appsFileName);
0309:                    }
0310:
0311:                } catch (Exception e) {
0312:                    throw new Exception("Cannot load Ear : " + e.getMessage());
0313:                }
0314:            }
0315:
0316:            /**
0317:             * Unload a ear file in the jonas server
0318:             * @param filename ear file, without ".ear" extension
0319:             * @throws Exception if an error occurs
0320:             */
0321:            public void unUseEar(String filename) throws Exception {
0322:                try {
0323:                    // Unload ear in EARServer if not already loaded.
0324:                    if (ictx == null) {
0325:                        ictx = getInitialContext();
0326:                    }
0327:                    if (admI == null) {
0328:                        admI = (AdmInterface) PortableRemoteObject
0329:                                .narrow(ictx.lookup(jonasName + "_Adm"),
0330:                                        AdmInterface.class);
0331:                    }
0332:                    if (admI.isEarLoaded(filename + ".ear")) {
0333:                        admI.removeEar(filename + ".ear");
0334:                    }
0335:                } catch (Exception e) {
0336:                    throw new Exception("Cannot unload Bean : "
0337:                            + e.getMessage());
0338:                }
0339:            }
0340:
0341:            /**
0342:             * Load a war file in the jonas server
0343:             * @param filename war file, without ".war" extension
0344:             * @throws Exception if an error occurs
0345:             */
0346:            public void useWar(String filename) throws Exception {
0347:
0348:                try {
0349:                    // Load war in JOnAS if not already loaded.
0350:                    if (ictx == null) {
0351:                        ictx = getInitialContext();
0352:                    }
0353:
0354:                    if (admI == null) {
0355:                        admI = (AdmInterface) PortableRemoteObject
0356:                                .narrow(ictx.lookup(jonasName + "_Adm"),
0357:                                        AdmInterface.class);
0358:                    }
0359:
0360:                    //Test in both directories (apps/ and apps/autoload)
0361:                    String webappsFileName = filename + ".war";
0362:                    String autoloadWebappsFileName = "autoload"
0363:                            + File.separator + filename + ".war";
0364:                    if (!admI.isWarLoaded(webappsFileName)
0365:                            && !admI.isWarLoaded(autoloadWebappsFileName)) {
0366:                        //if the file was in autoload, it was loaded
0367:                        admI.addWar(webappsFileName);
0368:                    }
0369:
0370:                } catch (Exception e) {
0371:                    throw new Exception("Cannot load War : " + e.getMessage());
0372:                }
0373:            }
0374:
0375:            /**
0376:             * Load a bean jar file in the jonas server
0377:             * @param filename jar file, without ".jar" extension
0378:             * @throws Exception if an error occurs
0379:             */
0380:            public void useBeans(String filename) throws Exception {
0381:                try {
0382:                    // Load bean in EJBServer if not already loaded.
0383:                    if (ictx == null) {
0384:                        ictx = getInitialContext();
0385:                    }
0386:                    if (admI == null) {
0387:                        admI = (AdmInterface) PortableRemoteObject
0388:                                .narrow(ictx.lookup(jonasName + "_Adm"),
0389:                                        AdmInterface.class);
0390:                    }
0391:                    if (!admI.isLoaded(filename + ".jar")) {
0392:                        admI.addBeans(filename + ".jar");
0393:                    }
0394:                } catch (Exception e) {
0395:                    throw new Exception("Cannot load Bean : " + e.getMessage());
0396:                }
0397:            }
0398:
0399:            /**
0400:             * Unload a bean jar file in the jonas server
0401:             * @param filename jar file, without ".jar" extension
0402:             * @throws Exception if an error occurs
0403:             */
0404:            public void unUseBeans(String filename) throws Exception {
0405:                try {
0406:                    // Load bean in EJBServer if not already loaded.
0407:                    if (ictx == null) {
0408:                        ictx = getInitialContext();
0409:                    }
0410:                    if (admI == null) {
0411:                        admI = (AdmInterface) PortableRemoteObject
0412:                                .narrow(ictx.lookup(jonasName + "_Adm"),
0413:                                        AdmInterface.class);
0414:                    }
0415:                    if (admI.isLoaded(filename + ".jar")) {
0416:                        admI.removeBeans(filename + ".jar");
0417:                    }
0418:                } catch (Exception e) {
0419:                    throw new Exception("Cannot unload Bean : "
0420:                            + e.getMessage());
0421:                }
0422:            }
0423:
0424:            /**
0425:             * Load a rar file in the jonas server
0426:             * @param filename rar file, without ".rar" extension
0427:             * @throws Exception if an error occurs
0428:             */
0429:            public void useRar(String filename) throws Exception {
0430:
0431:                try {
0432:                    // Load rar in JOnAS if not already loaded.
0433:                    if (ictx == null) {
0434:                        ictx = getInitialContext();
0435:                    }
0436:
0437:                    if (admI == null) {
0438:                        admI = (AdmInterface) PortableRemoteObject
0439:                                .narrow(ictx.lookup(jonasName + "_Adm"),
0440:                                        AdmInterface.class);
0441:                    }
0442:
0443:                    //Test in both directories (rars/ and rars/autoload)
0444:                    String raFileName = filename + ".rar";
0445:                    String autoloadRaFileName = "autoload" + File.separator
0446:                            + filename + ".rar";
0447:                    if (!admI.isRarLoaded(raFileName)
0448:                            && !admI.isRarLoaded(autoloadRaFileName)) {
0449:                        //if the file was in autoload, it was loaded
0450:                        admI.addRar(raFileName);
0451:                    }
0452:
0453:                } catch (Exception e) {
0454:                    throw new Exception("Cannot load Rar : " + e.getMessage());
0455:                }
0456:            }
0457:
0458:            /**
0459:             * Rar is loaded in the jonas server
0460:             * @param filename rar file, without ".rar" extension
0461:             * @return true if the rar is loaded, else false
0462:             * @throws Exception if an error occurs
0463:             */
0464:            public boolean isRarLoaded(String filename) throws Exception {
0465:
0466:                boolean found = false;
0467:
0468:                try {
0469:                    // Load rar in JOnAS if not already loaded.
0470:                    if (ictx == null) {
0471:                        ictx = getInitialContext();
0472:                    }
0473:
0474:                    if (admI == null) {
0475:                        admI = (AdmInterface) PortableRemoteObject
0476:                                .narrow(ictx.lookup(jonasName + "_Adm"),
0477:                                        AdmInterface.class);
0478:                    }
0479:
0480:                    //Test in both directories (rars/ and rars/autoload)
0481:                    String raFileName = filename + ".rar";
0482:                    String autoloadRaFileName = "autoload" + File.separator
0483:                            + filename + ".rar";
0484:                    if (admI.isRarLoaded(raFileName)
0485:                            || admI.isRarLoaded(autoloadRaFileName)) {
0486:                        //if the file was in autoload, it was loaded
0487:                        found = true;
0488:                    }
0489:
0490:                } catch (Exception e) {
0491:                    throw new Exception("Cannot load Rar : " + e.getMessage());
0492:                }
0493:
0494:                return found;
0495:            }
0496:
0497:            /**
0498:             * Create a mail factory in jonas server
0499:             * @param mail factory name of the datasource
0500:             * @param properties properties of the mail factory
0501:             * @throws Exception if an error occurs
0502:             */
0503:            protected void createMailFactory(String mailFactory,
0504:                    Properties properties) throws Exception {
0505:                MBeanServerConnection server = getMBeanServer();
0506:                ObjectName on = ObjectName
0507:                        .getInstance("jonas:type=service,name=mail");
0508:
0509:                // add properties in 'datasource'.properties
0510:                JonasAdminFiles.writeConfigFile(mailFactory + ".properties",
0511:                        properties);
0512:
0513:                Object[] params = new Object[] { mailFactory, properties,
0514:                        new Boolean(true) };
0515:                server.invoke(on, "createMailFactoryMBean", params,
0516:                        new String[] { String.class.getName(),
0517:                                Properties.class.getName(),
0518:                                Boolean.class.getName() });
0519:
0520:            }
0521:
0522:            /**
0523:             * Unbind a mail factory in jonas server
0524:             * @param mailFactory name of the mail factory
0525:             * @throws Exception if an error occurs
0526:             */
0527:            protected void unBindMailFactory(String mailFactory)
0528:                    throws Exception {
0529:                MBeanServerConnection server = getMBeanServer();
0530:                ObjectName on = ObjectName
0531:                        .getInstance("jonas:type=service,name=mail");
0532:
0533:                Object[] params = new Object[] { mailFactory };
0534:                server.invoke(on, "unbindMailFactoryMBean", params,
0535:                        new String[] { String.class.getName() });
0536:            }
0537:
0538:            /**
0539:             * Return if the mail factory is in mail factory list
0540:             * @param mailFactory the name of the mail factory
0541:             * @return true if the mail factory is in list, false else.
0542:             * @throws Exception if an error occurs.
0543:             */
0544:            protected boolean isInMailFactoryList(String mailFactory)
0545:                    throws Exception {
0546:                MBeanServerConnection server = getMBeanServer();
0547:                ObjectName on = ObjectName
0548:                        .getInstance("jonas:type=service,name=mail");
0549:
0550:                List list = (List) server.getAttribute(on,
0551:                        "MailFactoryPropertiesFiles");
0552:
0553:                return list.contains(mailFactory);
0554:            }
0555:
0556:            /**
0557:             * Load a datasource in jonas server
0558:             * @param datasource name of the datasource
0559:             * @param properties properties of the datasource
0560:             * @throws Exception if an error occurs
0561:             */
0562:            protected void useDatasource(String datasource,
0563:                    Properties properties) throws Exception {
0564:                MBeanServerConnection server = getMBeanServer();
0565:                ObjectName on = ObjectName
0566:                        .getInstance("jonas:type=service,name=database");
0567:
0568:                if (!((Boolean) server.invoke(on, "isLoadedDataSource",
0569:                        new Object[] { datasource },
0570:                        new String[] { String.class.getName() }))
0571:                        .booleanValue()) {
0572:                    // add properties in 'datasource'.properties
0573:                    JonasAdminFiles.writeConfigFile(datasource + ".properties",
0574:                            properties);
0575:
0576:                    Object[] params = new Object[] { datasource, properties,
0577:                            new Boolean(true) };
0578:                    server.invoke(on, "loadDataSource", params, new String[] {
0579:                            String.class.getName(), Properties.class.getName(),
0580:                            Boolean.class.getName() });
0581:                }
0582:            }
0583:
0584:            /**
0585:             * Check if database service is started (dbm)
0586:             * @return true if dbm started
0587:             * @throws Exception if an error occurs
0588:             */
0589:            protected boolean isDatabaseService() throws Exception {
0590:                boolean result = false;
0591:                MBeanServerConnection server = getMBeanServer();
0592:                ObjectName on = ObjectName
0593:                        .getInstance("jonas:type=service,name=database");
0594:                if (server.isRegistered(on)) {
0595:                    result = true;
0596:                }
0597:                return result;
0598:            }
0599:
0600:            /**
0601:             * Check if the Joram RAR is deplyed
0602:             * @return true if dbm started
0603:             * @throws Exception if an error occurs
0604:             */
0605:            protected boolean isJoramRar() throws Exception {
0606:                boolean result = false;
0607:                MBeanServerConnection server = getMBeanServer();
0608:                ObjectName on = ObjectName.getInstance(JORAM_ADMIN_ON);
0609:                if (server.isRegistered(on)) {
0610:                    result = true;
0611:                }
0612:                return result;
0613:            }
0614:
0615:            /**
0616:             * Unload a datasource in jonas server
0617:             * @param datasource name of the datasource
0618:             * @throws Exception if an error occurs
0619:             */
0620:            protected void unUseDatasource(String datasource) throws Exception {
0621:                MBeanServerConnection server = getMBeanServer();
0622:                ObjectName on = ObjectName
0623:                        .getInstance("jonas:type=service,name=database");
0624:
0625:                if (((Boolean) server.invoke(on, "isLoadedDataSource",
0626:                        new Object[] { datasource },
0627:                        new String[] { String.class.getName() }))
0628:                        .booleanValue()) {
0629:
0630:                    Object[] params = new Object[] { datasource };
0631:                    server.invoke(on, "unloadDataSource", params,
0632:                            new String[] { String.class.getName() });
0633:                }
0634:            }
0635:
0636:            /**
0637:             * Load a datasource in jonas server
0638:             * @param datasource name of the datasource
0639:             * @return boolean true if the datasource is loaded, false else
0640:             * @throws Exception if an error occurs
0641:             */
0642:            protected boolean isLoadedDatasource(String datasource)
0643:                    throws Exception {
0644:                MBeanServerConnection server = getMBeanServer();
0645:                ObjectName on = ObjectName
0646:                        .getInstance("jonas:type=service,name=database");
0647:
0648:                return ((Boolean) server.invoke(on, "isLoadedDataSource",
0649:                        new Object[] { datasource },
0650:                        new String[] { String.class.getName() }))
0651:                        .booleanValue();
0652:            }
0653:
0654:            /**
0655:             * Create a topic in a joram server
0656:             * @param name name of the topic
0657:             * @param idString id of the current joram server
0658:             * @throws Exception if an error occurs
0659:             */
0660:            protected void createTopic(String name, int idString)
0661:                    throws Exception {
0662:                MBeanServerConnection server = getMBeanServer();
0663:                ObjectName on = ObjectName
0664:                        .getInstance("joramClient:type=JoramAdapter,*");
0665:                ObjectName joramAdapterON = (ObjectName) server.queryNames(on,
0666:                        null).iterator().next();
0667:
0668:                Object[] params = new Object[] { new Integer(idString), name };
0669:                server.invoke(joramAdapterON, "createTopic", params,
0670:                        new String[] { "int", String.class.getName() });
0671:            }
0672:
0673:            /**
0674:             * Create a queue in a joram server
0675:             * @param name name of the queue
0676:             * @param idString id of the current joram server
0677:             * @throws Exception if an error occurs
0678:             */
0679:            protected void createQueue(String name, int idString)
0680:                    throws Exception {
0681:                MBeanServerConnection server = getMBeanServer();
0682:                ObjectName on = ObjectName
0683:                        .getInstance("joramClient:type=JoramAdapter,*");
0684:                ObjectName joramAdapterON = (ObjectName) server.queryNames(on,
0685:                        null).iterator().next();
0686:
0687:                Object[] params = new Object[] { new Integer(idString), name };
0688:                server.invoke(joramAdapterON, "createQueue", params,
0689:                        new String[] { "int", String.class.getName() });
0690:            }
0691:
0692:            /**
0693:             * Create a user in a joram server
0694:             * @param name name of the user
0695:             * @param password password of the user
0696:             * @throws Exception if an error occurs
0697:             */
0698:            protected void createLocalJmsUser(String name, String password)
0699:                    throws Exception {
0700:                MBeanServerConnection server = getMBeanServer();
0701:                ObjectName on = ObjectName
0702:                        .getInstance("joramClient:type=JoramAdapter,*");
0703:                ObjectName joramAdapterON = (ObjectName) server.queryNames(on,
0704:                        null).iterator().next();
0705:
0706:                Object[] params = new Object[] { name, password };
0707:                server
0708:                        .invoke(joramAdapterON, "createUser", params,
0709:                                new String[] { "java.lang.String",
0710:                                        "java.lang.String" });
0711:            }
0712:
0713:            /**
0714:             * Call the main method of a specific class with empty args
0715:             * @param classToLoad name of class which contains the main method
0716:             * @throws Exception if it fails
0717:             */
0718:            protected void callMainMethod(String classToLoad) throws Exception {
0719:                callMainMethod(classToLoad, new String[] {});
0720:            }
0721:
0722:            /**
0723:             * Call the main method of a specific class and the specific args
0724:             * @param classToLoad name of class which contains the main method
0725:             * @param args args to give to the main method
0726:             * @throws Exception if it fails
0727:             */
0728:            protected void callMainMethod(String classToLoad, String[] args)
0729:                    throws Exception {
0730:                //Build classloader
0731:                ClassLoader cl = Thread.currentThread().getContextClassLoader();
0732:                URL[] urls = new URL[1];
0733:                urls[0] = new File(System.getProperty("jonas.root")
0734:                        + File.separator + "examples" + File.separator
0735:                        + "classes").toURL();
0736:                URLClassLoader loader = new URLClassLoader(urls);
0737:                Thread.currentThread().setContextClassLoader(loader);
0738:                Class clazz = loader.loadClass(classToLoad);
0739:                Class[] argList = new Class[] { args.getClass() };
0740:                Method meth = clazz.getMethod("main", argList);
0741:                Object appli = meth.invoke(null, new Object[] { args });
0742:            }
0743:
0744:            /**
0745:             * Get current time
0746:             * @return A String Time with the format yyyy-MM-dd.HH-mm-ss
0747:             */
0748:            protected String getTime() {
0749:                Calendar cal = Calendar.getInstance();
0750:                Date date = cal.getTime();
0751:                SimpleDateFormat formatter;
0752:                formatter = new SimpleDateFormat("yyyy-MM-dd.HH-mm-ss");
0753:                return formatter.format(date);
0754:            }
0755:
0756:            /**
0757:             * Undeploy an EAR
0758:             * @param name String of EAR Name
0759:             * @throws Exception if error occurs
0760:             */
0761:            protected void undeployEar(String name) throws Exception {
0762:                String fileName = name + ".ear";
0763:
0764:                // Disable errors of javascript
0765:                HttpUnitOptions.setExceptionsThrownOnScriptError(false);
0766:                // Disable exception thrown on error status
0767:                HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
0768:
0769:                WebResponse wr = wc
0770:                        .getResponse(getAbsoluteUrl(URL_JONASADMIN_DEPLOYEAR));
0771:                WebForm[] webForms = wr.getForms();
0772:                WebForm webForm = webForms[0];
0773:
0774:                String params = webForm.getParameterValue("undeploy");
0775:                WebForm.Scriptable script = webForm.getScriptableObject();
0776:
0777:                // Disable errors of javascript
0778:                HttpUnitOptions.setExceptionsThrownOnScriptError(false);
0779:
0780:                if (params.length() == 0) {
0781:                    params += fileName;
0782:                } else {
0783:                    params += "," + fileName;
0784:                }
0785:
0786:                script.setParameterValue("undeploy", params);
0787:
0788:                WebResponse submitUndeploy = webForm.submit();
0789:
0790:                webForms = submitUndeploy.getForms();
0791:                webForm = webForms[0];
0792:
0793:                WebResponse endResp = webForm.submit();
0794:            }
0795:
0796:            /**
0797:             * Undeploy all EAR
0798:             * @throws Exception if error occurs
0799:             */
0800:            protected void undeployAllEar() throws Exception {
0801:                WebResponse wr = wc
0802:                        .getResponse(getAbsoluteUrl(URL_JONASADMIN_DEPLOYEAR));
0803:                undeployAll(wr);
0804:            }
0805:
0806:            /**
0807:             * Undeploy all Jars
0808:             * @throws Exception if error occurs
0809:             */
0810:            protected void undeployAllJar() throws Exception {
0811:                WebResponse wr = wc
0812:                        .getResponse(getAbsoluteUrl(URL_JONASADMIN_DEPLOYJAR));
0813:                undeployAll(wr);
0814:            }
0815:
0816:            /**
0817:             * Undeploy all
0818:             * @throws Exception if error occurs
0819:             */
0820:            protected void undeployAll(WebResponse wr) throws Exception {
0821:
0822:                WebForm[] webForms = wr.getForms();
0823:                WebForm webForm = webForms[0];
0824:
0825:                // Disable errors of javascript
0826:                HttpUnitOptions.setExceptionsThrownOnScriptError(false);
0827:                // Disable exception thrown on error status
0828:                HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
0829:
0830:                String params = webForm.getParameterValue("deploy");
0831:                WebForm.Scriptable script = webForm.getScriptableObject();
0832:
0833:                if (params.length() == 0) {
0834:                    params += webForm.getParameterValue("undeploy");
0835:                } else {
0836:                    params += "," + webForm.getParameterValue("undeploy");
0837:                }
0838:
0839:                script.setParameterValue("undeploy", params);
0840:
0841:                WebResponse submitUndeploy = webForm.submit();
0842:
0843:                webForms = submitUndeploy.getForms();
0844:                webForm = webForms[0];
0845:
0846:                WebResponse endResp = webForm.submit();
0847:
0848:                wc.getResponse(getAbsoluteUrl(URL_JONASADMIN));
0849:            }
0850:
0851:            /**
0852:             * Verify number of tabs and the selected tab
0853:             * @param tabTable the tab table
0854:             * @param nbTabs number of tabs
0855:             * @param selectedTab the numero of the selected tab
0856:             */
0857:            protected void testTabs(WebTable tabTable, int nbTabs,
0858:                    int selectedTab, String msg) {
0859:                TableCell selectedCell;
0860:                // - number of tabs
0861:                assertEquals("There are not " + nbTabs + " tabs. " + msg,
0862:                        nbTabs, (tabTable.getColumnCount() + 1) / 2);
0863:                // - selected tab
0864:                selectedCell = tabTable.getTableCell(1, (selectedTab * 2) - 2);
0865:                assertEquals("It is not the selected tab. ", 1, selectedCell
0866:                        .getElementsWithAttribute("src",
0867:                                "/jonasAdmin/images/dot.gif").length);
0868:            }
0869:
0870:            /**
0871:             * Get MBean Server connection
0872:             * @throws Exception if an error occurs
0873:             */
0874:            private void initMBeanServer() throws Exception {
0875:                String sCarolURL = jProp.getRegistryUrl();
0876:                String carolProtocol = jProp.getRegistryProtocol();
0877:                URI carolURL = new URI(sCarolURL);
0878:                int portNb = carolURL.getPort();
0879:                String port = String.valueOf(portNb);
0880:                String url = null;
0881:                Map env = null;
0882:                if (carolProtocol.equals("jrmp")) {
0883:                    // Treat JRMP case
0884:                    url = "service:jmx:rmi:///jndi/rmi://localhost:" + port
0885:                            + "/jrmpconnector_" + jonasName;
0886:                } else if (carolProtocol.equals("iiop")) {
0887:                    // Treat IIOP case
0888:                    url = "service:jmx:iiop:///jndi/iiop://localhost:" + port
0889:                            + "/iiopconnector_" + jonasName;
0890:                    env = new HashMap();
0891:                    if (orb == null) {
0892:                        initORB();
0893:                    }
0894:                    env.put("java.naming.corba.orb", orb);
0895:                } else if (carolProtocol.equals("jeremie")) {
0896:                    // Treat JEREMIE case
0897:                    url = "service:jmx:rmi:///jndi/jrmi://localhost:" + port
0898:                            + "/jeremieconnector_" + jonasName;
0899:                } else if (carolProtocol.equals("cmi")) {
0900:                    // Treat CMI
0901:                    url = "service:jmx:rmi:///jndi/cmi://localhost:" + port
0902:                            + "/cmiconnector_" + jonasName;
0903:                }
0904:
0905:                JMXServiceURL connURL = null;
0906:                try {
0907:                    connURL = new JMXServiceURL(url);
0908:                } catch (MalformedURLException e) {
0909:                    throw new Exception(
0910:                            "Can't create JMXServiceURL with string: " + url);
0911:                }
0912:
0913:                try {
0914:                    connector = JMXConnectorFactory.newJMXConnector(connURL,
0915:                            env);
0916:                } catch (MalformedURLException e1) {
0917:                    throw new Exception(
0918:                            "there is no provider for the protocol in " + url);
0919:                } catch (java.io.IOException e) {
0920:                    throw new Exception(
0921:                            "Connector client cannot be made because of a communication problem (used URL: "
0922:                                    + url + ")");
0923:                }
0924:                try {
0925:                    connector.connect(env);
0926:                    currentServerConnection = connector
0927:                            .getMBeanServerConnection();
0928:                } catch (java.io.IOException ioe) {
0929:                    throw new Exception(
0930:                            "connection could not be made because of a communication problem");
0931:                }
0932:            }
0933:
0934:            /**
0935:             * Get MBean Server connection
0936:             * @return a MBeanServer Connection
0937:             * @throws Exception if an error occurs
0938:             */
0939:            protected MBeanServerConnection getMBeanServer() throws Exception {
0940:                return currentServerConnection;
0941:            }
0942:
0943:            /**
0944:             * Close MBean Server connection
0945:             * @throws Exception if an error occurs
0946:             */
0947:            protected void closeMBeanServer() throws Exception {
0948:
0949:                try {
0950:                    if (connector != null) {
0951:                        connector.close();
0952:                    }
0953:                } catch (Exception e) {
0954:                    throw new Exception(
0955:                            "connection could not be close because of a communication problem.");
0956:                }
0957:            }
0958:
0959:            /**
0960:             * Create an orb for the client
0961:             *
0962:             */
0963:            private static void initORB() {
0964:                orb = ORB.init(new String[0], null);
0965:            }
0966:
0967:            /**
0968:             * Tear Down cleanUp action
0969:             * @throws Exception
0970:             */
0971:            public void tearDown() throws Exception {
0972:                // Close MBean Server Connection
0973:                closeMBeanServer();
0974:            }
0975:
0976:            /**
0977:             * Print a debug message
0978:             * @param msg The message
0979:             */
0980:            public void debug(String msg) {
0981:                System.out.println("DEBUG: " + msg);
0982:            }
0983:
0984:            protected ObjectName getJCAConnectionFactory(String name,
0985:                    String jcaResourceName) {
0986:                ObjectName on = J2eeObjectName.getJCAConnectionFactory(
0987:                        domainName, jcaResourceName, jonasName, name);
0988:                return on;
0989:            }
0990:
0991:            protected String getStringMBeanAttribute(ObjectName on,
0992:                    String attName) throws Exception {
0993:                MBeanServerConnection server = getMBeanServer();
0994:                return (String) server.getAttribute(on, attName);
0995:            }
0996:
0997:            protected Integer getIntegerMBeanAttribute(ObjectName on,
0998:                    String attName) throws Exception {
0999:                MBeanServerConnection server = getMBeanServer();
1000:                return (Integer) server.getAttribute(on, attName);
1001:            }
1002:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.