1 22 package org.jboss.aop.advice; 23 24 import org.jboss.aop.Advisor; 25 import org.jboss.aop.ClassAdvisor; 26 import org.jboss.aop.GeneratedClassAdvisor; 27 import org.jboss.aop.joinpoint.FieldJoinpoint; 28 import org.jboss.aop.joinpoint.Joinpoint; 29 30 36 public class AdviceFactory implements InterceptorFactory 37 { 38 private String advice; 39 private AspectDefinition aspect; 40 41 public AdviceFactory(AspectDefinition aspect, String adviceName) 42 { 43 this.aspect = aspect; 44 this.advice = adviceName; 45 } 46 47 public AspectDefinition getAspect() 48 { 49 return aspect; 50 } 51 52 public String getAdvice() 53 { 54 return advice; 55 } 56 57 public boolean isDeployed() 58 { 59 return aspect.isDeployed(); 60 } 61 62 public Interceptor create(Advisor advisor, Joinpoint joinpoint) 63 { 64 if (aspect.getScope() == Scope.PER_VM) 65 { 66 try 67 { 68 return PerVmAdvice.generateOptimized(joinpoint, advisor.getManager(), advice, aspect); 69 } 70 catch (Exception e) 71 { 72 throw new RuntimeException (e); } 74 } 75 else if (aspect.getScope() == Scope.PER_CLASS) 76 { 77 try 78 { 79 return PerClassAdvice.generate(joinpoint, advisor, advice, aspect); 80 } 81 catch (Exception e) 82 { 83 throw new RuntimeException (e); } 85 } 86 else if (aspect.getScope() == Scope.PER_INSTANCE) 87 { 88 return new PerInstanceAdvice(advice, aspect, advisor); 89 } 90 else if (aspect.getScope() == Scope.PER_JOINPOINT) 91 { 92 try 93 { 94 return PerJoinpointAdvice.createInterceptor(advisor, joinpoint, aspect, advice); 95 } 96 catch (Exception e) 97 { 98 throw new RuntimeException (e); } 100 } 101 else if (aspect.getScope() == Scope.PER_CLASS_JOINPOINT) 102 { 103 Object instance = null; 104 105 if (advisor instanceof GeneratedClassAdvisor) 106 { 107 instance = ((GeneratedClassAdvisor)advisor).getPerClassJoinpointAspect(aspect, joinpoint); 109 if (instance == null) 110 { 111 ((GeneratedClassAdvisor)advisor).addPerClassJoinpointAspect(aspect, joinpoint); 112 instance = ((GeneratedClassAdvisor)advisor).getPerClassJoinpointAspect(aspect, joinpoint); 113 } 114 } 115 else if (joinpoint instanceof FieldJoinpoint) 116 { 117 FieldJoinpoint field = (FieldJoinpoint) joinpoint; 118 ClassAdvisor classAdvisor = (ClassAdvisor) advisor; 119 instance = classAdvisor.getFieldAspect(field, aspect); 120 } 121 else 122 { 123 instance = aspect.getFactory().createPerJoinpoint(advisor, joinpoint); 124 } 125 126 try 127 { 128 return PerVmAdvice.generateInterceptor(joinpoint, instance, advice); 129 } 130 catch (Exception e) 131 { 132 throw new RuntimeException (e); 133 } 134 } 135 else 136 { 137 } 139 return null; 140 } 141 142 public String getName() 143 { 144 return aspect.getName() + "." + advice; 145 } 146 147 public boolean equals(Object obj) 148 { 149 if (this == obj) return true; 150 if (!(obj instanceof AdviceFactory)) return false; 151 152 AspectDefinition otherAspect = ((AdviceFactory)obj).getAspect(); 153 154 if (!this.aspect.getName().equals(otherAspect.getName())) return false; 155 if (!this.aspect.getFactory().getName().equals(otherAspect.getFactory().getName()))return false; 156 if (!this.advice.equals(((AdviceFactory)obj).advice))return false; 157 158 return true; 159 } 160 } 161 | Popular Tags |