use of java.net.JarURLConnection in project ibas-framework by color-coding.
the class BOFactory method loadClasses.
@Override
public Class<?>[] loadClasses(String packageName) {
// 第一个class类的集合
Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
String packageDirName = packageName.replace('.', '/');
// 定义一个枚举的集合 并进行循环来处理这个目录下的things
Enumeration<URL> dirs;
try {
ClassLoader classLoader = this.getClassLoader();
dirs = classLoader.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");
// 以文件的方式扫描整个包下的文件 并添加到集合中
this.findClassesInPackageByFile(packageName, filePath, classes);
} 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) {
// 如果是一个.class文件 而且不是目录
if (name.endsWith(".class") && !entry.isDirectory()) {
// 去掉后面的".class" 获取真正的类名
String className = name.substring(packageName.length() + 1, name.length() - 6);
try {
// 添加到classes
classes.add(classLoader.loadClass(packageName + '.' + className));
} catch (ClassNotFoundException e) {
// Logger.log(MessageLevel.DEBUG, e);
}
}
}
}
}
} catch (IOException e) {
Logger.log(MessageLevel.DEBUG, e);
}
}
}
} catch (IOException e) {
Logger.log(MessageLevel.DEBUG, e);
}
return classes.toArray(new Class<?>[] {});
}
use of java.net.JarURLConnection in project tomcat70 by apache.
the class JarURLResource method getJarFile.
@Override
public JarFile getJarFile() throws IOException {
URL jarFileUrl = new URL("jar:" + jarUrl + "!/");
JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection();
conn.setUseCaches(false);
conn.connect();
return conn.getJarFile();
}
use of java.net.JarURLConnection in project tomcat70 by apache.
the class JspCompilationContext method getLastModified.
public Long getLastModified(String resource) {
long result = -1;
if (resource.startsWith("file:/")) {
File f;
try {
f = new File(new URI(resource));
} catch (URISyntaxException e) {
return Long.valueOf(-1);
}
return Long.valueOf(f.lastModified());
}
URLConnection uc = null;
try {
URL jspUrl = getResource(resource);
if (jspUrl == null) {
incrementRemoved();
return Long.valueOf(result);
}
uc = jspUrl.openConnection();
if (uc instanceof JarURLConnection) {
result = ((JarURLConnection) uc).getJarEntry().getTime();
} 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 tomcat70 by apache.
the class JspCompilationContext method getJspLastModified.
/**
* @deprecated Will be removed in Tomcat 8.0.x. Use
* {@link #getLastModified(String)} instead.
*/
@Deprecated
public long getJspLastModified() {
long result = -1;
URLConnection uc = null;
try {
URL jspUrl = getResource(getJspFile());
if (jspUrl == null) {
incrementRemoved();
return result;
}
uc = jspUrl.openConnection();
if (uc instanceof JarURLConnection) {
result = ((JarURLConnection) uc).getJarEntry().getTime();
} 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 result;
}
use of java.net.JarURLConnection in project tomcat70 by apache.
the class ExpandWar method validate.
/**
* Validate the WAR file found at the specified URL.
*
* @param host Host war is being installed for
* @param war URL of the web application archive to be validated
* (must start with "jar:")
* @param pathname Context path name for web application
*
* @exception IllegalArgumentException if this is not a "jar:" URL or if the
* WAR file is invalid
* @exception IOException if an input/output error was encountered
* during validation
*/
public static void validate(Host host, URL war, String pathname) throws IOException {
// Make the appBase absolute
File appBase = new File(host.getAppBase());
if (!appBase.isAbsolute()) {
appBase = new File(System.getProperty(Globals.CATALINA_BASE_PROP), host.getAppBase());
}
File docBase = new File(appBase, pathname);
// Calculate the document base directory
String canonicalDocBasePrefix = docBase.getCanonicalPath();
if (!canonicalDocBasePrefix.endsWith(File.separator)) {
canonicalDocBasePrefix += File.separator;
}
JarURLConnection juc = (JarURLConnection) war.openConnection();
juc.setUseCaches(false);
JarFile jarFile = null;
try {
jarFile = juc.getJarFile();
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
String name = jarEntry.getName();
File expandedFile = new File(docBase, name);
if (!expandedFile.getCanonicalPath().startsWith(canonicalDocBasePrefix)) {
// Throw an exception to stop the deployment
throw new IllegalArgumentException(sm.getString("expandWar.illegalPath", war, name, expandedFile.getCanonicalPath(), canonicalDocBasePrefix));
}
}
} catch (IOException e) {
throw e;
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
}
jarFile = null;
}
}
}
Aggregations