001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package examples.logging;
008:
009: import org.codehaus.aspectwerkz.transform.inlining.deployer.Deployer;
010: import org.codehaus.aspectwerkz.transform.inlining.deployer.DeploymentHandle;
011: import org.codehaus.aspectwerkz.definition.DeploymentScope;
012: import org.codehaus.aspectwerkz.definition.SystemDefinition;
013: import org.codehaus.aspectwerkz.definition.SystemDefinitionContainer;
014:
015: /**
016: * serializable
017: *
018: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
019: */
020: public class Target {
021:
022: /**
023: * log level=1 flt=5.8F iconstant=org.codehaus.aspectwerkz.DeploymentModel.PER_CLASS
024: */
025: private int m_counter1;
026:
027: /**
028: * log level=1 iconstant=org.codehaus.aspectwerkz.DeploymentModel.PER_THREAD
029: */
030: private int m_counter2;
031:
032: public int getCounter() {
033: return m_counter1;
034: }
035:
036: public void increment() {
037: m_counter2 = m_counter2 + 1;
038: }
039:
040: /**
041: * log level=0
042: * sconstant=org.codehaus.aspectwerkz.transform.TransformationConstants.ASPECTWERKZ_PREFIX
043: */
044: public static int toLog1(int i) {
045: System.out.println("Target.toLog1()");
046: new Target().toLog2(new String[] { "parameter" });
047: return 1;
048: }
049:
050: /**
051: * log level=3 sarr={"Hello","World", "Jonas's car"}
052: */
053: public java.lang.String[] toLog2(java.lang.String[] arg) {
054: System.out.println("Target.toLog2()");
055: new Target().toLog3();
056: // throw new RuntimeException();
057: return null;
058: }
059:
060: /**
061: * log level=4 darr={4.5D,8.98665D,0.00000342}
062: */
063: public Object toLog3() {
064: System.out.println("Target.toLog3()");
065: return "result";
066: }
067:
068: public static void main(String[] args) {
069:
070: System.out
071: .println("-----------------------------------------------------------------------------------");
072: System.out
073: .println("---- deploy/undeploy using explicit prepared pointcut ----");
074: System.out
075: .println("-----------------------------------------------------------------------------------");
076:
077: run();
078: SystemDefinition def = SystemDefinitionContainer
079: .getDefinitionFor(Thread.currentThread()
080: .getContextClassLoader(), "samples");
081: DeploymentScope deploymentScope = def
082: .getDeploymentScope("prepareMethodsToLog");
083:
084: Deployer.deploy(LoggingAspect.class, deploymentScope);
085: run();
086: Deployer.undeploy(LoggingAspect.class);
087: run();
088:
089: System.out
090: .println("-----------------------------------------------------------------------------------");
091: System.out
092: .println("---- deploy/undeploy using deployment handle ----");
093: System.out
094: .println("-----------------------------------------------------------------------------------");
095:
096: run();
097: DeploymentHandle handle2 = Deployer.deploy(LoggingAspect.class);
098: run();
099: Deployer.undeploy(handle2);
100: run();
101:
102: System.out
103: .println("-----------------------------------------------------------------------------------");
104: System.out
105: .println("---- deploy using XML def and undeploy using handle ----");
106: System.out
107: .println("-----------------------------------------------------------------------------------");
108:
109: run();
110: String 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>";
111: Deployer.deploy(XmlDefLoggingAspect.class, aspectXmlDef);
112: run();
113: Deployer.undeploy(XmlDefLoggingAspect.class);
114: run();
115:
116: }
117:
118: private static void run() {
119: try {
120: System.out.println("Target.main");
121: Target.toLog1(3);
122: Target target = new Target();
123: target.increment();
124: target.getCounter();
125:
126: TargetOther.toLog1(new int[] { 1, 2, 3 }, null, null, 0);
127: } catch (Throwable e) {
128: System.out.println("The runtime exception went thru: "
129: + e.toString());
130: e.printStackTrace();
131: }
132: }
133:
134: public static class TargetOther {
135:
136: public static int[] toLog1(int i[], String[] a, String b, int c) {
137: System.out.println("TargetOther.toLog1()");
138: return i;
139: }
140: }
141:
142: public void dummy() {
143:
144: }
145: }
|