View Javadoc

1   /*
2    * Copyright 2013 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the
5    * "License"); 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.opensource.org/licenses/ecl1.php
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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.kuali.student.common.spring;
17  
18  import java.lang.reflect.Proxy;
19  import java.lang.reflect.TypeVariable;
20  
21  import org.kuali.rice.krad.uif.service.ViewHelperService;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.springframework.beans.BeansException;
25  import org.springframework.beans.factory.BeanFactory;
26  import org.springframework.beans.factory.FactoryBean;
27  import org.springframework.context.ApplicationContext;
28  import org.springframework.context.ApplicationContextAware;
29  
30  /**
31   * Create a Serializable Proxy for the beanName and java interface provided.
32   * <pre>{@code
33   *     <bean id="serializableList" class="org.kuali.student.common.spring.SerializableProxyFactoryBean">
34   *   
35   *         <constructor-arg index="0"><value>list</value></constructor-arg>
36   *         <constructor-arg index="1"><value>java.util.List</value></constructor-arg>
37   *     </bean>
38   *   
39   *     <bean id="list" class="java.util.ArrayList">
40   *           <constructor-arg>
41   *               <list>
42   *                   <value>Test</value>
43   *                   <value>Proxy</value>
44   *               </list>
45   *           </constructor-arg>
46   *     </bean>  
47   *}
48   * </pre>
49   * 
50   * When serialized only the bean name is saved and then on deserialization the real bean is retrieved from the application context.
51   * 
52   * 
53   * @author Kuali Student Team
54   * 
55   */
56  public class SerializableProxyFactoryBean<T> implements
57          FactoryBean<T>, ApplicationContextAware {
58  
59      private String beanName;
60  
61      private Class<?>beanClass;
62      
63      /**
64       * 
65       */
66      public SerializableProxyFactoryBean(String beanName, Class<?>beanClass) {
67          this.beanName = beanName;
68          this.beanClass = beanClass;
69      }
70  
71      /*
72       * (non-Javadoc)
73       * 
74       * @see
75       * org.springframework.context.ApplicationContextAware#setApplicationContext
76       * (org.springframework.context.ApplicationContext)
77       */
78      @Override
79      public void setApplicationContext(ApplicationContext applicationContext)
80              throws BeansException {
81          SerializableSpringBeanProxyInvocationHandler
82                  .setApplicationContext(applicationContext);
83  
84      }
85  
86      /*
87       * (non-Javadoc)
88       * 
89       * @see org.springframework.beans.factory.FactoryBean#getObject()
90       */
91      @Override
92      public T getObject() throws Exception {
93  
94          SerializableSpringBeanProxyInvocationHandler handler = new SerializableSpringBeanProxyInvocationHandler();
95  
96          handler.setBeanName(beanName);
97  
98          @SuppressWarnings("unchecked")
99          T proxy = (T) Proxy.newProxyInstance(
100                 getClass().getClassLoader(),
101                 new Class[] { beanClass }, handler);
102         
103         return proxy;
104     }
105 
106     /*
107      * (non-Javadoc)
108      * 
109      * @see org.springframework.beans.factory.FactoryBean#getObjectType()
110      */
111     @Override
112     public Class<?> getObjectType() {
113         
114         return beanClass;
115     }
116 
117     /*
118      * (non-Javadoc)
119      * 
120      * @see org.springframework.beans.factory.FactoryBean#isSingleton()
121      */
122     @Override
123     public boolean isSingleton() {
124         return false;
125     }
126 
127 }