You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by co...@apache.org on 2003/02/26 23:17:33 UTC
cvs commit: jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules MbeansDescriptorsDOMSource.java MbeansDescriptorsDigesterSource.java MbeansDescriptorsIntrospectionSource.java MbeansDescriptorsSerSource.java MbeansSource.java ModelerSource.java
costin 2003/02/26 14:17:32
Modified: modeler/src/java/org/apache/commons/modeler Registry.java
modeler/src/java/org/apache/commons/modeler/modules
MbeansDescriptorsDOMSource.java
MbeansDescriptorsDigesterSource.java
MbeansDescriptorsIntrospectionSource.java
MbeansDescriptorsSerSource.java MbeansSource.java
ModelerSource.java
Log:
Few more changes ( to the new APIs ).
Eliminate the side-effects - the descriptor reader will return a list.
Improve the MbeansSource - it can read jboss or mlet style xml files
and create the mbeans. It no longer needs the type to be specified.
Also added a method to allow support for lifecycle methods on the
mbeans.
Most JMX applications include a lifecycle mechanism - and the method names
and behavior is pretty common. If an mbean is used in such environment -
the init/start/stop/destroy will be called automatically. The intention
is to allow for such mbeans to work if used outside such a container, and
to support this behavior in tomcat standalone.
Revision Changes Path
1.20 +47 -4 jakarta-commons/modeler/src/java/org/apache/commons/modeler/Registry.java
Index: Registry.java
===================================================================
RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/Registry.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- Registry.java 20 Feb 2003 05:54:22 -0000 1.19
+++ Registry.java 26 Feb 2003 22:17:32 -0000 1.20
@@ -313,22 +313,65 @@
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);
+ }
+ }
+ }
+
+ public List load( String sourceType, Object source, String param)
+ throws Exception
+ {
if( log.isTraceEnabled())
- log.trace("loadDescriptors " + source );
+ log.trace("load " + source );
+
ModelerSource ds=getModelerSource(sourceType);
+ List mbeans=null;
if( source instanceof URL ) {
URL url=(URL)source;
InputStream stream=url.openStream();
- ds.loadDescriptors(this, url.toString(), param, stream);
+ mbeans=ds.loadDescriptors(this, url.toString(), param, stream);
}
if( source instanceof InputStream ) {
- ds.loadDescriptors(this, null, param, source);
+ mbeans=ds.loadDescriptors(this, null, param, source);
}
if( source instanceof Class ) {
- ds.loadDescriptors(this, ((Class)source).getName(), param, source);
+ mbeans=ds.loadDescriptors(this, ((Class)source).getName(), param, source);
+ }
+ return mbeans;
+ }
+
+ public void invoke( List mbeans, String operation, boolean failFirst )
+ throws Throwable
+ {
+ 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;
+ getMBeanServer().invoke(oN, operation,
+ new Object[] {}, new String[] {});
+
+ } catch( Throwable t ) {
+ if( failFirst ) throw t;
+ log.info("Error initializing " + current);
+ }
}
}
1.5 +7 -2 jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansDescriptorsDOMSource.java
Index: MbeansDescriptorsDOMSource.java
===================================================================
RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansDescriptorsDOMSource.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- MbeansDescriptorsDOMSource.java 23 Jan 2003 19:42:08 -0000 1.4
+++ MbeansDescriptorsDOMSource.java 26 Feb 2003 22:17:32 -0000 1.5
@@ -68,6 +68,8 @@
import org.apache.commons.logging.LogFactory;
import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
public class MbeansDescriptorsDOMSource extends ModelerSource
@@ -78,6 +80,7 @@
String location;
String type;
Object source;
+ List mbeans=new ArrayList();
public void setRegistry(Registry reg) {
this.registry=reg;
@@ -99,7 +102,7 @@
this.source=source;
}
- public void loadDescriptors( Registry registry, String location,
+ public List loadDescriptors( Registry registry, String location,
String type, Object source)
throws Exception
{
@@ -108,6 +111,7 @@
setType(type);
setSource(source);
execute();
+ return mbeans;
}
public void execute() throws Exception {
@@ -320,7 +324,8 @@
}
// Add the completed managed bean info to the registry
- registry.addManagedBean(managed);
+ //registry.addManagedBean(managed);
+ mbeans.add( managed );
}
1.5 +8 -4 jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansDescriptorsDigesterSource.java
Index: MbeansDescriptorsDigesterSource.java
===================================================================
RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansDescriptorsDigesterSource.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- MbeansDescriptorsDigesterSource.java 23 Jan 2003 19:42:08 -0000 1.4
+++ MbeansDescriptorsDigesterSource.java 26 Feb 2003 22:17:32 -0000 1.5
@@ -67,6 +67,8 @@
import java.io.InputStream;
import java.net.URL;
+import java.util.List;
+import java.util.ArrayList;
public class MbeansDescriptorsDigesterSource extends ModelerSource
{
@@ -77,6 +79,7 @@
String location;
String type;
Object source;
+ List mbeans=new ArrayList();
public void setRegistry(Registry reg) {
this.registry=reg;
@@ -98,7 +101,7 @@
this.source=source;
}
- public void loadDescriptors( Registry registry, String location,
+ public List loadDescriptors( Registry registry, String location,
String type, Object source)
throws Exception
{
@@ -107,6 +110,7 @@
setType(type);
setSource(source);
execute();
+ return mbeans;
}
public void execute() throws Exception {
@@ -126,7 +130,7 @@
url.toString());
// Push our registry object onto the stack
- digester.push(registry);
+ digester.push(mbeans);
// Configure the parsing rules
digester.addObjectCreate
@@ -136,8 +140,8 @@
("mbeans-descriptors/mbean");
digester.addSetNext
("mbeans-descriptors/mbean",
- "addManagedBean",
- "org.apache.commons.modeler.ManagedBean");
+ "add",
+ "java.lang.Object");
digester.addObjectCreate
("mbeans-descriptors/mbean/attribute",
1.7 +6 -2 jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansDescriptorsIntrospectionSource.java
Index: MbeansDescriptorsIntrospectionSource.java
===================================================================
RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansDescriptorsIntrospectionSource.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- MbeansDescriptorsIntrospectionSource.java 20 Feb 2003 05:48:34 -0000 1.6
+++ MbeansDescriptorsIntrospectionSource.java 26 Feb 2003 22:17:32 -0000 1.7
@@ -12,6 +12,8 @@
import java.lang.reflect.Modifier;
import java.util.Hashtable;
import java.util.Enumeration;
+import java.util.ArrayList;
+import java.util.List;
import javax.management.ObjectName;
@@ -23,6 +25,7 @@
String location;
String type;
Object source;
+ List mbeans=new ArrayList();
public void setRegistry(Registry reg) {
this.registry=reg;
@@ -44,7 +47,7 @@
this.source=source;
}
- public void loadDescriptors( Registry registry, String location,
+ public List loadDescriptors( Registry registry, String location,
String type, Object source)
throws Exception
{
@@ -53,6 +56,7 @@
setType(type);
setSource(source);
execute();
+ return mbeans;
}
public void execute() throws Exception {
@@ -62,7 +66,7 @@
if( managed==null ) return;
managed.setName( type );
- registry.addManagedBean(managed);
+ mbeans.add(managed);
} catch( Exception ex ) {
log.error( "Error reading descriptors ", ex);
1.4 +6 -2 jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansDescriptorsSerSource.java
Index: MbeansDescriptorsSerSource.java
===================================================================
RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansDescriptorsSerSource.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MbeansDescriptorsSerSource.java 21 Jan 2003 00:33:45 -0000 1.3
+++ MbeansDescriptorsSerSource.java 26 Feb 2003 22:17:32 -0000 1.4
@@ -11,6 +11,8 @@
import java.io.ObjectInputStream;
import java.io.EOFException;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
public class MbeansDescriptorsSerSource extends ModelerSource
@@ -20,6 +22,7 @@
String location;
String type;
Object source;
+ List mbeans=new ArrayList();
public void setRegistry(Registry reg) {
this.registry=reg;
@@ -41,7 +44,7 @@
this.source=source;
}
- public void loadDescriptors( Registry registry, String location,
+ public List loadDescriptors( Registry registry, String location,
String type, Object source)
throws Exception
{
@@ -50,6 +53,7 @@
setType(type);
setSource(source);
execute();
+ return mbeans;
}
public void execute() throws Exception {
@@ -73,7 +77,7 @@
ManagedBean beans[]=(ManagedBean[])obj;
// after all are read without error
for( int i=0; i<beans.length; i++ ) {
- registry.addManagedBean(beans[i]);
+ mbeans.add(beans[i]);
}
} catch( Exception ex ) {
1.6 +33 -5 jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansSource.java
Index: MbeansSource.java
===================================================================
RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/MbeansSource.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- MbeansSource.java 17 Feb 2003 01:01:35 -0000 1.5
+++ MbeansSource.java 26 Feb 2003 22:17:32 -0000 1.6
@@ -11,6 +11,8 @@
import javax.management.loading.MLet;
import java.io.InputStream;
import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
/** This will create mbeans based on a config file.
@@ -24,6 +26,7 @@
String location;
String type;
Object source;
+ List mbeans=new ArrayList();
static boolean loaderLoaded=false;
public void setRegistry(Registry reg) {
@@ -46,7 +49,16 @@
this.source=source;
}
- public void loadDescriptors( Registry registry, String location,
+ /** Return the list of mbeans created by this source.
+ * It can be used to implement runtime services.
+ *
+ * @return
+ */
+ public List getMBeans() {
+ return mbeans;
+ }
+
+ public List loadDescriptors( Registry registry, String location,
String type, Object source)
throws Exception
{
@@ -55,6 +67,7 @@
setType(type);
setSource(source);
execute();
+ return mbeans;
}
public void execute() throws Exception {
@@ -97,8 +110,12 @@
{
String nodeName=mbeanN.getNodeName();
- if( "mbean".equals(nodeName) || "MLET".equals(nodeName)) {
+ if( "mbean".equals(nodeName) || "MLET".equals(nodeName) ||
+ "service".equals(nodeName)) {
String code=DomUtil.getAttribute( mbeanN, "code" );
+ if( code==null ) {
+ code=DomUtil.getAttribute( mbeanN, "class" );
+ }
String objectName=DomUtil.getAttribute( mbeanN, "objectName" );
if( objectName==null ) {
objectName=DomUtil.getAttribute( mbeanN, "name" );
@@ -117,6 +134,7 @@
try {
ObjectName oname=new ObjectName(objectName);
server.createMBean(code, oname);
+ mbeans.add(oname);
// XXX Arguments, loader !!!
} catch( Exception ex ) {
log.error( "Error creating mbean " + objectName, ex);
@@ -182,7 +200,7 @@
Node descN, String objectName ) {
String attName=DomUtil.getAttribute(descN, "name");
String value=DomUtil.getAttribute(descN, "value");
- String type=DomUtil.getAttribute(descN, "type");
+ String type=null; // DomUtil.getAttribute(descN, "type");
if( value==null ) {
// The value may be specified as CDATA
value=DomUtil.getContent(descN);
@@ -192,8 +210,18 @@
log.debug("Set attribute " + objectName + " " + attName +
" " + value);
ObjectName oname=new ObjectName(objectName);
- Object valueO=getValueObject( value, type);
- server.setAttribute(oname, new Attribute(attName, valueO));
+ // find the type
+ MBeanInfo info=server.getMBeanInfo(oname);
+ MBeanAttributeInfo attInfo[]=info.getAttributes();
+ for( int i=0; i<attInfo.length; i++ ) {
+ if( attName.equals(attInfo[i].getName())) {
+ type=attInfo[i].getType();
+ Object valueO=getValueObject( value, type);
+ server.setAttribute(oname, new Attribute(attName, valueO));
+ return;
+ }
+ }
+ log.info("Can't find attribute " + objectName + " " + attName );
} catch( Exception ex) {
log.error("Error processing attribute " + objectName + " " +
attName + " " + value, ex);
1.2 +3 -1 jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/ModelerSource.java
Index: ModelerSource.java
===================================================================
RCS file: /home/cvs/jakarta-commons/modeler/src/java/org/apache/commons/modeler/modules/ModelerSource.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ModelerSource.java 21 Jan 2003 00:31:05 -0000 1.1
+++ ModelerSource.java 26 Feb 2003 22:17:32 -0000 1.2
@@ -1,16 +1,18 @@
package org.apache.commons.modeler.modules;
import org.apache.commons.modeler.Registry;
+import java.util.List;
/** Source for descriptor data. More sources can be added.
*
*/
public class ModelerSource {
- public void loadDescriptors( Registry registry, String location,
+ public List loadDescriptors( Registry registry, String location,
String type, Object source)
throws Exception
{
// TODO
+ return null;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org