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.zip;
016    
017    import com.liferay.portal.kernel.zip.ZipReader;
018    import com.liferay.portal.kernel.zip.ZipReaderFactory;
019    import com.liferay.portal.util.ClassLoaderUtil;
020    
021    import java.io.File;
022    import java.io.IOException;
023    import java.io.InputStream;
024    
025    /**
026     * @author Raymond Aug??
027     */
028    public class ZipReaderFactoryImpl implements ZipReaderFactory {
029    
030            @Override
031            public ZipReader getZipReader(File file) {
032                    ClassLoader portalClassLoader = ClassLoaderUtil.getPortalClassLoader();
033    
034                    ClassLoader contextClassLoader =
035                            ClassLoaderUtil.getContextClassLoader();
036    
037                    try {
038                            if (contextClassLoader != portalClassLoader) {
039                                    ClassLoaderUtil.setContextClassLoader(portalClassLoader);
040                            }
041    
042                            return new ZipReaderImpl(file);
043                    }
044                    finally {
045                            if (contextClassLoader != portalClassLoader) {
046                                    ClassLoaderUtil.setContextClassLoader(contextClassLoader);
047                            }
048                    }
049            }
050    
051            @Override
052            public ZipReader getZipReader(InputStream inputStream) throws IOException {
053                    ClassLoader portalClassLoader = ClassLoaderUtil.getPortalClassLoader();
054    
055                    ClassLoader contextClassLoader =
056                            ClassLoaderUtil.getContextClassLoader();
057    
058                    try {
059                            if (contextClassLoader != portalClassLoader) {
060                                    ClassLoaderUtil.setContextClassLoader(portalClassLoader);
061                            }
062    
063                            return new ZipReaderImpl(inputStream);
064                    }
065                    finally {
066                            if (contextClassLoader != portalClassLoader) {
067                                    ClassLoaderUtil.setContextClassLoader(contextClassLoader);
068                            }
069                    }
070            }
071    
072    }