01: package org.mockejb.interceptor;
02:
03: /**
04: * Loads the class implementing AspectSystem and returns the object of this class
05: * to the client. The class name is specified by the system property "interceptor.aspect.system".
06: * If system property is not provided, returns the default factory {@link AspectSystemImpl}
07: * provided by the framework.
08: *
09: * @author Alexander Ananiev
10: */
11: public class AspectSystemFactory {
12:
13: /**
14: * Name of the system property providing the name of the aspect system class
15: * to use instead of the default
16: */
17: public static final String ASPECT_SYSTEM_PROPERTY = "interceptor.aspect.system.class";
18:
19: public static final String THREAD_LOCAL_ASPECT_SYSTEM = "interceptor.aspect.system.thread";
20:
21: private static AspectSystem aspectSystem;
22:
23: /**
24: * Loads the aspect system class provided in the ASPECT_SYSTEM_PROPERTY
25: * system property.
26: * If the property is not provided, {@ AspectSystemImpl} class is used.
27: *
28: * We load it in the static block to avoid having to synchronize
29: */
30: static {
31:
32: aspectSystem = loadAspectSystem();
33: }
34:
35: private static ThreadLocal aspectSystemForThread = new ThreadLocal() {
36: protected Object initialValue() {
37: return loadAspectSystem();
38: }
39: };
40:
41: private static AspectSystem loadAspectSystem() {
42:
43: AspectSystem aspectSystem;
44:
45: String aspectSystemClassName = System
46: .getProperty(ASPECT_SYSTEM_PROPERTY);
47:
48: if (aspectSystemClassName != null) {
49: try {
50:
51: Class aspectSystemClass = Class.forName(
52: aspectSystemClassName, true,
53: AspectSystemFactory.class.getClassLoader());
54: aspectSystem = (AspectSystem) aspectSystemClass
55: .newInstance();
56:
57: } catch (ClassNotFoundException cnfe) {
58: throw new AspectException(cnfe);
59: } catch (InstantiationException ie) {
60: throw new AspectException(ie);
61: } catch (IllegalAccessException iae) {
62: throw new AspectException(iae);
63: }
64: } else
65: aspectSystem = new AspectSystemImpl();
66:
67: return aspectSystem;
68:
69: }
70:
71: /**
72: * If system property "interceptor.aspect.system.thread" set to true,
73: * returns AspectSystem instance which is stored in
74: * a ThreadLocal variable. This is convenient, for example,
75: * if test classes can run concurrently and each needs to set its
76: * own aspects .
77: * Otherwise returns the singleton aspect system (always the same instance per JVM)
78: * @return AspectSystem
79: */
80: public static AspectSystem getAspectSystem() {
81:
82: String threadLocalAspectSystemProp = System
83: .getProperty(THREAD_LOCAL_ASPECT_SYSTEM);
84:
85: if (threadLocalAspectSystemProp != null
86: && threadLocalAspectSystemProp.equalsIgnoreCase("true"))
87: return (AspectSystem) aspectSystemForThread.get();
88: else
89: return aspectSystem;
90:
91: }
92:
93: }
|