001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     matic
016 */
017package org.nuxeo.osgi.util.jar;
018
019import java.io.IOException;
020import java.lang.reflect.Field;
021import java.lang.reflect.InvocationTargetException;
022import java.lang.reflect.Method;
023import java.net.URL;
024import java.util.jar.JarFile;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028
029/**
030 * @author matic
031 */
032public class JarFileFactoryCloser {
033
034    private static final Log log = LogFactory.getLog(JarFileFactoryCloser.class);
035
036    protected boolean ok;
037
038    protected Object factory;
039
040    protected Method factoryGetMethod;
041
042    protected Method factoryCloseMethod;
043
044    public JarFileFactoryCloser() {
045        try {
046            introspectClasses();
047        } catch (ClassNotFoundException e) {
048            log.error("Cannot introspect jar file factory class", e);
049        } catch (SecurityException e) {
050            log.error("Cannot introspect jar file factory class", e);
051        } catch (NoSuchFieldException e) {
052            log.error("Cannot introspect jar file factory class", e);
053        } catch (IllegalAccessException e) {
054            log.error("Cannot introspect jar file factory class", e);
055        } catch (NoSuchMethodException e) {
056            log.error("Cannot introspect jar file factory class", e);
057        }
058    }
059
060    protected void introspectClasses() throws ClassNotFoundException, SecurityException, NoSuchFieldException,
061            IllegalAccessException, NoSuchMethodException {
062        Class<?> jarURLConnectionClass = loadClass("sun.net.www.protocol.jar.JarURLConnection");
063        Field jarFileFactoryField = jarURLConnectionClass.getDeclaredField("factory");
064        jarFileFactoryField.setAccessible(true);
065        factory = jarFileFactoryField.get(null);
066        Class<?> factoryClass = loadClass("sun.net.www.protocol.jar.JarFileFactory");
067        factoryGetMethod = factoryClass.getMethod("get", new Class<?>[] { URL.class });
068        factoryGetMethod.setAccessible(true);
069        factoryCloseMethod = factoryClass.getMethod("close", new Class<?>[] { JarFile.class });
070        factoryCloseMethod.setAccessible(true);
071        ok = true;
072    }
073
074    protected static Class<?> loadClass(String name) throws ClassNotFoundException {
075        return URLClassLoaderCloser.class.getClassLoader().loadClass(name);
076    }
077
078    public void close(URL location) throws IOException {
079        if (!ok) {
080            return;
081        }
082        JarFile jar = null;
083        try {
084            jar = (JarFile) factoryGetMethod.invoke(factory, new Object[] { location });
085            factoryCloseMethod.invoke(factory, jar);
086        } catch (IllegalAccessException e) {
087            throw new RuntimeException("Cannot use reflection on jar file factory", e);
088        } catch (InvocationTargetException e) {
089            throw new RuntimeException("Cannot use reflection on jar file factory", e);
090        }
091        jar.close();
092    }
093
094}