FELIX-141 Replaced Javassist with CGLIB (ASL'd)

git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@442626 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/jmxintrospector/pom.xml b/jmxintrospector/pom.xml
index 4da175a..e638d57 100644
--- a/jmxintrospector/pom.xml
+++ b/jmxintrospector/pom.xml
@@ -16,11 +16,11 @@
       <version>3.8.1</version>

       <scope>test</scope>

     </dependency>

-     <dependency>

-       <groupId>javassist</groupId>

-       <artifactId>javassist</artifactId>

-       <version>3.3</version>

-     </dependency> 

+    <dependency>

+      <groupId>cglib</groupId>

+      <artifactId>cglib</artifactId>

+      <version>2.1_3</version>

+    </dependency>

   </dependencies>

   <build>

     <plugins>

@@ -31,7 +31,7 @@
         <extensions>true</extensions>

         <configuration>

             <inlinedArtifacts>

-        	    <inlinedArtifact>javassist</inlinedArtifact>

+        	    <inlinedArtifact>cglib</inlinedArtifact>

           	</inlinedArtifacts>

           <osgiManifest>

             <bundleName>${name}</bundleName>

diff --git a/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/MBeanProxyFactory.java b/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/MBeanProxyFactory.java
index 753e6e9..04fae91 100644
--- a/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/MBeanProxyFactory.java
+++ b/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/MBeanProxyFactory.java
@@ -23,12 +23,12 @@
 import java.util.ArrayList;

 import java.util.List;

 

-import javassist.CannotCompileException;

+/*import javassist.CannotCompileException;

 import javassist.ClassPool;

 import javassist.CtClass;

 import javassist.CtMethod;

 import javassist.NotFoundException;

-

+*/

 import javax.management.MBeanAttributeInfo;

 import javax.management.MBeanInfo;

 import javax.management.MBeanNotificationInfo;

@@ -38,7 +38,12 @@
 import javax.management.NotificationEmitter;

 import javax.management.ObjectName;

 

-import org.apache.felix.jmxintrospector.classbean.TypeGetterMBean;

+import net.sf.cglib.core.NamingPolicy;

+import net.sf.cglib.core.Predicate;

+import net.sf.cglib.core.Signature;

+import net.sf.cglib.proxy.InterfaceMaker;

+

+import org.objectweb.asm.Type;

 

 public class MBeanProxyFactory {

 	private MBeanServerConnection mbeanServer;

@@ -49,47 +54,40 @@
 	public MBeanProxyFactory(MBeanServerConnection mbeanServer) {

 		super();

 		this.mbeanServer = mbeanServer;

-		

 	}

 

 	private Class getInterface(String oname)throws Exception{

 		ObjectName objectName=ObjectName.getInstance(oname);

-		

-		String ifaceName=mbeanServer.getObjectInstance(objectName).getClassName().replace('.', '_');

-		CtClass ctIface=null;

-		try{

-			ctIface=javassist.ClassPool.getDefault().get(ifaceName);

-			return this.getClass().getClassLoader().loadClass(ifaceName);

-		}catch (Exception e) {

-			ctIface=javassist.ClassPool.getDefault().makeInterface(ifaceName);

-			for (CtMethod m : addMethods(ctIface, objectName)) {

-				ctIface.addMethod(m);

+		String ifaceName=mbeanServer.getObjectInstance(objectName).getClassName();

+		InterfaceMaker maker=new MBeanInterfaceMaker(ifaceName);

+			for (Signature s : getSignatures(objectName)) {

+				maker.add(s, null);

 			}

 			try {

-			return ctIface.toClass(this.getClass().getClassLoader(), this.getClass().getProtectionDomain());

-			} catch (CannotCompileException cce) {

-				System.out.println(cce.getCause());

-				throw cce;

+				return maker.create();

+			} catch (Exception e) {

+				System.out.println(e.getCause());

+				throw e;

 			}

 		}

-		

+	private Type getType(String type) throws Exception{

+		System.out.println("getting Type for: "+type);

+		return JmxAsmHelper.getAsmType(type);

 	}

-	private List<CtMethod> addMethods(CtClass ctIface, ObjectName objectName)throws Exception{

-		ClassPool pool=javassist.ClassPool.getDefault();

-		List<CtMethod> methods=new ArrayList<CtMethod>();

+	private List<Signature> getSignatures(ObjectName objectName)throws Exception{

+		List<Signature> methods=new ArrayList<Signature>();

 		MBeanInfo minfo =mbeanServer.getMBeanInfo(objectName);

-		CtMethod m=null;

 		

 		for (MBeanAttributeInfo info : minfo.getAttributes()) {

 			String name=info.getName().substring(0, 1).toUpperCase()+info.getName().substring(1);

 			if(info.isReadable()){

 			if(info.isIs()){

-				m=new CtMethod(CtClass.booleanType, "is"+ name,null, ctIface);

-				methods.add(m);

+				methods.add(new Signature("is"+name, Type.BOOLEAN_TYPE, new Type[0]));

 			}

 			else{

 //				try {

-				m=new CtMethod(pool.get(info.getType()), "get"+ name,null, ctIface);

+				

+				methods.add(new Signature("get"+name, getType(info.getType()), new Type[0]));

 //				}catch (NotFoundException nfe) {

 //					String n=nfe.getMessage();

 //					String notFoundName=n.startsWith("[")?n.substring(1, n.length()):n;

@@ -97,23 +95,21 @@
 //					

 //					m=new CtMethod(pool.get(info.getType()), "get"+ name,null, ctIface);

 //				}

-				methods.add(m);

 				}

-

 			}

-			

 			if(info.isWritable()){

-				m=new CtMethod(CtClass.voidType, "set"+ name,new CtClass[]{pool.get(info.getType())} , ctIface);

-				methods.add(m);

+				Type [] params=new Type[]{getType(info.getType())};

+				Signature s=new Signature("set"+name, Type.VOID_TYPE, params);

+				methods.add(s);

 			}

 		}

 		for (MBeanOperationInfo info : minfo.getOperations()) {

-			CtClass[] params=new CtClass[info.getSignature().length];

+			Type[] params=new Type[info.getSignature().length];

 			for (int i = 0; i < params.length; i++) {

-				params[i]=pool.get(info.getSignature()[i].getType());

+				params[i]=getType(info.getSignature()[i].getType());

 			}

-			m=new CtMethod(pool.get(info.getReturnType()), info.getName(),params, ctIface);

-			methods.add(m);

+			Signature s=new Signature(info.getName(), getType(info.getReturnType()), params);

+			methods.add(s);

 

 		}

 		return methods;

@@ -188,5 +184,12 @@
 	public void setMbeanServer(MBeanServerConnection mbs) {

 		this.mbeanServer = mbs;

 	}

+	private class MBeanInterfaceMaker extends InterfaceMaker{

+		public MBeanInterfaceMaker(String namePrefix) {

+			super();

+			super.setNamePrefix(namePrefix);

+			super.setAttemptLoad(true);

+		}

+	}

 

 }

diff --git a/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/MBeanProxyManager.java b/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/MBeanProxyManager.java
index 1d935f3..b554342 100644
--- a/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/MBeanProxyManager.java
+++ b/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/MBeanProxyManager.java
@@ -19,15 +19,11 @@
 

 import java.lang.management.ManagementFactory;

 import java.util.ArrayList;

-import java.util.HashMap;

 import java.util.List;

-import java.util.Map;

 import java.util.Set;

-import java.util.logging.Level;

 import java.util.logging.Logger;

 

 import javax.management.MBeanServerConnection;

-import javax.management.MBeanServerFactory;

 import javax.management.ObjectName;

 import javax.management.remote.JMXConnectorFactory;

 import javax.management.remote.JMXServiceURL;

@@ -70,9 +66,9 @@
 			try {

 				objects.add(introspector.newProxyInstance(name

 						.toString()));

-			} catch (javassist.NotFoundException nfe) {

-				

-				logger.warning("ERROR"+nfe);

+			} catch (Exception e) {

+				e.printStackTrace();

+				logger.warning("ERROR: "+e);

 				continue;

 			}

 		}

diff --git a/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/classbean/TypeGetter.java b/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/classbean/TypeGetter.java
deleted file mode 100644
index e52277d..0000000
--- a/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/classbean/TypeGetter.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*

- *   Copyright 2005 The Apache Software Foundation

- *

- *   Licensed under the Apache License, Version 2.0 (the "License");

- *   you may not use this file except in compliance with the License.

- *   You may obtain a copy of the License at

- *

- *       http://www.apache.org/licenses/LICENSE-2.0

- *

- *   Unless required by applicable law or agreed to in writing, software

- *   distributed under the License is distributed on an "AS IS" BASIS,

- *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

- *   See the License for the specific language governing permissions and

- *   limitations under the License.

- *

- */

-

-package org.apache.felix.jmxintrospector.classbean;

-

-import java.util.Collection;

-

-import sun.security.krb5.internal.tools.Klist;

-

-import javassist.ClassPool;

-import javassist.CtClass;

-

-public class TypeGetter implements TypeGetterMBean {

-

-	public Class getType(String name) throws Exception{

-		CtClass cl=ClassPool.getDefault().get(name);

-		Collection classes=cl.getRefClasses();

-		CtClass[] all=ClassPool.getDefault().get((String[])classes.toArray(new String[classes.size()]));

-		for (CtClass klazz : all) {

-			klazz.toBytecode();

-			

-		} 

-		ClassLoader loader=this.getClass().getClassLoader();

-		try {

-			return  loader.loadClass(name);

-		} catch (ClassNotFoundException e) {

-			return null; //FIXME: should we implement an searching mechanism?

-			}

-	}

-	

-}

diff --git a/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/classbean/TypeGetterMBean.java b/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/classbean/TypeGetterMBean.java
deleted file mode 100644
index 9722e89..0000000
--- a/jmxintrospector/src/main/java/org/apache/felix/jmxintrospector/classbean/TypeGetterMBean.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*

- *   Copyright 2005 The Apache Software Foundation

- *

- *   Licensed under the Apache License, Version 2.0 (the "License");

- *   you may not use this file except in compliance with the License.

- *   You may obtain a copy of the License at

- *

- *       http://www.apache.org/licenses/LICENSE-2.0

- *

- *   Unless required by applicable law or agreed to in writing, software

- *   distributed under the License is distributed on an "AS IS" BASIS,

- *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

- *   See the License for the specific language governing permissions and

- *   limitations under the License.

- *

- */

-

-package org.apache.felix.jmxintrospector.classbean;

-

-public interface TypeGetterMBean {

-	public static final String MBEAN_NAME="org.apache.felix:class=TypeGetterMBean";

-	public Class getType (String name) throws Exception;

-}