package org.mockejb.interceptor;
public class AspectSystemFactory {
public static final String ASPECT_SYSTEM_PROPERTY ="interceptor.aspect.system.class";
public static final String THREAD_LOCAL_ASPECT_SYSTEM ="interceptor.aspect.system.thread";
private static AspectSystem aspectSystem;
static {
aspectSystem = loadAspectSystem();
}
private static ThreadLocal aspectSystemForThread = new ThreadLocal() {
protected Object initialValue() {
return loadAspectSystem();
}
};
private static AspectSystem loadAspectSystem() {
AspectSystem aspectSystem;
String aspectSystemClassName = System.getProperty( ASPECT_SYSTEM_PROPERTY );
if ( aspectSystemClassName != null ) {
try {
Class aspectSystemClass =
Class.forName( aspectSystemClassName, true, AspectSystemFactory.class.getClassLoader());
aspectSystem = (AspectSystem) aspectSystemClass.newInstance();
}
catch ( ClassNotFoundException cnfe ) {
throw new AspectException( cnfe );
}
catch ( InstantiationException ie ) {
throw new AspectException( ie );
}
catch ( IllegalAccessException iae ) {
throw new AspectException( iae );
}
}
else
aspectSystem = new AspectSystemImpl();
return aspectSystem;
}
public static AspectSystem getAspectSystem() {
String threadLocalAspectSystemProp = System.getProperty(THREAD_LOCAL_ASPECT_SYSTEM);
if ( threadLocalAspectSystemProp != null && threadLocalAspectSystemProp.equalsIgnoreCase("true"))
return (AspectSystem) aspectSystemForThread.get();
else
return aspectSystem;
}
}