use of java.util.jar.JarEntry in project hbase by apache.
the class TestClassFinder method packageAndLoadJar.
/**
* Makes a jar out of some class files. Unfortunately it's very tedious.
* @param filesInJar Files created via compileTestClass.
* @return path to the resulting jar file.
*/
private static String packageAndLoadJar(FileAndPath... filesInJar) throws Exception {
// First, write the bogus jar file.
String path = basePath + "jar" + jarCounter.incrementAndGet() + ".jar";
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
FileOutputStream fos = new FileOutputStream(path);
JarOutputStream jarOutputStream = new JarOutputStream(fos, manifest);
// Directory entries for all packages have to be added explicitly for
// resources to be findable via ClassLoader. Directory entries must end
// with "/"; the initial one is expected to, also.
Set<String> pathsInJar = new HashSet<>();
for (FileAndPath fileAndPath : filesInJar) {
String pathToAdd = fileAndPath.path;
while (pathsInJar.add(pathToAdd)) {
int ix = pathToAdd.lastIndexOf('/', pathToAdd.length() - 2);
if (ix < 0) {
break;
}
pathToAdd = pathToAdd.substring(0, ix);
}
}
for (String pathInJar : pathsInJar) {
jarOutputStream.putNextEntry(new JarEntry(pathInJar));
jarOutputStream.closeEntry();
}
for (FileAndPath fileAndPath : filesInJar) {
File file = fileAndPath.file;
jarOutputStream.putNextEntry(new JarEntry(fileAndPath.path + file.getName()));
byte[] allBytes = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(allBytes);
fis.close();
jarOutputStream.write(allBytes);
jarOutputStream.closeEntry();
}
jarOutputStream.close();
fos.close();
// Add the file to classpath.
File jarFile = new File(path);
assertTrue(jarFile.exists());
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[] { jarFile.toURI().toURL() });
return jarFile.getAbsolutePath();
}
use of java.util.jar.JarEntry 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()]);
}
use of java.util.jar.JarEntry in project tomcat by apache.
the class JspCompilationContext method getLastModified.
public Long getLastModified(String resource, Jar tagJar) {
long result = -1;
URLConnection uc = null;
try {
if (tagJar != null) {
if (resource.startsWith("/")) {
resource = resource.substring(1);
}
result = tagJar.getLastModified(resource);
} else {
URL jspUrl = getResource(resource);
if (jspUrl == null) {
incrementRemoved();
return Long.valueOf(result);
}
uc = jspUrl.openConnection();
if (uc instanceof JarURLConnection) {
JarEntry jarEntry = ((JarURLConnection) uc).getJarEntry();
if (jarEntry != null) {
result = jarEntry.getTime();
} else {
result = uc.getLastModified();
}
} else {
result = uc.getLastModified();
}
}
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug(Localizer.getMessage("jsp.error.lastModified", getJspFile()), e);
}
result = -1;
} finally {
if (uc != null) {
try {
uc.getInputStream().close();
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug(Localizer.getMessage("jsp.error.lastModified", getJspFile()), e);
}
result = -1;
}
}
}
return Long.valueOf(result);
}
use of java.util.jar.JarEntry in project tomcat by apache.
the class JarWarResource method getJarInputStreamWrapper.
@Override
protected JarInputStreamWrapper getJarInputStreamWrapper() {
JarFile warFile = null;
JarInputStream jarIs = null;
JarEntry entry = null;
try {
warFile = getArchiveResourceSet().openJarFile();
JarEntry jarFileInWar = warFile.getJarEntry(archivePath);
InputStream isInWar = warFile.getInputStream(jarFileInWar);
jarIs = new JarInputStream(isInWar);
entry = jarIs.getNextJarEntry();
while (entry != null && !entry.getName().equals(getResource().getName())) {
entry = jarIs.getNextJarEntry();
}
if (entry == null) {
return null;
}
return new JarInputStreamWrapper(entry, jarIs);
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("jarResource.getInputStreamFail", getResource().getName(), getBaseUrl()), e);
}
return null;
} finally {
if (entry == null) {
if (jarIs != null) {
try {
jarIs.close();
} catch (IOException ioe) {
// Ignore
}
}
if (warFile != null) {
getArchiveResourceSet().closeJarFile();
}
}
}
}
use of java.util.jar.JarEntry in project storm by apache.
the class Utils method extractDirFromJarImpl.
public void extractDirFromJarImpl(String jarpath, String dir, File destdir) {
try (JarFile jarFile = new JarFile(jarpath)) {
Enumeration<JarEntry> jarEnums = jarFile.entries();
while (jarEnums.hasMoreElements()) {
JarEntry entry = jarEnums.nextElement();
if (!entry.isDirectory() && entry.getName().startsWith(dir)) {
File aFile = new File(destdir, entry.getName());
aFile.getParentFile().mkdirs();
try (FileOutputStream out = new FileOutputStream(aFile);
InputStream in = jarFile.getInputStream(entry)) {
IOUtils.copy(in, out);
}
}
}
} catch (IOException e) {
LOG.info("Could not extract {} from {}", dir, jarpath);
}
}
Aggregations