| Method from org.apache.commons.modeler.Registry Detail: |
public void addManagedBean(ManagedBean bean) {
// XXX Use group + name
synchronized(descriptors) {
descriptors.put(bean.getName(), bean);
if( bean.getType() != null ) {
descriptorsByClass.put( bean.getType(), bean );
}
}
}
Add a new bean metadata to the set of beans known to this registry.
This is used by internal components. |
public Object convertValue(String type,
String value) {
Object objValue=value;
if( type==null || "java.lang.String".equals( type )) {
// string is default
objValue=value;
} else if( "javax.management.ObjectName".equals( type ) ||
"ObjectName".equals( type )) {
try {
objValue=new ObjectName( value );
} catch (MalformedObjectNameException e) {
return null;
}
} else if( "java.lang.Integer".equals( type ) ||
"int".equals( type )) {
objValue=new Integer( value );
} else if("java.lang.Long".equals( type ) ||
"long".equals( type )) {
objValue = new Long( value );
} else if( "java.lang.Boolean".equals( type ) ||
"boolean".equals( type )) {
objValue=Boolean.valueOf( value );
}
return objValue;
}
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 |
public ManagedBean findManagedBean(String name) {
// XXX Group ?? Use Group + Type
synchronized(descriptors) {
ManagedBean mb=((ManagedBean) descriptors.get(name));
if( mb==null )
mb=(ManagedBean)descriptorsByClass.get(name);
return mb;
}
}
Find and return the managed bean definition for the specified
bean name, if any; otherwise return null. |
public ManagedBean findManagedBean(Class beanClass,
String type) throws Exception {
return findManagedBean(null, beanClass, type);
}
|
public ManagedBean findManagedBean(Object bean,
Class beanClass,
String type) throws Exception {
if( bean!=null && beanClass==null ) {
beanClass=bean.getClass();
}
if( type==null ) {
type=beanClass.getName();
}
// first look for existing descriptor
ManagedBean managed = findManagedBean(type);
// Search for a descriptor in the same package
if( managed==null ) {
// check package and parent packages
if( log.isDebugEnabled() ) {
log.debug( "Looking for descriptor ");
}
findDescriptor( beanClass, type );
managed=findManagedBean(type);
}
if( bean instanceof DynamicMBean ) {
if( log.isDebugEnabled() ) {
log.debug( "Dynamic mbean support ");
}
// Dynamic mbean
loadDescriptors("MbeansDescriptorsDynamicMBeanSource",
bean, type);
managed=findManagedBean(type);
}
// Still not found - use introspection
if( managed==null ) {
if( log.isDebugEnabled() ) {
log.debug( "Introspecting ");
}
// introspection
loadDescriptors("MbeansDescriptorsIntrospectionSource",
beanClass, type);
managed=findManagedBean(type);
if( managed==null ) {
log.warn( "No metadata found for " + type );
return null;
}
managed.setName( type );
addManagedBean(managed);
}
return managed;
}
|
public String[] findManagedBeans() {
synchronized(descriptors) {
return ((String[]) descriptors.keySet().toArray(new String[0]));
}
}
Return the set of bean names for all managed beans known to
this registry. |
public String[] findManagedBeans(String group) {
ArrayList results = new ArrayList();
synchronized (descriptors) {
for (Iterator items = descriptors.values().iterator(); items.hasNext(); ) {
ManagedBean item = (ManagedBean) items.next();
if (group == null) {
if (item.getGroup() == null) {
results.add(item.getName());
}
} else if (group.equals(item.getGroup())) {
results.add(item.getName());
}
}
}
String values[] = new String[results.size()];
return ((String[]) results.toArray(values));
}
Return the set of bean names for all managed beans known to
this registry that belong to the specified group. |
public synchronized int getId(String domain,
String name) {
if( domain==null) {
domain="";
}
Hashtable domainTable=(Hashtable)idDomains.get( domain );
if( domainTable == null ) {
domainTable=new Hashtable();
idDomains.put( domain, domainTable);
}
if( name==null ) {
name="";
}
Integer i=(Integer)domainTable.get(name);
if( i!= null ) {
return i.intValue();
}
int id[]=(int [])ids.get( domain );
if( id == null ) {
id=new int[1];
ids.put( domain, id);
}
int code=id[0]++;
domainTable.put( name, new Integer( code ));
return code;
}
Return an int ID for faster access. Will be used for notifications
and for other operations we want to optimize. |
public synchronized MBeanServer getMBeanServer() {
long t1=System.currentTimeMillis();
if (server == null) {
if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) {
server=(MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
if( log.isDebugEnabled() ) {
log.debug("Using existing MBeanServer " + (System.currentTimeMillis() - t1 ));
}
} else {
server=MBeanServerFactory.createMBeanServer();
if( log.isDebugEnabled() ) {
log.debug("Creating MBeanServer"+ (System.currentTimeMillis() - t1 ));
}
}
}
return (server);
}
Factory method to create (if necessary) and return our
MBeanServer instance. |
public MBeanOperationInfo getMethodInfo(ObjectName oname,
String opName) {
String type=null;
MBeanInfo info;
try {
info=server.getMBeanInfo(oname);
} catch (Exception e) {
log.info( "Can't find metadata " + oname );
return null;
}
MBeanOperationInfo attInfo[]=info.getOperations();
for( int i=0; i< attInfo.length; i++ ) {
if( opName.equals(attInfo[i].getName())) {
return attInfo[i];
}
}
return null;
}
Find the operation info for a method |
public static synchronized Registry getRegistry() {
return getRegistry(null, null);
} Deprecated! Not - enough info - use the method that takes CL and domain
Get a "singelton" registry, or one per thread if setUseContextLoader
was called |
public static synchronized Registry getRegistry(Object key,
Object guard) {
Registry localRegistry;
if( perLoaderRegistries!=null ) {
if( key==null )
key=Thread.currentThread().getContextClassLoader();
if( key != null ) {
localRegistry=(Registry)perLoaderRegistries.get(key);
if( localRegistry == null ) {
localRegistry=new Registry();
localRegistry.guard=guard;
perLoaderRegistries.put( key, localRegistry );
return localRegistry;
}
if( localRegistry.guard != null &&
localRegistry.guard != guard ) {
return null; // XXX Should I throw a permission ex ?
}
return localRegistry;
}
}
// static
if (registry == null) {
registry = new Registry();
}
if( registry.guard != null &&
registry.guard != guard ) {
return null;
}
return (registry);
}
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. |
public static MBeanServer getServer() {
return Registry.getRegistry().getMBeanServer();
} Deprecated! Use - the instance method
Factory method to create (if necessary) and return our
MBeanServer instance. |
public String getType(ObjectName oname,
String attName) {
String type=null;
MBeanInfo info;
try {
info=server.getMBeanInfo(oname);
} catch (Exception e) {
log.info( "Can't find metadata for object" + oname );
return null;
}
MBeanAttributeInfo attInfo[]=info.getAttributes();
for( int i=0; i< attInfo.length; i++ ) {
if( attName.equals(attInfo[i].getName())) {
type=attInfo[i].getType();
return type;
}
}
return null;
}
Get the type of an attribute of the object, from the metadata. |
public void invoke(List mbeans,
String operation,
boolean failFirst) throws Exception {
if( mbeans==null ) {
return;
}
Iterator itr=mbeans.iterator();
while(itr.hasNext()) {
Object current=itr.next();
ObjectName oN=null;
try {
if( current instanceof ObjectName) {
oN=(ObjectName)current;
}
if( current instanceof String ) {
oN=new ObjectName( (String)current );
}
if( oN==null ) {
continue;
}
if( getMethodInfo(oN, operation) == null) {
continue;
}
getMBeanServer().invoke(oN, operation,
new Object[] {}, new String[] {});
} catch( Exception t ) {
if( failFirst ) throw t;
log.info("Error initializing " + current + " " + t.toString());
}
}
}
Invoke a operation on a list of mbeans. Can be used to implement
lifecycle operations. |
public List load(String sourceType,
Object source,
String param) throws Exception {
if( log.isTraceEnabled()) {
log.trace("load " + source );
}
String location=null;
String type=null;
Object inputsource=null;
if( source instanceof DynamicMBean ) {
sourceType="MbeansDescriptorsDynamicMBeanSource";
inputsource=source;
} else if( source instanceof URL ) {
URL url=(URL)source;
location=url.toString();
type=param;
inputsource=url.openStream();
if( sourceType == null ) {
sourceType = sourceTypeFromExt(location);
}
} else if( source instanceof File ) {
location=((File)source).getAbsolutePath();
inputsource=new FileInputStream((File)source);
type=param;
if( sourceType == null ) {
sourceType = sourceTypeFromExt(location);
}
} else if( source instanceof InputStream ) {
type=param;
inputsource=source;
} else if( source instanceof Class ) {
location=((Class)source).getName();
type=param;
inputsource=source;
if( sourceType== null ) {
sourceType="MbeansDescriptorsIntrospectionSource";
}
}
if( sourceType==null ) {
sourceType="MbeansDescriptorsDOMSource";
}
ModelerSource ds=getModelerSource(sourceType);
List mbeans=ds.loadDescriptors(this, location, type, inputsource);
return mbeans;
} Deprecated! bad - interface, mixing of metadata and mbeans
|
public void loadCachedDescriptors(Object source) throws Exception {
loadDescriptors("MbeansDescriptorsSerSource", source, null );
} 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. |
public void loadDescriptors(Object source) throws Exception {
loadDescriptors("MbeansDescriptorsDOMSource", source, null );
}
Load the registry from the XML input found in the specified input
stream. |
public synchronized void loadDescriptors(String packageName,
ClassLoader classLoader) {
String res=packageName.replace( '.", '/");
if( log.isTraceEnabled() ) {
log.trace("Finding descriptor " + res );
}
if( searchedPaths.get( packageName ) != null ) {
return;
}
String descriptors=res + "/mbeans-descriptors.ser";
URL dURL=classLoader.getResource( descriptors );
if( dURL == null ) {
descriptors=res + "/mbeans-descriptors.xml";
dURL=classLoader.getResource( descriptors );
}
if( dURL == null ) {
return;
}
log.debug( "Found " + dURL);
searchedPaths.put( packageName, dURL );
try {
if( descriptors.endsWith(".xml" ))
loadDescriptors("MbeansDescriptorsDOMSource", dURL, null);
else
loadDescriptors("MbeansDescriptorsSerSource", dURL, null);
} catch(Exception ex ) {
log.error("Error loading " + dURL);
}
}
Lookup the component descriptor in the package and
in the parent packages. |
public void loadDescriptors(String sourceType,
Object source,
String param) throws Exception {
List mbeans=load( sourceType, source, param );
if( mbeans == null) return;
Iterator itr=mbeans.iterator();
while( itr.hasNext() ) {
Object mb=itr.next();
if( mb instanceof ManagedBean) {
addManagedBean((ManagedBean)mb);
}
}
} Deprecated!
Experimental. Will become private, some code may still use it |
public List loadMBeans(Object source) throws Exception {
return loadMBeans( source, null );
}
|
public List loadMBeans(Object source,
ClassLoader cl) throws Exception {
return load("MbeansSource", source, null );
}
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. |
public void loadMetadata(Object source) throws Exception {
if( source instanceof ClassLoader ) {
loadMetaInfDescriptors((ClassLoader)source);
} else {
loadDescriptors( null, source, null );
}
}
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. |
public static void loadRegistry(InputStream stream) throws Exception {
Registry registry = getRegistry();
registry.loadMetadata(stream);
} Deprecated! use - normal class method instead
Load the registry from the XML input found in the specified input
stream. |
public void postDeregister() {
}
|
public void postRegister(Boolean registrationDone) {
}
|
public void preDeregister() throws Exception {
}
|
public ObjectName preRegister(MBeanServer server,
ObjectName name) throws Exception {
this.server=server;
return name;
}
|
public void registerComponent(Object bean,
String oname,
String type) throws Exception {
registerComponent(bean, new ObjectName(oname), type);
}
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. |
public void registerComponent(Object bean,
ObjectName oname,
String type) throws Exception {
if( log.isDebugEnabled() ) {
log.debug( "Managed= "+ oname);
}
if( bean ==null ) {
log.error("Null component " + oname );
return;
}
try {
if( type==null ) {
type=bean.getClass().getName();
}
ManagedBean managed = findManagedBean(bean.getClass(), type);
// The real mbean is created and registered
ModelMBean mbean = managed.createMBean(bean);
if( getMBeanServer().isRegistered( oname )) {
if( log.isDebugEnabled()) {
log.debug("Unregistering existing component " + oname );
}
getMBeanServer().unregisterMBean( oname );
}
getMBeanServer().registerMBean( mbean, oname);
} catch( Exception ex) {
log.error("Error registering " + oname, ex );
throw ex;
}
}
Register a component
XXX make it private |
public void registerComponent(Object bean,
String domain,
String type,
String name) throws Exception {
StringBuffer sb=new StringBuffer();
sb.append( domain ).append(":");
sb.append( name );
String nameStr=sb.toString();
ObjectName oname=new ObjectName( nameStr );
registerComponent(bean, oname, type );
} Deprecated! - - may still be used in code using pre-1.1 builds
|
public void removeManagedBean(ManagedBean bean) {
// TODO: change this to use group/name
synchronized (descriptors) {
descriptors.remove(bean.getName());
descriptorsByClass.remove( bean.getType());
}
}
Remove an existing bean from the set of beans known to this registry. |
public void resetMetadata() {
stop();
}
|
public void setGuard(Object guard) {
if( this.guard!=null ) {
return; // already set, only once
}
this.guard=guard;
}
Set a guard object that will prevent access to this registry
by unauthorized components |
public void setMBeanServer(MBeanServer server) {
this.server=server;
}
Set the MBeanServer to be utilized for our
registered management beans. |
public static void setServer(MBeanServer mbeanServer) {
Registry.getRegistry().setMBeanServer(mbeanServer);
} Deprecated! Use - the instance method
Set the MBeanServer to be utilized for our
registered management beans. |
public static void setUseContextClassLoader(boolean enable) {
if( enable ) {
perLoaderRegistries=new HashMap();
}
}
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 |
public void stop() {
synchronized(descriptors) {
descriptorsByClass.clear();
descriptors.clear();
searchedPaths.clear();
}
}
Lifecycle method - clean up the registry metadata. |
public void unregisterComponent(String oname) {
try {
unregisterComponent(new ObjectName(oname));
} catch (MalformedObjectNameException e) {
log.info("Error creating object name " + e );
}
}
Unregister a component. We'll first check if it is registered,
and mask all errors. This is mostly a helper. |
public void unregisterComponent(ObjectName oname) {
try {
if( getMBeanServer().isRegistered(oname)) {
getMBeanServer().unregisterMBean(oname);
}
} catch( Throwable t ) {
log.error( "Error unregistering mbean ", t);
}
}
Unregister a component. This is just a helper that
avoids exceptions by checking if the mbean is already registered |
public void unregisterComponent(String domain,
String name) {
try {
ObjectName oname=new ObjectName( domain + ":" + name );
// XXX remove from our tables.
getMBeanServer().unregisterMBean( oname );
} catch( Throwable t ) {
log.error( "Error unregistering mbean ", t );
}
}
|
public void unregisterRegistry(ClassLoader loader) {
// XXX Cleanup ?
perLoaderRegistries.remove(loader);
}
Called by a registry or by the container to unload a loader |