User: andreas 
  Date: 00/10/12 19:42:53

  Modified:    src/main/org/jboss/jmx/client ConnectorFactoryImpl.java
  Log:
  Make list of servers and protocols in both ways.
  
  Revision  Changes    Path
  1.3       +122 -73   jboss/src/main/org/jboss/jmx/client/ConnectorFactoryImpl.java
  
  Index: ConnectorFactoryImpl.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/jmx/client/ConnectorFactoryImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ConnectorFactoryImpl.java 2000/10/11 06:04:03     1.2
  +++ ConnectorFactoryImpl.java 2000/10/13 02:42:53     1.3
  @@ -11,6 +11,7 @@
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.Set;
  +import java.util.StringTokenizer;
   import java.util.Vector;
   
   import javax.management.DynamicMBean;
  @@ -53,61 +54,56 @@
        /**
        * Returns a list of available servers
        *
  +     * @param pProtocol                      Servers supporting this protocol if 
not null
  +     *                                                       or empty otherwise it 
will be ignored
        * @param pServerQuery           Query instance to filter the list of servers
        *
        * @return                                       A collection of available 
servers
        *                                                       names/identifications 
(String)
        **/
        public Collection getServers(
  +             String pProtocol
   //AS         ServerQuery pServerQuery
        ) {
  +             // Check if there is a protocol given to query for
  +             boolean lProtocolQuery = pProtocol != null && pProtocol.length() > 0;
  +             // Get all available connectors from the JNDI server
  +             Iterator lConnectors = getConnectorList();
                Vector lServers = new Vector();
  -             try {
  -                     InitialContext lNamingServer = new InitialContext();
  -                     // Lookup the JNDI server
  -                     NamingEnumeration enum = lNamingServer.list( "" );
  -                     while( enum.hasMore() ) {
  -                             NameClassPair lItem = (NameClassPair) enum.next();
  -                             System.out.println( "Naming Server item: " + lItem );
  -                             String lName = lItem.getName();
  -                             if( lName.indexOf( "jmx:" ) == 0 ) {
  -                                     lServers.add(
  -                                             lName.substring(
  -                                                     4,
  -                                                     lName.indexOf( ":", 5 )
  -                                             )
  -                                     );
  -                             }
  +             // Go through the connectors list and check if part of the list
  +             while( lConnectors.hasNext() ) {
  +                     ConnectorObject lConnector = (ConnectorObject) 
lConnectors.next();
  +                     if( !lProtocolQuery || 
lConnector.getProtocol().equalsIgnoreCase( pProtocol ) ) {
  +                             lServers.add( lConnector.getServer() );
                        }
                }
  -             catch( Exception e ) {
  -                     e.printStackTrace();
  -             }
                return lServers;
  -/*
  -             return Arrays.asList(
  -                     new String[] {
  -                             System.getProperty( "java.naming.provider.url" )
  -                     }
  -             );
  -*/
        }
        
        /**
        * Returns a list of available protocols (connectors)
        *
  -     * @param pServer                        Server name/identification to look up
  +     * @param pServer                        Server name/identification to look up 
if not
  +     *                                                       null or empty 
otherwise it will be ignored
        *
        * @return                                       A collection of available 
protocols (String)
        **/
        public Collection getProtocols(
                String pServer
        ) {
  -             return Arrays.asList(
  -                     new String[] {
  -                             "rmi"
  +             // Check if there is a protocol given to query for
  +             boolean lServerQuery = pServer != null && pServer.length() > 0;
  +             // Get all available connectors from the JNDI server
  +             Iterator lConnectors = getConnectorList();
  +             Vector lProtocols = new Vector();
  +             // Go through the connectors list and check if part of the list
  +             while( lConnectors.hasNext() ) {
  +                     ConnectorObject lConnector = (ConnectorObject) 
lConnectors.next();
  +                     if( !lServerQuery || lConnector.getServer().equalsIgnoreCase( 
pServer ) ) {
  +                             lProtocols.add( lConnector.getProtocol() );
                        }
  -             );
  +             }
  +             return lProtocols;
        }
   
        /**
  @@ -125,22 +121,21 @@
                String pProtocol
        ) {
                JMXConnector lConnector = null;
  -//AS         if( pServer.equals( "localhost" ) ) {
  -                     if( pProtocol.equals( "rmi" ) ) {
  -                             try {
  -                                     lConnector = new RMIClientConnectorImpl(
  -                                             pServer
  -                                     );
  -                                     mServer.registerMBean(
  -                                             lConnector,
  -                                             new ObjectName( 
"DefaultDomain:name=RMIConnectorTo" + pServer )
  -                                     );
  -                             }
  -                             catch( Exception e ) {
  -                                     e.printStackTrace();
  -                             }
  +             // At the moment only RMI protocol is supported (on the client side)
  +             if( pProtocol.equals( "rmi" ) ) {
  +                     try {
  +                             lConnector = new RMIClientConnectorImpl(
  +                                     pServer
  +                             );
  +                             mServer.registerMBean(
  +                                     lConnector,
  +                                     new ObjectName( 
"DefaultDomain:name=RMIConnectorTo" + pServer )
  +                             );
  +                     }
  +                     catch( Exception e ) {
  +                             e.printStackTrace();
                        }
  -//AS         }
  +             }
                System.out.println( "ConnectorFactoryImpl.createConnection(), got 
connector: " + lConnector );
                return lConnector;
        }
  @@ -155,37 +150,91 @@
                String pServer,
                String pProtocol
        ) {
  -             if( pServer.equals( "localhost" ) ) {
  -                     if( pProtocol.equals( "rmi" ) ) {
  -                             try {
  -                                     Set lConnectors = mServer.queryMBeans(
  -                                             new ObjectName( 
"DefaultDomain:name=RMIConnectorTo" + pServer ),
  -                                             null
  -                                     );
  -                                     System.out.println( 
"ConnectorFactoryImpl.removeConnection(), got connectors: " + lConnectors );
  -                                     if( !lConnectors.isEmpty() ) {
  -                                             Iterator i = lConnectors.iterator();
  -                                             while( i.hasNext() ) {
  -                                                     ObjectInstance lConnector = 
(ObjectInstance) i.next();
  -                                                     mServer.invoke(
  -                                                             
lConnector.getObjectName(),
  -                                                             "stop",
  -                                                             new Object[] {},
  -                                                             new String[] {}
  -                                                     );
  -                                                     mServer.unregisterMBean(
  -                                                             
lConnector.getObjectName()
  -                                                     );
  -                                                     System.out.println( 
"ConnectorFactoryImpl.removeConnection(), " +
  -                                                             "unregister MBean: " + 
lConnector.getObjectName()
  -                                                     );
  -                                             }
  +             if( pProtocol.equals( "rmi" ) ) {
  +                     try {
  +                             Set lConnectors = mServer.queryMBeans(
  +                                     new ObjectName( 
"DefaultDomain:name=RMIConnectorTo" + pServer ),
  +                                     null
  +                             );
  +                             System.out.println( 
"ConnectorFactoryImpl.removeConnection(), got connectors: " + lConnectors );
  +                             if( !lConnectors.isEmpty() ) {
  +                                     Iterator i = lConnectors.iterator();
  +                                     while( i.hasNext() ) {
  +                                             ObjectInstance lConnector = 
(ObjectInstance) i.next();
  +                                             mServer.invoke(
  +                                                     lConnector.getObjectName(),
  +                                                     "stop",
  +                                                     new Object[] {},
  +                                                     new String[] {}
  +                                             );
  +                                             mServer.unregisterMBean(
  +                                                     lConnector.getObjectName()
  +                                             );
  +                                             System.out.println( 
"ConnectorFactoryImpl.removeConnection(), " +
  +                                                     "unregister MBean: " + 
lConnector.getObjectName()
  +                                             );
                                        }
                                }
  -                             catch( Exception e ) {
  -                                     e.printStackTrace();
  +                     }
  +                     catch( Exception e ) {
  +                             e.printStackTrace();
  +                     }
  +             }
  +     }
  +     
  +     private Iterator getConnectorList() {
  +             Vector lServers = new Vector();
  +             try {
  +                     InitialContext lNamingServer = new InitialContext();
  +                     // Lookup the JNDI server
  +                     NamingEnumeration enum = lNamingServer.list( "" );
  +                     while( enum.hasMore() ) {
  +                             NameClassPair lItem = (NameClassPair) enum.next();
  +                             System.out.println( "Naming Server item: " + lItem );
  +                             ConnectorObject lConnector = new ConnectorObject( 
lItem.getName() );
  +                             if( lConnector.isValid() ) {
  +                                     lServers.add( lConnector );
                                }
                        }
  +             }
  +             catch( Exception e ) {
  +                     e.printStackTrace();
  +             }
  +             
  +             return lServers.iterator();
  +     }
  +     /**
  +     * If valid then it will containg the informations about a
  +     * remote JMX Connector
  +     **/
  +     private class ConnectorObject {
  +             private String                          mServerName = "";
  +             private String                          mProtocolName = "";
  +             private boolean                         mIsValid = false;
  +             
  +             public ConnectorObject( String pName ) {
  +                     if( pName != null || pName.length() > 0 ) {
  +                             StringTokenizer lName = new StringTokenizer( pName, 
":" );
  +                             if( lName.countTokens() == 3 ) {
  +                                     // Ignore "jmx"
  +                                     lName.nextToken();
  +                                     mServerName = lName.nextToken();
  +                                     mProtocolName = lName.nextToken();
  +                                     mIsValid = true;
  +                             }
  +                     }
  +             }
  +             
  +             public boolean isValid() {
  +                     return mIsValid;
  +             }
  +             
  +             public String getProtocol() {
  +                     return mProtocolName;
  +             }
  +             
  +             public String getServer() {
  +                     return mServerName;
                }
        }
   }
  
  
  

Reply via email to