1 package com.tc.admin; 2 3 import com.tc.util.event.EventMulticaster; 4 import com.tc.util.event.UpdateEventListener; 5 6 import java.io.IOException ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 10 import javax.management.ListenerNotFoundException ; 11 import javax.management.MBeanServerConnection ; 12 import javax.management.NotificationFilter ; 13 import javax.management.NotificationListener ; 14 import javax.management.remote.JMXConnector ; 15 import javax.management.remote.JMXConnectorFactory ; 16 import javax.management.remote.JMXServiceURL ; 17 import javax.security.auth.Subject ; 18 19 22 public final class AuthenticatingJMXConnector implements JMXConnector { 23 24 private final Map m_env; 25 private final Map m_authEnv; 26 private final JMXServiceURL m_url; private Map m_conEnv; 28 private JMXConnector m_connector; 29 private final EventMulticaster m_authObserver; 30 private final EventMulticaster m_collapseObserver; 31 private final EventMulticaster m_exceptionObserver; 32 private boolean m_authenticating; 33 private boolean m_securityEnabled; 34 private final Object m_error_lock; 35 private Exception m_error; 36 37 public AuthenticatingJMXConnector(JMXServiceURL url, Map env) throws IOException { 38 if (false) throw new IOException (); (this.m_env = new HashMap ()).putAll(env); 40 (this.m_authEnv = new HashMap ()).putAll(env); 41 this.m_url = url; 42 this.m_error_lock = new Object (); 43 this.m_authObserver = new EventMulticaster(); 44 this.m_collapseObserver = new EventMulticaster(); 45 this.m_exceptionObserver = new EventMulticaster(); 46 } 47 48 private synchronized JMXConnector getConnector() { 49 return m_connector; 50 } 51 52 private synchronized void setConnector(JMXConnector connector) { 53 this.m_connector = connector; 54 } 55 56 public void addAuthenticationListener(UpdateEventListener listener) { 57 m_authObserver.addListener(listener); 58 } 59 60 public void addCollapseListener(UpdateEventListener listener) { 61 this.m_collapseObserver.addListener(listener); 62 } 63 64 public void addExceptionListener(UpdateEventListener listener) { 65 this.m_exceptionObserver.addListener(listener); 66 } 67 68 public void addConnectionNotificationListener(NotificationListener arg0, NotificationFilter arg1, Object arg2) { 69 getConnector().addConnectionNotificationListener(arg0, arg1, arg2); 70 } 71 72 public void close() throws IOException { 73 getConnector().close(); 74 } 75 76 public void connect() throws IOException { 77 getConnector().connect(); 78 } 79 80 public synchronized void connect(Map conEnv) throws IOException { 81 try { 82 m_conEnv = conEnv; 83 setConnector(JMXConnectorFactory.newJMXConnector(m_url, m_env)); 84 getConnector().connect(m_conEnv); 85 } catch (RuntimeException e) { 86 if (e instanceof SecurityException ) { 87 m_securityEnabled = true; 88 try { 89 Thread.sleep(500); 90 } catch (InterruptedException ie) { 91 m_collapseObserver.fireUpdateEvent(); 92 return; 93 } 94 m_authenticating = true; 95 m_authObserver.fireUpdateEvent(); 96 try { 97 while (m_authenticating) 98 wait(); 99 } catch (InterruptedException ie) { 100 m_exceptionObserver.fireUpdateEvent(); 101 return; 102 } 103 } else { 104 throw e; 105 } 106 } catch (IOException e) { 107 m_exceptionObserver.fireUpdateEvent(); 108 throw e; 109 } 110 throwExceptions(); 111 } 112 113 private void throwExceptions() throws IOException { 114 synchronized (m_error_lock) { 115 if (m_error != null) { 116 m_exceptionObserver.fireUpdateEvent(); 117 if (m_error instanceof IOException ) throw (IOException ) m_error; 118 else if (m_error instanceof RuntimeException ) throw (RuntimeException ) m_error; 119 } 120 } 121 if (m_securityEnabled) throw new AuthenticationException(); 122 } 123 124 public synchronized void handleOkClick(String username, String password) { 125 m_authEnv.put("jmx.remote.credentials", new String [] { username, password }); 126 try { 127 m_collapseObserver.fireUpdateEvent(); 128 setConnector(JMXConnectorFactory.newJMXConnector(m_url, m_authEnv)); 129 getConnector().connect(m_conEnv); 130 } catch (Exception e) { 131 synchronized (m_error_lock) { 132 m_error = e; 133 } 134 } 135 m_authenticating = false; 136 notifyAll(); 137 } 138 139 public String getConnectionId() throws IOException { 140 return getConnector().getConnectionId(); 141 } 142 143 public MBeanServerConnection getMBeanServerConnection() throws IOException { 144 return getConnector().getMBeanServerConnection(); 145 } 146 147 public MBeanServerConnection getMBeanServerConnection(Subject arg0) throws IOException { 148 return getConnector().getMBeanServerConnection(arg0); 149 } 150 151 public void removeConnectionNotificationListener(NotificationListener arg0, NotificationFilter arg1, Object arg2) 152 throws ListenerNotFoundException { 153 getConnector().removeConnectionNotificationListener(arg0, arg1, arg2); 154 } 155 156 public void removeConnectionNotificationListener(NotificationListener arg0) throws ListenerNotFoundException { 157 getConnector().removeConnectionNotificationListener(arg0); 158 } 159 160 162 public static class AuthenticationException extends RuntimeException { 163 public AuthenticationException() { 164 super(); 165 } 166 } 167 } 168 | Popular Tags |