com.l2jfree.gameserver.util.JarClassLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.l2jfree.gameserver.util.JarClassLoader.java

Source

/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jfree.gameserver.util;

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This is a class loader for the dynamic extensions used by DynamicExtension
 * class.
 * 
 * @version $Revision: $ $Date: $
 * @author galun
 */
public class JarClassLoader extends ClassLoader {
    private static Log _log = LogFactory.getLog(JarClassLoader.class);
    HashSet<String> _jars = new HashSet<String>();

    public void addJarFile(String filename) {
        _jars.add(filename);
    }

    @Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            byte[] b = loadClassData(name);
            return defineClass(name, b, 0, b.length);
        } catch (Exception e) {
            throw new ClassNotFoundException(name, e);
        }
    }

    private byte[] loadClassData(String name) throws IOException {
        byte[] classData = null;
        for (String jarFile : _jars) {
            ZipFile zipFile = null;
            DataInputStream zipStream = null;

            try {
                File file = new File(jarFile);
                zipFile = new ZipFile(file);
                String fileName = name.replace('.', '/') + ".class";
                ZipEntry entry = zipFile.getEntry(fileName);
                if (entry == null)
                    continue;
                classData = new byte[(int) entry.getSize()];
                zipStream = new DataInputStream(zipFile.getInputStream(entry));
                zipStream.readFully(classData, 0, (int) entry.getSize());
                break;
            } catch (IOException e) {
                _log.warn(jarFile + ":", e);
                continue;
            } finally {
                try {
                    if (zipFile != null)
                        zipFile.close();
                } catch (Exception e) {
                    _log.warn("", e);
                }
                try {
                    if (zipStream != null)
                        zipStream.close();
                } catch (Exception e) {
                    _log.warn("", e);
                }
            }
        }
        if (classData == null)
            throw new IOException("class not found in " + _jars);
        return classData;
    }
}