001    /**
002     * Copyright (c) 2000-2010 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.util.PortalClassLoaderUtil;
018    import com.liferay.portal.kernel.zip.ZipReader;
019    import com.liferay.portal.kernel.zip.ZipReaderFactory;
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 = PortalClassLoaderUtil.getClassLoader();
032    
033                    Thread currentThread = Thread.currentThread();
034    
035                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
036    
037                    try {
038                            if (contextClassLoader != portalClassLoader) {
039                                    currentThread.setContextClassLoader(portalClassLoader);
040                            }
041    
042                            return new ZipReaderImpl(file);
043                    }
044                    finally {
045                            if (contextClassLoader != portalClassLoader) {
046                                    currentThread.setContextClassLoader(contextClassLoader);
047                            }
048                    }
049            }
050    
051            public ZipReader getZipReader(InputStream inputStream) throws IOException {
052                    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
053    
054                    Thread currentThread = Thread.currentThread();
055    
056                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
057    
058                    try {
059                            if (contextClassLoader != portalClassLoader) {
060                                    currentThread.setContextClassLoader(portalClassLoader);
061                            }
062    
063                            return new ZipReaderImpl(inputStream);
064                    }
065                    finally {
066                            if (contextClassLoader != portalClassLoader) {
067                                    currentThread.setContextClassLoader(contextClassLoader);
068                            }
069                    }
070            }
071    
072    }