FileDocCategorySizeDatePackage
UrlConnector.javaAPI DocGlassfish v2 API12944Fri May 04 22:36:22 BST 2007com.sun.enterprise.admin.jmx.remote

UrlConnector

public abstract class UrlConnector extends Object implements JMXConnector
Abstract class that implements the JMXConnector connected to a URL. It maintains a state and the state transitions are defined. It is important to know that it handles a specific protocol which is based on Java Serialization . Both clients and servers are exchanging following:
  • Instances of MBeanServerRequestMessage {@link javax.management.remote.message.MBeanServerRequestMessage}. This object encapsulates all the objects that clients send.
  • Instance of MBeanServerResponseMessage {@link javax.management.remote.message.MBeanServerRequestMessage}. This object returns the response from MBeanServer invocation.
serialVersionUIDs are defined in both these classes. Both client and server sides have to agree on the same versions of classes whose objects are transmitted. Note that a concrete implementation of this class has to provide the actual wire transport, e.g. Http.
author
Kedar Mhaswade
since
S1AS8.0
version
1.0

Fields Summary
private static final Logger
logger
protected final JMXServiceURL
serviceUrl
protected final Map
environment
protected final URL
connectionUrl
private MBeanServerConnection
mbsc
private int
state
private final Object
stateLock
private final NotificationBroadcasterSupport
connectionNotifier
private static final int
CREATED
private static final int
CONNECTED
private static final int
CLOSED
private static final String
PROTOCOL_PREFIX
private final String
NULL_STR_MESSAGE
Constructors Summary
protected UrlConnector(JMXServiceURL serviceUrl, Map environment)
The only constructor that initialzes the connector for a client to use.

param
serviceUrl specifies the JMXServiceURL which the server exposes.
param
environment specifies the Map containing name-value pairs. The connector should be in the CREATED state after constructor returns. It has to be in CONNECTED state before calling any MBeanServerConnection method on it.

    
                                                                            
         
        logMap(environment);
        this.serviceUrl		= serviceUrl;
        this.environment	= environment;
        validateJmxServiceUrl();
        validateEnvironment();
        this.connectionUrl	= serviceUrl2Url(serviceUrl);
        changeState(CREATED);
        connectionNotifier	= new NotificationBroadcasterSupport();
        logger.fine("Connector created to the url: " + this.connectionUrl);
    
Methods Summary
public voidaddConnectionNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)

        connectionNotifier.addNotificationListener(listener, filter, handback);
    
private voidassertState(int legalState, java.lang.String message)

        synchronized(stateLock) {
            if (state != legalState) {
                throw new IllegalStateException(message);
            }
        }
    
private voidassertStateNot(int illegalState, java.lang.String message)

        synchronized(stateLock) {
            if (state == illegalState) {
                throw new IllegalStateException(message);
            }
        }
    
private voidchangeState(int toState)

                                /* Since states are not directly changeable by the end-user, it needs
                                   to be asserted if there is any program error */
        assert	(toState == CREATED || toState == CONNECTED || toState == CLOSED):
            ("This is illegal state transition, to: " + toState);
            synchronized(stateLock) {
                state = toState;
            }
    
public voidclose()
Closes the connector and underlying connection to the server, if any.

throws
IOException if there is an error in closing the connection

        final String message = "UrlConnector.close: Requires that connector is CONNECTED";
        try {
            assertState(CONNECTED, message);
            //physicalConnection.close();
            /* should be actually closed, when I can take care of persistent connections etc. 
             as of now, it is a no-op. */
	    ClientNotificationManager notifMgr =
                ((RemoteMBeanServerConnection)mbsc).getNotificationManager();
            if (notifMgr != null)
                notifMgr.close();

        }
        catch(Exception e) {
            throw new IOException(e.getMessage());
        }
    
public voidconnect()
Connects to the remote server. Since there are no defaults, this method as of now throws an UnsupportedException.

throws
UnsupportedException
throws
IOException if could not be connected
see
#connect(Map)

        final String msg = "Environment has to be provided";
        throw new UnsupportedOperationException(msg);
    
public voidconnect(java.util.Map env)
Attempts to connect to the remote server and creates the MBeanServerConnection instance that is used by clients. Returns immediately (does nothing), if connector is already in CONNECTED state. Sun ONE implementation requires that provided Map contains documented values.

param
env a Map containing supported environment.
throws
IOException if the connection could not be established.

        final String message = "UrlConnector.connect: Requires that connector is not CLOSED";
        assertStateNot(CLOSED, message);
        if (connected()) {
            return;
        }
        try {
            mbsc = MBeanServerConnectionFactory.getRemoteMBeanServerConnection(environment, serviceUrl);
            changeState(CONNECTED);
        }
        catch (Exception e) {
	    e.printStackTrace();
            throw new IOException(e.getMessage());
        }
    
private booleanconnected()

        synchronized(stateLock) {
            return ( state == CONNECTED );
        }
    
public java.lang.StringgetConnectionId()
Retunrs the connection-id of the connection.

        return "TODO";
        //return (physicalConnection.getConnectionId());
    
public javax.management.MBeanServerConnectiongetMBeanServerConnection()

        final String message = "Connector should be in CONNECTED state";
        assertState(CONNECTED, message);
        return ( mbsc );
    
public javax.management.MBeanServerConnectiongetMBeanServerConnection(javax.security.auth.Subject delegationSubject)

        
        return ( null );
    
private java.lang.StringgetTransport(java.lang.String proprietoryProtocolString)

        return proprietoryProtocolString.substring(PROTOCOL_PREFIX.length());
    
private voidlogMap(java.util.Map env)

        final Iterator iter = env.keySet().iterator();
        while (iter.hasNext()) {
            final String key = (String) iter.next();
            final String str = (env.get(key) == null)?null:env.get(key).toString();
            logger.fine(str);
        }
    
public voidremoveConnectionNotificationListener(javax.management.NotificationListener listener)

    
public voidremoveConnectionNotificationListener(javax.management.NotificationListener l, javax.management.NotificationFilter f, java.lang.Object handback)

    
protected java.net.URLserviceUrl2Url(javax.management.remote.JMXServiceURL serviceUrl)
Utility method. If the passed serviceUrl is valid, it should always create a valid URL.

        try {
            final String transportProtocol	= getTransport(serviceUrl.getProtocol());
            final String host			= serviceUrl.getHost();
            final int port			= serviceUrl.getPort();
            
/* BEGIN -- S1WS_MOD */
            String remainder = serviceUrl.getURLPath();
            if (remainder == null || remainder.trim().length() == 0)
                remainder = DefaultConfiguration.DEFAULT_SERVLET_CONTEXT_ROOT;
/* END -- S1WS_MOD */
            return ( new URL(transportProtocol, host, port, remainder) );
        }
        catch (MalformedURLException mu) {
            throw new RuntimeException(mu.getMessage());
        }
    
protected voidvalidateEnvironment()

        final boolean userPresent	= environment.containsKey(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME);
        final boolean pwdPresent	= environment.containsKey(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME);
        logger.fine("USERPRESENT: " + userPresent);
        logger.fine("PWDPRESENT: " + pwdPresent);
        if (! (userPresent && pwdPresent) ) {
            throw new IllegalArgumentException("User and Password has to be there in the map");
        }
        final String adminUser = (String)
        environment.get(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME);
        final String adminPassword = (String)
        environment.get(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME);
        //validateString(adminUser);
        //validateString(adminPassword);
    
protected abstract voidvalidateJmxServiceUrl()

private voidvalidateString(java.lang.String str)

        //This may not be required -- username/password could be empty strings.
        if (str == null || str.length() == 0) {
            throw new RuntimeException(NULL_STR_MESSAGE);
        }