1 package attrib4j.examples;
2
3 import java.lang.reflect.Method;
4 import java.util.Vector;
5
6 import attrib4j.Attribute;
7 import attrib4j.Attributes;
8 import attrib4j.Log;
9 import attrib4j.examples.attributes.TransactionAttribute;
10
11 /***
12 * A simple example class that demonstrates the use of custom
13 * attributes. The idea is to associate EJB like transaction
14 * attributes to a method, much like would be described in the
15 * deployment descriptor.
16 *
17 * @author <a href="mailto:mpollack@speakeasy.org">Mark Pollack</a>
18 *
19 * @Transaction("Never")
20 */
21 public class TransactionExample {
22
23 /***
24 * Create an instance.
25 */
26 public TransactionExample() {
27
28 }
29
30 /***
31 *
32 * @param inputData represents information to use in a transaction.
33 *
34 *
35 * @attrib4j.examples.attributes.Transaction("Required")
36 *
37 */
38 public void doDatabaseWork(Vector inputData) {
39
40 //would do database related work here
41 System.out.println("Working inside attribute demarcated transaction.");
42 }
43
44 /***
45 * Show how attributes can be extracted at runtime.
46 * @throws NoSuchMethodException if cannot find declared method on class.
47 */
48 public void extractAttributes() throws NoSuchMethodException {
49
50 //Get a reference to the java.lang.Class that we want
51 //to examine for attributes.
52 Class cls = TransactionExample.class;
53
54 //Get the class level attributes.
55 Attribute[] customClassAttribute = Attributes.getAttributes(cls);
56
57 //Manipulate the attributes.
58 //For the example, just cast to TransactionAttribute since we know
59 //it is there.
60 TransactionAttribute classTransAttribute =
61 (TransactionAttribute) customClassAttribute[0];
62
63 System.out.println(
64 "Found class level custom TransactionAttribute. Value = "
65 + classTransAttribute.getAsString());
66
67 //Get a reference to the method we want to examine for
68 //attributes.
69 Class[] methodSignature = new Class[] { Vector.class };
70 Method doDatabaseWorkMethod =
71 cls.getDeclaredMethod("doDatabaseWork", methodSignature);
72
73 //Get the method level attributes.
74 Attribute[] customMethodAttributes =
75 Attributes.getAttributes(doDatabaseWorkMethod);
76
77 //Manipulate the attributes.
78 //For the example, just cast to TransactionAttribute since we know
79 //it is there.
80 TransactionAttribute methodTransAttribute =
81 (TransactionAttribute) customMethodAttributes[0];
82
83 System.out.println(
84 "Found method level custom TransactionAttribute. Value = "
85 + methodTransAttribute.getAsString());
86
87 }
88
89 /***
90 * Run the example program. It will execute a method that has been
91 * decorated with an attribute, just to show that the functionality has not
92 * been altered. Then it will extract the attributes from the class.
93 * @param args The command line arguments, not used.
94 */
95 public static void main(String[] args) {
96 System.out.println("\nRunning TransationExamaple\n");
97 //turn off debug verbositiy of Attrib4j.
98 Log.setLevel(Log.WARNING);
99 TransactionExample te = new TransactionExample();
100 te.doDatabaseWork(new Vector());
101 try {
102 te.extractAttributes();
103 } catch (NoSuchMethodException e) {
104 e.printStackTrace();
105 }
106 }
107 }
This page was automatically generated by Maven