use of java.util.jar.JarFile in project liquibase by liquibase.
the class Main method configureClassLoader.
protected void configureClassLoader() throws CommandLineParsingException {
final List<URL> urls = new ArrayList<URL>();
if (this.classpath != null) {
String[] classpath;
if (isWindows()) {
classpath = this.classpath.split(";");
} else {
classpath = this.classpath.split(":");
}
Logger logger = LogFactory.getInstance().getLog();
for (String classpathEntry : classpath) {
File classPathFile = new File(classpathEntry);
if (!classPathFile.exists()) {
throw new CommandLineParsingException(classPathFile.getAbsolutePath() + " does not exist");
}
try {
if (classpathEntry.endsWith(".war")) {
addWarFileClasspathEntries(classPathFile, urls);
} else if (classpathEntry.endsWith(".ear")) {
JarFile earZip = new JarFile(classPathFile);
Enumeration<? extends JarEntry> entries = earZip.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().toLowerCase().endsWith(".jar")) {
File jar = extract(earZip, entry);
URL newUrl = new URL("jar:" + jar.toURI().toURL() + "!/");
urls.add(newUrl);
logger.debug("Adding '" + newUrl + "' to classpath");
jar.deleteOnExit();
} else if (entry.getName().toLowerCase().endsWith("war")) {
File warFile = extract(earZip, entry);
addWarFileClasspathEntries(warFile, urls);
}
}
} else {
URL newUrl = new File(classpathEntry).toURI().toURL();
logger.debug("Adding '" + newUrl + "' to classpath");
urls.add(newUrl);
}
} catch (Exception e) {
throw new CommandLineParsingException(e);
}
}
}
if (includeSystemClasspath) {
classLoader = AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {
@Override
public URLClassLoader run() {
return new URLClassLoader(urls.toArray(new URL[urls.size()]), Thread.currentThread().getContextClassLoader());
}
});
} else {
classLoader = AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {
@Override
public URLClassLoader run() {
return new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
}
});
}
ServiceLocator.getInstance().setResourceAccessor(new ClassLoaderResourceAccessor(classLoader));
Thread.currentThread().setContextClassLoader(classLoader);
}
use of java.util.jar.JarFile in project liquibase by liquibase.
the class Main method addWarFileClasspathEntries.
private void addWarFileClasspathEntries(File classPathFile, List<URL> urls) throws IOException {
Logger logger = LogFactory.getInstance().getLog();
URL url = new URL("jar:" + classPathFile.toURI().toURL() + "!/WEB-INF/classes/");
logger.info("adding '" + url + "' to classpath");
urls.add(url);
JarFile warZip = new JarFile(classPathFile);
Enumeration<? extends JarEntry> entries = warZip.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.getName().startsWith("WEB-INF/lib") && entry.getName().toLowerCase().endsWith(".jar")) {
File jar = extract(warZip, entry);
URL newUrl = new URL("jar:" + jar.toURI().toURL() + "!/");
logger.info("adding '" + newUrl + "' to classpath");
urls.add(newUrl);
jar.deleteOnExit();
}
}
}
use of java.util.jar.JarFile in project rest.li by linkedin.
the class TestParseqTraceDebugRequestHandler method testStaticContent.
/**
* Tests the static content retrieval from the parseq trace debug request handler. It enumerates through all
* files imported into the JAR containing the parseq trace debug request handler, skips the ones that should
* not be served and verifies the rest can be retrieved. This test makes sure all files we import are actually
* servicable by the parseq trace debug request handler.
* @throws IOException
*/
@Test
public void testStaticContent() throws IOException {
ClassLoader classLoader = ParseqTraceDebugRequestHandler.class.getClassLoader();
//Collect all files under tracevis folder in the jar containing the parseq trace debug request handler.
Enumeration<URL> resources = classLoader.getResources(ParseqTraceDebugRequestHandler.class.getName().replace('.', '/') + ".class");
List<String> files = new ArrayList<String>();
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof JarURLConnection) {
JarURLConnection jarURLConnection = (JarURLConnection) urlConnection;
JarFile jar = jarURLConnection.getJarFile();
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry currentEntry = entries.nextElement();
if (!currentEntry.isDirectory()) {
String entry = currentEntry.getName();
if (entry.startsWith("tracevis/")) {
files.add(entry);
}
}
}
}
}
Assert.assertTrue(files.size() > 0);
// All other files should be retrievable from the parseq trace debug request handler.
for (String file : files) {
final String mimeType = determineMediaType(file);
final URI uri = URI.create("http://host/abc/12/__debug/parseqtrace/" + file.substring(file.indexOf('/') + 1));
executeRequestThroughParseqDebugHandler(uri, new RequestExecutionCallback<RestResponse>() {
@Override
public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
Assert.fail("Static content cannot be retrieved for " + uri.toString());
}
@Override
public void onSuccess(RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
Assert.assertEquals(result.getHeader(RestConstants.HEADER_CONTENT_TYPE), mimeType);
}
});
}
}
use of java.util.jar.JarFile in project JessMA by ldcsaa.
the class PackageHelper method scanResourceNamesByJar.
/**
* 在 jar 文件中扫描资源文件
*
* @param url : jar 文件的 URL
* @param basePackagePath : jar 文件中的包路径
* @param recursive : 是否扫描子目录
* @param filter : 资源文件过滤器,参考:{@link PackageHelper.ResourceFilter}
* @param names : 扫描结果集合
*/
public static final void scanResourceNamesByJar(URL url, String basePackagePath, final boolean recursive, final ResourceFilter filter, Set<String> names) {
basePackagePath = adjustBasePackagePath(basePackagePath);
try {
JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
int nameIndex = name.lastIndexOf(PATH_SEP_CHAR);
if (entry.isDirectory() || !name.startsWith(basePackagePath))
continue;
if (!recursive && nameIndex != basePackagePath.length())
continue;
String packagePath = (nameIndex != -1 ? name.substring(0, nameIndex) : EMPTY_STR);
String simpleName = (nameIndex != -1 ? name.substring(nameIndex + 1) : name);
if (filter != null && !filter.accept(packagePath, simpleName))
continue;
names.add(name);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of java.util.jar.JarFile in project JessMA by ldcsaa.
the class PackageHelper method scanPackageNamesByJar.
/**
* 在 jar 文件中扫描子包
*
* @param url : jar 文件的 URL
* @param basePackagePath : jar 文件中的包路径
* @param filter : 包过滤器,参考:{@link PackageHelper.PackageFilter}
* @param names : 扫描结果集合
*/
public static final void scanPackageNamesByJar(URL url, String basePackagePath, final PackageFilter filter, Set<String> names) {
basePackagePath = adjustBasePackagePath(basePackagePath);
try {
JarFile jar = ((JarURLConnection) url.openConnection()).getJarFile();
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (!entry.isDirectory() || !name.startsWith(basePackagePath))
continue;
if (name.endsWith(PATH_SEP_STR))
name = name.substring(0, name.length() - 1);
if (name.equals(basePackagePath))
continue;
String packageName = name.replace(PATH_SEP_CHAR, PACKAGE_SEP_CHAR);
if (filter == null || filter.accept(packageName))
names.add(packageName);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations