use of java.net.URLClassLoader in project jetty.project by eclipse.
the class AntWebAppContext method dump.
public static void dump(ClassLoader loader) {
while (loader != null) {
System.err.println(loader);
if (loader instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) loader).getURLs();
if (urls != null) {
for (URL u : urls) System.err.println("\t" + u + "\n");
}
}
loader = loader.getParent();
}
}
use of java.net.URLClassLoader in project jetty.project by eclipse.
the class AntWebInfConfiguration method preConfigure.
@Override
public void preConfigure(final WebAppContext context) throws Exception {
//Make a temp directory for the webapp if one is not already set
resolveTempDirectory(context);
//Extract webapp if necessary
unpack(context);
//Apply an initial ordering to the jars which governs which will be scanned for META-INF
//info and annotations. The ordering is based on inclusion patterns.
String tmp = (String) context.getAttribute(WEBINF_JAR_PATTERN);
Pattern webInfPattern = (tmp == null ? null : Pattern.compile(tmp));
tmp = (String) context.getAttribute(CONTAINER_JAR_PATTERN);
Pattern containerPattern = (tmp == null ? null : Pattern.compile(tmp));
//Apply ordering to container jars - if no pattern is specified, we won't
//match any of the container jars
PatternMatcher containerJarNameMatcher = new PatternMatcher() {
public void matched(URI uri) throws Exception {
context.getMetaData().addContainerResource(Resource.newResource(uri));
}
};
ClassLoader loader = context.getClassLoader();
if (loader != null) {
loader = loader.getParent();
if (loader != null) {
URI[] containerUris = null;
if (loader instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) loader).getURLs();
if (urls != null) {
containerUris = new URI[urls.length];
int i = 0;
for (URL u : urls) {
try {
containerUris[i] = u.toURI();
} catch (URISyntaxException e) {
containerUris[i] = new URI(u.toString().replaceAll(" ", "%20"));
}
i++;
}
}
} else if (loader instanceof AntClassLoader) {
AntClassLoader antLoader = (AntClassLoader) loader;
String[] paths = antLoader.getClasspath().split(new String(new char[] { File.pathSeparatorChar }));
if (paths != null) {
containerUris = new URI[paths.length];
int i = 0;
for (String p : paths) {
File f = new File(p);
containerUris[i] = f.toURI();
i++;
}
}
}
containerJarNameMatcher.match(containerPattern, containerUris, false);
}
}
//Apply ordering to WEB-INF/lib jars
PatternMatcher webInfJarNameMatcher = new PatternMatcher() {
@Override
public void matched(URI uri) throws Exception {
context.getMetaData().addWebInfJar(Resource.newResource(uri));
}
};
List<Resource> jars = findJars(context);
//Convert to uris for matching
URI[] uris = null;
if (jars != null) {
uris = new URI[jars.size()];
int i = 0;
for (Resource r : jars) {
uris[i++] = r.getURI();
}
}
//null is inclusive, no pattern == all jars match
webInfJarNameMatcher.match(webInfPattern, uris, true);
//No pattern to appy to classes, just add to metadata
context.getMetaData().setWebInfClassesDirs(findClassDirs(context));
}
use of java.net.URLClassLoader in project jetty.project by eclipse.
the class JarScanner method scan.
/**
* Find jar names from the classloader matching a pattern.
*
* If the pattern is null and isNullInclusive is true, then
* all jar names in the classloader will match.
*
* A pattern is a set of acceptable jar names. Each acceptable
* jar name is a regex. Each regex can be separated by either a
* "," or a "|". If you use a "|" this or's together the jar
* name patterns. This means that ordering of the matches is
* unimportant to you. If instead, you want to match particular
* jar names, and you want to match them in order, you should
* separate the regexs with "," instead.
*
* Eg "aaa-.*\\.jar|bbb-.*\\.jar"
* Will iterate over the jar names in the classloader and match
* in any order.
*
* Eg "aaa-*\\.jar,bbb-.*\\.jar"
* Will iterate over the jar names in the classloader, matching
* all those starting with "aaa-" first, then "bbb-".
*
* If visitParent is true, then the pattern is applied to the
* parent loader hierarchy. If false, it is only applied to the
* classloader passed in.
*
* @param pattern the pattern to use for jar matching
* @param loader the class loader to look for jars in
* @param isNullInclusive if true, an empty pattern means all names match, if false, none match
* @param visitParent if true, visit parent classloaders too
* @throws Exception if unable to scan
*/
public void scan(Pattern pattern, ClassLoader loader, boolean isNullInclusive, boolean visitParent) throws Exception {
while (loader != null) {
if (loader instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) loader).getURLs();
if (urls != null) {
URI[] uris = new URI[urls.length];
int i = 0;
for (URL u : urls) uris[i++] = u.toURI();
scan(pattern, uris, isNullInclusive);
}
}
if (visitParent)
loader = loader.getParent();
else
loader = null;
}
}
use of java.net.URLClassLoader in project jetty.project by eclipse.
the class AbstractProxySerializationTest method testProxySerialization.
@Test
public void testProxySerialization() throws Exception {
String contextPath = "";
String servletMapping = "/server";
int scavengePeriod = 10;
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = createSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(scavengePeriod);
TestServer server = new TestServer(0, 20, scavengePeriod, cacheFactory, storeFactory);
ServletContextHandler context = server.addContext(contextPath);
InputStream is = this.getClass().getClassLoader().getResourceAsStream("proxy-serialization.jar");
File testDir = MavenTestingUtils.getTargetTestingDir("proxy-serialization");
testDir.mkdirs();
File extractedJar = new File(testDir, "proxy-serialization.jar");
extractedJar.createNewFile();
IO.copy(is, new FileOutputStream(extractedJar));
URLClassLoader loader = new URLClassLoader(new URL[] { extractedJar.toURI().toURL() }, Thread.currentThread().getContextClassLoader());
context.setClassLoader(loader);
context.addServlet("TestServlet", servletMapping);
customizeContext(context);
try {
server.start();
int port = server.getPort();
HttpClient client = new HttpClient();
client.start();
try {
ContentResponse response = client.GET("http://localhost:" + port + contextPath + servletMapping + "?action=create");
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
//stop the context to be sure the sesssion will be passivated
context.stop();
//restart the context
context.start();
// Make another request using the session id from before
Request request = client.newRequest("http://localhost:" + port + contextPath + servletMapping + "?action=test");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
use of java.net.URLClassLoader in project jetty.project by eclipse.
the class ReloadedSessionMissingClassTest method testSessionReloadWithMissingClass.
@Test
public void testSessionReloadWithMissingClass() throws Exception {
Resource.setDefaultUseCaches(false);
String contextPath = "/foo";
File unpackedWarDir = testdir.getEmptyPathDir().toFile();
File webInfDir = new File(unpackedWarDir, "WEB-INF");
webInfDir.mkdir();
File webXml = new File(webInfDir, "web.xml");
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<web-app xmlns=\"http://java.sun.com/xml/ns/j2ee\"\n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xsi:schemaLocation=\"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd\"\n" + " version=\"2.4\">\n" + "\n" + "<session-config>\n" + " <session-timeout>1</session-timeout>\n" + "</session-config>\n" + "</web-app>";
FileWriter w = new FileWriter(webXml);
w.write(xml);
w.close();
File foobarJar = MavenTestingUtils.getTestResourceFile("foobar.jar");
File foobarNOfooJar = MavenTestingUtils.getTestResourceFile("foobarNOfoo.jar");
URL[] foobarUrls = new URL[] { foobarJar.toURI().toURL() };
URL[] barUrls = new URL[] { foobarNOfooJar.toURI().toURL() };
URLClassLoader loaderWithFoo = new URLClassLoader(foobarUrls, Thread.currentThread().getContextClassLoader());
URLClassLoader loaderWithoutFoo = new URLClassLoader(barUrls, Thread.currentThread().getContextClassLoader());
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
SessionDataStoreFactory storeFactory = JdbcTestHelper.newSessionDataStoreFactory();
((AbstractSessionDataStoreFactory) storeFactory).setGracePeriodSec(TestServer.DEFAULT_SCAVENGE_SEC);
TestServer server1 = new TestServer(0, TestServer.DEFAULT_MAX_INACTIVE, TestServer.DEFAULT_SCAVENGE_SEC, cacheFactory, storeFactory);
WebAppContext webApp = server1.addWebAppContext(unpackedWarDir.getCanonicalPath(), contextPath);
webApp.getSessionHandler().getSessionCache().setRemoveUnloadableSessions(true);
webApp.setClassLoader(loaderWithFoo);
webApp.addServlet("Bar", "/bar");
server1.start();
int port1 = server1.getPort();
try (StacklessLogging stackless = new StacklessLogging(Log.getLogger("org.eclipse.jetty.server.session"))) {
HttpClient client = new HttpClient();
client.start();
try {
// Perform one request to server1 to create a session
ContentResponse response = client.GET("http://localhost:" + port1 + contextPath + "/bar?action=set");
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
String sessionId = (String) webApp.getServletContext().getAttribute("foo");
assertNotNull(sessionId);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
//Stop the webapp
webApp.stop();
webApp.setClassLoader(loaderWithoutFoo);
//restart webapp
webApp.start();
Request request = client.newRequest("http://localhost:" + port1 + contextPath + "/bar?action=get");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
String afterStopSessionId = (String) webApp.getServletContext().getAttribute("foo.session");
Boolean fooPresent = (Boolean) webApp.getServletContext().getAttribute("foo.present");
assertFalse(fooPresent);
assertNotNull(afterStopSessionId);
assertTrue(!afterStopSessionId.equals(sessionId));
} finally {
client.stop();
}
} finally {
server1.stop();
}
}
Aggregations