1 23 24 29 package com.sun.enterprise.admin.jmx.remote; 30 31 import java.util.Collections ; 32 import java.util.Map ; 33 import java.util.Iterator ; 34 import java.net.URL ; 35 import java.net.MalformedURLException ; 36 import java.io.IOException ; 37 import javax.security.auth.Subject ; 38 import java.util.logging.Logger ; 39 40 import javax.management.NotificationBroadcasterSupport ; 41 import javax.management.NotificationListener ; 42 import javax.management.NotificationFilter ; 43 import javax.management.MBeanServerConnection ; 44 import javax.management.ListenerNotFoundException ; 45 46 import javax.management.remote.JMXServiceURL ; 47 import javax.management.remote.JMXConnector ; 48 49 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 50 import com.sun.enterprise.admin.jmx.remote.internal.RemoteMBeanServerConnection; 51 import com.sun.enterprise.admin.jmx.remote.notification.ClientNotificationManager; 52 53 54 71 72 public abstract class UrlConnector implements JMXConnector { 73 74 private static final Logger logger = Logger.getLogger( 75 DefaultConfiguration.JMXCONNECTOR_LOGGER); 77 78 protected final JMXServiceURL serviceUrl; 79 protected final Map environment; 80 protected final URL connectionUrl; 81 82 private MBeanServerConnection mbsc; 83 private int state; 84 private final Object stateLock = new Object (); 85 private final NotificationBroadcasterSupport connectionNotifier; 86 87 private static final int CREATED = 1; 88 private static final int CONNECTED = 2; 89 private static final int CLOSED = 3; 90 91 92 private static final String PROTOCOL_PREFIX = "s1as"; 93 94 101 protected UrlConnector(JMXServiceURL serviceUrl, Map environment) { 102 logMap(environment); 104 this.serviceUrl = serviceUrl; 105 this.environment = environment; 106 validateJmxServiceUrl(); 107 validateEnvironment(); 108 this.connectionUrl = serviceUrl2Url(serviceUrl); 109 changeState(CREATED); 110 connectionNotifier = new NotificationBroadcasterSupport (); 111 logger.fine("Connector created to the url: " + this.connectionUrl); 112 } 113 114 private void logMap(Map env) { 115 final Iterator iter = env.keySet().iterator(); 116 while (iter.hasNext()) { 117 final String key = (String ) iter.next(); 118 final String str = (env.get(key) == null)?null:env.get(key).toString(); 119 logger.fine(str); 120 } 121 } 122 123 public void addConnectionNotificationListener(NotificationListener listener, 124 NotificationFilter filter, Object handback) { 125 connectionNotifier.addNotificationListener(listener, filter, handback); 126 } 127 128 131 public void close() throws IOException { 132 final String message = "UrlConnector.close: Requires that connector is CONNECTED"; 133 try { 134 assertState(CONNECTED, message); 135 138 ClientNotificationManager notifMgr = 139 ((RemoteMBeanServerConnection)mbsc).getNotificationManager(); 140 if (notifMgr != null) 141 notifMgr.close(); 142 143 } 144 catch(Exception e) { 145 throw new IOException (e.getMessage()); 146 } 147 } 148 149 155 public void connect() throws IOException { 156 final String msg = "Environment has to be provided"; 157 throw new UnsupportedOperationException (msg); 158 } 159 160 166 public void connect(Map env) throws IOException { 167 final String message = "UrlConnector.connect: Requires that connector is not CLOSED"; 168 assertStateNot(CLOSED, message); 169 if (connected()) { 170 return; 171 } 172 try { 173 mbsc = MBeanServerConnectionFactory.getRemoteMBeanServerConnection(environment, serviceUrl); 174 changeState(CONNECTED); 175 } 176 catch (Exception e) { 177 e.printStackTrace(); 178 throw new IOException (e.getMessage()); 179 } 180 } 181 182 184 public String getConnectionId() throws IOException { 185 return "TODO"; 186 } 188 189 public MBeanServerConnection getMBeanServerConnection() throws IOException { 190 final String message = "Connector should be in CONNECTED state"; 191 assertState(CONNECTED, message); 192 return ( mbsc ); 193 } 194 195 public MBeanServerConnection getMBeanServerConnection(Subject delegationSubject) 196 throws IOException { 197 198 return ( null ); 199 } 200 201 public void removeConnectionNotificationListener(NotificationListener listener) 202 throws ListenerNotFoundException { 203 } 204 205 public void removeConnectionNotificationListener(NotificationListener l, 206 NotificationFilter f, Object handback) throws ListenerNotFoundException { 207 } 208 209 protected void validateEnvironment() throws RuntimeException { 210 final boolean userPresent = environment.containsKey(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME); 211 final boolean pwdPresent = environment.containsKey(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME); 212 logger.fine("USERPRESENT: " + userPresent); 213 logger.fine("PWDPRESENT: " + pwdPresent); 214 if (! (userPresent && pwdPresent) ) { 215 throw new IllegalArgumentException ("User and Password has to be there in the map"); 216 } 217 final String adminUser = (String ) 218 environment.get(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME); 219 final String adminPassword = (String ) 220 environment.get(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME); 221 } 224 225 protected abstract void validateJmxServiceUrl() throws RuntimeException ; 226 227 private void validateString(String str) throws RuntimeException { 228 if (str == null || str.length() == 0) { 230 throw new RuntimeException (NULL_STR_MESSAGE); 231 } 232 } 233 236 protected URL serviceUrl2Url(JMXServiceURL serviceUrl) throws RuntimeException { 237 try { 238 final String transportProtocol = getTransport(serviceUrl.getProtocol()); 239 final String host = serviceUrl.getHost(); 240 final int port = serviceUrl.getPort(); 241 242 243 String remainder = serviceUrl.getURLPath(); 244 if (remainder == null || remainder.trim().length() == 0) 245 remainder = DefaultConfiguration.DEFAULT_SERVLET_CONTEXT_ROOT; 246 247 return ( new URL (transportProtocol, host, port, remainder) ); 248 } 249 catch (MalformedURLException mu) { 250 throw new RuntimeException (mu.getMessage()); 251 } 252 } 253 254 private String getTransport(String proprietoryProtocolString) { 255 return proprietoryProtocolString.substring(PROTOCOL_PREFIX.length()); 256 } 257 258 private void assertState(int legalState, String message) throws IllegalStateException { 259 synchronized(stateLock) { 260 if (state != legalState) { 261 throw new IllegalStateException (message); 262 } 263 } 264 } 265 private void assertStateNot(int illegalState, String message) throws IllegalStateException { 266 synchronized(stateLock) { 267 if (state == illegalState) { 268 throw new IllegalStateException (message); 269 } 270 } 271 } 272 273 private void changeState(int toState) { 274 276 assert (toState == CREATED || toState == CONNECTED || toState == CLOSED): 277 ("This is illegal state transition, to: " + toState); 278 synchronized(stateLock) { 279 state = toState; 280 } 281 } 282 283 private boolean connected() { 284 synchronized(stateLock) { 285 return ( state == CONNECTED ); 286 } 287 } 288 private final String NULL_STR_MESSAGE = "String is null"; 290 291 private void debuggerHook() { 292 try { 293 System.out.println("Attached the debugger? Provide an integer to go ahead..."); 295 final int r = System.in.read(); 296 } 297 catch(Exception e) {} 298 } 299 } 300 | Popular Tags |