Methods Summary |
---|
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.
// XXX Use group + name
descriptors.put(bean.getName(), bean);
if( bean.getType() != null ) {
descriptorsByClass.put( bean.getType(), bean );
}
|
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
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=new Boolean( value );
}
return objValue;
|
private void | findDescriptor(java.lang.Class beanClass, java.lang.String type)Lookup the component descriptor in the package and
in the parent packages.
if( type==null ) {
type=beanClass.getName();
}
ClassLoader classLoader=null;
if( beanClass!=null ) {
classLoader=beanClass.getClassLoader();
}
if( classLoader==null ) {
classLoader=Thread.currentThread().getContextClassLoader();
}
if( classLoader==null ) {
classLoader=this.getClass().getClassLoader();
}
String className=type;
String pkg=className;
while( pkg.indexOf( ".") > 0 ) {
int lastComp=pkg.lastIndexOf( ".");
if( lastComp <= 0 ) return;
pkg=pkg.substring(0, lastComp);
if( searchedPaths.get( pkg ) != null ) {
return;
}
loadDescriptors(pkg, classLoader);
}
return;
|
public ManagedBean | findManagedBean(java.lang.String name)Find and return the managed bean definition for the specified
bean name, if any; otherwise return null .
// XXX Group ?? Use Group + Type
ManagedBean mb=((ManagedBean) descriptors.get(name));
if( mb==null )
mb=(ManagedBean)descriptorsByClass.get(name);
return mb;
|
public ManagedBean | findManagedBean(java.lang.Object bean, java.lang.Class beanClass, java.lang.String type)Find or load metadata.
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 ManagedBean | findManagedBean(java.lang.Class beanClass, java.lang.String type)
return findManagedBean(null, beanClass, type);
|
public java.lang.String[] | findManagedBeans()Return the set of bean names for all managed beans known to
this registry.
return ((String[]) descriptors.keySet().toArray(new String[0]));
|
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.
ArrayList results = new ArrayList();
Iterator items = descriptors.values().iterator();
while (items.hasNext()) {
ManagedBean item = (ManagedBean) items.next();
if ((group == null) && (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));
|
public synchronized 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.
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;
|
public synchronized javax.management.MBeanServer | getMBeanServer()Factory method to create (if necessary) and return our
MBeanServer instance.
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);
|
public javax.management.MBeanOperationInfo | getMethodInfo(javax.management.ObjectName oname, java.lang.String opName)Find the operation info for a method
String type=null;
MBeanInfo info=null;
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;
|
private org.apache.tomcat.util.modeler.modules.ModelerSource | getModelerSource(java.lang.String type)
if( type==null ) type="MbeansDescriptorsDigesterSource";
if( type.indexOf( ".") < 0 ) {
type="org.apache.tomcat.util.modeler.modules." + type;
}
Class c=Class.forName( type );
ModelerSource ds=(ModelerSource)c.newInstance();
return ds;
|
public static synchronized org.apache.tomcat.util.modeler.Registry | getRegistry()Get a "singelton" registry, or one per thread if setUseContextLoader
was called
return getRegistry(null, null);
|
public static synchronized org.apache.tomcat.util.modeler.Registry | getRegistry(java.lang.Object key, java.lang.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.
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.key=key;
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);
|
public static javax.management.MBeanServer | getServer()Factory method to create (if necessary) and return our
MBeanServer instance.
return Registry.getRegistry().getMBeanServer();
|
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.
String type=null;
MBeanInfo info=null;
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;
|
public void | invoke(java.util.List mbeans, java.lang.String operation, boolean failFirst)Invoke a operation on a list of mbeans. Can be used to implement
lifecycle operations.
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());
}
}
|
public java.util.List | load(java.lang.String sourceType, java.lang.Object source, java.lang.String param)Experimental.
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="MbeansDescriptorsDigesterSource";
}
ModelerSource ds=getModelerSource(sourceType);
List mbeans=ds.loadDescriptors(this, location, type, inputsource);
return mbeans;
|
public void | loadCachedDescriptors(java.lang.Object source)Load the registry from a cached .ser file. This is typically 2-3 times
faster than parsing the XML.
loadDescriptors("MbeansDescriptorsSerSource", source, null );
|
public void | loadDescriptors(java.lang.String packageName, java.lang.ClassLoader classLoader)Lookup the component descriptor in the package and
in the parent packages.
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("MbeansDescriptorsDigesterSource", dURL, null);
else
loadDescriptors("MbeansDescriptorsSerSource", dURL, null);
return;
} catch(Exception ex ) {
log.error("Error loading " + dURL);
}
return;
|
public void | loadDescriptors(java.lang.String sourceType, java.lang.Object source, java.lang.String param)Experimental. Will become private, some code may still use it
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);
}
}
|
public void | loadDescriptors(java.lang.Object source)Load the registry from the XML input found in the specified input
stream.
loadDescriptors("MbeansDescriptorsDigesterSource", source, null );
|
public java.util.List | loadMBeans(java.lang.Object source, java.lang.ClassLoader cl)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.
return load("MbeansSource", source, null );
|
public void | loadMetadata(java.lang.Object source)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.
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.
loadDescriptors( null, source, null );
|
public static void | loadRegistry(java.io.InputStream stream)Load the registry from the XML input found in the specified input
stream.
Registry registry = getRegistry();
registry.loadMetadata(stream);
|
public void | postDeregister()
|
public void | postRegister(java.lang.Boolean registrationDone)
|
public void | preDeregister()
|
public javax.management.ObjectName | preRegister(javax.management.MBeanServer server, javax.management.ObjectName name)
this.server=server;
return name;
|
public void | registerComponent(java.lang.Object bean, javax.management.ObjectName oname, java.lang.String type)Register a component
XXX make it private
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
DynamicMBean 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;
}
|
public void | registerComponent(java.lang.Object bean, java.lang.String domain, java.lang.String type, java.lang.String name)
StringBuffer sb=new StringBuffer();
sb.append( domain ).append(":");
sb.append( name );
String nameStr=sb.toString();
ObjectName oname=new ObjectName( nameStr );
registerComponent(bean, oname, type );
|
public void | registerComponent(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.
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.
registerComponent(bean, new ObjectName(oname), type);
|
public void | removeManagedBean(ManagedBean bean)Remove an existing bean from the set of beans known to this registry.
// TODO: change this to use group/name
descriptors.remove(bean.getName());
descriptorsByClass.remove( bean.getType());
|
public void | resetMetadata()
stop();
|
public void | setMBeanServer(javax.management.MBeanServer server)Set the MBeanServer to be utilized for our
registered management beans.
this.server=server;
|
public static void | setServer(javax.management.MBeanServer mbeanServer)Set the MBeanServer to be utilized for our
registered management beans.
Registry.getRegistry().setMBeanServer(mbeanServer);
|
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
if( enable ) {
perLoaderRegistries=new HashMap();
}
|
private java.lang.String | sourceTypeFromExt(java.lang.String s)
if( s.endsWith( ".ser")) {
return "MbeansDescriptorsSerSource";
}
else if( s.endsWith(".xml")) {
return "MbeansDescriptorsDigesterSource";
}
return null;
|
public void | stop()Lifecycle method - clean up the registry metadata.
Called from resetMetadata().
descriptorsByClass = new HashMap();
descriptors = new HashMap();
searchedPaths=new HashMap();
|
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
try {
if( getMBeanServer().isRegistered(oname)) {
getMBeanServer().unregisterMBean(oname);
}
} catch( Throwable t ) {
log.error( "Error unregistering mbean ", t);
}
|
public void | unregisterComponent(java.lang.String domain, java.lang.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 | 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.
try {
unregisterComponent(new ObjectName(oname));
} catch (MalformedObjectNameException e) {
log.info("Error creating object name " + e );
}
|
public void | unregisterRegistry(java.lang.ClassLoader loader)Called by a registry or by the container to unload a loader
// XXX Cleanup ?
perLoaderRegistries.remove(loader);
|