1 25 26 package org.objectweb.jonas.jtests.clients.management; 27 28 import java.net.MalformedURLException ; 29 import java.net.URI ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 35 import javax.management.MBeanServerConnection ; 36 import javax.management.MalformedObjectNameException ; 37 import javax.management.ObjectName ; 38 import javax.management.remote.JMXConnector ; 39 import javax.management.remote.JMXConnectorFactory ; 40 import javax.management.remote.JMXServiceURL ; 41 import javax.naming.InitialContext ; 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 57 58 public class F_Connectors extends JTestCase { 59 60 63 public F_Connectors(String name) { 64 super(name); 65 } 66 67 70 protected void setUp() { 71 super.setUp(); 72 } 73 74 77 public static Test suite() { 78 return new TestSuite(F_Connectors.class); 79 } 80 81 public static void main (String args[]) { 82 String testtorun = null; 83 for (int argn = 0; argn < args.length; argn++) { 85 String 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 { 99 String sOn = jonasName + ":j2eeType=J2EEServer,name=" + jonasName; 102 ObjectName on = null; 103 try { 104 on = ObjectName.getInstance(sOn); 105 } catch (MalformedObjectNameException e) { 106 fail("Can't create ObjectName for J2EEServer MBean using String: " + sOn); 108 } 109 110 ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository.getConfigurations(); 112 Properties myCarolConfig = new Properties (); 113 for (int c = 0; c < protocolConfigurations.length; c++) { 114 String protocol = protocolConfigurations[c].getName(); 115 myCarolConfig.setProperty(protocol, protocolConfigurations[c].getProviderURL()); 117 } 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 it = myCarolConfig.keySet().iterator(); it.hasNext();) { 126 String carolProtocol = (String ) it.next(); 127 String sCarolURL = (String ) myCarolConfig.get(carolProtocol); 128 URI carolURL = new URI (sCarolURL); 129 int portNb = carolURL.getPort(); 130 String port = String.valueOf(portNb); 131 String url = null; 132 Map env = null; 133 if (carolProtocol.equals("jrmp")) { 134 url = "service:jmx:rmi:///jndi/rmi://localhost:" + port + "/jrmpconnector_" + jonasName; 136 } else if (carolProtocol.equals("iiop")) { 137 url = "service:jmx:iiop:///jndi/iiop://localhost:" + port + "/iiopconnector_" + jonasName; 139 env = new HashMap (); 140 env.put("java.naming.corba.orb", new InitialContext ().lookup("java:comp/ORB")); 141 } else if (carolProtocol.equals("jeremie")) { 142 url = "service:jmx:rmi:///jndi/jrmi://localhost:" + port + "/jeremieconnector_" + jonasName; 144 } else if (carolProtocol.equals("cmi")) { 145 url = "service:jmx:rmi:///jndi/cmi://localhost:" + port + "/cmiconnector_" + jonasName; 147 } else { 148 continue; 149 } 150 JMXServiceURL connURL = null; 153 try { 154 connURL = new JMXServiceURL (url); 155 } catch (MalformedURLException e) { 156 fail("Can't create JMXServiceURL with string: " + url); 157 } 158 JMXConnector connector = null; 159 try { 160 connector = JMXConnectorFactory.newJMXConnector(connURL, env); 161 } catch (MalformedURLException e1) { 162 fail("there is no provider for the protocol in " + url); 163 } catch (java.io.IOException e) { 164 fail("Connector client cannot be made because of a communication problem (used URL: " + url + ")"); 165 } 166 MBeanServerConnection currentServerConnection = null; 167 try { 168 connector.connect(env); 169 currentServerConnection = connector.getMBeanServerConnection(); 170 } catch (java.io.IOException ioe) { 171 fail("connection could not be made because of a communication problem"); 172 } 173 try { 175 foundJ2EEServerMBean = currentServerConnection.isRegistered(on); 176 } catch (java.io.IOException 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 |