[jboss-user] [JBoss AOP] - Re: Dynamic AOP

mane81 do-not-reply at jboss.com
Thu Oct 23 18:40:33 EDT 2008


I tried to simulate the situation described with this simple example:
the classes in filesystem are:
DynamicAOP/bin/AspectCreator.class  
DynamicAOP/bin/AspectCreator$MyThread.class 
DynamicAOP/bin/Caller.class

DynamicAOP/SimpleAspect.class

The classpath points to DynamicAOP/bin/ so that SimpleAspect is out of the classpath scope.

///////// SimpleAspect ////////////////////////////
@Aspect
public class SimpleAspect  {
	
	@Bind (pointcut="execution(public void AspectCreator->setCond())")
	public Object invoke(Invocation invocation) throws Throwable {
		System.out.println("<<< Entering Simplenterceptor for: " + invocation.getClass().getName());
		return invocation.invokeNext();
	}
}
/////////////////////////////////////////////////////////////

/////////////////// Caller //////////////////////////////
@Prepare("all(this)")
public class Caller {
	private AspectCreator ac;

	public Caller() {
		System.out.println("Caller new instance");
		ac = new AspectCreator();
	}
	
	public void method1() {
		System.out.println("Caller.method1()");
	}
	
	public void method2() {
		System.out.println("Caller.method2()");
	}
	
	public void run() throws Exception {
		while(true) {
			
			method1();
			method2();
			
			java.util.concurrent.TimeUnit.MILLISECONDS.sleep(4000);

			ac.setCond();
			java.util.concurrent.TimeUnit.MILLISECONDS.sleep(4000);

			method1();
			method2();
		}
	}
	
	public static void main(String[] args) throws Exception {
		Caller caller = new Caller();
		caller.run();
	}
}
///////////////// Caller ////////////////////////////////////////////////////////////////////////////////

///////////// AspectCreator ///////////////////////////////////////////////////////////////////
@Prepare ("all(this)")
public class AspectCreator {
	private MyThread thread;
	private Boolean cond;
	
	public AspectCreator() {
		cond = false;
		thread = new MyThread();
		thread.start();
	}
	
	public void setCond() {
		synchronized (cond) {
			cond = true;
		}
	}
	
	public boolean getCond() {
		synchronized (cond) {
			return cond;
		}
	}
	
	public void injectAspect() throws Exception {
		System.out.println("Injecting aspect");
		
		ClassLoader oldCl = Thread.currentThread().getContextClassLoader();

		URLClassLoader ucl = null;
		ucl = new URLClassLoader(new URL[] {new File("/home/lele/java/eclipse_ws/DynamicAOP/").toURI().toURL()}, oldCl);
		
		AspectManager manager = AspectManager.instance(ucl);
		AspectAnnotationLoader loader = new AspectAnnotationLoader(manager);
		loader.setClassLoader(ucl);
		
//		ucl.loadClass("SimpleAspect");

		InputStream in = null;
		in = new DataInputStream(new FileInputStream("/home/lele/java/eclipse_ws/DynamicAOP/SimpleAspect.class"));
	
		List list = new ArrayList();
		list.add(in);
		Iterator iterator = list.iterator();

		loader.deployInputStreamIterator(iterator);
		
		System.out.println("Aspect injected");		
	}
	
	class MyThread extends Thread {
		public void run()  {
			while(true) {
				try {
					java.util.concurrent.TimeUnit.MILLISECONDS.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("Checking condition...");
				boolean value = getCond();
				if (value) {
					try {
						injectAspect();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				synchronized (cond) {
					cond = false;
				}
			}
		}
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




And the output is:
$ ./run-loadHotSwap.sh /home/lele/java/eclipse_ws/DynamicAOP/bin/ -aopclasspath /home/lele/java/eclipse_ws/DynamicAOP/bin/ Caller
Caller new instance
Caller.method1()
Caller.method2()
Checking condition...
Checking condition...
Checking condition...
Checking condition...
Injecting aspect
java.lang.NullPointerException
	at org.jboss.aop.pointcut.MethodMatcher.(MethodMatcher.java:72)
	at org.jboss.aop.pointcut.ExecutionMethodMatcher.(ExecutionMethodMatcher.java:53)
	at org.jboss.aop.pointcut.PointcutExpression.matchesExecution(PointcutExpression.java:118)
	at org.jboss.aop.Advisor.resolveMethodPointcut(Advisor.java:1333)
	at org.jboss.aop.GeneratedClassAdvisor.resolveMethodPointcut(GeneratedClassAdvisor.java:309)
	at org.jboss.aop.ClassAdvisor.resolvePointcuts(ClassAdvisor.java:751)
	at org.jboss.aop.ClassAdvisor.doRebuildInterceptorsForAddedBinding(ClassAdvisor.java:1160)
	at org.jboss.aop.ClassAdvisor$RebuildInterceptorsAction$2.rebuildInterceptorsForAddedBinding(ClassAdvisor.java:2348)
	at org.jboss.aop.ClassAdvisor.rebuildInterceptorsForAddedBinding(ClassAdvisor.java:1139)
	at org.jboss.aop.GeneratedClassAdvisor.access$1501(GeneratedClassAdvisor.java:65)
	at org.jboss.aop.GeneratedClassAdvisor$ClassAdvisorStrategy.rebuildInterceptorsForAddedBinding(GeneratedClassAdvisor.java:1538)
	at org.jboss.aop.GeneratedClassAdvisor.rebuildInterceptorsForAddedBinding(GeneratedClassAdvisor.java:244)
	at org.jboss.aop.Advisor.newBindingAdded(Advisor.java:1124)
	at org.jboss.aop.AspectManager.updateAdvisorsForAddedBinding(AspectManager.java:1498)
	at org.jboss.aop.AspectManager.updateAdvisorsForAddedBinding(AspectManager.java:1521)
	at org.jboss.aop.AspectManager.addBinding(AspectManager.java:1456)
	at org.jboss.aop.AspectManagerAnnotationLoaderStrategy.deployAspectMethodBinding(AspectManagerAnnotationLoaderStrategy.java:102)
	at org.jboss.aop.AspectAnnotationLoader.deployAspectMethodBindings(AspectAnnotationLoader.java:422)
	at org.jboss.aop.AspectAnnotationLoader.deployAspect(AspectAnnotationLoader.java:213)
	at org.jboss.aop.AspectAnnotationLoader.deployClassFile(AspectAnnotationLoader.java:125)
	at org.jboss.aop.AspectAnnotationLoader.deployInputStreamIterator(AspectAnnotationLoader.java:115)
	at AspectCreator.injectAspect(AspectCreator.java:59)
	at AspectCreator$MyThread.run(AspectCreator.java:76)
Checking condition...
Checking condition...
.....

I can't figure out why this exception is thrown!
Thanks for the help,
Emanuele

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4184320#4184320

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4184320



More information about the jboss-user mailing list