1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }