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.shared;
18  
19  import it.openutils.spring.rmibernate.client.HibernateRmiProxyFactoryBean;
20  import it.openutils.spring.rmibernate.client.aspects.ClientSerializationInterceptor;
21  import it.openutils.spring.rmibernate.client.aspects.HibernateLazyLoaderAspect;
22  import it.openutils.spring.rmibernate.server.aspects.util.EntitySerializer;
23  
24  import java.io.ObjectStreamException;
25  import java.io.Serializable;
26  
27  import net.sf.cglib.proxy.Callback;
28  import net.sf.cglib.proxy.Enhancer;
29  
30  
31  /**
32   * Store lazy reference
33   * @author mmolaschi
34   * @version $Id: LazyReference.java 716 2008-03-03 14:35:57Z fcarone $
35   */
36  public class LazyReference implements Serializable
37  {
38  
39      /**
40       * UID
41       */
42      private static final long serialVersionUID = 421364413345638150L;
43  
44      private Serializable id;
45  
46      private String fieldName;
47  
48      private String className;
49  
50      private String fieldClassName;
51  
52      /**
53       * default constructor
54       */
55      public LazyReference()
56      {
57  
58      }
59  
60      /**
61       * Constructor
62       * @param className className
63       * @param fieldName fieldName
64       * @param fieldClassName fieldClassName
65       * @param id id of current entity
66       */
67      public LazyReference(String className, String fieldName, String fieldClassName, Serializable id)
68      {
69          this.className = className;
70          this.fieldName = fieldName;
71          this.fieldClassName = fieldClassName;
72          this.id = id;
73      }
74  
75      /**
76       * Called on deserialization, creates proxy for remote lazy loading
77       * @return proxy for remote lazy loading
78       * @throws ObjectStreamException exception reading
79       * @throws ClassNotFoundException class not found
80       */
81      @SuppressWarnings("unchecked")
82      Object readResolve() throws ObjectStreamException, ClassNotFoundException
83      {
84          // get proxy superclass
85          Class superclass = Class.forName(fieldClassName);
86          Class[] interfaces = new Class[]{Serializable.class };
87  
88          // if superclass is an interface add it to interfaces
89          if (superclass.isInterface())
90          {
91              interfaces = new Class[]{superclass, Serializable.class, EntitySerializer.class };
92          }
93  
94          HibernateLazyLoaderAspect hlla = new HibernateLazyLoaderAspect(
95              className,
96              fieldName,
97              id,
98              HibernateRmiProxyFactoryBean.hibernateLazyServiceTL.get());
99  
100         // return proxy
101         return Enhancer.create(superclass, interfaces, new HibernateLazyLoaderCallbackFilter(), new Callback[]{
102             hlla,
103             new ClientSerializationInterceptor(hlla) });
104     }
105 
106     public String getClassName()
107     {
108         return className;
109     }
110 
111     public void setClassName(String className)
112     {
113         this.className = className;
114     }
115 
116     public Serializable getId()
117     {
118         return id;
119     }
120 
121     public void setId(Serializable id)
122     {
123         this.id = id;
124     }
125 
126     public String getFieldName()
127     {
128         return fieldName;
129     }
130 
131     public void setFieldName(String fieldName)
132     {
133         this.fieldName = fieldName;
134     }
135 
136     public String getFieldClassName()
137     {
138         return fieldClassName;
139     }
140 
141     public void setFieldClassName(String fieldClassName)
142     {
143         this.fieldClassName = fieldClassName;
144     }
145 
146 }