use of java.net.JarURLConnection in project grails-core by grails.
the class PathMatchingResourcePatternResolver method doFindPathMatchingJarResources.
/**
* Find all resources in jar files that match the given location pattern
* via the Ant-style PathMatcher.
* @param rootDirResource the root directory as Resource
* @param subPattern the sub pattern to match (below the root directory)
* @return the Set of matching Resource instances
* @throws IOException in case of I/O errors
* @see java.net.JarURLConnection
*/
protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource, String subPattern) throws IOException {
URLConnection con = rootDirResource.getURL().openConnection();
JarFile jarFile;
String jarFileUrl;
String rootEntryPath;
boolean newJarFile = false;
if (con instanceof JarURLConnection) {
// Should usually be the case for traditional JAR files.
JarURLConnection jarCon = (JarURLConnection) con;
GrailsResourceUtils.useCachesIfNecessary(jarCon);
jarFile = jarCon.getJarFile();
jarFileUrl = jarCon.getJarFileURL().toExternalForm();
JarEntry jarEntry = jarCon.getJarEntry();
rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
} else {
// No JarURLConnection -> need to resort to URL file parsing.
// We'll assume URLs of the format "jar:path!/entry", with the protocol
// being arbitrary as long as following the entry format.
// We'll also handle paths with and without leading "file:" prefix.
String urlFile = rootDirResource.getURL().getFile();
int separatorIndex = urlFile.indexOf(GrailsResourceUtils.JAR_URL_SEPARATOR);
if (separatorIndex != -1) {
jarFileUrl = urlFile.substring(0, separatorIndex);
rootEntryPath = urlFile.substring(separatorIndex + GrailsResourceUtils.JAR_URL_SEPARATOR.length());
jarFile = getJarFile(jarFileUrl);
} else {
jarFile = new JarFile(urlFile);
jarFileUrl = urlFile;
rootEntryPath = "";
}
newJarFile = true;
}
try {
if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) {
// Root entry path must end with slash to allow for proper matching.
// The Sun JRE does not return a slash here, but BEA JRockit does.
rootEntryPath = rootEntryPath + "/";
}
Set<Resource> result = new LinkedHashSet<Resource>(8);
for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
String entryPath = entry.getName();
if (entryPath.startsWith(rootEntryPath)) {
String relativePath = entryPath.substring(rootEntryPath.length());
if (getPathMatcher().match(subPattern, relativePath)) {
result.add(rootDirResource.createRelative(relativePath));
}
}
}
return result;
} finally {
// not from JarURLConnection, which might cache the file reference.
if (newJarFile) {
jarFile.close();
}
}
}
use of java.net.JarURLConnection in project tomcat by apache.
the class Compiler method isOutDated.
/**
* Determine if a compilation is necessary by checking the time stamp of the
* JSP page with that of the corresponding .class or .java file. If the page
* has dependencies, the check is also extended to its dependents, and so
* on. This method can by overridden by a subclasses of Compiler.
*
* @param checkClass
* If true, check against .class file, if false, check against
* .java file.
* @return <code>true</code> if the source generation and compilation
* should occur
*/
public boolean isOutDated(boolean checkClass) {
if (jsw != null && (ctxt.getOptions().getModificationTestInterval() > 0)) {
if (jsw.getLastModificationTest() + (ctxt.getOptions().getModificationTestInterval() * 1000) > System.currentTimeMillis()) {
return false;
}
jsw.setLastModificationTest(System.currentTimeMillis());
}
// Test the target file first. Unless there is an error checking the
// last modified time of the source (unlikely) the target is going to
// have to be checked anyway. If the target doesn't exist (likely during
// startup) this saves an unnecessary check of the source.
File targetFile;
if (checkClass) {
targetFile = new File(ctxt.getClassFileName());
} else {
targetFile = new File(ctxt.getServletJavaFileName());
}
if (!targetFile.exists()) {
return true;
}
long targetLastModified = targetFile.lastModified();
if (checkClass && jsw != null) {
jsw.setServletClassLastModifiedTime(targetLastModified);
}
Long jspRealLastModified = ctxt.getLastModified(ctxt.getJspFile());
if (jspRealLastModified.longValue() < 0) {
// Something went wrong - assume modification
return true;
}
if (targetLastModified != jspRealLastModified.longValue()) {
if (log.isDebugEnabled()) {
log.debug("Compiler: outdated: " + targetFile + " " + targetLastModified);
}
return true;
}
// directives) have been changed.
if (jsw == null) {
return false;
}
Map<String, Long> depends = jsw.getDependants();
if (depends == null) {
return false;
}
for (Entry<String, Long> include : depends.entrySet()) {
try {
String key = include.getKey();
URL includeUrl;
long includeLastModified = 0;
if (key.startsWith("jar:jar:")) {
// Assume we constructed this correctly
int entryStart = key.lastIndexOf("!/");
String entry = key.substring(entryStart + 2);
try (Jar jar = JarFactory.newInstance(new URL(key.substring(4, entryStart)))) {
includeLastModified = jar.getLastModified(entry);
}
} else {
if (key.startsWith("jar:") || key.startsWith("file:")) {
includeUrl = new URL(key);
} else {
includeUrl = ctxt.getResource(include.getKey());
}
if (includeUrl == null) {
return true;
}
URLConnection iuc = includeUrl.openConnection();
if (iuc instanceof JarURLConnection) {
includeLastModified = ((JarURLConnection) iuc).getJarEntry().getTime();
} else {
includeLastModified = iuc.getLastModified();
}
iuc.getInputStream().close();
}
if (includeLastModified != include.getValue().longValue()) {
return true;
}
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Problem accessing resource. Treat as outdated.", e);
}
return true;
}
}
return false;
}
use of java.net.JarURLConnection 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.net.JarURLConnection in project blade by biezhi.
the class JarReaderImpl method getClasses.
private Set<ClassInfo> getClasses(final URL url, final String packageDirName, String packageName, final Class<?> parent, final Class<? extends Annotation> annotation, final boolean recursive, Set<ClassInfo> classes) {
try {
if (url.toString().startsWith(JAR_FILE) || url.toString().startsWith(WSJAR_FILE)) {
// Get jar file
JarFile jarFile = ((JarURLConnection) url.openConnection()).getJarFile();
// From the jar package to get an enumeration class
Enumeration<JarEntry> eje = jarFile.entries();
while (eje.hasMoreElements()) {
// Get an entity in jar can be a directory and some other documents in the jar package
// such as META-INF and other documents
JarEntry entry = eje.nextElement();
String name = entry.getName();
// if start with '/'
if (name.charAt(0) == '/') {
name = name.substring(1);
}
// If the first half is the same as the defined package name
if (!name.startsWith(packageDirName)) {
continue;
}
int idx = name.lastIndexOf('/');
// If the end of "/" is a package
if (idx != -1) {
// Get the package name and replace "/" with "."
packageName = name.substring(0, idx).replace('/', '.');
}
// If it can be iterated and is a package
if (idx == -1 && !recursive) {
continue;
}
// If it is a .class file and not a directory
if (!name.endsWith(".class") || entry.isDirectory()) {
continue;
}
// Remove the following ".class" to get the real class name
String className = name.substring(packageName.length() + 1, name.length() - 6);
// Add to classes
Class<?> clazz = Class.forName(packageName + '.' + className);
if (null != parent && null != annotation) {
if (null != clazz.getSuperclass() && clazz.getSuperclass().equals(parent) && null != clazz.getAnnotation(annotation)) {
classes.add(new ClassInfo(clazz));
}
continue;
}
if (null != parent) {
if (null != clazz.getSuperclass() && clazz.getSuperclass().equals(parent)) {
classes.add(new ClassInfo(clazz));
}
continue;
}
if (null != annotation) {
if (null != clazz.getAnnotation(annotation)) {
classes.add(new ClassInfo(clazz));
}
continue;
}
classes.add(new ClassInfo(clazz));
}
}
} catch (IOException e) {
log.error("The scan error when the user to define the view from a jar package file.", e);
} catch (ClassNotFoundException e) {
log.error("", e);
}
return classes;
}
use of java.net.JarURLConnection in project MSEC by Tencent.
the class ClassUtils method loadClasses.
/**
* �Ӱ�package�л�ȡ���е�Class
*
* @param pack
* @return
*/
public static void loadClasses(String pack) {
// �Ƿ�ѭ������
boolean recursive = true;
// ��ȡ�������� �������滻
String packageName = pack;
String packageDirName = packageName.replace('.', '/');
// ����һ��ö�ٵļ��� ������ѭ�����������Ŀ¼�µ�things
Enumeration<URL> dirs;
try {
dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
// ѭ��������ȥ
while (dirs.hasMoreElements()) {
// ��ȡ��һ��Ԫ��
URL url = dirs.nextElement();
// �õ�Э�������
String protocol = url.getProtocol();
// ��������ļ�����ʽ�����ڷ�������
if ("file".equals(protocol)) {
// ��ȡ��������·��
String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
// ���ļ��ķ�ʽɨ���������µ��ļ� ����ӵ�������
findAndAddClassesInPackageByFile(packageName, filePath, recursive);
} else if ("jar".equals(protocol)) {
// �����jar���ļ�
// ����һ��JarFile
JarFile jar;
try {
// ��ȡjar
jar = ((JarURLConnection) url.openConnection()).getJarFile();
// �Ӵ�jar�� �õ�һ��ö����
Enumeration<JarEntry> entries = jar.entries();
// ͬ���Ľ���ѭ������
while (entries.hasMoreElements()) {
// ��ȡjar���һ��ʵ�� ������Ŀ¼ ��һЩjar����������ļ� ��META-INF���ļ�
JarEntry entry = entries.nextElement();
String name = entry.getName();
// �������/��ͷ��
if (name.charAt(0) == '/') {
// ��ȡ������ַ���
name = name.substring(1);
}
// ���ǰ�벿�ֺͶ���İ�����ͬ
if (name.startsWith(packageDirName)) {
int idx = name.lastIndexOf('/');
// �����"/"��β ��һ����
if (idx != -1) {
// ��ȡ���� ��"/"�滻��"."
packageName = name.substring(0, idx).replace('/', '.');
}
// ������Ե�����ȥ ������һ����
if ((idx != -1) || recursive) {
// �����һ��.class�ļ� ���Ҳ���Ŀ¼
if (name.endsWith(".class") && !entry.isDirectory()) {
// ȥ�������".class" ��ȡ����������
String className = name.substring(packageName.length() + 1, name.length() - 6);
try {
// ��ӵ�classes
Class.forName(packageName + '.' + className);
} catch (ClassNotFoundException e) {
// log
// .error("����û��Զ�����ͼ����� �Ҳ��������.class�ļ�");
e.printStackTrace();
}
}
}
}
}
} catch (IOException e) {
// log.error("��ɨ���û�������ͼʱ��jar����ȡ�ļ�����");
e.printStackTrace();
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
Aggregations