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.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            public ZipReader getZipReader(File file) {
031                    ClassLoader portalClassLoader = ClassLoaderUtil.getPortalClassLoader();
032    
033                    ClassLoader contextClassLoader =
034                            ClassLoaderUtil.getContextClassLoader();
035    
036                    try {
037                            if (contextClassLoader != portalClassLoader) {
038                                    ClassLoaderUtil.setContextClassLoader(portalClassLoader);
039                            }
040    
041                            return new ZipReaderImpl(file);
042                    }
043                    finally {
044                            if (contextClassLoader != portalClassLoader) {
045                                    ClassLoaderUtil.setContextClassLoader(contextClassLoader);
046                            }
047                    }
048            }
049    
050            public ZipReader getZipReader(InputStream inputStream) throws IOException {
051                    ClassLoader portalClassLoader = ClassLoaderUtil.getPortalClassLoader();
052    
053                    ClassLoader contextClassLoader =
054                            ClassLoaderUtil.getContextClassLoader();
055    
056                    try {
057                            if (contextClassLoader != portalClassLoader) {
058                                    ClassLoaderUtil.setContextClassLoader(portalClassLoader);
059                            }
060    
061                            return new ZipReaderImpl(inputStream);
062                    }
063                    finally {
064                            if (contextClassLoader != portalClassLoader) {
065                                    ClassLoaderUtil.setContextClassLoader(contextClassLoader);
066                            }
067                    }
068            }
069    
070    }