AppserverConnectionSourcepublic final class AppserverConnectionSource extends Object implements com.sun.appserv.management.client.ConnectionSource, NotificationListenerSupports connectivity to Sun Appserver 8.1 and later (not available
prior to 8.1).
This is the only official way
to get a connection to the appserver.
Helper methods to simplify connecting are available in
{@link com.sun.appserv.management.helper.Connect}.
If the server is running with TLS enabled, then you must use the constructor
that includes TLSParams. Here is an example of how to connect using TLS:
final File trustStore = new File( "~/.keystore" );
final char[] trustStorePassword = "changeme".toCharArray(); // or whatever it is
final HandshakeCompletedListener listener = new HandshakeCompletedListenerImpl();
final TrustStoreTrustManager trustMgr = new TrustStoreTrustManager( trustStore, trustStorePassword);
trustMgr.setPrompt( true );
final TLSParams tlsParams = new TLSParams( new X509TrustManager[] { trustMgr }, listener );
final AppserverConnectionSource src =
new AppserverConnectionSource( AppserverConnectionSource.PROTOCOL_RMI,
"localhost", 8686, "admin", "admin123",
tlsParams,
null );
final DomainRoot domainRoot = src.getDomainRoot();
If security is not an issue, it is recommended to simply disable TLS on the
server. However, you can also connect using TLS whereby the
server certificate is blindly trusted:
final TLSParams tlsParams = new TLSParams( TrustAnyTrustManager.getInstanceArray(), null );
final AppserverConnectionSource src =
new AppserverConnectionSource( AppserverConnectionSource.PROTOCOL_RMI,
"localhost", 8686, "admin", "admin123",
tlsParams,
null );
final DomainRoot domainRoot = src.getDomainRoot();
|
Fields Summary |
---|
private final String | mHost | private final int | mPort | private final String | mProtocol | private final String | mUser | private final String | mPassword | private final TLSParams | mTLSParams | private final Map | mReserved | protected JMXConnector | mJMXConnector | private static final boolean | DISABLE_HANDSHAKE_COMPLETED_CHECKFIXME FIXME FIXME
This should be removed after the http connector supports the HandshakeCompletedListener | public static final String | TRUST_MANAGERS_KEY[used internally] | public static final String | HANDSHAKE_COMPLETED_LISTENER_KEY[used internally] | private static final String | PROTOCOL_PREFIX | public static final String | PROTOCOL_RMIRMI protocol to the Appserver.
Do not use the literal value of this constant in your code;
it is subject to change | public static final String | DEFAULT_PROTOCOLDefault protocol to the Appserver eg PROTOCOL_RMI. | public static final String | PROTOCOL_HTTPInternal unsupported protocol. Not supported for external use.
Do not use the literal value of this constant in your code;
it is subject to change | private static final String | INTERNAL_HTTP | private static final String | INTERNAL_HTTPS | private static final String | HTTP_FACTORY_PACKAGESPackages for http(s) JMXConnectorProvider. | public static final String | DEFAULT_TRUST_STORE_NAMEName of the default TrustStore file, located in the client's
home directory. | public static final String | DEFAULT_TRUST_STORE_PASSWORDThe default pasword for {@link #DEFAULT_TRUST_STORE_NAME}. | private static final String | APPSERVER_JNDI_NAME |
Constructors Summary |
---|
public AppserverConnectionSource(String host, int port, String user, String userPassword, Map reserved)Create a new instance using the default protocol without TLS.
this( DEFAULT_PROTOCOL, host, port, user, userPassword, reserved);
| public AppserverConnectionSource(String protocol, String host, int port, String user, String userPassword, Map reserved)Create a new instance connecting to the specified host/port with
the specified protocol without TLS.
Note:The only supported protocol is {@link #PROTOCOL_RMI}.
Use of any other protocol is not supported and these protocols are
subject to change or removal at any time.
this ( protocol, host, port, user, userPassword, null, reserved );
| public AppserverConnectionSource(String protocol, String host, int port, String user, String userPassword, TLSParams tlsParams, Map reserved)Create a new instance which will use TLS for security if specified.
if ( reserved != null && reserved.keySet().size() != 0 )
{
throw new IllegalArgumentException( "No parameters may be passed in 'reserved' Map" );
}
if ( isSupportedProtocol( protocol ) )
{
mHost = host;
mPort = port;
mProtocol = protocol;
mUser = user;
mPassword = userPassword;
mTLSParams = tlsParams;
mReserved = reserved;
}
else
{
throw new IllegalArgumentException( "unsupported protocol: " + protocol +
", use either PROTOCOL_RMI or PROTOCOL_HTTP" );
}
|
Methods Summary |
---|
private javax.management.remote.JMXConnector | createNew()
final Map<String,Object> env = getCredentialsEnv( mUser, mPassword );
// NetBeans fix; classloader must be more than system classloader
env.put("jmx.remote.protocol.provider.class.loader",
this.getClass().getClassLoader());
final HandshakeCompletedListenerImpl hcListener =
new HandshakeCompletedListenerImpl( getSuppliedHandshakeCompletedListener() );
JMXServiceURL url = null;
if ( mProtocol.equals( PROTOCOL_HTTP ) )
{
if ( useTLS() )
{
//env.put( TRUST_MANAGERS_KEY, getTrustManagers() );
final X509TrustManager[] tms = getTrustManagers();
if (tms != null && tms.length >= 1) {
env.put( TRUST_MANAGERS_KEY, tms[0]);
}
env.put( HANDSHAKE_COMPLETED_LISTENER_KEY, hcListener );
}
env.put( "com.sun.enterprise.as.http.auth", "BASIC" );
env.put( "USER", mUser );
env.put( "PASSWORD", mPassword );
// so our JMXConnectorProvider may be found
env.put( JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, HTTP_FACTORY_PACKAGES );
final String internalProtocol = useTLS() ? INTERNAL_HTTPS : INTERNAL_HTTP;
url = new JMXServiceURL( internalProtocol, mHost, mPort);
}
else if ( mProtocol.equals( PROTOCOL_RMI ) )
{
if ( useTLS() )
{
// the only way we can communicate with/control the RMI stub is through
// this special singleton class
final AdminRMISSLClientSocketFactoryEnvImpl rmiEnv =
AdminRMISSLClientSocketFactoryEnvImpl.getInstance();
rmiEnv.setHandshakeCompletedListener( hcListener );
rmiEnv.setTrustManagers( getTrustManagers() );
}
final String s = "service:jmx:rmi:///jndi/rmi://" +
mHost + ":" + mPort + APPSERVER_JNDI_NAME;
url = new JMXServiceURL( s );
}
else
{
assert( false );
}
final JMXConnector conn = JMXConnectorFactory.connect( url, env );
/*
If the connection was established with RMI, it could have been an insecure
connection if a on-TLS stub was downloaded. Verify that the
a TLS Handshake was actually completed.
*/
if ( ! disableHandShakeCompletedCheck() )
{
if ( useTLS() && hcListener.getLastEvent() == null )
{
conn.close();
throw new IOException( "Connection could not be established using TLS; server is not using TLS" );
}
}
else
{
//warning( "HandshakeCompletedCheck is temporarily disabled for PROTOCOL_HTTP" );
/* This has been commented out -- See CR: 6172198. HTTPS/JMX Connector Implementation
does not have to accept HandshakeCompletedListener for 8.1*/
}
conn.addConnectionNotificationListener( this, null, conn );
return( conn );
| private boolean | disableHandShakeCompletedCheck()
return( DISABLE_HANDSHAKE_COMPLETED_CHECK && mProtocol.equals( PROTOCOL_HTTP ) );
| private java.lang.Object | envGet(java.lang.String key)
return( mReserved == null ? null : mReserved.get( key ) );
| private java.util.Map | getCredentialsEnv(java.lang.String user, java.lang.String password)
final HashMap<String,Object> env = new HashMap<String,Object>();
final String[] credentials = new String[] { mUser, mPassword };
env.put( JMXConnector.CREDENTIALS, credentials );
return( env );
| public com.sun.appserv.management.DomainRoot | getDomainRoot(){@link DomainRoot} will be returned. Upon return, AMX is
ready for use. If the server has just been started, there
may be a slight delay until AMX is ready for use; this method
returns only once AMX is ready for use.
final DomainRoot domainRoot =
ProxyFactory.getInstance( this ).getDomainRoot( true );
return domainRoot;
| public javax.management.MBeanServerConnection | getExistingMBeanServerConnection()
try
{
return( getJMXConnector( false ).getMBeanServerConnection() );
}
catch( IOException e )
{
}
return( null );
| public javax.management.remote.JMXConnector | getJMXConnector(boolean forceNew)If the connection has already been created, return the existing JMXConnector
unless 'forceNew' is true or the connection is currently null.
if ( forceNew || mJMXConnector == null )
{
mJMXConnector = createNew();
getMBeanServerConnection( false ); // make sure it works...
}
return( mJMXConnector );
| public javax.management.MBeanServerConnection | getMBeanServerConnection(boolean forceNew)
return( getJMXConnector( forceNew ).getMBeanServerConnection() );
| private final javax.net.ssl.HandshakeCompletedListener | getSuppliedHandshakeCompletedListener()
return( mTLSParams == null ? null : mTLSParams.getHandshakeCompletedListener() );
| private final javax.net.ssl.X509TrustManager[] | getTrustManagers()
return( mTLSParams == null ? null : mTLSParams.getTrustManagers() );
| public void | handleNotification(javax.management.Notification notifIn, java.lang.Object handback)Used internally as callback for {@link javax.management.NotificationListener}.
DO NOT CALL THIS METHOD.
if ( notifIn instanceof JMXConnectionNotification )
{
final JMXConnectionNotification notif = (JMXConnectionNotification)notifIn;
final String type = notif.getType();
if ( type.equals( JMXConnectionNotification.FAILED) ||
type.equals( JMXConnectionNotification.CLOSED ) )
{
mJMXConnector = null;
}
}
| public static boolean | isSupportedProtocol(java.lang.String protocol)
return( protocol != null &&
(
protocol.equals( PROTOCOL_HTTP ) ||
protocol.equals( PROTOCOL_RMI )
)
);
| public java.lang.String | toString()
return(
"protocol=" + mProtocol +
", host=" + mHost +
", port=" + mPort +
", user=" + mUser +
", useTLS={" + useTLS() + "}" +
", mReserved=" + (mReserved == null ? "null" : MapUtil.toString( mReserved )) );
| private final boolean | useTLS()
return( mTLSParams != null );
| private void | warning(java.lang.String msg)
System.out.println( "\n***\nWARNING: " + msg );
|
|