1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
18  
19  /**
20   * <a href="SimplePojoClp.java.html"><b><i>View Source</i></b></a>
21   *
22   * <p>
23   * A class loader proxy able to serialize simple POJOs between two class
24   * loaders. It only works for simple POJOs following the Java Beans semantics.
25   * The local and remote classes do not have to match or even be derived from
26   * each other as long as their properties match. Any bean properties that the
27   * source bean exposes but the target bean does not will silently be ignored.
28   * </p>
29   *
30   * @author Micha Kiener
31   * @author Brian Wing Shun Chan
32   */
33  public class SimplePojoClp<T> {
34  
35      public SimplePojoClp(
36              Class<? extends T> localImplementationClass,
37              ClassLoader remoteClassLoader)
38          throws ClassNotFoundException {
39  
40          this(
41              localImplementationClass, remoteClassLoader,
42              localImplementationClass.getName());
43      }
44  
45      public SimplePojoClp(
46              Class<? extends T> localImplementationClass,
47              ClassLoader remoteClassLoader, String remoteImplementationClassName)
48          throws ClassNotFoundException {
49  
50          _localImplementationClass = localImplementationClass;
51          _remoteClassLoader = remoteClassLoader;
52          _remoteImplementationClass = _remoteClassLoader.loadClass(
53              remoteImplementationClassName);
54      }
55  
56      public T createLocalObject(Object remoteInstance)
57          throws IllegalAccessException, InstantiationException {
58  
59          T localInstance = _localImplementationClass.newInstance();
60  
61          BeanPropertiesUtil.copyProperties(
62              remoteInstance, localInstance, _localImplementationClass);
63  
64          return localInstance;
65      }
66  
67      public Object createRemoteObject(T localInstance)
68          throws IllegalAccessException, InstantiationException {
69  
70          Object remoteInstance = _remoteImplementationClass.newInstance();
71  
72          BeanPropertiesUtil.copyProperties(
73              localInstance, remoteInstance, _remoteImplementationClass);
74  
75          return remoteInstance;
76      }
77  
78      private Class<? extends T> _localImplementationClass;
79      private ClassLoader _remoteClassLoader;
80      private Class<?> _remoteImplementationClass;
81  
82  }