001    /**
002     * Copyright (c) 2000-2012 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.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    }