00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.jmx.server;
00032
00033 import java.io.IOException;
00034 import java.io.Serializable;
00035 import java.util.Enumeration;
00036 import java.util.Map;
00037 import java.util.Vector;
00038
00039 import javax.management.ListenerNotFoundException;
00040 import javax.management.MBeanServerConnection;
00041 import javax.management.Notification;
00042 import javax.management.NotificationFilter;
00043 import javax.management.NotificationListener;
00044 import javax.management.remote.JMXConnector;
00045 import javax.management.remote.JMXServiceURL;
00046 import javax.security.auth.Subject;
00047
00048 import org.objectweb.proactive.ActiveObjectCreationException;
00049 import org.objectweb.proactive.ProActive;
00050 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00051 import org.objectweb.proactive.jmx.ProActiveConnection;
00052 import org.objectweb.proactive.jmx.ProActiveJMXConstants;
00053 import org.objectweb.proactive.jmx.listeners.ProActiveConnectionNotificationEmitter;
00054
00055
00061 public class ProActiveConnector implements JMXConnector, Serializable, NotificationListener {
00062 private static final long serialVersionUID = -4295401093312884914L;
00063 private static final int CLOSED = 0;
00064 private static final int OPEN = 1;
00065 private ProActiveConnection connection;
00066 private ProActiveServerImpl paServer;
00067 private JMXServiceURL jmxServiceURL;
00068 private transient ProActiveConnectionNotificationEmitter emitter;
00069 private Map env;
00070 private int state = CLOSED;
00071
00072 static {
00073 ProActiveConfiguration.load();
00074 }
00075
00080 public ProActiveConnector() {
00081 }
00082
00083
00084
00085
00086 private ProActiveConnector(ProActiveServerImpl paServer,
00087 JMXServiceURL address, Map environment) {
00088 if ((paServer == null) && (address == null)) {
00089 throw new IllegalArgumentException(
00090 "proactive server jmxServiceURL both null");
00091 }
00092 this.emitter = new ProActiveConnectionNotificationEmitter(this);
00093 this.paServer = paServer;
00094 this.jmxServiceURL = address;
00095 this.env = environment;
00096 }
00097
00103 public ProActiveConnector(JMXServiceURL url, Map environment) {
00104 this(null, url, environment);
00105 }
00106
00110 public void connect() throws IOException {
00111 connect(null);
00112 }
00113
00117 public void connect(Map arg0) throws IOException {
00118 try {
00119 String hostname = this.jmxServiceURL.getHost();
00120 int port = this.jmxServiceURL.getPort();
00121
00122 String protocol = System.getProperty(
00123 "proactive.communication.protocol");
00124 String lookupUrl = protocol + "://" + hostname + ":" + port +
00125 ProActiveJMXConstants.SERVER_REGISTERED_NAME;
00126 ProActiveServerImpl paServer = (ProActiveServerImpl) ProActive.lookupActive(ProActiveServerImpl.class.getName(),
00127 lookupUrl);
00128
00129 this.connection = paServer.newClient();
00130 } catch (ActiveObjectCreationException e) {
00131 e.printStackTrace();
00132 this.emitter.sendConnectionNotificationFailed();
00133 throw new IOException(e.getMessage());
00134 } catch (IOException e) {
00135 this.emitter.sendConnectionNotificationFailed();
00136 e.printStackTrace();
00137 throw new IOException(e.getMessage());
00138 }
00139 this.state = OPEN;
00140 emitter.sendConnectionNotificationOpened();
00141 }
00142
00146 public synchronized MBeanServerConnection getMBeanServerConnection()
00147 throws IOException {
00148 return getMBeanServerConnection(null);
00149 }
00150
00154 public synchronized MBeanServerConnection getMBeanServerConnection(
00155 Subject delegationSubject) throws IOException {
00156 return connection;
00157 }
00158
00162 public void close() throws IOException {
00163 this.state = CLOSED;
00164 emitter.sendConnectionNotificationClosed();
00165 }
00166
00170 public void addConnectionNotificationListener(
00171 NotificationListener listener, NotificationFilter filter,
00172 Object handback) {
00173 this.listeners.addElement(listener);
00174 this.emitter.addNotificationListener(this, filter, handback);
00175 }
00176
00180 public void removeConnectionNotificationListener(
00181 NotificationListener listener) throws ListenerNotFoundException {
00182 this.emitter.removeNotificationListener(listener);
00183 }
00184
00188 public void removeConnectionNotificationListener(
00189 NotificationListener listener, NotificationFilter filter,
00190 Object handback) throws ListenerNotFoundException {
00191 this.emitter.removeNotificationListener(listener, filter, handback);
00192 }
00193
00197 public String getConnectionId() throws IOException {
00198 return "" + this.hashCode();
00199 }
00200
00201 private Vector <NotificationListener>listeners = new Vector <NotificationListener>();
00202
00203 public void handleNotification(Notification notification, Object handback) {
00204 Enumeration<NotificationListener> e= listeners.elements();
00205 while(e.hasMoreElements()) {
00206 e.nextElement().handleNotification(notification,handback);
00207 }
00208 }
00209
00210
00211 }