View Javadoc

1   /*************************************************
2    * Copyright (c) Shen Li. All rights reserved.  *
3    * http://joyaop.sourceforge.net                *
4    * -------------------------------------------- *
5    * Distributable under LGPL license.            *
6    * See terms of license at gnu.org.             *
7    ************************************************/
8   package net.sf.joyaop.impl;
9   
10  import net.sf.joyaop.impl.aspect.BaseRuntimeAspectImpl;
11  
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  /***
16   * Not thread-safe. It ensures that the proxies contain no duplicate runtime aspect instance.  
17   *
18   * @author Shen Li
19   */
20  public class RuntimeAspectInstanceFactory {
21      private final Map aspectInstances = new HashMap();
22      private final Class originalClass;
23  
24      public RuntimeAspectInstanceFactory(Class originalClass) {
25          this.originalClass = originalClass;
26      }
27  
28      public RuntimeAspectInstance getRuntimeAspectInstance(BaseRuntimeAspectImpl aspect) {
29          RuntimeAspectInstance aspectInstance = (RuntimeAspectInstance) aspectInstances.get(aspect.getAspectClass());
30          if (aspectInstance == null) {
31              aspectInstance = aspect.createRuntimeAspectInstance(originalClass);
32              aspectInstances.put(aspect.getAspectClass(), aspectInstance);
33          }
34          return aspectInstance;
35      }
36  }