/*
* ClassLoader - JarFileLoader.java, Oct 3, 2011 1:06:13 PM
*
* Copyright 2011 Varra Ltd, Inc. All rights reserved.
* Varra proprietary/confidential. Use is subject to license terms.
*/
package com.varra.net;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.jar.JarFile;
import com.varra.util.EnhancedTimerTask;
import com.varra.util.GlobalThread;
/**
* The Class JarFileLoader.
*
* @author Rajakrishna V. Reddy
* @version 1.0
*/
public class JarFileLoader extends URLClassLoader
{
/**
* Instantiates a new jar file loader.
*
* @param urls
* the urls
*/
public JarFileLoader()
{
super(new URL[] {});
}
/**
* Adds the file.
*
* @param file
* the file
* @throws MalformedURLException
* the malformed url exception
*/
public void addFile(File file) throws MalformedURLException
{
addURL(file.toURI().toURL());
}
/**
* Closes all open jar files.
*/
public void close()
{
try
{
Class<?> clazz = java.net.URLClassLoader.class;
Field ucp = clazz.getDeclaredField("ucp");
ucp.setAccessible(true);
Object sunMiscURLClassPath = ucp.get(this);
Field loaders = sunMiscURLClassPath.getClass().getDeclaredField("loaders");
loaders.setAccessible(true);
Object collection = loaders.get(sunMiscURLClassPath);
for (Object sunMiscURLClassPathJarLoader : ((Collection<?>) collection).toArray())
{
try
{
Field loader = sunMiscURLClassPathJarLoader.getClass().getDeclaredField("jar");
loader.setAccessible(true);
Object jarFile = loader.get(sunMiscURLClassPathJarLoader);
((JarFile) jarFile).close();
}
catch (Throwable t)
{
// if we got this far, this is probably not a JAR loader so
// skip it
}
}
}
catch (Throwable t)
{
// probably not a SUN VM
}
return;
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String args[])
{
try
{
System.out.println("First attempt...");
Class.forName("com.varra.temp.Class1");
}
catch (Exception ex)
{
System.out.println("Failed.");
}
try
{
JarFileLoader clazzLoader = new JarFileLoader();
clazzLoader.addFile(new File("/krishna/RapidHealthAgent/FileWatcher/bin/filewatcher-1.0.jar"));
clazzLoader.addFile(new File("/krishna/RapidHealthAgent/FileTailer/bin/filetailer-1.0.jar"));
Package[] packages = clazzLoader.getPackages();
for (Package package1 : packages)
{
if (package1.getName().startsWith("com"))
{
System.out.println("B4 Paks: "+package1.getName());
}
}
System.out.println("Second attempt...");
Class<?> fileWatcherClass = clazzLoader.loadClass("com.mt.filewatcher.FileWatcher");
final Method method = fileWatcherClass.getMethod("getFileWatcher", null);
final Object fileWatcher = method.invoke(null, null);
final GlobalThread globalThread = GlobalThread.getGlobalThread(1);
globalThread.start();
globalThread.onTimerTask((EnhancedTimerTask) fileWatcher);
Class<?> testTailer = clazzLoader.loadClass("com.mt.filetailer.TestTailer");
testTailer.newInstance();
System.out.println("loadClass: " + testTailer);
packages = clazzLoader.getPackages();
for (Package package1 : packages)
{
if (package1.getName().startsWith("com"))
{
System.out.println("B4 Paks: "+package1.getName());
}
}
System.out.println("Success!");
}
catch (Exception ex)
{
System.out.println("Failed.");
ex.printStackTrace();
}
}
}
* ClassLoader - JarFileLoader.java, Oct 3, 2011 1:06:13 PM
*
* Copyright 2011 Varra Ltd, Inc. All rights reserved.
* Varra proprietary/confidential. Use is subject to license terms.
*/
package com.varra.net;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collection;
import java.util.jar.JarFile;
import com.varra.util.EnhancedTimerTask;
import com.varra.util.GlobalThread;
/**
* The Class JarFileLoader.
*
* @author Rajakrishna V. Reddy
* @version 1.0
*/
public class JarFileLoader extends URLClassLoader
{
/**
* Instantiates a new jar file loader.
*
* @param urls
* the urls
*/
public JarFileLoader()
{
super(new URL[] {});
}
/**
* Adds the file.
*
* @param file
* the file
* @throws MalformedURLException
* the malformed url exception
*/
public void addFile(File file) throws MalformedURLException
{
addURL(file.toURI().toURL());
}
/**
* Closes all open jar files.
*/
public void close()
{
try
{
Class<?> clazz = java.net.URLClassLoader.class;
Field ucp = clazz.getDeclaredField("ucp");
ucp.setAccessible(true);
Object sunMiscURLClassPath = ucp.get(this);
Field loaders = sunMiscURLClassPath.getClass().getDeclaredField("loaders");
loaders.setAccessible(true);
Object collection = loaders.get(sunMiscURLClassPath);
for (Object sunMiscURLClassPathJarLoader : ((Collection<?>) collection).toArray())
{
try
{
Field loader = sunMiscURLClassPathJarLoader.getClass().getDeclaredField("jar");
loader.setAccessible(true);
Object jarFile = loader.get(sunMiscURLClassPathJarLoader);
((JarFile) jarFile).close();
}
catch (Throwable t)
{
// if we got this far, this is probably not a JAR loader so
// skip it
}
}
}
catch (Throwable t)
{
// probably not a SUN VM
}
return;
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String args[])
{
try
{
System.out.println("First attempt...");
Class.forName("com.varra.temp.Class1");
}
catch (Exception ex)
{
System.out.println("Failed.");
}
try
{
JarFileLoader clazzLoader = new JarFileLoader();
clazzLoader.addFile(new File("/krishna/RapidHealthAgent/FileWatcher/bin/filewatcher-1.0.jar"));
clazzLoader.addFile(new File("/krishna/RapidHealthAgent/FileTailer/bin/filetailer-1.0.jar"));
Package[] packages = clazzLoader.getPackages();
for (Package package1 : packages)
{
if (package1.getName().startsWith("com"))
{
System.out.println("B4 Paks: "+package1.getName());
}
}
System.out.println("Second attempt...");
Class<?> fileWatcherClass = clazzLoader.loadClass("com.mt.filewatcher.FileWatcher");
final Method method = fileWatcherClass.getMethod("getFileWatcher", null);
final Object fileWatcher = method.invoke(null, null);
final GlobalThread globalThread = GlobalThread.getGlobalThread(1);
globalThread.start();
globalThread.onTimerTask((EnhancedTimerTask) fileWatcher);
Class<?> testTailer = clazzLoader.loadClass("com.mt.filetailer.TestTailer");
testTailer.newInstance();
System.out.println("loadClass: " + testTailer);
packages = clazzLoader.getPackages();
for (Package package1 : packages)
{
if (package1.getName().startsWith("com"))
{
System.out.println("B4 Paks: "+package1.getName());
}
}
System.out.println("Success!");
}
catch (Exception ex)
{
System.out.println("Failed.");
ex.printStackTrace();
}
}
}
No comments:
Post a Comment