use of java.net.JarURLConnection in project jetty.project by eclipse.
the class MetaInfConfiguration method getTlds.
/**
* Find all .tld files in the given jar.
*
* @param uri the uri to jar file
* @return the collection of tlds as url references
* @throws IOException if unable to scan the jar file
*/
public Collection<URL> getTlds(URI uri) throws IOException {
HashSet<URL> tlds = new HashSet<URL>();
String jarUri = uriJarPrefix(uri, "!/");
URL url = new URL(jarUri);
JarURLConnection jarConn = (JarURLConnection) url.openConnection();
jarConn.setUseCaches(Resource.getDefaultUseCaches());
JarFile jarFile = jarConn.getJarFile();
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry e = entries.nextElement();
String name = e.getName();
if (name.startsWith("META-INF") && name.endsWith(".tld")) {
tlds.add(new URL(jarUri + name));
}
}
if (!Resource.getDefaultUseCaches())
jarFile.close();
return tlds;
}
use of java.net.JarURLConnection in project jetty.project by eclipse.
the class JarFileResource method exists.
/* ------------------------------------------------------------ */
/**
* Returns true if the represented resource exists.
*/
@Override
public boolean exists() {
if (_exists)
return true;
if (_urlString.endsWith("!/")) {
String file_url = _urlString.substring(4, _urlString.length() - 2);
try {
return newResource(file_url).exists();
} catch (Exception e) {
LOG.ignore(e);
return false;
}
}
boolean check = checkConnection();
// Is this a root URL?
if (_jarUrl != null && _path == null) {
// Then if it exists it is a directory
_directory = check;
return true;
} else {
// Can we find a file for it?
boolean close_jar_file = false;
JarFile jar_file = null;
if (check)
// Yes
jar_file = _jarFile;
else {
// No - so lets look if the root entry exists.
try {
JarURLConnection c = (JarURLConnection) ((new URL(_jarUrl)).openConnection());
c.setUseCaches(getUseCaches());
jar_file = c.getJarFile();
close_jar_file = !getUseCaches();
} catch (Exception e) {
LOG.ignore(e);
}
}
// Do we need to look more closely?
if (jar_file != null && _entry == null && !_directory) {
// OK - we have a JarFile, lets look for the entry
JarEntry entry = jar_file.getJarEntry(_path);
if (entry == null) {
// the entry does not exist
_exists = false;
} else if (entry.isDirectory()) {
_directory = true;
_entry = entry;
} else {
// Let's confirm is a file
JarEntry directory = jar_file.getJarEntry(_path + '/');
if (directory != null) {
_directory = true;
_entry = directory;
} else {
// OK is a file
_directory = false;
_entry = entry;
}
}
}
if (close_jar_file && jar_file != null) {
try {
jar_file.close();
} catch (IOException ioe) {
LOG.ignore(ioe);
}
}
}
_exists = (_directory || _entry != null);
return _exists;
}
use of java.net.JarURLConnection in project mongo-java-driver by mongodb.
the class ClientMetadataHelper method getDriverVersion.
private static String getDriverVersion() {
String driverVersion = "unknown";
try {
CodeSource codeSource = InternalStreamConnectionInitializer.class.getProtectionDomain().getCodeSource();
if (codeSource != null && codeSource.getLocation() != null) {
String path = codeSource.getLocation().getPath();
URL jarUrl = path.endsWith(".jar") ? new URL("jar:file:" + path + "!/") : null;
if (jarUrl != null) {
JarURLConnection jarURLConnection = (JarURLConnection) jarUrl.openConnection();
Manifest manifest = jarURLConnection.getManifest();
String version = (String) manifest.getMainAttributes().get(new Attributes.Name("Build-Version"));
if (version != null) {
driverVersion = version;
}
}
}
} catch (SecurityException e) {
// do nothing
} catch (IOException e) {
// do nothing
}
return driverVersion;
}
use of java.net.JarURLConnection in project languagetool by languagetool-org.
the class JLanguageTool method getBuildDate.
/**
* Returns the build date or {@code null} if not run from JAR.
*/
@Nullable
private static String getBuildDate() {
try {
URL res = JLanguageTool.class.getResource(JLanguageTool.class.getSimpleName() + ".class");
if (res == null) {
// this will happen on Android, see http://stackoverflow.com/questions/15371274/
return null;
}
Object connObj = res.openConnection();
if (connObj instanceof JarURLConnection) {
JarURLConnection conn = (JarURLConnection) connObj;
Manifest manifest = conn.getManifest();
return manifest.getMainAttributes().getValue("Implementation-Date");
} else {
return null;
}
} catch (IOException e) {
throw new RuntimeException("Could not get build date from JAR", e);
}
}
use of java.net.JarURLConnection in project hive by apache.
the class ClassNameCompleter method getClassNames.
public static String[] getClassNames() throws IOException {
Set urls = new HashSet();
for (ClassLoader loader = Thread.currentThread().getContextClassLoader(); loader != null; loader = loader.getParent()) {
if (!(loader instanceof URLClassLoader)) {
continue;
}
urls.addAll(Arrays.asList(((URLClassLoader) loader).getURLs()));
}
// Now add the URL that holds java.lang.String. This is because
// some JVMs do not report the core classes jar in the list of
// class loaders.
Class[] systemClasses = new Class[] { String.class, javax.swing.JFrame.class };
for (int i = 0; i < systemClasses.length; i++) {
URL classURL = systemClasses[i].getResource("/" + systemClasses[i].getName().replace('.', '/') + clazzFileNameExtension);
if (classURL != null) {
URLConnection uc = classURL.openConnection();
if (uc instanceof JarURLConnection) {
urls.add(((JarURLConnection) uc).getJarFileURL());
}
}
}
Set classes = new HashSet();
for (Iterator i = urls.iterator(); i.hasNext(); ) {
URL url = (URL) i.next();
try {
File file = new File(url.getFile());
if (file.isDirectory()) {
Set files = getClassFiles(file.getAbsolutePath(), new HashSet(), file, new int[] { 200 });
classes.addAll(files);
continue;
}
if (!isJarFile(file)) {
continue;
}
JarFile jf = new JarFile(file);
for (Enumeration e = jf.entries(); e.hasMoreElements(); ) {
JarEntry entry = (JarEntry) e.nextElement();
if (entry == null) {
continue;
}
String name = entry.getName();
if (isClazzFile(name)) {
/* only use class file */
classes.add(name);
} else if (isJarFile(name)) {
classes.addAll(getClassNamesFromJar(name));
} else {
continue;
}
}
} catch (IOException e) {
throw new IOException(String.format("Error reading classpath entry: %s", url), e);
}
}
// now filter classes by changing "/" to "." and trimming the
// trailing ".class"
Set classNames = new TreeSet();
for (Iterator i = classes.iterator(); i.hasNext(); ) {
String name = (String) i.next();
classNames.add(name.replace('/', '.').substring(0, name.length() - 6));
}
return (String[]) classNames.toArray(new String[classNames.size()]);
}
Aggregations