View Javadoc

1   /*
2    * CacheAspect.java
3    * Created on 18 Июль 2006 г., 14:46
4    */
5   
6   package org.weda.enhance.impl;
7   
8   import java.lang.reflect.Field;
9   import org.jboss.aop.joinpoint.FieldInvocation;
10  import org.jboss.aop.joinpoint.FieldReadInvocation;
11  import org.jboss.aop.joinpoint.FieldWriteInvocation;
12  import org.weda.NamedObject;
13  import org.weda.cache.CacheEntity;
14  import org.weda.cache.CacheService;
15  import org.weda.cache.impl.SimpleCacheEntity;
16  
17  /**
18   *
19   * @author Mikhail Titov
20   */
21  public class CacheAspect {
22  
23      public Object getObjectFromCache(FieldReadInvocation joinPoint)
24          throws Throwable
25      {
26          Object value = null;
27          Field field = joinPoint.getField();
28          String id = getCacheEntityId(joinPoint);
29          CacheEntity cacheEntity = 
30                  AspectHelperImpl.INSTANCE
31                      .getCacheManager().getCacheService().get(id);        
32          return cacheEntity==null ? null : cacheEntity.getValue();
33      }
34      
35      public Object cacheObject(FieldWriteInvocation joinPoint){
36          CacheService cacheService = 
37                  AspectHelperImpl.INSTANCE.getCacheManager().getCacheService();
38          Object value = joinPoint.getValue();
39          String id = getCacheEntityId(joinPoint);
40          if (value==null)
41              cacheService.release(id);
42          else
43              cacheService.setCacheEntity(id, new SimpleCacheEntity(value));
44          return null;
45      }
46      
47      private String getCacheEntityId(FieldInvocation fieldInv){
48          Field field = fieldInv.getField();
49          return 
50                  field.getDeclaringClass().getName()+"#"
51                  +field.getName()+"#"
52                  +((NamedObject)fieldInv.getTargetObject()).getObjectName();
53          
54      }
55  }