org/objectweb/proactive/jmx/server/ProActiveConnector.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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      * creates a ProActive Connector
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 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1