001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: F_Connectors.java 6817 2005-05-24 14:49:19Z danesa $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.management;
027:
028: import java.net.MalformedURLException;
029: import java.net.URI;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Properties;
034:
035: import javax.management.MBeanServerConnection;
036: import javax.management.MalformedObjectNameException;
037: import javax.management.ObjectName;
038: import javax.management.remote.JMXConnector;
039: import javax.management.remote.JMXConnectorFactory;
040: import javax.management.remote.JMXServiceURL;
041: import javax.naming.InitialContext;
042:
043: import junit.framework.Test;
044: import junit.framework.TestSuite;
045:
046: import org.objectweb.carol.util.configuration.ConfigurationRepository;
047: import org.objectweb.carol.util.configuration.ProtocolConfiguration;
048:
049: import org.objectweb.jonas.jtests.util.JTestCase;
050:
051: /**
052: * JMX Service test suite
053: *
054: * @author Adriana Danes
055: *
056: */
057:
058: public class F_Connectors extends JTestCase {
059:
060: /**
061: * Constructor
062: */
063: public F_Connectors(String name) {
064: super (name);
065: }
066:
067: /**
068: * init environment
069: */
070: protected void setUp() {
071: super .setUp();
072: }
073:
074: /**
075: * This suite is all test cases
076: */
077: public static Test suite() {
078: return new TestSuite(F_Connectors.class);
079: }
080:
081: public static void main(String args[]) {
082: String testtorun = null;
083: // Get args
084: for (int argn = 0; argn < args.length; argn++) {
085: String s_arg = args[argn];
086:
087: if (s_arg.equals("-n")) {
088: testtorun = args[++argn];
089: }
090: }
091: if (testtorun == null) {
092: junit.textui.TestRunner.run(suite());
093: } else {
094: junit.textui.TestRunner.run(new F_Connectors(testtorun));
095: }
096: }
097:
098: public void testConnectJmxRemote() throws Exception {
099: // Prepare ObjectName for J2EEServer MBean
100: // This is a well known MBeans which should be registered in the MBean Server
101: String sOn = jonasName + ":j2eeType=J2EEServer,name="
102: + jonasName;
103: ObjectName on = null;
104: try {
105: on = ObjectName.getInstance(sOn);
106: } catch (MalformedObjectNameException e) {
107: // Can't test the connection if no ObjectName
108: fail("Can't create ObjectName for J2EEServer MBean using String: "
109: + sOn);
110: }
111:
112: // Determine protocols used by Carol and their configuration
113: ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository
114: .getConfigurations();
115: Properties myCarolConfig = new Properties();
116: for (int c = 0; c < protocolConfigurations.length; c++) {
117: String protocol = protocolConfigurations[c].getName();
118: //if (!protocol.equals("cmi")) {
119: myCarolConfig.setProperty(protocol,
120: protocolConfigurations[c].getProviderURL());
121: //}
122: }
123:
124: int nbProtocols = myCarolConfig.size();
125: if (nbProtocols == 0) {
126: fail("Can't find any protocol in Carol configuration");
127: }
128: boolean foundJ2EEServerMBean = true;
129: for (Iterator it = myCarolConfig.keySet().iterator(); it
130: .hasNext();) {
131: String carolProtocol = (String) it.next();
132: String sCarolURL = (String) myCarolConfig
133: .get(carolProtocol);
134: URI carolURL = new URI(sCarolURL);
135: int portNb = carolURL.getPort();
136: String port = String.valueOf(portNb);
137: String url = null;
138: Map env = null;
139: if (carolProtocol.equals("jrmp")) {
140: // Treat JRMP case
141: url = "service:jmx:rmi:///jndi/rmi://localhost:" + port
142: + "/jrmpconnector_" + jonasName;
143: } else if (carolProtocol.equals("iiop")) {
144: // Treat IIOP case
145: url = "service:jmx:iiop:///jndi/iiop://localhost:"
146: + port + "/iiopconnector_" + jonasName;
147: env = new HashMap();
148: env.put("java.naming.corba.orb", new InitialContext()
149: .lookup("java:comp/ORB"));
150: } else if (carolProtocol.equals("jeremie")) {
151: // Treat JEREMIE case
152: url = "service:jmx:rmi:///jndi/jrmi://localhost:"
153: + port + "/jeremieconnector_" + jonasName;
154: } else if (carolProtocol.equals("cmi")) {
155: // Treat CMI
156: url = "service:jmx:rmi:///jndi/cmi://localhost:" + port
157: + "/cmiconnector_" + jonasName;
158: } else {
159: continue;
160: }
161: //System.out.println("===Use URL=== " + url);
162: // Try to connect to the MBeanServer
163: JMXServiceURL connURL = null;
164: try {
165: connURL = new JMXServiceURL(url);
166: } catch (MalformedURLException e) {
167: fail("Can't create JMXServiceURL with string: " + url);
168: }
169: JMXConnector connector = null;
170: try {
171: connector = JMXConnectorFactory.newJMXConnector(
172: connURL, env);
173: } catch (MalformedURLException e1) {
174: fail("there is no provider for the protocol in " + url);
175: } catch (java.io.IOException e) {
176: fail("Connector client cannot be made because of a communication problem (used URL: "
177: + url + ")");
178: }
179: MBeanServerConnection currentServerConnection = null;
180: try {
181: connector.connect(env);
182: currentServerConnection = connector
183: .getMBeanServerConnection();
184: } catch (java.io.IOException ioe) {
185: fail("connection could not be made because of a communication problem");
186: }
187: // Look up J2EEServer MBean in the MBeanServer
188: try {
189: foundJ2EEServerMBean = currentServerConnection
190: .isRegistered(on);
191: } catch (java.io.IOException ioe) {
192: fail("Connected, via " + carolProtocol
193: + ", but can't find MBean " + on.toString()
194: + " in the MBeanServer");
195: }
196:
197: }
198: assertTrue(foundJ2EEServerMBean);
199: }
200: }
|