001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.process;
016    
017    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    import java.io.ObjectInputStream;
022    import java.io.ObjectStreamClass;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class PortalClassLoaderObjectInputStream extends ObjectInputStream {
031    
032            public PortalClassLoaderObjectInputStream(InputStream inputStream)
033                    throws IOException {
034    
035                    super(inputStream);
036    
037                    _portalClassLoader = PortalClassLoaderUtil.getClassLoader();
038            }
039    
040            @Override
041            protected Class<?> resolveClass(ObjectStreamClass objectStreamClass)
042                    throws ClassNotFoundException {
043    
044                    String name = objectStreamClass.getName();
045    
046                    try {
047                            return Class.forName(name, false, _portalClassLoader);
048                    }
049                    catch (ClassNotFoundException cnfe) {
050                            Class<?> clazz = _primaryClasses.get(name);
051    
052                            if (clazz != null) {
053                                    return clazz;
054                            }
055                            else {
056                                    throw cnfe;
057                            }
058                    }
059            }
060    
061            private static final Map<String, Class<?>> _primaryClasses =
062                    new HashMap<String, Class<?>>(8, 1.0F);
063    
064            private final ClassLoader _portalClassLoader;
065    
066            static {
067                    _primaryClasses.put("boolean", boolean.class);
068                    _primaryClasses.put("byte", byte.class);
069                    _primaryClasses.put("char", char.class);
070                    _primaryClasses.put("short", short.class);
071                    _primaryClasses.put("int", int.class);
072                    _primaryClasses.put("long", long.class);
073                    _primaryClasses.put("float", float.class);
074                    _primaryClasses.put("double", double.class);
075                    _primaryClasses.put("void", void.class);
076            }
077    
078    }