use of java.net.JarURLConnection in project tomcat70 by apache.
the class StandardJarScanner method process.
/*
* Scan a URL for JARs with the optional extensions to look at all files
* and all directories.
*/
protected void process(JarScannerCallback callback, URL url) throws IOException {
if (log.isTraceEnabled()) {
log.trace(sm.getString("jarScan.jarUrlStart", url));
}
URLConnection conn = url.openConnection();
if (conn instanceof JarURLConnection) {
callback.scan((JarURLConnection) conn);
} else {
String urlStr = url.toString();
if (urlStr.startsWith("file:") || urlStr.startsWith("jndi:") || urlStr.startsWith("http:") || urlStr.startsWith("https:")) {
if (urlStr.endsWith(Constants.JAR_EXT)) {
URL jarURL = UriUtil.buildJarUrl(urlStr);
callback.scan((JarURLConnection) jarURL.openConnection());
} else {
File f;
try {
f = new File(url.toURI());
if (f.isFile() && scanAllFiles) {
// Treat this file as a JAR
URL jarURL = UriUtil.buildJarUrl(f);
callback.scan((JarURLConnection) jarURL.openConnection());
} else if (f.isDirectory() && scanAllDirectories) {
File metainf = new File(f.getAbsoluteFile() + File.separator + "META-INF");
if (metainf.isDirectory()) {
callback.scan(f);
}
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
// Wrap the exception and re-throw
IOException ioe = new IOException();
ioe.initCause(t);
throw ioe;
}
}
}
}
}
use of java.net.JarURLConnection in project tomcat70 by apache.
the class UrlJar method createJarInputStream.
private NonClosingJarInputStream createJarInputStream() throws IOException {
JarURLConnection jarConn = (JarURLConnection) url.openConnection();
URL resourceURL = jarConn.getJarFileURL();
URLConnection resourceConn = resourceURL.openConnection();
resourceConn.setUseCaches(false);
return new NonClosingJarInputStream(resourceConn.getInputStream());
}
use of java.net.JarURLConnection in project tomcat70 by apache.
the class BaseDirContext method addResourcesJar.
// ------------------------------------------------------------- Properties
/**
* Add a resources JAR. The contents of /META-INF/resources/ will be used if
* a requested resource can not be found in the main context.
*/
public void addResourcesJar(URL url) {
try {
JarURLConnection conn = (JarURLConnection) url.openConnection();
JarFile jarFile = conn.getJarFile();
ZipEntry entry = jarFile.getEntry("/");
WARDirContext warDirContext = new WARDirContext(jarFile, new WARDirContext.Entry("/", entry));
warDirContext.loadEntries();
altDirContexts.add(warDirContext);
} catch (IOException ioe) {
log.warn(sm.getString("resources.addResourcesJarFail", url), ioe);
}
}
use of java.net.JarURLConnection in project elki by elki-project.
the class CheckELKIServices method checkServices.
/**
* Retrieve all properties and check them.
*
* @param update Folder to update service files in
*/
public void checkServices(String update) {
TreeSet<String> props = new TreeSet<>();
Enumeration<URL> us;
try {
us = getClass().getClassLoader().getResources(ELKIServiceLoader.RESOURCE_PREFIX);
} catch (IOException e) {
throw new AbortException("Error enumerating service folders.", e);
}
while (us.hasMoreElements()) {
URL u = us.nextElement();
try {
if (("jar".equals(u.getProtocol()))) {
JarURLConnection con = (JarURLConnection) u.openConnection();
try (JarFile jar = con.getJarFile()) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
String prop = entries.nextElement().getName();
if (prop.startsWith(ELKIServiceLoader.RESOURCE_PREFIX)) {
props.add(prop.substring(ELKIServiceLoader.RESOURCE_PREFIX.length()));
} else if (prop.startsWith(ELKIServiceLoader.FILENAME_PREFIX)) {
props.add(prop.substring(ELKIServiceLoader.FILENAME_PREFIX.length()));
}
}
}
continue;
}
if ("file".equals(u.getProtocol())) {
props.addAll(Arrays.asList(new File(u.toURI()).list()));
}
} catch (IOException | URISyntaxException e) {
throw new AbortException("Error enumerating service folders.", e);
}
}
for (String prop : props) {
if (LOG.isVerbose()) {
LOG.verbose("Checking property: " + prop);
}
checkService(prop, update);
}
}
use of java.net.JarURLConnection in project elki by elki-project.
the class CheckParameterizables method checkParameterizables.
/**
* Validate all "Parameterizable" objects for parts of the API contract that
* cannot be specified in Java interfaces (such as constructors, static
* methods)
*/
public void checkParameterizables() {
LoggingConfiguration.setVerbose(Level.VERBOSE);
knownParameterizables = new ArrayList<>();
try {
Enumeration<URL> us = getClass().getClassLoader().getResources(ELKIServiceLoader.RESOURCE_PREFIX);
while (us.hasMoreElements()) {
URL u = us.nextElement();
if ("file".equals(u.getProtocol())) {
for (String prop : new File(u.toURI()).list()) {
try {
knownParameterizables.add(Class.forName(prop));
} catch (ClassNotFoundException e) {
LOG.warning("Service file name is not a class name: " + prop);
continue;
}
}
} else if (("jar".equals(u.getProtocol()))) {
JarURLConnection con = (JarURLConnection) u.openConnection();
try (JarFile jar = con.getJarFile()) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
String prop = entries.nextElement().getName();
if (prop.startsWith(ELKIServiceLoader.RESOURCE_PREFIX)) {
prop = prop.substring(ELKIServiceLoader.RESOURCE_PREFIX.length());
} else if (prop.startsWith(ELKIServiceLoader.FILENAME_PREFIX)) {
prop = prop.substring(ELKIServiceLoader.FILENAME_PREFIX.length());
} else {
continue;
}
try {
knownParameterizables.add(Class.forName(prop));
} catch (ClassNotFoundException e) {
LOG.warning("Service file name is not a class name: " + prop);
continue;
}
}
}
}
}
} catch (IOException | URISyntaxException e) {
throw new AbortException("Error enumerating service folders.", e);
}
final String internal = de.lmu.ifi.dbs.elki.utilities.optionhandling.Parameterizer.class.getPackage().getName();
for (final Class<?> cls : ELKIServiceRegistry.findAllImplementations(Object.class, false, false)) {
// Classes in the same package are special and don't cause warnings.
if (cls.getName().startsWith(internal)) {
continue;
}
try {
State state = State.NO_CONSTRUCTOR;
state = checkV3Parameterization(cls, state);
if (state == State.ERROR) {
continue;
}
state = checkDefaultConstructor(cls, state);
if (state == State.ERROR) {
continue;
}
boolean expectedParameterizer = checkSupertypes(cls);
if (state == State.NO_CONSTRUCTOR && expectedParameterizer) {
LOG.verbose(//
"Class " + cls.getName() + " implements a parameterizable interface, but doesn't have a public and parameterless constructor!");
}
if (state == State.INSTANTIABLE && !expectedParameterizer) {
LOG.verbose(//
"Class " + cls.getName() + " has a parameterizer, but there is no service file for any of its interfaces.");
}
} catch (NoClassDefFoundError e) {
LOG.verbose("Class discovered but not found: " + cls.getName() + " (missing: " + e.getMessage() + ")");
}
}
}
Aggregations