org.apache.commons.modeler

Class Registry

Implemented Interfaces:
MBeanRegistration, RegistryMBean

public class Registry
extends java.lang.Object
implements RegistryMBean, MBeanRegistration

Registry for modeler MBeans. This is the main entry point into modeler. It provides methods to create and manipulate model mbeans and simplify their use. Starting with version 1.1, this is no longer a singleton and the static methods are strongly deprecated. In a container environment we can expect different applications to use different registries. This class is itself an mbean. IMPORTANT: public methods not marked with @since x.x are experimental or internal. Should not be used.
Authors:
Craig R. McClanahan
Costin Manolache

Field Summary

static String
MODELER_MANIFEST
Experimental support for manifest-based discovery.
private HashMap
descriptors
The set of ManagedBean instances for the beans this registry knows about, keyed by name.
private HashMap
descriptorsByClass
List of managed byeans, keyed by class name
private Object
guard
private Hashtable
idDomains
private Hashtable
ids
private Object
key
private static Log
log
The Log instance to which we will write our log messages.
private static HashMap
perLoaderRegistries
Will be used to isolate different apps and enhance security
private static Registry
registry
The registry instance created by our factory method the first time it is called.
private HashMap
searchedPaths
private MBeanServer
server
The MBeanServer instance that we will use to register management beans.

Constructor Summary

Registry()

Method Summary

void
addManagedBean(ManagedBean bean)
Add a new bean metadata to the set of beans known to this registry.
Object
convertValue(String type, String value)
EXPERIMENTAL Convert a string to object, based on type.
private void
findDescriptor(Class beanClass, String type)
Lookup the component descriptor in the package and in the parent packages.
ManagedBean
findManagedBean(Class beanClass, String type)
ManagedBean
findManagedBean(Object bean, Class beanClass, String type)
Find or load metadata.
ManagedBean
findManagedBean(String name)
Find and return the managed bean definition for the specified bean name, if any; otherwise return null.
String[]
findManagedBeans()
Return the set of bean names for all managed beans known to this registry.
String[]
findManagedBeans(String group)
Return the set of bean names for all managed beans known to this registry that belong to the specified group.
int
getId(String domain, String name)
Return an int ID for faster access.
MBeanServer
getMBeanServer()
Factory method to create (if necessary) and return our MBeanServer instance.
MBeanOperationInfo
getMethodInfo(ObjectName oname, String opName)
Find the operation info for a method
private ModelerSource
getModelerSource(String type)
static Registry
getRegistry()
Deprecated. Not enough info - use the method that takes CL and domain
static Registry
getRegistry(Object key, Object guard)
Factory method to create (if necessary) and return our Registry instance.
static MBeanServer
getServer()
Deprecated. Use the instance method
String
getType(ObjectName oname, String attName)
Get the type of an attribute of the object, from the metadata.
void
invoke(List mbeans, String operation, boolean failFirst)
Invoke a operation on a list of mbeans.
List
load(String sourceType, Object source, String param)
Deprecated. bad interface, mixing of metadata and mbeans
void
loadCachedDescriptors(Object source)
Deprecated. Loaded automatically or using a File or Url ending in .ser
void
loadDescriptors(Object source)
Load the registry from the XML input found in the specified input stream.
void
loadDescriptors(String packageName, ClassLoader classLoader)
Lookup the component descriptor in the package and in the parent packages.
void
loadDescriptors(String sourceType, Object source, String param)
Deprecated.
List
loadMBeans(Object source)
List
loadMBeans(Object source, ClassLoader cl)
Load an extended mlet file.
private void
loadMetaInfDescriptors(ClassLoader cl)
Discover all META-INF/modeler.xml files in classpath and register the components
void
loadMetadata(Object source)
Load descriptors.
static void
loadRegistry(InputStream stream)
Deprecated. use normal class method instead
void
postDeregister()
void
postRegister(Boolean registrationDone)
void
preDeregister()
ObjectName
preRegister(MBeanServer server, ObjectName name)
void
registerComponent(Object bean, ObjectName oname, String type)
Register a component XXX make it private
void
registerComponent(Object bean, String oname, String type)
Register a bean by creating a modeler mbean and adding it to the MBeanServer.
void
registerComponent(Object bean, String domain, String type, String name)
Deprecated. - may still be used in code using pre-1.1 builds
void
removeManagedBean(ManagedBean bean)
Remove an existing bean from the set of beans known to this registry.
void
resetMetadata()
void
setGuard(Object guard)
Set a guard object that will prevent access to this registry by unauthorized components
void
setMBeanServer(MBeanServer server)
Set the MBeanServer to be utilized for our registered management beans.
static void
setServer(MBeanServer mbeanServer)
Deprecated. Use the instance method
static void
setUseContextClassLoader(boolean enable)
Allow containers to isolate apps.
private String
sourceTypeFromExt(String s)
void
stop()
Lifecycle method - clean up the registry metadata.
void
unregisterComponent(ObjectName oname)
Unregister a component.
void
unregisterComponent(String oname)
Unregister a component.
void
unregisterComponent(String domain, String name)
void
unregisterRegistry(ClassLoader loader)
Called by a registry or by the container to unload a loader

Field Details

MODELER_MANIFEST

public static String MODELER_MANIFEST
Experimental support for manifest-based discovery.

descriptors

private HashMap descriptors
The set of ManagedBean instances for the beans this registry knows about, keyed by name.

descriptorsByClass

private HashMap descriptorsByClass
List of managed byeans, keyed by class name

guard

private Object guard

idDomains

private Hashtable idDomains

ids

private Hashtable ids

key

private Object key

log

private static Log log
The Log instance to which we will write our log messages.

perLoaderRegistries

private static HashMap perLoaderRegistries
Will be used to isolate different apps and enhance security

registry

private static Registry registry
The registry instance created by our factory method the first time it is called.

searchedPaths

private HashMap searchedPaths

server

private MBeanServer server
The MBeanServer instance that we will use to register management beans.

Constructor Details

Registry

public Registry()

Method Details

addManagedBean

public void addManagedBean(ManagedBean bean)
Add a new bean metadata to the set of beans known to this registry. This is used by internal components.
Parameters:
bean - The managed bean to be added
Since:
1.0

convertValue

public Object convertValue(String type,
                           String value)
EXPERIMENTAL Convert a string to object, based on type. Used by several components. We could provide some pluggability. It is here to keep things consistent and avoid duplication in other tasks
Parameters:
type - Fully qualified class name of the resulting value
value - String value to be converted
Returns:
Converted value

findDescriptor

private void findDescriptor(Class beanClass,
                            String type)
Lookup the component descriptor in the package and in the parent packages.
Parameters:
beanClass -
type -

findManagedBean

public ManagedBean findManagedBean(Class beanClass,
                                   String type)
            throws Exception

findManagedBean

public ManagedBean findManagedBean(Object bean,
                                   Class beanClass,
                                   String type)
            throws Exception
Find or load metadata.

findManagedBean

public ManagedBean findManagedBean(String name)
Find and return the managed bean definition for the specified bean name, if any; otherwise return null.
Parameters:
name - Name of the managed bean to be returned. Since 1.1, both short names or the full name of the class can be used.
Since:
1.0

findManagedBeans

public String[] findManagedBeans()
Return the set of bean names for all managed beans known to this registry.
Since:
1.0

findManagedBeans

public String[] findManagedBeans(String group)
Return the set of bean names for all managed beans known to this registry that belong to the specified group.
Parameters:
group - Name of the group of interest, or null to select beans that do not belong to a group
Since:
1.0

getId

public int getId(String domain,
                 String name)
Return an int ID for faster access. Will be used for notifications and for other operations we want to optimize.
Specified by:
getId in interface RegistryMBean
Parameters:
domain - Namespace
name - Type of the notification
Returns:
An unique id for the domain:name combination
Since:
1.1

getMBeanServer

public MBeanServer getMBeanServer()
Factory method to create (if necessary) and return our MBeanServer instance.

getMethodInfo

public MBeanOperationInfo getMethodInfo(ObjectName oname,
                                        String opName)
Find the operation info for a method
Parameters:
oname -
opName -
Returns:
the operation info for the specified operation

getModelerSource

private ModelerSource getModelerSource(String type)
            throws Exception

getRegistry

public static Registry getRegistry()

Deprecated. Not enough info - use the method that takes CL and domain

Get a "singelton" registry, or one per thread if setUseContextLoader was called
Since:
1.0

getRegistry

public static Registry getRegistry(Object key,
                                   Object guard)
Factory method to create (if necessary) and return our Registry instance. Use this method to obtain a Registry - all other static methods are deprecated and shouldn't be used. The current version uses a static - future versions could use the thread class loader.
Parameters:
key - Support for application isolation. If null, the context class loader will be used ( if setUseContextClassLoader is called ) or the default registry is returned.
guard - Prevent access to the registry by untrusted components
Since:
1.1

getServer

public static MBeanServer getServer()

Deprecated. Use the instance method

Factory method to create (if necessary) and return our MBeanServer instance.
Since:
1.0

getType

public String getType(ObjectName oname,
                      String attName)
Get the type of an attribute of the object, from the metadata.
Parameters:
oname -
attName -
Returns:
null if metadata about the attribute is not found
Since:
1.1

invoke

public void invoke(List mbeans,
                   String operation,
                   boolean failFirst)
            throws Exception
Invoke a operation on a list of mbeans. Can be used to implement lifecycle operations.
Specified by:
invoke in interface RegistryMBean
Parameters:
mbeans - list of ObjectName on which we'll invoke the operations
operation - Name of the operation ( init, start, stop, etc)
failFirst - If false, exceptions will be ignored
Since:
1.1

load

public List load(String sourceType,
                 Object source,
                 String param)
            throws Exception

Deprecated. bad interface, mixing of metadata and mbeans

Experimental.
Parameters:
sourceType -
source -
param -
Returns:
List of descriptors

loadCachedDescriptors

public void loadCachedDescriptors(Object source)
            throws Exception

Deprecated. Loaded automatically or using a File or Url ending in .ser

Load the registry from a cached .ser file. This is typically 2-3 times faster than parsing the XML.
Parameters:
source - Source to be used to load. Can be an InputStream or URL.

loadDescriptors

public void loadDescriptors(Object source)
            throws Exception
Load the registry from the XML input found in the specified input stream.
Parameters:
source - Source to be used to load. Can be an InputStream or URL.

loadDescriptors

public void loadDescriptors(String packageName,
                            ClassLoader classLoader)
Lookup the component descriptor in the package and in the parent packages.
Parameters:
packageName -

loadDescriptors

public void loadDescriptors(String sourceType,
                            Object source,
                            String param)
            throws Exception

Deprecated.

Experimental. Will become private, some code may still use it
Parameters:
sourceType -
source -
param -

loadMBeans

public List loadMBeans(Object source)
            throws Exception

loadMBeans

public List loadMBeans(Object source,
                       ClassLoader cl)
            throws Exception
Load an extended mlet file. The source can be an URL, File or InputStream. All mbeans will be instantiated, registered and the attributes will be set. The result is a list of ObjectNames.
Specified by:
loadMBeans in interface RegistryMBean
Parameters:
source - InputStream or URL of the file
cl - ClassLoader to be used to load the mbeans, or null to use the default JMX mechanism ( i.e. all registered loaders )
Returns:
List of ObjectName for the loaded mbeans
Since:
1.1

loadMetaInfDescriptors

private void loadMetaInfDescriptors(ClassLoader cl)
Discover all META-INF/modeler.xml files in classpath and register the components
Since:
EXPERIMENTAL

loadMetadata

public void loadMetadata(Object source)
            throws Exception
Load descriptors. The source can be a File or URL or InputStream for the descriptors file. In the case of File and URL, if the extension is ".ser" a serialized version will be loaded. Also ( experimental for now ) a ClassLoader - in which case META-INF/ will be used. This method should be used to explicitely load metadata - but this is not required in most cases. The registerComponent() method will find metadata in the same pacakge.
Specified by:
loadMetadata in interface RegistryMBean
Parameters:
source -

loadRegistry

public static void loadRegistry(InputStream stream)
            throws Exception

Deprecated. use normal class method instead

Load the registry from the XML input found in the specified input stream.
Parameters:
stream - InputStream containing the registry configuration information
Since:
1.0

postDeregister

public void postDeregister()

postRegister

public void postRegister(Boolean registrationDone)

preDeregister

public void preDeregister()
            throws Exception

preRegister

public ObjectName preRegister(MBeanServer server,
                              ObjectName name)
            throws Exception

registerComponent

public void registerComponent(Object bean,
                              ObjectName oname,
                              String type)
            throws Exception
Register a component XXX make it private
Parameters:
bean -
oname -
type -

registerComponent

public void registerComponent(Object bean,
                              String oname,
                              String type)
            throws Exception
Register a bean by creating a modeler mbean and adding it to the MBeanServer. If metadata is not loaded, we'll look up and read a file named "mbeans-descriptors.ser" or "mbeans-descriptors.xml" in the same package or parent. If the bean is an instance of DynamicMBean. it's metadata will be converted to a model mbean and we'll wrap it - so modeler services will be supported If the metadata is still not found, introspection will be used to extract it automatically. If an mbean is already registered under this name, it'll be first unregistered. If the component implements MBeanRegistration, the methods will be called. If the method has a method "setRegistry" that takes a RegistryMBean as parameter, it'll be called with the current registry.
Specified by:
registerComponent in interface RegistryMBean
Parameters:
bean - Object to be registered
oname - Name used for registration
type - The type of the mbean, as declared in mbeans-descriptors. If null, the name of the class will be used. This can be used as a hint or by subclasses.
Since:
1.1

registerComponent

public void registerComponent(Object bean,
                              String domain,
                              String type,
                              String name)
            throws Exception

Deprecated. - may still be used in code using pre-1.1 builds


removeManagedBean

public void removeManagedBean(ManagedBean bean)
Remove an existing bean from the set of beans known to this registry.
Parameters:
bean - The managed bean to be removed
Since:
1.0

resetMetadata

public void resetMetadata()

setGuard

public void setGuard(Object guard)
Set a guard object that will prevent access to this registry by unauthorized components
Parameters:
guard -
Since:
1.1

setMBeanServer

public void setMBeanServer(MBeanServer server)
Set the MBeanServer to be utilized for our registered management beans.
Parameters:
server - The new MBeanServer instance

setServer

public static void setServer(MBeanServer mbeanServer)

Deprecated. Use the instance method

Set the MBeanServer to be utilized for our registered management beans.
Parameters:
mbeanServer - The new MBeanServer instance
Since:
1.0

setUseContextClassLoader

public static void setUseContextClassLoader(boolean enable)
Allow containers to isolate apps. Can be called only once. It is highly recommended you call this method if using Registry in a container environment. The default is false for backward compatibility
Parameters:
enable -
Since:
1.1

sourceTypeFromExt

private String sourceTypeFromExt(String s)

stop

public void stop()
Lifecycle method - clean up the registry metadata.
Specified by:
stop in interface RegistryMBean
Since:
1.1

unregisterComponent

public void unregisterComponent(ObjectName oname)
Unregister a component. This is just a helper that avoids exceptions by checking if the mbean is already registered
Parameters:
oname -

unregisterComponent

public void unregisterComponent(String oname)
Unregister a component. We'll first check if it is registered, and mask all errors. This is mostly a helper.
Specified by:
unregisterComponent in interface RegistryMBean
Parameters:
oname -
Since:
1.1

unregisterComponent

public void unregisterComponent(String domain,
                                String name)

unregisterRegistry

public void unregisterRegistry(ClassLoader loader)
Called by a registry or by the container to unload a loader
Parameters:
loader -

Copyright (c) 2001-2003 - Apache Software Foundation