All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.allcolor.yahp.converter.CClassLoader Maven / Gradle / Ivy

Go to download

YaHP is a java library which permits to convert an html document into a pdf document.

The newest version!
/*
 * Copyright (C) 2005 by Quentin Anciaux
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This library 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 Library General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *	@author Quentin Anciaux
 */
package org.allcolor.yahp.converter;

import java.beans.Introspector;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.net.URLStreamHandlerFactory;

import java.security.AccessControlContext;
import java.security.ProtectionDomain;

import java.sql.Driver;
import java.sql.DriverManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.LookAndFeel;
import javax.swing.UIManager;

/**
 * This is a custom tree classloader
 * 
 * @author Quentin Anciaux
 * @version 1.0
 */
public final class CClassLoader extends URLClassLoader {
	/** classloader namespace */
	public static final String CCLASSLOADER_NAMESPACE = "org.allcolor::CClassLoader.loadClass::";

	private static ThreadLocal contextLoader = new ThreadLocal();

	/** DEBUG log level */
	private static final int DEBUG = 1;

	/** FATAL log level */
	private static final int FATAL = 2;

	/** INFO log level */
	private static final int INFO = 0;

	/** use for logging */
	private static final Logger log = Logger.getLogger(CClassLoader.class
			.getName());

	/** contains all mandatory loaders */
	private static final Map mandatoryLoadersMap = new HashMap();

	/** name of the rootloader */
	public static final String ROOT_LOADER = "rootLoader";

	/** handle to the root loader */
	private static CClassLoader rootLoader = CClassLoader.createLoader(
			(CClassLoader.class.getClassLoader() != null) ? CClassLoader.class
					.getClassLoader() : ClassLoader.getSystemClassLoader(),
			CClassLoader.ROOT_LOADER);

	/**
	 * Ser the URLClassLoader that will return to calls to getURLs()
	 */
	private static volatile URLClassLoader urlLoader = null;

	protected void finalize() throws Throwable {
		System.out.println("Finalizing classloader : "+this.finalizepath);
		this.finalizepath = null;
	}
	
	/**
	 * Create a new loader
	 * 
	 * @param parent
	 *            a reference to the parent loader
	 * @param name
	 *            loader name
	 * 
	 * @return a new loader
	 */
	private static final CClassLoader createLoader(final ClassLoader parent,
			final String name) {
		try {
			return new CClassLoader(parent, name);
		} catch (final Exception ignore) {
			return null;
		}
	} // end createLoader()

	/**
	 * Creates and allocates a memory URL
	 * 
	 * @param entryName
	 *            name of the entry
	 * @param entry
	 *            byte array of the entry
	 * @return the created URL or null if an error occurs.
	 */
	public static URL createMemoryURL(final String entryName, final byte[] entry) {
		try {
			final Class c = ClassLoader.getSystemClassLoader().loadClass(
					"org.allcolor.yahp.converter.CMemoryURLHandler");
			final Method m = c.getDeclaredMethod("createMemoryURL",
					new Class[] { String.class, byte[].class });
			m.setAccessible(true);
			return (URL) m.invoke(null, new Object[] { entryName, entry });
		} catch (final Exception ignore) {
			ignore.printStackTrace();
			return null;
		}
	}
	
	private static final void clearMap(Map map) {
		for(Iterator it = map.entrySet().iterator();it.hasNext();) {
			Map.Entry e = (Map.Entry)it.next();
			Object value = e.getKey();
			if ((value != null)
					&& (value.getClass()
							.getClassLoader() != null)
					&& (value.getClass()
							.getClassLoader()
							.getClass() == CClassLoader.class)) {
				System.out
				.println("Resseting thread local "
						+ value);
				it.remove();
				continue;
			}
			if (value instanceof Map) {
				clearMap((Map)value);
			} else if (value instanceof List) {
				clearList((List)value);
			} else if (value instanceof Set) {
				clearSet((Set)value);
			}
			value = e.getValue();
			if ((value != null)
					&& (value.getClass()
							.getClassLoader() != null)
					&& (value.getClass()
							.getClassLoader()
							.getClass() == CClassLoader.class)) {
				System.out
				.println("Resseting thread local "
						+ value);
				it.remove();
				continue;
			}
			if (value instanceof Map) {
				clearMap((Map)value);
			} else if (value instanceof List) {
				clearList((List)value);
			} else if (value instanceof Set) {
				clearSet((Set)value);
			} else if (value instanceof Object[]) {
				clearArray((Object[])value);
			}
		}
	}
	
	private static final void clearArray(Object [] array) {
		for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy