Package org.apache.commons.modeler
Class Registry
- java.lang.Object
-
- org.apache.commons.modeler.Registry
-
- All Implemented Interfaces:
javax.management.MBeanRegistration,RegistryMBean
public class Registry extends java.lang.Object implements RegistryMBean, javax.management.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.- Author:
- Craig R. McClanahan, Costin Manolache
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.HashMapdescriptorsThe set of ManagedBean instances for the beans this registry knows about, keyed by name.private java.util.HashMapdescriptorsByClassList of managed byeans, keyed by class nameprivate java.lang.Objectguardprivate java.util.HashtableidDomainsprivate java.util.Hashtableidsprivate static org.apache.commons.logging.LoglogThe Log instance to which we will write our log messages.static java.lang.StringMODELER_MANIFESTExperimental support for manifest-based discovery.private static java.util.HashMapperLoaderRegistriesWill be used to isolate different apps and enhance securityprivate static RegistryregistryThe registry instance created by our factory method the first time it is called.private java.util.MapsearchedPathsprivate javax.management.MBeanServerserverTheMBeanServerinstance that we will use to register management beans.
-
Constructor Summary
Constructors Constructor Description Registry()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddManagedBean(ManagedBean bean)Add a new bean metadata to the set of beans known to this registry.java.lang.ObjectconvertValue(java.lang.String type, java.lang.String value)EXPERIMENTAL Convert a string to object, based on type.private voidfindDescriptor(java.lang.Class beanClass, java.lang.String type)Lookup the component descriptor in the package and in the parent packages.ManagedBeanfindManagedBean(java.lang.Class beanClass, java.lang.String type)ManagedBeanfindManagedBean(java.lang.Object bean, java.lang.Class beanClass, java.lang.String type)Find or load metadata.ManagedBeanfindManagedBean(java.lang.String name)Find and return the managed bean definition for the specified bean name, if any; otherwise returnnull.java.lang.String[]findManagedBeans()Return the set of bean names for all managed beans known to this registry.java.lang.String[]findManagedBeans(java.lang.String group)Return the set of bean names for all managed beans known to this registry that belong to the specified group.intgetId(java.lang.String domain, java.lang.String name)Return an int ID for faster access.javax.management.MBeanServergetMBeanServer()Factory method to create (if necessary) and return ourMBeanServerinstance.javax.management.MBeanOperationInfogetMethodInfo(javax.management.ObjectName oname, java.lang.String opName)Find the operation info for a methodprivate ModelerSourcegetModelerSource(java.lang.String type)static RegistrygetRegistry()Deprecated.Not enough info - use the method that takes CL and domainstatic RegistrygetRegistry(java.lang.Object key, java.lang.Object guard)Factory method to create (if necessary) and return ourRegistryinstance.static javax.management.MBeanServergetServer()Deprecated.Use the instance methodjava.lang.StringgetType(javax.management.ObjectName oname, java.lang.String attName)Get the type of an attribute of the object, from the metadata.voidinvoke(java.util.List mbeans, java.lang.String operation, boolean failFirst)Invoke a operation on a list of mbeans.java.util.Listload(java.lang.String sourceType, java.lang.Object source, java.lang.String param)Deprecated.bad interface, mixing of metadata and mbeansvoidloadCachedDescriptors(java.lang.Object source)Deprecated.Loaded automatically or using a File or Url ending in .servoidloadDescriptors(java.lang.Object source)Load the registry from the XML input found in the specified input stream.voidloadDescriptors(java.lang.String packageName, java.lang.ClassLoader classLoader)Lookup the component descriptor in the package and in the parent packages.voidloadDescriptors(java.lang.String sourceType, java.lang.Object source, java.lang.String param)Deprecated.java.util.ListloadMBeans(java.lang.Object source)java.util.ListloadMBeans(java.lang.Object source, java.lang.ClassLoader cl)Load an extended mlet file.voidloadMetadata(java.lang.Object source)Load descriptors.private voidloadMetaInfDescriptors(java.lang.ClassLoader cl)Discover all META-INF/modeler.xml files in classpath and register the componentsstatic voidloadRegistry(java.io.InputStream stream)Deprecated.use normal class method insteadvoidpostDeregister()voidpostRegister(java.lang.Boolean registrationDone)voidpreDeregister()javax.management.ObjectNamepreRegister(javax.management.MBeanServer server, javax.management.ObjectName name)voidregisterComponent(java.lang.Object bean, java.lang.String oname, java.lang.String type)Register a bean by creating a modeler mbean and adding it to the MBeanServer.voidregisterComponent(java.lang.Object bean, java.lang.String domain, java.lang.String type, java.lang.String name)Deprecated.- may still be used in code using pre-1.1 buildsvoidregisterComponent(java.lang.Object bean, javax.management.ObjectName oname, java.lang.String type)Register a component XXX make it privatevoidremoveManagedBean(ManagedBean bean)Remove an existing bean from the set of beans known to this registry.voidresetMetadata()voidsetGuard(java.lang.Object guard)Set a guard object that will prevent access to this registry by unauthorized componentsvoidsetMBeanServer(javax.management.MBeanServer server)Set theMBeanServerto be utilized for our registered management beans.static voidsetServer(javax.management.MBeanServer mbeanServer)Deprecated.Use the instance methodstatic voidsetUseContextClassLoader(boolean enable)Allow containers to isolate apps.private java.lang.StringsourceTypeFromExt(java.lang.String s)voidstop()Lifecycle method - clean up the registry metadata.voidunregisterComponent(java.lang.String oname)Unregister a component.voidunregisterComponent(java.lang.String domain, java.lang.String name)voidunregisterComponent(javax.management.ObjectName oname)Unregister a component.voidunregisterRegistry(java.lang.ClassLoader loader)Called by a registry or by the container to unload a loader
-
-
-
Field Detail
-
MODELER_MANIFEST
public static java.lang.String MODELER_MANIFEST
Experimental support for manifest-based discovery.
-
log
private static org.apache.commons.logging.Log log
The Log instance to which we will write our log messages.
-
perLoaderRegistries
private static java.util.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.
-
server
private javax.management.MBeanServer server
TheMBeanServerinstance that we will use to register management beans.
-
descriptors
private final java.util.HashMap descriptors
The set of ManagedBean instances for the beans this registry knows about, keyed by name.
-
descriptorsByClass
private final java.util.HashMap descriptorsByClass
List of managed byeans, keyed by class name
-
searchedPaths
private final java.util.Map searchedPaths
-
guard
private java.lang.Object guard
-
idDomains
private java.util.Hashtable idDomains
-
ids
private java.util.Hashtable ids
-
-
Method Detail
-
getRegistry
public static Registry getRegistry(java.lang.Object key, java.lang.Object guard)
Factory method to create (if necessary) and return ourRegistryinstance. 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
-
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
-
setGuard
public void setGuard(java.lang.Object guard)
Set a guard object that will prevent access to this registry by unauthorized components- Parameters:
guard-- Since:
- 1.1
-
stop
public void stop()
Lifecycle method - clean up the registry metadata.- Specified by:
stopin interfaceRegistryMBean- Since:
- 1.1
-
loadMBeans
public java.util.List loadMBeans(java.lang.Object source, java.lang.ClassLoader cl) throws java.lang.ExceptionLoad 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:
loadMBeansin interfaceRegistryMBean- Parameters:
source- InputStream or URL of the filecl- 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
- Throws:
java.lang.Exception- Since:
- 1.1
-
loadMetadata
public void loadMetadata(java.lang.Object source) throws java.lang.ExceptionLoad 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:
loadMetadatain interfaceRegistryMBean- Parameters:
source-- Throws:
java.lang.Exception
-
registerComponent
public void registerComponent(java.lang.Object bean, java.lang.String oname, java.lang.String type) throws java.lang.ExceptionRegister 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:
registerComponentin interfaceRegistryMBean- Parameters:
bean- Object to be registeredoname- Name used for registrationtype- 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.- Throws:
java.lang.Exception- Since:
- 1.1
-
unregisterComponent
public void unregisterComponent(java.lang.String oname)
Unregister a component. We'll first check if it is registered, and mask all errors. This is mostly a helper.- Specified by:
unregisterComponentin interfaceRegistryMBean- Parameters:
oname-- Since:
- 1.1
-
invoke
public void invoke(java.util.List mbeans, java.lang.String operation, boolean failFirst) throws java.lang.ExceptionInvoke a operation on a list of mbeans. Can be used to implement lifecycle operations.- Specified by:
invokein interfaceRegistryMBean- Parameters:
mbeans- list of ObjectName on which we'll invoke the operationsoperation- Name of the operation ( init, start, stop, etc)failFirst- If false, exceptions will be ignored- Throws:
java.lang.Exception- Since:
- 1.1
-
getId
public int getId(java.lang.String domain, java.lang.String name)Return an int ID for faster access. Will be used for notifications and for other operations we want to optimize.- Specified by:
getIdin interfaceRegistryMBean- Parameters:
domain- Namespacename- Type of the notification- Returns:
- An unique id for the domain:name combination
- Since:
- 1.1
-
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
-
findManagedBean
public ManagedBean findManagedBean(java.lang.String name)
Find and return the managed bean definition for the specified bean name, if any; otherwise returnnull.- 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 java.lang.String[] findManagedBeans()
Return the set of bean names for all managed beans known to this registry.- Since:
- 1.0
-
findManagedBeans
public java.lang.String[] findManagedBeans(java.lang.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, ornullto select beans that do not belong to a group- Since:
- 1.0
-
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
-
getServer
public static javax.management.MBeanServer getServer()
Deprecated.Use the instance methodFactory method to create (if necessary) and return ourMBeanServerinstance.- Since:
- 1.0
-
setServer
public static void setServer(javax.management.MBeanServer mbeanServer)
Deprecated.Use the instance methodSet theMBeanServerto be utilized for our registered management beans.- Parameters:
mbeanServer- The newMBeanServerinstance- Since:
- 1.0
-
loadRegistry
public static void loadRegistry(java.io.InputStream stream) throws java.lang.ExceptionDeprecated.use normal class method insteadLoad the registry from the XML input found in the specified input stream.- Parameters:
stream- InputStream containing the registry configuration information- Throws:
java.lang.Exception- if any parsing or processing error occurs- Since:
- 1.0
-
getRegistry
public static Registry getRegistry()
Deprecated.Not enough info - use the method that takes CL and domainGet a "singelton" registry, or one per thread if setUseContextLoader was called- Since:
- 1.0
-
getType
public java.lang.String getType(javax.management.ObjectName oname, java.lang.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
-
getMethodInfo
public javax.management.MBeanOperationInfo getMethodInfo(javax.management.ObjectName oname, java.lang.String opName)Find the operation info for a method- Parameters:
oname-opName-- Returns:
- the operation info for the specified operation
-
unregisterComponent
public void unregisterComponent(javax.management.ObjectName oname)
Unregister a component. This is just a helper that avoids exceptions by checking if the mbean is already registered- Parameters:
oname-
-
getMBeanServer
public javax.management.MBeanServer getMBeanServer()
Factory method to create (if necessary) and return ourMBeanServerinstance.
-
findManagedBean
public ManagedBean findManagedBean(java.lang.Object bean, java.lang.Class beanClass, java.lang.String type) throws java.lang.Exception
Find or load metadata.- Throws:
java.lang.Exception
-
convertValue
public java.lang.Object convertValue(java.lang.String type, java.lang.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 valuevalue- String value to be converted- Returns:
- Converted value
-
load
public java.util.List load(java.lang.String sourceType, java.lang.Object source, java.lang.String param) throws java.lang.ExceptionDeprecated.bad interface, mixing of metadata and mbeansExperimental.- Parameters:
sourceType-source-param-- Returns:
- List of descriptors
- Throws:
java.lang.Exception
-
sourceTypeFromExt
private java.lang.String sourceTypeFromExt(java.lang.String s)
-
registerComponent
public void registerComponent(java.lang.Object bean, javax.management.ObjectName oname, java.lang.String type) throws java.lang.ExceptionRegister a component XXX make it private- Parameters:
bean-oname-type-- Throws:
java.lang.Exception
-
loadDescriptors
public void loadDescriptors(java.lang.String packageName, java.lang.ClassLoader classLoader)Lookup the component descriptor in the package and in the parent packages.- Parameters:
packageName-
-
loadDescriptors
public void loadDescriptors(java.lang.String sourceType, java.lang.Object source, java.lang.String param) throws java.lang.ExceptionDeprecated.Experimental. Will become private, some code may still use it- Parameters:
sourceType-source-param-- Throws:
java.lang.Exception
-
loadMetaInfDescriptors
private void loadMetaInfDescriptors(java.lang.ClassLoader cl)
Discover all META-INF/modeler.xml files in classpath and register the components- Since:
- EXPERIMENTAL
-
findDescriptor
private void findDescriptor(java.lang.Class beanClass, java.lang.String type)Lookup the component descriptor in the package and in the parent packages.- Parameters:
beanClass-type-
-
getModelerSource
private ModelerSource getModelerSource(java.lang.String type) throws java.lang.Exception
- Throws:
java.lang.Exception
-
preRegister
public javax.management.ObjectName preRegister(javax.management.MBeanServer server, javax.management.ObjectName name) throws java.lang.Exception- Specified by:
preRegisterin interfacejavax.management.MBeanRegistration- Throws:
java.lang.Exception
-
postRegister
public void postRegister(java.lang.Boolean registrationDone)
- Specified by:
postRegisterin interfacejavax.management.MBeanRegistration
-
preDeregister
public void preDeregister() throws java.lang.Exception- Specified by:
preDeregisterin interfacejavax.management.MBeanRegistration- Throws:
java.lang.Exception
-
postDeregister
public void postDeregister()
- Specified by:
postDeregisterin interfacejavax.management.MBeanRegistration
-
unregisterRegistry
public void unregisterRegistry(java.lang.ClassLoader loader)
Called by a registry or by the container to unload a loader- Parameters:
loader-
-
findManagedBean
public ManagedBean findManagedBean(java.lang.Class beanClass, java.lang.String type) throws java.lang.Exception
- Throws:
java.lang.Exception
-
setMBeanServer
public void setMBeanServer(javax.management.MBeanServer server)
Set theMBeanServerto be utilized for our registered management beans.- Parameters:
server- The newMBeanServerinstance
-
resetMetadata
public void resetMetadata()
-
loadDescriptors
public void loadDescriptors(java.lang.Object source) throws java.lang.ExceptionLoad 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.- Throws:
java.lang.Exception- if any parsing or processing error occurs
-
registerComponent
public void registerComponent(java.lang.Object bean, java.lang.String domain, java.lang.String type, java.lang.String name) throws java.lang.ExceptionDeprecated.- may still be used in code using pre-1.1 builds- Throws:
java.lang.Exception
-
unregisterComponent
public void unregisterComponent(java.lang.String domain, java.lang.String name)
-
loadMBeans
public java.util.List loadMBeans(java.lang.Object source) throws java.lang.Exception- Throws:
java.lang.Exception
-
loadCachedDescriptors
public void loadCachedDescriptors(java.lang.Object source) throws java.lang.ExceptionDeprecated.Loaded automatically or using a File or Url ending in .serLoad 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.- Throws:
java.lang.Exception- if any parsing or processing error occurs
-
-