View Javadoc

1   /*
2    * Copyright Openmind http://www.openmindonline.it
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package it.openutils.spring.rmibernate.client.aspects;
18  
19  import it.openutils.spring.rmibernate.client.pagination.Paginator;
20  import it.openutils.spring.rmibernate.shared.managers.HibernateLazyService;
21  
22  import java.io.Serializable;
23  
24  import net.sf.cglib.proxy.LazyLoader;
25  
26  
27  /**
28   * Aspect that does remote lazy loading on cglib proxy
29   * @author mmolaschi
30   * @version $Id: HibernateLazyLoaderAspect.java 716 2008-03-03 14:35:57Z fcarone $
31   */
32  public class HibernateLazyLoaderAspect implements LazyLoader, Serializable
33  {
34  
35      /**
36       * UID
37       */
38      private static final long serialVersionUID = -365731708075101363L;
39  
40      private static ThreadLocal<Paginator> paginator = new ThreadLocal<Paginator>();
41  
42      private String className;
43  
44      private String fieldName;
45  
46      private Serializable id;
47  
48      private Object localReference;
49  
50      private HibernateLazyService hibernateLazyService;
51  
52      /**
53       * Constructor
54       * @param className parent class name
55       * @param fieldName field to be lazy loaded
56       * @param id id of current entity
57       */
58      public HibernateLazyLoaderAspect(String className, String fieldName, Serializable id, HibernateLazyService hibernateLazyService)
59      {
60          this.className = className;
61          this.fieldName = fieldName;
62          this.id = id;
63          this.hibernateLazyService = hibernateLazyService;
64      }
65  
66      /**
67       * Set pagination for next calls in current thread
68       * @param from starting row number
69       * @param size max number of rows
70       */
71      public static void setPagination(int from, int size)
72      {
73          paginator.set(new Paginator(from, size));
74      }
75  
76      /**
77       * Disable pagination for next calls in current thread
78       */
79      public static void resetPagination()
80      {
81          paginator.set(null);
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      public Object loadObject() throws Exception
88      {
89          // @todo handle localreference with paginator
90          // Load from remote
91          if (paginator.get() != null)
92          {
93              return hibernateLazyService.invoke(className, fieldName, id, paginator.get().getFrom(), paginator.get().getSize());
94          }
95          if (localReference == null)
96          {
97              localReference = hibernateLazyService.invoke(className, fieldName, id);
98          }
99          return localReference;
100     }
101 
102 }