001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    
019    /**
020     * <p>
021     * A class loader proxy able to serialize simple POJOs between two class
022     * loaders. It only works for simple POJOs following the Java Beans semantics.
023     * The local and remote classes do not have to match or even be derived from
024     * each other as long as their properties match. Any bean properties that the
025     * source bean exposes but the target bean does not will silently be ignored.
026     * </p>
027     *
028     * @author Micha Kiener
029     * @author Brian Wing Shun Chan
030     */
031    public class SimplePojoClp<T> {
032    
033            public SimplePojoClp(
034                            Class<? extends T> localImplementationClass,
035                            ClassLoader remoteClassLoader)
036                    throws ClassNotFoundException {
037    
038                    this(
039                            localImplementationClass, remoteClassLoader,
040                            localImplementationClass.getName());
041            }
042    
043            public SimplePojoClp(
044                            Class<? extends T> localImplementationClass,
045                            ClassLoader remoteClassLoader, String remoteImplementationClassName)
046                    throws ClassNotFoundException {
047    
048                    _localImplementationClass = localImplementationClass;
049                    _remoteClassLoader = remoteClassLoader;
050                    _remoteImplementationClass = _remoteClassLoader.loadClass(
051                            remoteImplementationClassName);
052            }
053    
054            public T createLocalObject(Object remoteInstance)
055                    throws IllegalAccessException, InstantiationException {
056    
057                    T localInstance = _localImplementationClass.newInstance();
058    
059                    BeanPropertiesUtil.copyProperties(
060                            remoteInstance, localInstance, _localImplementationClass);
061    
062                    return localInstance;
063            }
064    
065            public Object createRemoteObject(T localInstance)
066                    throws IllegalAccessException, InstantiationException {
067    
068                    Object remoteInstance = _remoteImplementationClass.newInstance();
069    
070                    BeanPropertiesUtil.copyProperties(
071                            localInstance, remoteInstance, _remoteImplementationClass);
072    
073                    return remoteInstance;
074            }
075    
076            private Class<? extends T> _localImplementationClass;
077            private ClassLoader _remoteClassLoader;
078            private Class<?> _remoteImplementationClass;
079    
080    }