use of org.apache.tomcat.Jar in project tomcat by apache.
the class TestTldScanner method scan.
private static void scan(TldScanner.TldScannerCallback callback, File webapp, String path) throws Exception {
String fullPath = new File(webapp, path).toURI().toString();
URL jarUrl = new URL("jar:" + fullPath + "!/");
try (Jar jar = JarFactory.newInstance(jarUrl)) {
callback.scan(jar, path, true);
}
}
use of org.apache.tomcat.Jar in project tomcat by apache.
the class ContextConfig method processResourceJARs.
/**
* Scan JARs that contain web-fragment.xml files that will be used to
* configure this application to see if they also contain static resources.
* If static resources are found, add them to the context. Resources are
* added in web-fragment.xml priority order.
* @param fragments The set of fragments that will be scanned for
* static resources
*/
protected void processResourceJARs(Set<WebXml> fragments) {
for (WebXml fragment : fragments) {
URL url = fragment.getURL();
try {
if ("jar".equals(url.getProtocol()) || url.toString().endsWith(".jar")) {
try (Jar jar = JarFactory.newInstance(url)) {
jar.nextEntry();
String entryName = jar.getEntryName();
while (entryName != null) {
if (entryName.startsWith("META-INF/resources/")) {
context.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", url, "/META-INF/resources");
break;
}
jar.nextEntry();
entryName = jar.getEntryName();
}
}
} else if ("file".equals(url.getProtocol())) {
File file = new File(url.toURI());
File resources = new File(file, "META-INF/resources/");
if (resources.isDirectory()) {
context.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", resources.getAbsolutePath(), null, "/");
}
}
} catch (IOException ioe) {
log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
} catch (URISyntaxException e) {
log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
}
}
}
use of org.apache.tomcat.Jar in project tomcat by apache.
the class TldCache method getLastModified.
private long[] getLastModified(TldResourcePath tldResourcePath) {
long[] result = new long[2];
result[0] = -1;
result[1] = -1;
try {
String webappPath = tldResourcePath.getWebappPath();
if (webappPath != null) {
// webappPath will be null for JARs containing TLDs that are on
// the class path but not part of the web application
URL url = servletContext.getResource(tldResourcePath.getWebappPath());
URLConnection conn = url.openConnection();
result[0] = conn.getLastModified();
if ("file".equals(url.getProtocol())) {
// Reading the last modified time opens an input stream so we
// need to make sure it is closed again otherwise the TLD file
// will be locked until GC runs.
conn.getInputStream().close();
}
}
try (Jar jar = tldResourcePath.openJar()) {
if (jar != null) {
result[1] = jar.getLastModified(tldResourcePath.getEntryName());
}
}
} catch (IOException e) {
// Ignore (shouldn't happen)
}
return result;
}
use of org.apache.tomcat.Jar in project tomcat by apache.
the class TesterPerformance method testClassParserPerformance.
@Test
public void testClassParserPerformance() throws IOException {
File libDir = new File(JAR_LOCATION);
String[] libs = libDir.list();
Assert.assertNotNull(libs);
Set<URL> jarURLs = new HashSet<>();
for (String lib : libs) {
if (!lib.toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
continue;
}
jarURLs.add(new URL("jar:" + new File(libDir, lib).toURI().toURL().toExternalForm() + "!/"));
}
long duration = 0;
for (URL jarURL : jarURLs) {
try (Jar jar = JarFactory.newInstance(jarURL)) {
jar.nextEntry();
String jarEntryName = jar.getEntryName();
while (jarEntryName != null) {
if (jarEntryName.endsWith(".class")) {
InputStream is = jar.getEntryInputStream();
long start = System.nanoTime();
ClassParser cp = new ClassParser(is);
cp.parse();
duration += System.nanoTime() - start;
}
jar.nextEntry();
jarEntryName = jar.getEntryName();
}
}
}
System.out.println("ClassParser performance test took: " + duration + " ns");
}
Aggregations