KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > bsh > BshAspects


1 package dynaop.bsh;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStreamReader JavaDoc;
5 import java.io.Reader JavaDoc;
6 import java.net.URL JavaDoc;
7
8 import bsh.Interpreter;
9
10 import dynaop.Aspects;
11 import dynaop.ConfigurationException;
12 import dynaop.Pointcuts;
13 import dynaop.util.Classes;
14 import dynaop.util.NestedException;
15
16 /**
17  * Executes BeanShell script on the classpath at "<code>/dynaop.bsh</code>".
18  * Script implicitly supports methods from {@link dynaop.Aspects}
19  * and static methods and fields from {@link dynaop.Pointcuts}.
20  *
21  * <p>You can use a <code>Class</code> or
22  * class name regular expression in place of <code>ClassPointcut</code>.
23  * You can use a <code>Method</code> or method signature regular expression in
24  * place of <code>MethodPointcut</code>.</p>
25  *
26  * <p>Example script:</p>
27  *
28  * <p><pre>
29  * // Create pointcut - instances of Person.
30  * people = instancesOf(Person.class);
31  *
32  * // Create pointcut - instances of Book (uses Class in
33  * // place of ClassPointcut).
34  * books = Book.class;
35  *
36  * // Apply IdentifiableMixin to people and books. IdentifiableMixin should
37  * // implement Object methods as well.
38  * mixin(
39  * union(people, books),
40  * new Class[] { Identifiable.class, Object.class },
41  * IdentifiableMixin.class
42  * );
43  *
44  * // Apply AuthorMixin to people. Automatically adds Author interface.
45  * mixin(people, AuthorMixin.class);
46  *
47  * // Add PersonProxy interface to people. PersonProxy extends all of the
48  * // added interfaces and helps reduce casting.
49  * mixin(people, new Class[] { PersonProxy.class });
50  *
51  * // Apply transaction interceptor to Person.setName(String) using a
52  * // regular expression.
53  * interceptor(people, ".*setName.*", new TransactionInterceptor());
54  *
55  * // Apply a per-instance interceptor to all methods.
56  * interceptor(people, ALL_METHODS, PerInstanceInterceptor.class,
57  * new Closure() {
58  * execute(interceptor) {
59  * // BeanShell syntactic sugar, calls PerInstanceInterceptor.setName().
60  * interceptor.name = "per-instance interceptor";
61  * interceptor.foo = 5;
62  * }
63  * }
64  * );
65  *
66  * // Apply a counter mixin to books that will be used to count sales.
67  * // Use a custom mixin factory implementation that loads the mixin from a
68  * // persistent store.
69  * mixin(
70  * books,
71  * new Class[] { Counter.class },
72  * new MyMixinFactory(CounterMixin.java)
73  * );
74  * </p></pre>
75  *
76  * @author Bob Lee (crazybob@crazybob.org)
77  */

78 public class BshAspects extends Aspects {
79
80     public BshAspects(URL JavaDoc url) {
81         if (url == null)
82             throw new ConfigurationException(getDefaultConfigurationFile() +
83                     " not found.");
84
85         Reader JavaDoc reader = null;
86         try {
87             Interpreter interpreter = new Interpreter();
88             
89             BshHelper helper = new BshHelper(this);
90
91             interpreter.set("$_dynaop_helper", helper);
92             interpreter.eval("importObject($_dynaop_helper);");
93             interpreter.eval("import dynaop.*;");
94             interpreter.eval("static import " +
95                     Pointcuts.class.getName() + ".*;");
96             
97             reader = new InputStreamReader JavaDoc(url.openStream());
98             interpreter.eval(reader);
99         }
100         catch (Exception JavaDoc e) {
101             throw NestedException.wrap(e);
102         }
103         finally {
104             try {
105                 if (reader != null)
106                     reader.close();
107             }
108             catch (IOException JavaDoc e) {}
109         }
110     }
111
112     public BshAspects() {
113         this(Classes.getClassLoader().getResource(
114             getDefaultConfigurationFile()));
115     }
116     
117     static String JavaDoc getDefaultConfigurationFile() {
118         return System.getProperty("dynaop.bsh", "dynaop.bsh");
119     }
120 }
121
Popular Tags