1 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 21 public class Target { 22 23 26 private int m_counter1; 27 28 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 45 public static int toLog1(int i) { 46 System.out.println("Target.toLog1()"); 47 new Target().toLog2( 48 new String []{ 49 "parameter" 50 } 51 ); 52 return 1; 53 } 54 55 58 public java.lang.String [] toLog2(java.lang.String [] arg) { 59 System.out.println("Target.toLog2()"); 60 new Target().toLog3(); 61 return null; 63 } 64 65 68 public Object toLog3() { 69 System.out.println("Target.toLog3()"); 70 return "result"; 71 } 72 73 public static void main(String [] 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 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 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 [] a, String b, int c) { 133 System.out.println("TargetOther.toLog1()"); 134 return i; 135 } 136 } 137 138 public void dummy() { 139 140 } 141 } | Popular Tags |