You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by hl...@apache.org on 2003/08/05 16:07:30 UTC
cvs commit: jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service ClassFactory.java
hlship 2003/08/05 07:07:30
Modified: hivemind/src/java/org/apache/commons/hivemind/service/impl
AbstractServiceInterceptorFactory.java
MethodFabImpl.java ClassFactoryImpl.java
ClassFabImpl.java
hivemind/src/java/org/apache/commons/hivemind/service
ClassFactory.java
Log:
Change ClassFactory interface and implementations to support a seperate Javassist ClassPool for each module.
Revision Changes Path
1.4 +4 -3 jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/AbstractServiceInterceptorFactory.java
Index: AbstractServiceInterceptorFactory.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/AbstractServiceInterceptorFactory.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AbstractServiceInterceptorFactory.java 1 Aug 2003 18:25:33 -0000 1.3
+++ AbstractServiceInterceptorFactory.java 5 Aug 2003 14:07:17 -0000 1.4
@@ -59,11 +59,11 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
-import java.util.LinkedList;
import org.apache.commons.hivemind.HiveMind;
import org.apache.commons.hivemind.Initializable;
import org.apache.commons.hivemind.InterceptorStack;
+import org.apache.commons.hivemind.Module;
import org.apache.commons.hivemind.ServiceExtensionPoint;
import org.apache.commons.hivemind.ServiceInterceptorFactory;
import org.apache.commons.hivemind.service.ClassFab;
@@ -88,10 +88,11 @@
public void createInterceptor(InterceptorStack stack)
{
Class serviceInterfaceClass = stack.getServiceInterface();
+ Module module = stack.getServiceExtensionPoint().getModule();
String name ="$Interceptor_" + Long.toHexString(System.currentTimeMillis()) + "$" + _uid++;
- ClassFab classFab = _factory.newClass(name, getInterceptorSuperclass());
+ ClassFab classFab = _factory.newClass(name, getInterceptorSuperclass(), module);
classFab.addInterface(serviceInterfaceClass);
1.2 +5 -2 jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/MethodFabImpl.java
Index: MethodFabImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/MethodFabImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MethodFabImpl.java 9 Jul 2003 11:27:24 -0000 1.1
+++ MethodFabImpl.java 5 Aug 2003 14:07:18 -0000 1.2
@@ -57,6 +57,7 @@
package org.apache.commons.hivemind.service.impl;
+import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
@@ -67,17 +68,19 @@
public class MethodFabImpl implements MethodFab
{
private ClassFactoryImpl _factory;
+ private ClassPool _pool;
private CtMethod _method;
- public MethodFabImpl(ClassFactoryImpl factory, CtMethod method)
+ public MethodFabImpl(ClassFactoryImpl factory, ClassPool pool, CtMethod method)
{
_factory = factory;
+ _pool = pool;
_method = method;
}
public void addCatch(Class exceptionClass, String catchBody)
{
- CtClass ctException = _factory.getClass(exceptionClass);
+ CtClass ctException = _factory.getClass(_pool, exceptionClass);
try
{
1.2 +44 -12 jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/ClassFactoryImpl.java
Index: ClassFactoryImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/ClassFactoryImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ClassFactoryImpl.java 9 Jul 2003 11:27:24 -0000 1.1
+++ ClassFactoryImpl.java 5 Aug 2003 14:07:18 -0000 1.2
@@ -57,11 +57,17 @@
package org.apache.commons.hivemind.service.impl;
+import java.util.HashMap;
+import java.util.Map;
+
+import javassist.ClassPath;
import javassist.ClassPool;
import javassist.CtClass;
+import javassist.LoaderClassPath;
import javassist.NotFoundException;
import org.apache.commons.hivemind.HiveMind;
+import org.apache.commons.hivemind.Module;
import org.apache.commons.hivemind.service.ClassFab;
import org.apache.commons.hivemind.service.ClassFactory;
import org.apache.tapestry.ApplicationRuntimeException;
@@ -74,17 +80,20 @@
*/
public class ClassFactoryImpl implements ClassFactory
{
- private ClassPool _pool = ClassPool.getDefault();
+ /**
+ * Map of ClassPool, keyed on module id.
+ */
+ private Map _poolMap = new HashMap();
- public ClassFab newClass(String name, Class superClass)
+ public ClassFab newClass(String name, Class superClass, Module module)
{
- // TODO: Class loading issues. Ensure that class loader for superClass
- // is known to _pool.
- CtClass ctSuperClass = getClass(superClass);
+ ClassPool pool = findPool(module);
+
+ CtClass ctSuperClass = getClass(pool, superClass);
try
{
- CtClass ctNewClass = _pool.makeClass(name, ctSuperClass);
+ CtClass ctNewClass = pool.makeClass(name, ctSuperClass);
return new ClassFabImpl(this, ctNewClass);
}
@@ -101,11 +110,11 @@
}
- public CtClass getClass(Class inputClass)
+ public CtClass getClass(ClassPool pool, Class inputClass)
{
try
{
- return _pool.get(inputClass.getName());
+ return pool.get(inputClass.getName());
}
catch (NotFoundException ex)
{
@@ -115,21 +124,44 @@
}
}
- public Class createClass(CtClass _ctClass)
+ public Class createClass(CtClass ctClass)
{
try
{
- return _pool.writeAsClass(_ctClass.getName());
+ ClassPool pool = ctClass.getClassPool();
+
+ return pool.writeAsClass(ctClass.getName());
}
catch (Exception ex)
{
throw new ApplicationRuntimeException(
HiveMind.format(
"ClassFactoryImpl.unable-to-write-class",
- _ctClass.getName(),
+ ctClass.getName(),
ex.getMessage()),
ex);
}
}
+ private synchronized ClassPool findPool(Module module)
+ {
+ String id = module.getModuleId();
+
+ ClassPool result = (ClassPool) _poolMap.get(id);
+
+ if (result == null)
+ {
+ result = new ClassPool(null);
+ result.appendSystemPath();
+
+ ClassLoader loader = module.getResourceResolver().getClassLoader();
+ ClassPath path = new LoaderClassPath(loader);
+
+ result.appendClassPath(path);
+
+ _poolMap.put(id, result);
+ }
+
+ return result;
+ }
}
1.2 +8 -5 jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/ClassFabImpl.java
Index: ClassFabImpl.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/ClassFabImpl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ClassFabImpl.java 9 Jul 2003 11:27:24 -0000 1.1
+++ ClassFabImpl.java 5 Aug 2003 14:07:18 -0000 1.2
@@ -58,6 +58,7 @@
package org.apache.commons.hivemind.service.impl;
import javassist.CannotCompileException;
+import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
@@ -72,23 +73,25 @@
{
private ClassFactoryImpl _factory;
private CtClass _ctClass;
+ private ClassPool _pool;
public ClassFabImpl(ClassFactoryImpl factory, CtClass ctClass)
{
_factory = factory;
_ctClass = ctClass;
+ _pool = ctClass.getClassPool();
}
public void addInterface(Class interfaceClass)
{
- CtClass ctInterfaceClass = _factory.getClass(interfaceClass);
+ CtClass ctInterfaceClass = _factory.getClass(_pool, interfaceClass);
_ctClass.addInterface(ctInterfaceClass);
}
public void addField(String name, Class type)
{
- CtClass ctType = _factory.getClass(type);
+ CtClass ctType = _factory.getClass(_pool, type);
try
{
@@ -115,7 +118,7 @@
Class[] exceptions,
String body)
{
- CtClass ctReturnType = _factory.getClass(returnType);
+ CtClass ctReturnType = _factory.getClass(_pool, returnType);
CtClass[] ctParameters = convertClasses(parameterTypes);
CtClass[] ctExceptions = convertClasses(exceptions);
@@ -141,7 +144,7 @@
// Return a MethodFab so the caller can add catches.
- return new MethodFabImpl(_factory, method);
+ return new MethodFabImpl(_factory, _pool, method);
}
@@ -179,7 +182,7 @@
for (int i = 0; i < count; i++)
{
- CtClass ctClass = _factory.getClass(inputClasses[i]);
+ CtClass ctClass = _factory.getClass(_pool, inputClasses[i]);
result[i] = ctClass;
}
1.2 +4 -2 jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/ClassFactory.java
Index: ClassFactory.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/ClassFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ClassFactory.java 9 Jul 2003 11:27:23 -0000 1.1
+++ ClassFactory.java 5 Aug 2003 14:07:30 -0000 1.2
@@ -57,6 +57,8 @@
package org.apache.commons.hivemind.service;
+import org.apache.commons.hivemind.Module;
+
/**
* Service used when dynamically creating new classes.
*
@@ -71,5 +73,5 @@
* is public and concrete.
*/
- public ClassFab newClass(String name, Class superClass);
+ public ClassFab newClass(String name, Class superClass, Module module);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org