package net.sourceforge.javautil.web.server.application.impl;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import net.sourceforge.javautil.classloader.impl.ClassContext;
import net.sourceforge.javautil.classloader.impl.StandardClassLoaderHeiarchy;
import net.sourceforge.javautil.classloader.resolver.impl.ClassDependencyPoolImpl;
import net.sourceforge.javautil.classloader.resolver.impl.maven.MavenRepositoryUtil;
import net.sourceforge.javautil.classloader.source.ClassSource;
import net.sourceforge.javautil.classloader.source.LibDirectoryClassSource;
import net.sourceforge.javautil.classloader.source.VirtualDirectoryClassSource;
import net.sourceforge.javautil.classloader.util.ClassPackageUtil;
import net.sourceforge.javautil.common.ArchiveUtil;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.io.IVirtualDirectory;
import net.sourceforge.javautil.common.io.IVirtualFile;
import net.sourceforge.javautil.common.io.impl.Directory;
import net.sourceforge.javautil.common.io.impl.DirectoryRoot;
import net.sourceforge.javautil.common.io.impl.SimplePath;
import net.sourceforge.javautil.common.io.impl.SystemDirectory;
import net.sourceforge.javautil.common.io.impl.SystemFile;
import net.sourceforge.javautil.web.server.application.IWebApplication;
import net.sourceforge.javautil.web.server.application.WebApplicationClassLoaderHeiarchy;
import net.sourceforge.javautil.web.server.application.WebApplicationDeploymentContext;
/**
* A class loader that will setup standard sources for {@link WebApplicationStandard}'s.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: WebApplicationClassLoader.java 2586 2010-11-15 04:14:32Z ponderator $
*/
public class WebApplicationClassLoader extends ClassContext {
protected final WebApplicationDeploymentContext ctx;
public WebApplicationClassLoader(WebApplicationDeploymentContext ctx) {
super(new WebApplicationClassLoaderHeiarchy(ctx.getApplication()));
this.ctx = ctx;
this.initialize();
}
@Override public URL getResource(String name) {
URL resource = super.getResource(name);
if (resource == null) {
try {
return ctx.getServletContext().getResource(name);
} catch (MalformedURLException e) {
throw ThrowableManagerRegistry.caught(e);
}
}
return resource;
}
/**
* This will initialize the class loader.
*/
protected void initialize () {
IVirtualDirectory webInf = this.ctx.getApplication().getRoot().getDirectory("WEB-INF");
if (webInf != null) {
IVirtualDirectory classes = webInf.getDirectory("classes");
if (classes != null) this.nonPackageResources.add( new VirtualDirectoryClassSource(classes) );
IVirtualDirectory lib = webInf.getDirectory("lib");
if (lib != null) this.nonPackageResources.add(new LibDirectoryClassSource(lib, false));
}
}
}