1 8 9 package mx4j.tools.remote; 10 11 import java.io.IOException ; 12 import java.lang.ref.WeakReference ; 13 import java.security.AccessControlContext ; 14 import java.security.AccessController ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 19 import javax.management.remote.JMXAuthenticator ; 20 import javax.management.remote.JMXConnectorServer ; 21 import javax.security.auth.Subject ; 22 23 import mx4j.remote.MX4JRemoteUtils; 24 25 31 public abstract class AbstractConnectionManager implements ConnectionManager 32 { 33 private AbstractJMXConnectorServer server; 34 private final Map environment; 35 private final AccessControlContext context; 36 private final Map connections = new HashMap (); 37 private volatile boolean closed; 38 39 45 protected AbstractConnectionManager(AbstractJMXConnectorServer server, Map environment) 46 { 47 this.server = server; 48 this.environment = environment; 49 this.context = AccessController.getContext(); 50 } 51 52 protected void setJMXConnectorServer(AbstractJMXConnectorServer server) 53 { 54 this.server = server; 55 } 56 57 65 public synchronized Connection connect(Object credentials) throws IOException , SecurityException 66 { 67 if (isClosed()) throw new IOException ("This connection manager is already closed " + this); 68 69 Subject subject = authenticate(credentials); 70 String connectionId = createConnectionID(subject); 71 72 Connection client = doConnect(connectionId, subject); 73 WeakReference weak = new WeakReference (client); 74 75 synchronized (connections) 76 { 77 connections.put(connectionId, weak); 78 } 79 80 server.connectionOpened(connectionId, "Connection opened " + client, null); 81 82 return client; 83 } 84 85 90 protected String createConnectionID(Subject subject) 91 { 92 return MX4JRemoteUtils.createConnectionID(getProtocol(), null, -1, subject); 93 } 94 95 107 protected abstract Connection doConnect(String connectionId, Subject subject) throws IOException ; 108 109 115 public synchronized void close() throws IOException 116 { 117 if (isClosed()) return; 118 closed = true; 119 doClose(); 120 closeConnections(); 121 } 122 123 128 protected abstract void doClose() throws IOException ; 129 130 private void closeConnections() throws IOException 131 { 132 IOException clientException = null; 133 synchronized (connections) 134 { 135 while (!connections.isEmpty()) 136 { 137 Iterator entries = connections.entrySet().iterator(); 139 Map.Entry entry = (Map.Entry )entries.next(); 140 WeakReference weak = (WeakReference )entry.getValue(); 141 Connection connection = (Connection)weak.get(); 142 if (connection == null) 143 { 144 entries.remove(); 146 continue; 147 } 148 else 149 { 150 try 151 { 152 connection.close(); 153 } 154 catch (IOException x) 155 { 156 if (clientException == null) clientException = x; 157 } 158 } 159 } 160 } 161 if (clientException != null) throw clientException; 162 } 163 164 171 public void closeConnection(Connection connection) throws IOException 172 { 173 String connectionID = connection.getConnectionId(); 174 WeakReference weak = null; 175 synchronized (connections) 176 { 177 weak = (WeakReference )connections.remove(connectionID); 178 } 179 if (weak == null) return; 181 182 Connection client = (Connection)weak.get(); 183 if (connection != client) throw new IOException ("Could not find active connection " + connection + ", expecting " + client); 184 185 doCloseConnection(connection); 186 187 server.connectionClosed(connectionID, "Closed connection " + connection, null); 188 } 189 190 193 protected abstract void doCloseConnection(Connection connection) throws IOException ; 194 195 198 protected boolean isClosed() 199 { 200 return closed; 201 } 202 203 206 protected Map getEnvironment() 207 { 208 return environment; 209 } 210 211 216 protected AccessControlContext getSecurityContext() 217 { 218 return context; 219 } 220 221 225 protected Subject authenticate(Object credentials) throws IOException , SecurityException 226 { 227 Map environment = getEnvironment(); 228 if (environment != null) 229 { 230 JMXAuthenticator authenticator = (JMXAuthenticator )environment.get(JMXConnectorServer.AUTHENTICATOR); 231 if (authenticator != null) 232 { 233 return authenticator.authenticate(credentials); 234 } 235 } 236 return null; 237 } 238 } 239 | Popular Tags |