KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > AuthenticatingJMXConnector


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 JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.management.ListenerNotFoundException JavaDoc;
11 import javax.management.MBeanServerConnection JavaDoc;
12 import javax.management.NotificationFilter JavaDoc;
13 import javax.management.NotificationListener JavaDoc;
14 import javax.management.remote.JMXConnector JavaDoc;
15 import javax.management.remote.JMXConnectorFactory JavaDoc;
16 import javax.management.remote.JMXServiceURL JavaDoc;
17 import javax.security.auth.Subject JavaDoc;
18
19 /**
20  * Decorator for <tt>JMXConnector</tt>
21  */

22 public final class AuthenticatingJMXConnector implements JMXConnector JavaDoc {
23
24   private final Map JavaDoc m_env;
25   private final Map JavaDoc m_authEnv;
26   private final JMXServiceURL JavaDoc m_url; // @NOTThreadSafe
27
private Map JavaDoc m_conEnv;
28   private JMXConnector JavaDoc 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 JavaDoc m_error_lock;
35   private Exception JavaDoc m_error;
36
37   public AuthenticatingJMXConnector(JMXServiceURL JavaDoc url, Map JavaDoc env) throws IOException JavaDoc {
38     if (false) throw new IOException JavaDoc(); // quiet compiler
39
(this.m_env = new HashMap JavaDoc()).putAll(env);
40     (this.m_authEnv = new HashMap JavaDoc()).putAll(env);
41     this.m_url = url;
42     this.m_error_lock = new Object JavaDoc();
43     this.m_authObserver = new EventMulticaster();
44     this.m_collapseObserver = new EventMulticaster();
45     this.m_exceptionObserver = new EventMulticaster();
46   }
47
48   private synchronized JMXConnector JavaDoc getConnector() {
49     return m_connector;
50   }
51
52   private synchronized void setConnector(JMXConnector JavaDoc 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 JavaDoc arg0, NotificationFilter JavaDoc arg1, Object JavaDoc arg2) {
69     getConnector().addConnectionNotificationListener(arg0, arg1, arg2);
70   }
71
72   public void close() throws IOException JavaDoc {
73     getConnector().close();
74   }
75
76   public void connect() throws IOException JavaDoc {
77     getConnector().connect();
78   }
79
80   public synchronized void connect(Map JavaDoc conEnv) throws IOException JavaDoc {
81     try {
82       m_conEnv = conEnv;
83       setConnector(JMXConnectorFactory.newJMXConnector(m_url, m_env));
84       getConnector().connect(m_conEnv);
85     } catch (RuntimeException JavaDoc e) {
86       if (e instanceof SecurityException JavaDoc) {
87         m_securityEnabled = true;
88         try {
89           Thread.sleep(500);
90         } catch (InterruptedException JavaDoc 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 JavaDoc ie) {
100           m_exceptionObserver.fireUpdateEvent();
101           return;
102         }
103       } else {
104         throw e;
105       }
106     } catch (IOException JavaDoc e) {
107       m_exceptionObserver.fireUpdateEvent();
108       throw e;
109     }
110     throwExceptions();
111   }
112
113   private void throwExceptions() throws IOException JavaDoc {
114     synchronized (m_error_lock) {
115       if (m_error != null) {
116         m_exceptionObserver.fireUpdateEvent();
117         if (m_error instanceof IOException JavaDoc) throw (IOException JavaDoc) m_error;
118         else if (m_error instanceof RuntimeException JavaDoc) throw (RuntimeException JavaDoc) m_error;
119       }
120     }
121     if (m_securityEnabled) throw new AuthenticationException();
122   }
123
124   public synchronized void handleOkClick(String JavaDoc username, String JavaDoc password) {
125     m_authEnv.put("jmx.remote.credentials", new String JavaDoc[] { username, password });
126     try {
127       m_collapseObserver.fireUpdateEvent();
128       setConnector(JMXConnectorFactory.newJMXConnector(m_url, m_authEnv));
129       getConnector().connect(m_conEnv);
130     } catch (Exception JavaDoc e) {
131       synchronized (m_error_lock) {
132         m_error = e;
133       }
134     }
135     m_authenticating = false;
136     notifyAll();
137   }
138
139   public String JavaDoc getConnectionId() throws IOException JavaDoc {
140     return getConnector().getConnectionId();
141   }
142
143   public MBeanServerConnection JavaDoc getMBeanServerConnection() throws IOException JavaDoc {
144     return getConnector().getMBeanServerConnection();
145   }
146
147   public MBeanServerConnection JavaDoc getMBeanServerConnection(Subject JavaDoc arg0) throws IOException JavaDoc {
148     return getConnector().getMBeanServerConnection(arg0);
149   }
150
151   public void removeConnectionNotificationListener(NotificationListener JavaDoc arg0, NotificationFilter JavaDoc arg1, Object JavaDoc arg2)
152       throws ListenerNotFoundException JavaDoc {
153     getConnector().removeConnectionNotificationListener(arg0, arg1, arg2);
154   }
155
156   public void removeConnectionNotificationListener(NotificationListener JavaDoc arg0) throws ListenerNotFoundException JavaDoc {
157     getConnector().removeConnectionNotificationListener(arg0);
158   }
159
160   // --------------------------------------------------------------------------------
161

162   public static class AuthenticationException extends RuntimeException JavaDoc {
163     public AuthenticationException() {
164       super();
165     }
166   }
167 }
168
Popular Tags