KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > logging > Target


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package examples.logging;
9
10 import org.codehaus.aspectwerkz.transform.inlining.deployer.Deployer;
11 import org.codehaus.aspectwerkz.transform.inlining.deployer.DeploymentHandle;
12 import org.codehaus.aspectwerkz.definition.DeploymentScope;
13 import org.codehaus.aspectwerkz.definition.SystemDefinition;
14 import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer;
15
16 /**
17  * serializable
18  *
19  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
20  */

21 public class Target {
22
23     /**
24      * log level=1 flt=5.8F iconstant=org.codehaus.aspectwerkz.DeploymentModel.PER_CLASS
25      */

26     private int m_counter1;
27
28     /**
29      * log level=1 iconstant=org.codehaus.aspectwerkz.DeploymentModel.PER_THREAD
30      */

31     private int m_counter2;
32
33     public int getCounter() {
34         return m_counter1;
35     }
36
37     public void increment() {
38         m_counter2 = m_counter2 + 1;
39     }
40
41     /**
42      * log level=0
43      * sconstant=org.codehaus.aspectwerkz.transform.TransformationConstants.ASPECTWERKZ_PREFIX
44      */

45     public static int toLog1(int i) {
46         System.out.println("Target.toLog1()");
47         new Target().toLog2(
48                 new String JavaDoc[]{
49                     "parameter"
50                 }
51         );
52         return 1;
53     }
54
55     /**
56      * log level=3 sarr={"Hello","World", "Jonas's car"}
57      */

58     public java.lang.String JavaDoc[] toLog2(java.lang.String JavaDoc[] arg) {
59         System.out.println("Target.toLog2()");
60         new Target().toLog3();
61 // throw new RuntimeException();
62
return null;
63     }
64
65     /**
66      * log level=4 darr={4.5D,8.98665D,0.00000342}
67      */

68     public Object JavaDoc toLog3() {
69         System.out.println("Target.toLog3()");
70         return "result";
71     }
72
73     public static void main(String JavaDoc[] args) {
74
75         System.out.println("-----------------------------------------------------------------------------------");
76         System.out.println("---- deploy/undeploy using explicit prepared pointcut ----");
77         System.out.println("-----------------------------------------------------------------------------------");
78
79         run();
80         SystemDefinition def = SystemDefinitionContainer.getDefinitionFor(
81                 Thread.currentThread().getContextClassLoader(), "samples"
82         );
83         DeploymentScope deploymentScope = def.getDeploymentScope("prepareMethodsToLog");
84
85         Deployer.deploy(LoggingAspect.class, deploymentScope);
86         run();
87         Deployer.undeploy(LoggingAspect.class);
88         run();
89
90
91         System.out.println("-----------------------------------------------------------------------------------");
92         System.out.println("---- deploy/undeploy using deployment handle ----");
93         System.out.println("-----------------------------------------------------------------------------------");
94
95         run();
96         DeploymentHandle handle2 = Deployer.deploy(LoggingAspect.class);
97         run();
98         Deployer.undeploy(handle2);
99         run();
100
101
102         System.out.println("-----------------------------------------------------------------------------------");
103         System.out.println("---- deploy using XML def and undeploy using handle ----");
104         System.out.println("-----------------------------------------------------------------------------------");
105
106         run();
107         String JavaDoc aspectXmlDef = "<aspect class=\"examples.logging.XmlDefLoggingAspect\"><pointcut name=\"methodsToLog\" expression=\"execution(* examples.logging.Target.toLog*(..))\"/><advice name=\"logMethod\" type=\"around\" bind-to=\"methodsToLog\"/><advice name=\"logBefore\" type=\"before\" bind-to=\"methodsToLog\"/></aspect>";
108         Deployer.deploy(XmlDefLoggingAspect.class, aspectXmlDef);
109         run();
110         Deployer.undeploy(XmlDefLoggingAspect.class);
111         run();
112
113     }
114
115     private static void run() {
116         try {
117             System.out.println("Target.main");
118             Target.toLog1(3);
119             Target target = new Target();
120             target.increment();
121             target.getCounter();
122
123             TargetOther.toLog1(new int[]{1, 2, 3}, null, null, 0);
124         } catch (Throwable JavaDoc e) {
125             System.out.println("The runtime exception went thru: " + e.toString());
126             e.printStackTrace();
127         }
128     }
129
130     public static class TargetOther {
131
132         public static int[] toLog1(int i[], String JavaDoc[] a, String JavaDoc b, int c) {
133             System.out.println("TargetOther.toLog1()");
134             return i;
135         }
136     }
137
138     public void dummy() {
139
140     }
141 }
Popular Tags