APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.management.remote

public Class JMXConnectorFactory

extends Object
Class Inheritance
Imports
com.sun.jmx.mbeanserver.Util, java.io.IOException, .UncheckedIOException, java.net.MalformedURLException, java.util.Collections, .HashMap, .Map, .ServiceLoader, .ServiceLoader.Provider, .StringTokenizer, java.util.function.Predicate, java.util.stream.Stream, com.sun.jmx.remote.util.ClassLogger, .EnvHelp

Factory to create JMX API connector clients. There are no instances of this class.

Connections are usually made using the connect method of this class. More advanced applications can separate the creation of the connector client, using newJMXConnector and the establishment of the connection itself, using JMXConnector#connect(Map).

Each client is created by an instance of JMXConnectorProvider. This instance is found as follows. Suppose the given JMXServiceURL looks like "service:jmx:protocol:remainder". Then the factory will attempt to find the appropriate JMXConnectorProvider for protocol. Each occurrence of the character + or - in protocol is replaced by . or _, respectively.

A provider package list is searched for as follows:

  1. If the environment parameter to newJMXConnector contains the key jmx.remote.protocol.provider.pkgs then the associated value is the provider package list.
  2. Otherwise, if the system property jmx.remote.protocol.provider.pkgs exists, then its value is the provider package list.
  3. Otherwise, there is no provider package list.

The provider package list is a string that is interpreted as a list of non-empty Java package names separated by vertical bars (|). If the string is empty, then so is the provider package list. If the provider package list is not a String, or if it contains an element that is an empty string, a JMXProviderException is thrown.

If the provider package list exists and is not empty, then for each element pkg of the list, the factory will attempt to load the class

pkg.protocol.ClientProvider

If the environment parameter to newJMXConnector contains the key jmx.remote.protocol.provider.class.loader then the associated value is the class loader to use to load the provider. If the associated value is not an instance of java.lang.ClassLoader, an java.lang.IllegalArgumentException is thrown.

If the jmx.remote.protocol.provider.class.loader key is not present in the environment parameter, the calling thread's context class loader is used.

If the attempt to load this class produces a ClassNotFoundException, the search for a handler continues with the next element of the list.

Otherwise, a problem with the provider found is signalled by a JMXProviderException whose cause indicates the underlying exception, as follows:

If no provider is found by the above steps, including the default case where there is no provider package list, then the implementation will use its own provider for protocol, or it will throw a MalformedURLException if there is none. An implementation may choose to find providers by other means. For example, it may support service providers, where the service interface is JMXConnectorProvider.

Every implementation must support the RMI connector protocol with the default RMI transport, specified with string rmi.

Once a provider is found, the result of the newJMXConnector method is the result of calling newJMXConnector on the provider.

The Map parameter passed to the JMXConnectorProvider is a new read-only Map that contains all the entries that were in the environment parameter to JMXConnectorFactory.newJMXConnector, if there was one. Additionally, if the jmx.remote.protocol.provider.class.loader key is not present in the environment parameter, it is added to the new read-only Map. The associated value is the calling thread's context class loader.

Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static interface
JMXConnectorFactory.ConnectorFactory<P, C>

A factory function that can create a connector from a provider.

private static class
JMXConnectorFactory.ProviderFinder<P, C>

An instance of ProviderFinder is used to traverse a Stream<Provider<P>> and find the first implementation of P that supports creating a connector C from the given JMXServiceURL.

Field Summary

Modifier and TypeField and Description
public static final String
DEFAULT_CLASS_LOADER

Name of the attribute that specifies the default class loader.

private static final ClassLogger
public static final String
PROTOCOL_PROVIDER_CLASS_LOADER

Name of the attribute that specifies the class loader for loading protocol providers.

private static final String
public static final String
PROTOCOL_PROVIDER_PACKAGES

Name of the attribute that specifies the provider packages that are consulted when looking for the handler for a protocol.

Constructor Summary

AccessConstructor and Description
private
JMXConnectorFactory()

There are no instances of this class.

Method Summary

Modifier and TypeMethod and Description
public static JMXConnector

Returns:

a JMXConnector whose connect method has been called.
connect
(JMXServiceURL
the address of the connector server to connect to.
serviceURL
)

Creates a connection to the connector server at the given address.

public static JMXConnector

Returns:

a JMXConnector representing the newly-made connection. Each successful call to this method produces a different object.
connect
(JMXServiceURL
the address of the connector server to connect to.
serviceURL
,
Map<String, ?>
a set of attributes to determine how the connection is made. This parameter can be null. Keys in this map must be Strings. The appropriate type of each associated value depends on the attribute. The contents of environment are not changed by this call.
environment
)

Creates a connection to the connector server at the given address.

private static JMXConnector

Returns:

an instance of JMXConnector if a provider was found from which one could be instantiated, null otherwise.
getConnectorAsService
(ClassLoader
The ClassLoader to use when looking up an implementation of the service. If null, then only installed services will be considered.
loader
,
JMXServiceURL
The JMXServiceURL of the connector for which a provider is requested.
url
,
Map<String, ?> map, Predicate<ServiceLoader.Provider<?>>
A filter used to exclude or return provider implementations. Typically the filter will either exclude system services (system default implementations) or only retain those. This can allow to first look for custom implementations (e.g. deployed on the CLASSPATH with META-INF/services) and then only default to system implementations.
filter
)

Creates a JMXConnector from the first JMXConnectorProvider service supporting the given url that can be loaded from the given loader.

pack-priv static <P, C> C

Returns:

an instance of C if a provider P was found from which one could be instantiated, null otherwise.
getConnectorAsService
(Class<P>
The service type for which an implementation should be looked up from the ServiceLoader. This will be either JMXConnectorProvider.class or JMXConnectorServerProvider.class
providerClass
,
ClassLoader
The ClassLoader to use when looking up an implementation of the service. If null, then only installed services will be considered.
loader
,
JMXServiceURL
The JMXServiceURL of the connector for which a provider is requested.
url
,
Predicate<ServiceLoader.Provider<?>>
A filter used to exclude or return provider implementations. Typically the filter will either exclude system services (system default implementations) or only retain those. This can allow to first look for custom implementations (e.g. deployed on the CLASSPATH with META-INF/services) and then only default to system implementations.
filter
,
JMXConnectorFactory.ConnectorFactory<P, C>
A functional factory that can attempt to create an instance of connector C from a provider P. Typically, this is a simple wrapper over JMXConnectorProvider::newJMXConnector or JMXConnectorProviderServer::newJMXConnectorServer.
factory
)

Creates a connector from a provider loaded from the ServiceLoader.

pack-priv static <T> T
getProvider(JMXServiceURL serviceURL, final Map<String, Object> environment, String providerClassName, Class<T> targetInterface, final ClassLoader loader)

pack-priv static <T> T
getProvider(String protocol, String pkgs, ClassLoader loader, String providerClassName, Class<T> targetInterface)

pack-priv static boolean

Returns:

true if this provider is a default system provider.
isSystemProvider
(ServiceLoader.Provider<?>
the provider to test.
provider
)

Checks whether the given provider is our system provider for the RMI connector.

private static <K, V> Map<K, V>
private static <K> Map<K, Object>
newHashMap(Map<K, ?> map)

public static JMXConnector

Returns:

a JMXConnector representing the new connector client. Each successful call to this method produces a different object.
newJMXConnector
(JMXServiceURL
the address of the connector server to connect to.
serviceURL
,
Map<String, ?>
a set of attributes to determine how the connection is made. This parameter can be null. Keys in this map must be Strings. The appropriate type of each associated value depends on the attribute. The contents of environment are not changed by this call.
environment
)

Creates a connector client for the connector server at the given address.

private static String
pack-priv static ClassLoader
resolveClassLoader(Map<String, ?> environment)

private static String
private static ClassLoader
wrap(final ClassLoader parent)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_CLASS_LOADERback to summary
public static final String DEFAULT_CLASS_LOADER

Name of the attribute that specifies the default class loader. This class loader is used to deserialize return values and exceptions from remote MBeanServerConnection calls. The value associated with this attribute is an instance of ClassLoader.

loggerback to summary
private static final ClassLogger logger
PROTOCOL_PROVIDER_CLASS_LOADERback to summary
public static final String PROTOCOL_PROVIDER_CLASS_LOADER

Name of the attribute that specifies the class loader for loading protocol providers. The value associated with this attribute is an instance of ClassLoader.

PROTOCOL_PROVIDER_DEFAULT_PACKAGEback to summary
private static final String PROTOCOL_PROVIDER_DEFAULT_PACKAGE
PROTOCOL_PROVIDER_PACKAGESback to summary
public static final String PROTOCOL_PROVIDER_PACKAGES

Name of the attribute that specifies the provider packages that are consulted when looking for the handler for a protocol. The value associated with this attribute is a string with package names separated by vertical bars (|).

Constructor Detail

JMXConnectorFactoryback to summary
private JMXConnectorFactory()

There are no instances of this class.

Method Detail

connectback to summary
public static JMXConnector connect(JMXServiceURL serviceURL) throws IOException

Creates a connection to the connector server at the given address.

This method is equivalent to connect(serviceURL, null).

Parameters
serviceURL:JMXServiceURL

the address of the connector server to connect to.

Returns:JMXConnector

a JMXConnector whose connect method has been called.

Exceptions
IOException:
if the connector client or the connection cannot be made because of a communication problem.
NullPointerException:
if serviceURL is null.
SecurityException:
if the connection cannot be made for security reasons.
connectback to summary
public static JMXConnector connect(JMXServiceURL serviceURL, Map<String, ?> environment) throws IOException

Creates a connection to the connector server at the given address.

This method is equivalent to:

JMXConnector conn = JMXConnectorFactory.newJMXConnector(serviceURL,
                                                        environment);
conn.connect(environment);
Parameters
serviceURL:JMXServiceURL

the address of the connector server to connect to.

environment:Map<String, ?>

a set of attributes to determine how the connection is made. This parameter can be null. Keys in this map must be Strings. The appropriate type of each associated value depends on the attribute. The contents of environment are not changed by this call.

Returns:JMXConnector

a JMXConnector representing the newly-made connection. Each successful call to this method produces a different object.

Exceptions
IOException:
if the connector client or the connection cannot be made because of a communication problem.
NullPointerException:
if serviceURL is null.
SecurityException:
if the connection cannot be made for security reasons.
getConnectorAsServiceback to summary
private static JMXConnector getConnectorAsService(ClassLoader loader, JMXServiceURL url, Map<String, ?> map, Predicate<ServiceLoader.Provider<?>> filter) throws IOException

Creates a JMXConnector from the first JMXConnectorProvider service supporting the given url that can be loaded from the given loader.

Parses the list of JMXConnectorProvider services that can be loaded from the given loader, only retaining those that satisfy the given filter. Then for each provider, attempts to create a new JMXConnector. The first JMXConnector successfully created is returned.

The filter predicate is usually used to either exclude system providers or only retain system providers (see isSystemProvider(...) above).

Parameters
loader:ClassLoader

The ClassLoader to use when looking up an implementation of the service. If null, then only installed services will be considered.

url:JMXServiceURL

The JMXServiceURL of the connector for which a provider is requested.

filter:Predicate<ServiceLoader.Provider<?>>

A filter used to exclude or return provider implementations. Typically the filter will either exclude system services (system default implementations) or only retain those. This can allow to first look for custom implementations (e.g. deployed on the CLASSPATH with META-INF/services) and then only default to system implementations.

Returns:JMXConnector

an instance of JMXConnector if a provider was found from which one could be instantiated, null otherwise.

Exceptions
IOException:
if no connector could not be instantiated, and at least one provider threw an exception that wasn't a MalformedURLException or a JMProviderException.
JMXProviderException:
if a provider for the protocol in url was found, but couldn't create the connector some reason.
getConnectorAsServiceback to summary
pack-priv static <P, C> C getConnectorAsService(Class<P> providerClass, ClassLoader loader, JMXServiceURL url, Predicate<ServiceLoader.Provider<?>> filter, JMXConnectorFactory.ConnectorFactory<P, C> factory) throws IOException

Creates a connector from a provider loaded from the ServiceLoader.

The pair (P,C) will be either one of:
a. (JMXConnectorProvider, JMXConnector) or
b. (JMXConnectorServerProvider, JMXConnectorServer)

Parameters
providerClass:Class<P>

The service type for which an implementation should be looked up from the ServiceLoader. This will be either JMXConnectorProvider.class or JMXConnectorServerProvider.class

loader:ClassLoader

The ClassLoader to use when looking up an implementation of the service. If null, then only installed services will be considered.

url:JMXServiceURL

The JMXServiceURL of the connector for which a provider is requested.

filter:Predicate<ServiceLoader.Provider<?>>

A filter used to exclude or return provider implementations. Typically the filter will either exclude system services (system default implementations) or only retain those. This can allow to first look for custom implementations (e.g. deployed on the CLASSPATH with META-INF/services) and then only default to system implementations.

factory:JMXConnectorFactory.ConnectorFactory<P, C>

A functional factory that can attempt to create an instance of connector C from a provider P. Typically, this is a simple wrapper over JMXConnectorProvider::newJMXConnector or JMXConnectorProviderServer::newJMXConnectorServer.

Returns:C

an instance of C if a provider P was found from which one could be instantiated, null otherwise.

Exceptions
IOException:
if C could not be instantiated, and at least one provider P threw an exception that wasn't a MalformedURLException or a JMProviderException.
JMXProviderException:
if a provider P for the protocol in url was found, but couldn't create the connector C for some reason.
getProviderback to summary
pack-priv static <T> T getProvider(JMXServiceURL serviceURL, final Map<String, Object> environment, String providerClassName, Class<T> targetInterface, final ClassLoader loader) throws IOException
getProviderback to summary
pack-priv static <T> T getProvider(String protocol, String pkgs, ClassLoader loader, String providerClassName, Class<T> targetInterface) throws IOException
isSystemProviderback to summary
pack-priv static boolean isSystemProvider(ServiceLoader.Provider<?> provider)

Checks whether the given provider is our system provider for the RMI connector. If providers for additional protocols are added in the future then the name of their modules may need to be added here. System providers will be loaded only if no other provider is found.

Parameters
provider:ServiceLoader.Provider<?>

the provider to test.

Returns:boolean

true if this provider is a default system provider.

newHashMapback to summary
private static <K, V> Map<K, V> newHashMap()
newHashMapback to summary
private static <K> Map<K, Object> newHashMap(Map<K, ?> map)
newJMXConnectorback to summary
public static JMXConnector newJMXConnector(JMXServiceURL serviceURL, Map<String, ?> environment) throws IOException

Creates a connector client for the connector server at the given address. The resultant client is not connected until its connect method is called.

Parameters
serviceURL:JMXServiceURL

the address of the connector server to connect to.

environment:Map<String, ?>

a set of attributes to determine how the connection is made. This parameter can be null. Keys in this map must be Strings. The appropriate type of each associated value depends on the attribute. The contents of environment are not changed by this call.

Returns:JMXConnector

a JMXConnector representing the new connector client. Each successful call to this method produces a different object.

Exceptions
IOException:
if the connector client cannot be made because of a communication problem.
NullPointerException:
if serviceURL is null.
MalformedURLException:
if there is no provider for the protocol in serviceURL.
JMXProviderException:
if there is a provider for the protocol in serviceURL but it cannot be used for some reason.
protocol2packageback to summary
private static String protocol2package(String protocol)
resolveClassLoaderback to summary
pack-priv static ClassLoader resolveClassLoader(Map<String, ?> environment)
resolvePkgsback to summary
private static String resolvePkgs(Map<String, ?> env) throws JMXProviderException
Annotations
@SuppressWarnings:removal
wrapback to summary
private static ClassLoader wrap(final ClassLoader parent)
javax.management.remote back to summary

pack-priv Interface JMXConnectorFactory.ConnectorFactory<P, C>

Annotations
@FunctionalInterface

A factory function that can create a connector from a provider. The pair (P,C) will be either one of: a. (JMXConnectorProvider, JMXConnector) or b. (JMXConnectorServerProvider, JMXConnectorServer)

Method Summary

Modifier and TypeMethod and Description
public C
apply(P provider)

Method Detail

applyback to summary
public C apply(P provider) throws Exception
javax.management.remote back to summary

private final Class JMXConnectorFactory.ProviderFinder<P, C>

extends Object
implements Predicate<ServiceLoader.Provider<P>>
Class Inheritance
All Implemented Interfaces
java.util.function.Predicate

An instance of ProviderFinder is used to traverse a Stream<Provider<P>> and find the first implementation of P that supports creating a connector C from the given JMXServiceURL.

The pair (P,C) will be either one of:
a. (JMXConnectorProvider, JMXConnector) or
b. (JMXConnectorServerProvider, JMXConnectorServer)

The first connector successfully created while traversing the stream is stored in the ProviderFinder instance. After that, the ProviderFinder::test method, if called, will always return false, skipping the remaining providers.

An instance of ProviderFinder is always expected to be used in conjunction with Stream::findFirst, so that the stream traversal is stopped as soon as a matching provider is found.

At the end of the stream traversal, the ProviderFinder::get method can be used to obtain the connector instance (an instance of C) that was created. If no connector could be created, and an exception was encountered while traversing the stream and attempting to create the connector, then that exception will be thrown by ProviderFinder::get, wrapped, if needed, inside an IOException.

If any JMXProviderException is encountered while traversing the stream and attempting to create the connector, that exception will be wrapped in an UncheckedIOException and thrown immediately within the stream, thus interrupting the traversal.

If no matching provider was found (no provider found or attempting factory.apply always returned null or threw a MalformedURLException, indicating the provider didn't support the protocol asked for by the JMXServiceURL), then ProviderFinder::get will simply return null.

Field Summary

Modifier and TypeField and Description
private C
private IOException
pack-priv final JMXConnectorFactory.ConnectorFactory<P, C>
pack-priv final JMXServiceURL

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv C

Returns:

an instance of C if a provider was found from which C could be instantiated, null otherwise.
get
()

Returns an instance of C if a provider was found from which C could be instantiated.

public boolean

Returns:

true for the first provider sp for which C could be instantiated, false otherwise.
test
(ServiceLoader.Provider<P>
a candidate provider for instantiating C.
sp
)

Implements java.util.function.Predicate.test.

Returns true for the first provider sp that can be used to obtain an instance of C from the given factory.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectionback to summary
private C connection
exceptionback to summary
private IOException exception
factoryback to summary
pack-priv final JMXConnectorFactory.ConnectorFactory<P, C> factory
urlback to summary
pack-priv final JMXServiceURL url

Constructor Detail

ProviderFinderback to summary
pack-priv ProviderFinder(JMXConnectorFactory.ConnectorFactory<P, C> factory, JMXServiceURL url)

Method Detail

getback to summary
pack-priv C get() throws IOException

Returns an instance of C if a provider was found from which C could be instantiated.

Returns:C

an instance of C if a provider was found from which C could be instantiated, null otherwise.

Exceptions
IOException:
if C could not be instantiated, and at least one provider threw an exception that wasn't a MalformedURLException or a JMProviderException.
testback to summary
public boolean test(ServiceLoader.Provider<P> sp)

Implements java.util.function.Predicate.test.

Returns true for the first provider sp that can be used to obtain an instance of C from the given factory.

Parameters
sp:ServiceLoader.Provider<P>

a candidate provider for instantiating C.

Returns:boolean

true for the first provider sp for which C could be instantiated, false otherwise.

Exceptions
UncheckedIOException:
if sp throws a JMXProviderException. The JMXProviderException is set as the root cause.