use of java.net.URLClassLoader in project jetty.project by eclipse.
the class JspcMojo method compile.
public void compile() throws Exception {
ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
//set up the classpath of the webapp
List<URL> webAppUrls = setUpWebAppClassPath();
//set up the classpath of the container (ie jetty and jsp jars)
Set<URL> pluginJars = getPluginJars();
Set<URL> providedJars = getProvidedScopeJars(pluginJars);
//Make a classloader so provided jars will be on the classpath
List<URL> sysUrls = new ArrayList<URL>();
sysUrls.addAll(providedJars);
URLClassLoader sysClassLoader = new URLClassLoader((URL[]) sysUrls.toArray(new URL[0]), currentClassLoader);
//make a classloader with the webapp classpath
URLClassLoader webAppClassLoader = new URLClassLoader((URL[]) webAppUrls.toArray(new URL[0]), sysClassLoader);
StringBuffer webAppClassPath = new StringBuffer();
for (int i = 0; i < webAppUrls.size(); i++) {
if (getLog().isDebugEnabled())
getLog().debug("webappclassloader contains: " + webAppUrls.get(i));
webAppClassPath.append(new File(webAppUrls.get(i).toURI()).getCanonicalPath());
if (getLog().isDebugEnabled())
getLog().debug("added to classpath: " + ((URL) webAppUrls.get(i)).getFile());
if (i + 1 < webAppUrls.size())
webAppClassPath.append(System.getProperty("path.separator"));
}
//Interpose a fake classloader as the webapp class loader. This is because the Apache JspC class
//uses a TldScanner which ignores jars outside of the WEB-INF/lib path on the webapp classloader.
//It will, however, look at all jars on the parents of the webapp classloader.
URLClassLoader fakeWebAppClassLoader = new URLClassLoader(new URL[0], webAppClassLoader);
Thread.currentThread().setContextClassLoader(fakeWebAppClassLoader);
if (jspc == null)
jspc = new JettyJspC();
jspc.setWebXmlFragment(webXmlFragment);
jspc.setUriroot(webAppSourceDirectory);
jspc.setOutputDir(generatedClasses);
jspc.setClassLoader(fakeWebAppClassLoader);
jspc.setScanAllDirectories(scanAllDirectories);
jspc.setCompile(true);
if (sourceVersion != null)
jspc.setCompilerSourceVM(sourceVersion);
if (targetVersion != null)
jspc.setCompilerTargetVM(targetVersion);
// JspC#setExtensions() does not exist, so
// always set concrete list of files that will be processed.
String jspFiles = getJspFiles(webAppSourceDirectory);
try {
if (jspFiles == null | jspFiles.equals("")) {
getLog().info("No files selected to precompile");
} else {
getLog().info("Compiling " + jspFiles + " from includes=" + includes + " excludes=" + excludes);
jspc.setJspFiles(jspFiles);
jspc.execute();
}
} finally {
Thread.currentThread().setContextClassLoader(currentClassLoader);
}
}
use of java.net.URLClassLoader in project jetty.project by eclipse.
the class Classpath method getClassLoader.
public ClassLoader getClassLoader() {
int cnt = elements.size();
URL[] urls = new URL[cnt];
for (int i = 0; i < cnt; i++) {
try {
urls[i] = elements.get(i).toURI().toURL();
StartLog.debug("URLClassLoader.url[%d] = %s", i, urls[i]);
} catch (MalformedURLException e) {
StartLog.warn(e);
}
}
StartLog.debug("Loaded %d URLs into URLClassLoader", urls.length);
ClassLoader parent = Thread.currentThread().getContextClassLoader();
if (parent == null) {
parent = Classpath.class.getClassLoader();
}
if (parent == null) {
parent = ClassLoader.getSystemClassLoader();
}
return new Loader(urls, parent);
}
use of java.net.URLClassLoader in project jetty.project by eclipse.
the class Slf4jHelper method createTestClassLoader.
public static ClassLoader createTestClassLoader(ClassLoader parentClassLoader) throws MalformedURLException {
File testJarDir = MavenTestingUtils.getTargetFile("test-jars");
// trigger @Ignore if dir not there
Assume.assumeTrue(testJarDir.exists());
File[] jarfiles = testJarDir.listFiles(new FileFilter() {
public boolean accept(File path) {
if (!path.isFile()) {
return false;
}
return path.getName().endsWith(".jar");
}
});
// trigger @Ignore if no jar files.
Assume.assumeTrue(jarfiles.length > 0);
URL[] urls = new URL[jarfiles.length];
for (int i = 0; i < jarfiles.length; i++) {
urls[i] = jarfiles[i].toURI().toURL();
// System.out.println("Adding test-jar => " + urls[i]);
}
return new URLClassLoader(urls, parentClassLoader);
}
use of java.net.URLClassLoader in project jetty.project by eclipse.
the class TestJNDI method testThreadContextClassloaderAndCurrentContext.
@Test
public void testThreadContextClassloaderAndCurrentContext() throws Exception {
//create a jetty context, and start it so that its classloader it created
//and it is the current context
ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
ContextHandler ch = new ContextHandler();
URLClassLoader chLoader = new URLClassLoader(new URL[0], currentLoader);
ch.setClassLoader(chLoader);
Server server = new Server();
HandlerList hl = new HandlerList();
server.setHandler(hl);
hl.addHandler(ch);
//Create another one
ContextHandler ch2 = new ContextHandler();
URLClassLoader ch2Loader = new URLClassLoader(new URL[0], currentLoader);
ch2.setClassLoader(ch2Loader);
hl.addHandler(ch2);
try {
ch.setContextPath("/ch");
ch.addEventListener(new ServletContextListener() {
private Context comp;
private Object testObj = new Object();
public void contextInitialized(ServletContextEvent sce) {
try {
InitialContext initCtx = new InitialContext();
Context java = (Context) initCtx.lookup("java:");
assertNotNull(java);
comp = (Context) initCtx.lookup("java:comp");
assertNotNull(comp);
Context env = ((Context) comp).createSubcontext("env");
assertNotNull(env);
env.bind("ch", testObj);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public void contextDestroyed(ServletContextEvent sce) {
try {
assertNotNull(comp);
assertEquals(testObj, comp.lookup("env/ch"));
comp.destroySubcontext("env");
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
});
//Starting the context makes it current and creates a classloader for it
ch.start();
ch2.setContextPath("/ch2");
ch2.addEventListener(new ServletContextListener() {
private Context comp;
private Object testObj = new Object();
public void contextInitialized(ServletContextEvent sce) {
try {
InitialContext initCtx = new InitialContext();
comp = (Context) initCtx.lookup("java:comp");
assertNotNull(comp);
//another context's bindings should not be visible
Context env = ((Context) comp).createSubcontext("env");
try {
env.lookup("ch");
fail("java:comp/env visible from another context!");
} catch (NameNotFoundException e) {
//expected
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public void contextDestroyed(ServletContextEvent sce) {
try {
assertNotNull(comp);
comp.destroySubcontext("env");
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
});
//make the new context the current one
ch2.start();
} finally {
ch.stop();
ch2.stop();
Thread.currentThread().setContextClassLoader(currentLoader);
}
}
use of java.net.URLClassLoader in project jetty.project by eclipse.
the class TestJNDI method testIt.
@Test
public void testIt() throws Exception {
//set up some classloaders
Thread currentThread = Thread.currentThread();
ClassLoader currentLoader = currentThread.getContextClassLoader();
ClassLoader childLoader1 = new URLClassLoader(new URL[0], currentLoader);
ClassLoader childLoader2 = new URLClassLoader(new URL[0], currentLoader);
try {
//Uncomment to aid with debug
/*
javaRootURLContext.getRoot().addListener(new NamingContext.Listener()
{
public void unbind(NamingContext ctx, Binding binding)
{
System.err.println("java unbind "+binding+" from "+ctx.getName());
}
public Binding bind(NamingContext ctx, Binding binding)
{
System.err.println("java bind "+binding+" to "+ctx.getName());
return binding;
}
});
localContextRoot.getRoot().addListener(new NamingContext.Listener()
{
public void unbind(NamingContext ctx, Binding binding)
{
System.err.println("local unbind "+binding+" from "+ctx.getName());
}
public Binding bind(NamingContext ctx, Binding binding)
{
System.err.println("local bind "+binding+" to "+ctx.getName());
return binding;
}
});
*/
//Set up the tccl before doing any jndi operations
currentThread.setContextClassLoader(childLoader1);
InitialContext initCtx = new InitialContext();
//Test we can lookup the root java: naming tree
Context sub0 = (Context) initCtx.lookup("java:");
assertNotNull(sub0);
//already be bound
try {
Context sub1 = sub0.createSubcontext("comp");
fail("Comp should already be bound");
} catch (NameAlreadyBoundException e) {
//expected exception
}
//check bindings at comp
Context sub1 = (Context) initCtx.lookup("java:comp");
assertNotNull(sub1);
Context sub2 = sub1.createSubcontext("env");
assertNotNull(sub2);
initCtx.bind("java:comp/env/rubbish", "abc");
assertEquals("abc", initCtx.lookup("java:comp/env/rubbish"));
//check binding LinkRefs
LinkRef link = new LinkRef("java:comp/env/rubbish");
initCtx.bind("java:comp/env/poubelle", link);
assertEquals("abc", initCtx.lookup("java:comp/env/poubelle"));
//check binding References
StringRefAddr addr = new StringRefAddr("blah", "myReferenceable");
Reference ref = new Reference(java.lang.String.class.getName(), addr, MyObjectFactory.class.getName(), null);
initCtx.bind("java:comp/env/quatsch", ref);
assertEquals(MyObjectFactory.myString, initCtx.lookup("java:comp/env/quatsch"));
//test binding something at java:
Context sub3 = initCtx.createSubcontext("java:zero");
initCtx.bind("java:zero/one", "ONE");
assertEquals("ONE", initCtx.lookup("java:zero/one"));
//change the current thread's classloader to check distinct naming
currentThread.setContextClassLoader(childLoader2);
Context otherSub1 = (Context) initCtx.lookup("java:comp");
assertTrue(!(sub1 == otherSub1));
try {
initCtx.lookup("java:comp/env/rubbish");
fail("env should not exist for this classloader");
} catch (NameNotFoundException e) {
//expected
}
//put the thread's classloader back
currentThread.setContextClassLoader(childLoader1);
//test rebind with existing binding
initCtx.rebind("java:comp/env/rubbish", "xyz");
assertEquals("xyz", initCtx.lookup("java:comp/env/rubbish"));
//test rebind with no existing binding
initCtx.rebind("java:comp/env/mullheim", "hij");
assertEquals("hij", initCtx.lookup("java:comp/env/mullheim"));
//test that the other bindings are already there
assertEquals("xyz", initCtx.lookup("java:comp/env/poubelle"));
//test java:/comp/env/stuff
assertEquals("xyz", initCtx.lookup("java:/comp/env/poubelle/"));
//test list Names
NamingEnumeration nenum = initCtx.list("java:comp/env");
HashMap results = new HashMap();
while (nenum.hasMore()) {
NameClassPair ncp = (NameClassPair) nenum.next();
results.put(ncp.getName(), ncp.getClassName());
}
assertEquals(4, results.size());
assertEquals("java.lang.String", results.get("rubbish"));
assertEquals("javax.naming.LinkRef", results.get("poubelle"));
assertEquals("java.lang.String", results.get("mullheim"));
assertEquals("javax.naming.Reference", results.get("quatsch"));
//test list Bindings
NamingEnumeration benum = initCtx.list("java:comp/env");
assertEquals(4, results.size());
//test NameInNamespace
assertEquals("comp/env", sub2.getNameInNamespace());
//test close does nothing
Context closeCtx = (Context) initCtx.lookup("java:comp/env");
closeCtx.close();
//test what happens when you close an initial context
InitialContext closeInit = new InitialContext();
closeInit.close();
//check locking the context
Context ectx = (Context) initCtx.lookup("java:comp");
ectx.bind("crud", "xxx");
ectx.addToEnvironment("org.eclipse.jndi.immutable", "TRUE");
assertEquals("xxx", initCtx.lookup("java:comp/crud"));
try {
ectx.bind("crud2", "xxx2");
} catch (NamingException ne) {
//expected failure to modify immutable context
}
initCtx.close();
} finally {
//make some effort to clean up
InitialContext ic = new InitialContext();
Context java = (Context) ic.lookup("java:");
java.destroySubcontext("zero");
java.destroySubcontext("fee");
currentThread.setContextClassLoader(childLoader1);
Context comp = (Context) ic.lookup("java:comp");
comp.destroySubcontext("env");
comp.unbind("crud");
comp.unbind("crud2");
currentThread.setContextClassLoader(currentLoader);
}
}
Aggregations