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 org.codehaus.aspectwerkz.definition; 9 10 import org.codehaus.aspectwerkz.MethodComparator; 11 12 import java.util.ArrayList; 13 import java.util.Collection; 14 import java.util.Collections; 15 import java.util.Comparator; 16 import java.util.HashMap; 17 import java.util.List; 18 import java.util.Map; 19 20 /*** 21 * Holds the meta-data for the aspect. 22 * 23 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a> 24 * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a> 25 */ 26 public class AspectDefinition { 27 /*** 28 * The name of the aspect. 29 */ 30 private String m_name; 31 32 /*** 33 * The name of the aspect prefixed by the system uuid 34 */ 35 private String m_fullQualifiedName; 36 37 /*** 38 * The aspect class name. 39 */ 40 private final String m_className; 41 42 /*** 43 * The deployment model for the aspect. 44 */ 45 private String m_deploymentModel = "perJVM"; 46 47 /*** 48 * The around advices. 49 */ 50 private final List m_aroundAdvices = new ArrayList(); 51 52 /*** 53 * The before advices. 54 */ 55 private final List m_beforeAdvices = new ArrayList(); 56 57 /*** 58 * The after advices. 59 */ 60 private final List m_afterAdvices = new ArrayList(); 61 62 /*** 63 * The interface introductions (pure interfaces) 64 */ 65 private final List m_interfaceIntroductions = new ArrayList(); 66 67 /*** 68 * The pointcuts definitions. The implementation introductions 69 */ 70 private final List m_introductions = new ArrayList(); 71 72 /*** 73 * The pointcuts. 74 */ 75 private final List m_pointcutDefs = new ArrayList(); 76 77 /*** 78 * The parameters passed to the advice at definition time. 79 */ 80 private Map m_parameters = new HashMap(); 81 82 /*** 83 * The container implementation class name. 84 */ 85 private String m_containerClassName; 86 87 /*** 88 * Creates a new aspect meta-data instance. 89 * 90 * @param name the name of the aspect 91 * @param className the class name of the aspect 92 */ 93 public AspectDefinition(final String name, final String className, final String uuid) { 94 if (name == null) { 95 throw new IllegalArgumentException("aspect name can not be null"); 96 } 97 if (className == null) { 98 throw new IllegalArgumentException("aspect class name can not be null"); 99 } 100 m_name = name; 101 m_className = className; 102 m_fullQualifiedName = uuid + "/" + name; 103 } 104 105 /*** 106 * Returns the pattern for the aspect 107 * 108 * @return the pattern 109 */ 110 public String getName() { 111 return m_name; 112 } 113 114 /*** 115 * Returns the pattern for the aspect 116 * 117 * @return the pattern 118 */ 119 public String getFullQualifiedName() { 120 return m_fullQualifiedName; 121 } 122 123 /*** 124 * Sets the name for the aspect. 125 * 126 * @param name the name 127 */ 128 public void setName(final String name) { 129 m_name = name.trim(); 130 } 131 132 /*** 133 * Returns the class name. 134 * 135 * @return the class name 136 */ 137 public String getClassName() { 138 return m_className; 139 } 140 141 /*** 142 * Sets the deployment model. 143 * 144 * @param deploymentModel the deployment model 145 */ 146 public void setDeploymentModel(final String deploymentModel) { 147 m_deploymentModel = deploymentModel; 148 } 149 150 /*** 151 * Returns the deployment model. 152 * 153 * @return the deployment model 154 */ 155 public String getDeploymentModel() { 156 return m_deploymentModel; 157 } 158 159 /*** 160 * Adds a new around advice. 161 * 162 * @param adviceMetaData the around advice 163 */ 164 public void addAroundAdvice(final AdviceDefinition adviceMetaData) { 165 m_aroundAdvices.add(adviceMetaData); 166 } 167 168 /*** 169 * Remove an around advice. Experimental 170 * 171 * @param adviceMetaData the around advice 172 */ 173 public void removeAroundAdvice(final AdviceDefinition adviceMetaData) { 174 m_aroundAdvices.remove(adviceMetaData); 175 } 176 177 /*** 178 * Returns the around advices. 179 * 180 * @return the around advices 181 */ 182 public List getAroundAdvices() { 183 return m_aroundAdvices; 184 } 185 186 /*** 187 * Adds a new before advice. 188 * 189 * @param adviceMetaData the before advice 190 */ 191 public void addBeforeAdvice(final AdviceDefinition adviceMetaData) { 192 m_beforeAdvices.add(adviceMetaData); 193 } 194 195 /*** 196 * Returns the before advices. 197 * 198 * @return the before advices 199 */ 200 public List getBeforeAdvices() { 201 return m_beforeAdvices; 202 } 203 204 /*** 205 * Adds a new after advice. 206 * 207 * @param adviceMetaData the after advice 208 */ 209 public void addAfterAdvice(final AdviceDefinition adviceMetaData) { 210 m_afterAdvices.add(adviceMetaData); 211 } 212 213 /*** 214 * Returns the after advices. 215 * 216 * @return the after advices 217 */ 218 public List getAfterAdvices() { 219 return m_afterAdvices; 220 } 221 222 /*** 223 * Adds a new pure interface introduction. 224 * 225 * @param interfaceIntroductionMetaData the introduction 226 */ 227 public void addInterfaceIntroduction(final InterfaceIntroductionDefinition interfaceIntroductionMetaData) { 228 m_interfaceIntroductions.add(interfaceIntroductionMetaData); 229 } 230 231 /*** 232 * Adds a new implementation introduction. 233 * 234 * @param introductionMetaData the introduction 235 */ 236 public void addIntroduction(final IntroductionDefinition introductionMetaData) { 237 m_introductions.add(introductionMetaData); 238 } 239 240 /*** 241 * Returns the interface introductions. 242 * 243 * @return the introductions 244 */ 245 public List getInterfaceIntroductions() { 246 return m_interfaceIntroductions; 247 } 248 249 /*** 250 * Returns the implementation introductions. 251 * 252 * @return the introductions 253 */ 254 public List getIntroductions() { 255 return m_introductions; 256 } 257 258 /*** 259 * Adds a new pointcut definition. 260 * 261 * @param pointcutDef the pointcut definition 262 */ 263 public void addPointcut(final PointcutDefinition pointcutDef) { 264 m_pointcutDefs.add(pointcutDef); 265 } 266 267 /*** 268 * Returns the pointcuts. 269 * 270 * @return the pointcuts 271 */ 272 public Collection getPointcuts() { 273 return m_pointcutDefs; 274 } 275 276 /*** 277 * Adds a new parameter to the advice. 278 * 279 * @param name the name of the parameter 280 * @param value the value for the parameter 281 */ 282 public void addParameter(final String name, final String value) { 283 m_parameters.put(name, value); 284 } 285 286 /*** 287 * Returns the parameters as a Map. 288 * 289 * @return the parameters 290 */ 291 public Map getParameters() { 292 return m_parameters; 293 } 294 295 /*** 296 * Sets the name of the container implementation class. 297 * 298 * @param containerClassName the container class name 299 */ 300 public void setContainerClassName(final String containerClassName) { 301 m_containerClassName = containerClassName; 302 } 303 304 /*** 305 * Returns the name of the container implementation class. 306 * 307 * @return the container class name 308 */ 309 public String getContainerClassName() { 310 return m_containerClassName; 311 } 312 313 /*** 314 * Returns all the advices for this aspect. 315 * 316 * @return all the advices 317 * @TODO: gets sorted every time, have a flag? 318 */ 319 public List getAllAdvices() { 320 final List allAdvices = new ArrayList(); 321 allAdvices.addAll(m_aroundAdvices); 322 allAdvices.addAll(m_beforeAdvices); 323 allAdvices.addAll(m_afterAdvices); 324 return sortAdvices(allAdvices); 325 } 326 327 /*** 328 * Sorts the advice by method. 329 * 330 * @param advices a list with the advices to sort 331 * @return a sorted list with the advices 332 */ 333 public static List sortAdvices(final List advices) { 334 Collections.sort(advices, new Comparator() { 335 private Comparator m_comparator = MethodComparator.getInstance(MethodComparator.NORMAL_METHOD); 336 337 public int compare(final Object obj1, final Object obj2) { 338 AdviceDefinition advice1 = (AdviceDefinition) obj1; 339 AdviceDefinition advice2 = (AdviceDefinition) obj2; 340 return m_comparator.compare(advice1.getMethod(), advice2.getMethod()); 341 } 342 }); 343 return advices; 344 } 345 }