KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > management > F_Connectors


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

25
26 package org.objectweb.jonas.jtests.clients.management;
27
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URI JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import javax.management.MBeanServerConnection JavaDoc;
36 import javax.management.MalformedObjectNameException JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import javax.management.remote.JMXConnector JavaDoc;
39 import javax.management.remote.JMXConnectorFactory JavaDoc;
40 import javax.management.remote.JMXServiceURL JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42
43 import junit.framework.Test;
44 import junit.framework.TestSuite;
45
46 import org.objectweb.carol.util.configuration.ConfigurationRepository;
47 import org.objectweb.carol.util.configuration.ProtocolConfiguration;
48
49 import org.objectweb.jonas.jtests.util.JTestCase;
50
51 /**
52 * JMX Service test suite
53 *
54 * @author Adriana Danes
55 *
56 */

57
58 public class F_Connectors extends JTestCase {
59
60     /**
61      * Constructor
62      */

63     public F_Connectors(String JavaDoc name) {
64         super(name);
65     }
66
67     /**
68      * init environment
69      */

70     protected void setUp() {
71         super.setUp();
72     }
73
74     /**
75      * This suite is all test cases
76      */

77     public static Test suite() {
78         return new TestSuite(F_Connectors.class);
79     }
80
81     public static void main (String JavaDoc args[]) {
82         String JavaDoc testtorun = null;
83         // Get args
84
for (int argn = 0; argn < args.length; argn++) {
85             String JavaDoc s_arg = args[argn];
86
87             if (s_arg.equals("-n")) {
88                 testtorun = args[++argn];
89             }
90         }
91         if (testtorun == null) {
92             junit.textui.TestRunner.run(suite());
93         } else {
94             junit.textui.TestRunner.run(new F_Connectors(testtorun));
95         }
96     }
97
98     public void testConnectJmxRemote() throws Exception JavaDoc {
99         // Prepare ObjectName for J2EEServer MBean
100
// This is a well known MBeans which should be registered in the MBean Server
101
String JavaDoc sOn = jonasName + ":j2eeType=J2EEServer,name=" + jonasName;
102         ObjectName JavaDoc on = null;
103         try {
104             on = ObjectName.getInstance(sOn);
105         } catch (MalformedObjectNameException JavaDoc e) {
106             // Can't test the connection if no ObjectName
107
fail("Can't create ObjectName for J2EEServer MBean using String: " + sOn);
108         }
109
110         // Determine protocols used by Carol and their configuration
111
ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository.getConfigurations();
112         Properties JavaDoc myCarolConfig = new Properties JavaDoc();
113         for (int c = 0; c < protocolConfigurations.length; c++) {
114             String JavaDoc protocol = protocolConfigurations[c].getName();
115             //if (!protocol.equals("cmi")) {
116
myCarolConfig.setProperty(protocol, protocolConfigurations[c].getProviderURL());
117             //}
118
}
119
120         int nbProtocols = myCarolConfig.size();
121         if (nbProtocols == 0) {
122             fail("Can't find any protocol in Carol configuration");
123         }
124         boolean foundJ2EEServerMBean = true;
125         for (Iterator JavaDoc it = myCarolConfig.keySet().iterator(); it.hasNext();) {
126             String JavaDoc carolProtocol = (String JavaDoc) it.next();
127             String JavaDoc sCarolURL = (String JavaDoc) myCarolConfig.get(carolProtocol);
128             URI JavaDoc carolURL = new URI JavaDoc(sCarolURL);
129             int portNb = carolURL.getPort();
130             String JavaDoc port = String.valueOf(portNb);
131             String JavaDoc url = null;
132             Map JavaDoc env = null;
133             if (carolProtocol.equals("jrmp")) {
134                 // Treat JRMP case
135
url = "service:jmx:rmi:///jndi/rmi://localhost:" + port + "/jrmpconnector_" + jonasName;
136             } else if (carolProtocol.equals("iiop")) {
137                 // Treat IIOP case
138
url = "service:jmx:iiop:///jndi/iiop://localhost:" + port + "/iiopconnector_" + jonasName;
139                 env = new HashMap JavaDoc();
140                 env.put("java.naming.corba.orb", new InitialContext JavaDoc().lookup("java:comp/ORB"));
141             } else if (carolProtocol.equals("jeremie")) {
142                 // Treat JEREMIE case
143
url = "service:jmx:rmi:///jndi/jrmi://localhost:" + port + "/jeremieconnector_" + jonasName;
144             } else if (carolProtocol.equals("cmi")) {
145                 // Treat CMI
146
url = "service:jmx:rmi:///jndi/cmi://localhost:" + port + "/cmiconnector_" + jonasName;
147             } else {
148                 continue;
149             }
150             //System.out.println("===Use URL=== " + url);
151
// Try to connect to the MBeanServer
152
JMXServiceURL JavaDoc connURL = null;
153             try {
154                 connURL = new JMXServiceURL JavaDoc(url);
155             } catch (MalformedURLException JavaDoc e) {
156                 fail("Can't create JMXServiceURL with string: " + url);
157             }
158             JMXConnector JavaDoc connector = null;
159             try {
160                 connector = JMXConnectorFactory.newJMXConnector(connURL, env);
161             } catch (MalformedURLException JavaDoc e1) {
162                 fail("there is no provider for the protocol in " + url);
163             } catch (java.io.IOException JavaDoc e) {
164                 fail("Connector client cannot be made because of a communication problem (used URL: " + url + ")");
165             }
166             MBeanServerConnection JavaDoc currentServerConnection = null;
167             try {
168                 connector.connect(env);
169                 currentServerConnection = connector.getMBeanServerConnection();
170             } catch (java.io.IOException JavaDoc ioe) {
171                 fail("connection could not be made because of a communication problem");
172             }
173             // Look up J2EEServer MBean in the MBeanServer
174
try {
175                 foundJ2EEServerMBean = currentServerConnection.isRegistered(on);
176             } catch (java.io.IOException JavaDoc ioe) {
177                 fail("Connected, via " + carolProtocol + ", but can't find MBean " + on.toString() + " in the MBeanServer");
178             }
179
180         }
181         assertTrue(foundJ2EEServerMBean);
182     }
183 }
Popular Tags