1 23 24 39 40 package com.sun.enterprise.admin.jmx.remote.server.rmi; 41 import javax.management.remote.JMXServiceURL ; 42 import java.net.MalformedURLException ; 43 import java.util.logging.Logger ; 44 45 50 51 public class JmxServiceUrlFactory { 52 53 public static final String RMI_JSR160_CS_JNDI_SUFFIX = "/management/rmi-jmx-connector"; 54 public static final String JCONSOLE_RMI_JSR160_CS_JNDI_SUFFIX = "/jmxrmi"; 55 private static final Logger logger = Logger.getLogger("javax.enterprise.system.tools.admin"); 57 private JmxServiceUrlFactory() { 58 } 59 60 75 public static JMXServiceURL forRmiJrmpWithoutStub(final int port) { 76 JMXServiceURL url; 77 try { 78 final String s = "service:jmx:rmi://" + localhost() + ":" + port; 79 url = new JMXServiceURL (s); 80 final String msg = "JmxServiceUrlFactory=>JMXServiceURL is: " + url; 81 logger.fine(msg); 82 } 83 catch (MalformedURLException m) { 84 url = null; 85 } 87 assert (url != null): "Something seriously wrong, can't form the JMXServiceURL"; 88 return ( url ); 89 } 90 91 106 107 public static JMXServiceURL forRmiIiopWithoutIor(final int port) { 108 JMXServiceURL url; 109 try { 110 final String s = "service:jmx:iiop://" + localhost() + ":" + port; 111 url = new JMXServiceURL (s); 112 final String msg = "JMXServiceURL is: " + url; 113 logger.fine(msg); 114 } 115 catch (MalformedURLException m) { 116 url = null; 117 } 119 assert (url != null): "Something seriously wrong, can't form the JMXServiceURL"; 120 return ( url ); 121 } 122 123 137 138 public static JMXServiceURL forRmiWithJndiInAppserver(final String host, final int port) { 139 return ( forRmiWithJndi(host, port, RMI_JSR160_CS_JNDI_SUFFIX, false) ); 140 } 141 142 public static JMXServiceURL forJconsoleOverRmiWithJndiInAppserver(final String host, final int port) { 143 return ( forRmiWithJndi(host, port, JCONSOLE_RMI_JSR160_CS_JNDI_SUFFIX, false) ); 144 } 145 146 161 private static JMXServiceURL forRmiWithJndi(final String host, final int port, final String jn, final boolean addHost) { 162 JMXServiceURL url; 163 String hps = ""; if (host == null || jn == null) { 165 throw new IllegalArgumentException ("Null Argument"); 166 } 167 if (! jn.startsWith("/")) 168 throw new IllegalArgumentException ("jndi-name must start with a /"); 169 if (addHost) { 170 hps = host + ":" + port; 171 } 172 try { 173 final String s = "service:jmx:rmi://" + hps + "/jndi/rmi://" + host + ":" + port + jn; 174 url = new JMXServiceURL (s); 175 final String msg = "JMXServiceURL is: " + url; 176 logger.fine(msg); 177 } 178 catch (MalformedURLException m) { 179 url = null; 180 } 182 assert (url != null): "Something seriously wrong, can't form the JMXServiceURL"; 183 return ( url ); 184 } 185 189 private static JMXServiceURL forRmiWithJndi(final int port, final String jn) { 190 return ( forRmiWithJndi(localhost(), port, jn, false) ); 191 } 192 193 197 private static JMXServiceURL forRmiWithJndi(final int port) { 198 return ( forRmiWithJndi(port, RMI_JSR160_CS_JNDI_SUFFIX) ); 199 } 200 203 public static JMXServiceURL forJmxmp(final String host, final int port) { 204 JMXServiceURL url; 205 if (host == null) 206 throw new IllegalArgumentException ("Null Host"); 207 try { 208 url = new JMXServiceURL ("jmxmp", host, port); 209 final String msg = "JMXServiceURL is: " + url; 210 logger.fine(msg); 211 } 212 catch (MalformedURLException m) { 213 url = null; 214 } 216 assert (url != null): "Something seriously wrong, can't form the JMXServiceURL"; 217 return ( url ); 218 } 219 220 public static JMXServiceURL forJmxmp(final int port) { 221 return ( forJmxmp(localhost(), port) ); 222 } 223 224 static String localhost() throws RuntimeException { 225 String h; 226 try { 227 h = java.net.InetAddress.getLocalHost().getCanonicalHostName(); 228 } 229 catch (java.net.UnknownHostException e) { 230 h = "localhost"; 231 } 232 return ( h ); 233 } 234 235 private static String concat(final String s1, final String s2, final String s3, final String s4) { 236 final StringBuffer sb = new StringBuffer (); 237 if (s1 != null) 238 sb.append(s1); 239 if (s2 != null) 240 sb.append(s2); 241 if (s3 != null) 242 sb.append(s3); 243 if (s4 != null) 244 sb.append(s4); 245 return ( sb.toString() ); 246 } 247 } | Popular Tags |