use of java.net.JarURLConnection 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);
}
}
use of java.net.JarURLConnection 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.net.JarURLConnection in project fabric8 by jboss-fuse.
the class LogQuerySupport method jarIndex.
protected String jarIndex(URL url) throws IOException {
StringBuilder buffer = new StringBuilder();
JarURLConnection uc = (JarURLConnection) url.openConnection();
return jarIndex(uc.getJarFile());
}
use of java.net.JarURLConnection in project xwiki-platform by xwiki.
the class FilesystemResourceReferenceCopier method getJARFile.
private File getJARFile(String resourceName) throws IOException {
// Get the JAR URL by looking up the passed resource name to extract the location of the JAR
URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(resourceName);
// "url($!services.webjars.url(...))". In this case ignore it and don't copy the resource to the file system.
if (resourceURL != null) {
JarURLConnection connection = (JarURLConnection) resourceURL.openConnection();
URL jarURL = connection.getJarFileURL();
File file;
try {
file = new File(jarURL.toURI());
} catch (URISyntaxException e) {
file = new File(jarURL.getPath());
}
return file;
} else {
LOGGER.debug("Cannot construct JAR File for resource [{}] which couldn't be found in the context " + "ClassLoader.", resourceName);
return null;
}
}
use of java.net.JarURLConnection in project liferay-ide by liferay.
the class BladeCLI method execute.
public static String[] execute(String args) {
Project project = new Project();
Java javaTask = new Java();
javaTask.setProject(project);
javaTask.setFork(true);
javaTask.setFailonerror(true);
Properties properties = System.getProperties();
boolean needToCopy = true;
File temp = new File(properties.getProperty("user.home"), ".liferay-ide");
File bladeJar = new File(temp, "blade.jar");
ClassLoader bladeClassLoader = BladeCLI.class.getClassLoader();
URL url = bladeClassLoader.getResource("/libs/blade.jar");
try (InputStream in = bladeClassLoader.getResourceAsStream("/libs/blade.jar")) {
JarURLConnection jarUrlConnection = (JarURLConnection) url.openConnection();
JarEntry jarEntry = jarUrlConnection.getJarEntry();
Long bladeJarTimestamp = jarEntry.getTime();
if (bladeJar.exists()) {
Long destTimestamp = bladeJar.lastModified();
if (destTimestamp < bladeJarTimestamp) {
bladeJar.delete();
} else {
needToCopy = false;
}
}
if (needToCopy) {
FileUtil.writeFile(bladeJar, in);
bladeJar.setLastModified(bladeJarTimestamp);
}
} catch (IOException ioe) {
}
javaTask.setJar(bladeJar);
javaTask.setArgs(args);
DefaultLogger logger = new DefaultLogger();
project.addBuildListener(logger);
StringBufferOutputStream out = new StringBufferOutputStream();
logger.setOutputPrintStream(new PrintStream(out));
logger.setMessageOutputLevel(Project.MSG_INFO);
javaTask.executeJava();
List<String> lines = new ArrayList<>();
Scanner scanner = new Scanner(out.toString());
while (scanner.hasNextLine()) {
String nextLine = scanner.nextLine();
lines.add(nextLine.replaceAll(".*\\[null\\] ", ""));
}
scanner.close();
boolean hasErrors = false;
StringBuilder errors = new StringBuilder();
for (String line : lines) {
if (line.startsWith("Error")) {
hasErrors = true;
} else if (hasErrors) {
errors.append(line);
}
}
return lines.toArray(new String[0]);
}
Aggregations