use of java.util.jar.JarFile in project hackpad by dropbox.
the class JarClassLoader method findClass.
protected Class findClass(String name) {
Class c = null;
String jarPath = getJarPath();
if (jarPath != null) {
JarFile jarFile = null;
try {
jarFile = new JarFile(jarPath);
c = loadClassData(jarFile, name);
} catch (IOException ioe) {
/* ignore */
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException ioe) {
/* ignore */
}
}
}
}
return c;
}
use of java.util.jar.JarFile in project hackpad by dropbox.
the class JarClassLoader method getJarPath.
private static String getJarPath() {
if (jarPath != null) {
return jarPath;
}
String classname = JarClassLoader.class.getName().replace('.', '/') + ".class";
String classpath = System.getProperty("java.class.path");
String[] classpaths = classpath.split(System.getProperty("path.separator"));
for (int i = 0; i < classpaths.length; i++) {
String path = classpaths[i];
JarFile jarFile = null;
JarEntry jarEntry = null;
try {
jarFile = new JarFile(path);
jarEntry = findJarEntry(jarFile, classname);
} catch (IOException ioe) {
/* ignore */
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException ioe) {
/* ignore */
}
}
}
if (jarEntry != null) {
jarPath = path;
break;
}
}
return jarPath;
}
use of java.util.jar.JarFile 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.JarFile in project midpoint by Evolveum.
the class ClassPathUtil method getFromJar.
/**
* Get clasess from JAR
*
* @param srcUrl
* @param packageName
* @return
*/
@SuppressWarnings("rawtypes")
private static void getFromJar(URL srcUrl, String packageName, Consumer<Class> consumer) {
// sample:
// file:/C:/.m2/repository/test-util/1.9-SNAPSHOT/test-util-1.9-SNAPSHOT.jar!/test-data/opendj.template
// output:
// file/C:/.m2/repository/test-util/1.9-SNAPSHOT/test-util-1.9-SNAPSHOT.jar
String srcName = srcUrl.getPath().split("!/")[0];
//solution to make it work in Weblogic, because we have to make the path absolute, that means with scheme, that means basically with prefix file:/
//in Tomcat the form is jar:file:/
//in Weblogic the form is only zip:/
LOGGER.trace("srcUrl.getProtocol(): {}", srcUrl.getProtocol());
if ("zip".equals(srcUrl.getProtocol())) {
srcName = "file:" + srcName;
}
LOGGER.trace("srcName: {}", srcName);
// Probably hepls fix error in windows with URI
File jarTmp = null;
try {
jarTmp = new File(new URI(srcName));
} catch (URISyntaxException ex) {
LOGGER.error("Error converting jar " + srcName + " name to URI:", ex);
return;
}
if (!jarTmp.isFile()) {
LOGGER.error("Is {} not a file.", srcName);
}
JarFile jar = null;
try {
jar = new JarFile(jarTmp);
} catch (IOException ex) {
LOGGER.error("Error during open JAR " + srcName, ex);
return;
}
String path = packageName.replace('.', '/');
Enumeration<JarEntry> entries = jar.entries();
LOGGER.trace("PATH:" + path);
JarEntry e;
while (entries.hasMoreElements()) {
e = entries.nextElement();
// get name and replace inner class
String name = e.getName().replace('$', '/');
// skip other files in other packas and skip non class files
if (!name.contains(path) || !name.contains(".class")) {
continue;
}
// Skip all that are not in package
if (name.matches(path + "/.+/.*.class")) {
continue;
}
LOGGER.trace("JAR Candidate: {}", name);
try {
// to create class
// Convert name back to package
Class clazz = Class.forName(name.replace('/', '.').replace(".class", ""));
consumer.accept(clazz);
} catch (ClassNotFoundException ex) {
LOGGER.error("Error during loading class {} from {}. ", name, jar.getName());
}
}
try {
jar.close();
} catch (IOException ex) {
LOGGER.error("Error during close JAR {} " + srcName, ex);
return;
}
}
use of java.util.jar.JarFile in project midpoint by Evolveum.
the class ClassPathUtil method extractFilesFromClassPath.
/**
* Extracts all files in a directory on a classPath (system resource) to
* a directory on a file system.
*/
public static boolean extractFilesFromClassPath(String srcPath, String dstPath, boolean overwrite) throws URISyntaxException, IOException {
URL src = ClassPathUtil.class.getClassLoader().getResource(srcPath);
if (src == null) {
LOGGER.debug("No resource for {}", srcPath);
return false;
}
URI srcUrl = src.toURI();
// URL srcUrl = ClassLoader.getSystemResource(srcPath);
LOGGER.trace("URL: {}", srcUrl);
if (srcUrl.getPath().contains("!/")) {
// e.g. file:/C:/Documents%20and%20Settings/user/.m2/repository/com/evolveum/midpoint/infra/test-util/2.1-SNAPSHOT/test-util-2.1-SNAPSHOT.jar
URI srcFileUri = new URI(srcUrl.getPath().split("!/")[0]);
File srcFile = new File(srcFileUri);
JarFile jar = new JarFile(srcFile);
Enumeration<JarEntry> entries = jar.entries();
JarEntry jarEntry;
while (entries.hasMoreElements()) {
jarEntry = entries.nextElement();
// skip other files
if (!jarEntry.getName().contains(srcPath)) {
LOGGER.trace("Not relevant: ", jarEntry.getName());
continue;
}
// prepare destination file
String filepath = jarEntry.getName().substring(srcPath.length());
File dstFile = new File(dstPath, filepath);
if (!overwrite && dstFile.exists()) {
LOGGER.debug("Skipping file {}: exists", dstFile);
continue;
}
if (jarEntry.isDirectory()) {
dstFile.mkdirs();
continue;
}
InputStream is = ClassLoader.getSystemResourceAsStream(jarEntry.getName());
LOGGER.debug("Copying {} from {} to {} ", jarEntry.getName(), srcFile, dstFile);
copyFile(is, jarEntry.getName(), dstFile);
}
jar.close();
} else {
try {
File file = new File(srcUrl);
File[] files = file.listFiles();
for (File subFile : files) {
File dstFile = new File(dstPath, subFile.getName());
if (subFile.isDirectory()) {
LOGGER.debug("Copying directory {} to {} ", subFile, dstFile);
MiscUtil.copyDirectory(subFile, dstFile);
} else {
LOGGER.debug("Copying file {} to {} ", subFile, dstFile);
MiscUtil.copyFile(subFile, dstFile);
}
}
} catch (Exception ex) {
throw new IOException(ex);
}
}
return true;
}
Aggregations