use of java.net.JarURLConnection in project vespa by vespa-engine.
the class FilesApplicationPackage method allSdsOnClassPath.
private static void allSdsOnClassPath(List<String> usedNames, Map<String, String> ret) throws IOException {
Enumeration<java.net.URL> resources = FilesApplicationPackage.class.getClassLoader().getResources(ApplicationPackage.SEARCH_DEFINITIONS_DIR.getRelative());
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
String protocol = resource.getProtocol();
if ("file".equals(protocol)) {
File file;
try {
file = new File(resource.toURI());
} catch (URISyntaxException e) {
continue;
}
// only interested in directories
if (file.isDirectory()) {
List<File> sdFiles = getSearchDefinitionFiles(file);
for (File sdFile : sdFiles) {
String sdName = sdFile.getName();
if (usedNames.contains(sdName)) {
throw new IllegalArgumentException("The search definition name '" + sdName + "' found in classpath already used earlier in classpath.");
}
usedNames.add(sdName);
String contents = IOUtils.readAll(new FileReader(sdFile));
ret.put(sdFile.getName(), contents);
}
}
} else if ("jar".equals(protocol)) {
JarURLConnection jarConnection = (JarURLConnection) resource.openConnection();
JarFile jarFile = jarConnection.getJarFile();
for (Map.Entry<String, String> entry : ApplicationPackage.getBundleSdFiles("", jarFile).entrySet()) {
String sdName = entry.getKey();
if (usedNames.contains(sdName)) {
throw new IllegalArgumentException("The search definitions name '" + sdName + "' used in bundle '" + jarFile.getName() + "' " + "is already used in classpath or previous bundle.");
}
usedNames.add(sdName);
String sdPayload = entry.getValue();
ret.put(sdName, sdPayload);
}
}
}
}
use of java.net.JarURLConnection in project sagacity-sqltoy by chenrenfei.
the class ScanEntityAndSqlResource method getPackageEntities.
/**
* @todo 从指定包package中获取所有的sqltoy实体对象
* @param pack
* @param recursive
* 是否循环迭代下级目录
* @return
*/
public static Set<Class<?>> getPackageEntities(String pack, boolean recursive, String charset) {
// 第一个class类的集合
Set<Class<?>> entities = new LinkedHashSet<Class<?>>();
// 获取包的名字 并进行替换
String packageName = pack;
// 剔除第一个字符为目录的符合
if (packageName.charAt(0) == '/') {
packageName = packageName.substring(1);
}
String packageDirName = packageName.replace('.', '/');
// 定义一个枚举的集合 并进行循环来处理这个目录下的things
Enumeration<URL> dirs;
try {
dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
// 循环迭代下去
URL url;
String protocol;
while (dirs.hasMoreElements()) {
// 获取下一个元素
url = dirs.nextElement();
// 得到协议的名称
protocol = url.getProtocol();
// 如果是以文件的形式保存在服务器上
if ("file".equals(protocol)) {
// 获取包的物理路径
String filePath = URLDecoder.decode(url.getFile(), (charset == null) ? "UTF-8" : charset);
// 以文件的方式扫描整个包下的文件 并添加到集合中
addEntitiesInPackage(packageName, filePath, recursive, entities);
} else if ("jar".equals(protocol)) {
// 如果是jar包文件
System.out.println("jar类型的扫描,加载sql.xml文件");
// 定义一个JarFile
JarFile jar;
try {
// 获取jar
jar = ((JarURLConnection) url.openConnection()).getJarFile();
// 从此jar包 得到一个枚举类
Enumeration<JarEntry> entries = jar.entries();
// 同样的进行循环迭代
JarEntry entry;
String name;
String loadClass;
while (entries.hasMoreElements()) {
// 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件
entry = entries.nextElement();
name = entry.getName();
// 如果前半部分和定义的包名相同
if (name.startsWith(packageDirName) && name.endsWith(".class") && !entry.isDirectory()) {
// 去掉后面的".class" 获取真正的类名
loadClass = name.substring(0, name.length() - 6).replace("/", ".");
try {
// 添加到classes
Class entityClass = Thread.currentThread().getContextClassLoader().loadClass(loadClass);
// 判定是否是sqltoy实体对象
if (isSqlToyEntity(entityClass))
entities.add(entityClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
// log.error("在扫描用户定义视图时从jar包获取文件出错");
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return entities;
}
use of java.net.JarURLConnection in project openj9 by eclipse.
the class CustomURLClassLoader method locateClass.
private int locateClass(String className) {
int classAtEntry = -1;
String name = className.replace('.', '/').concat(".class");
for (int index = 0; index < urls.length; index++) {
URL currentUrl = urls[index];
if (currentUrl.getProtocol().equals("jar")) {
JarEntry entry = null;
JarFile jf = (JarFile) jarCache.get(currentUrl);
if (jf == null) {
/* First time we have encountered this jar.
* Lets cache its metaData.
*/
try {
URL jarFileUrl = ((JarURLConnection) currentUrl.openConnection()).getJarFileURL();
JarURLConnection jarUrlConnection = (JarURLConnection) new URL("jar", "", jarFileUrl.toExternalForm() + "!/").openConnection();
try {
jf = jarUrlConnection.getJarFile();
} catch (Exception e) {
}
if (jf != null) {
jarCache.put(currentUrl, jf);
}
} catch (Exception e) {
e.printStackTrace();
}
if (jf != null) {
Manifest manifest = null;
java.security.cert.Certificate[] certs = null;
URL csUrl = currentUrl;
CodeSource codeSource;
try {
manifest = jf.getManifest();
} catch (Exception e) {
e.printStackTrace();
}
entry = jf.getJarEntry(name);
if (entry != null) {
certs = entry.getCertificates();
}
codeSource = new CodeSource(csUrl, certs);
CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
metaData.manifest = manifest;
metaData.codeSource = codeSource;
metaDataArray[index] = metaData;
}
}
if (entry == null && jf != null) {
entry = jf.getJarEntry(name);
}
if (entry != null) {
/* We have the first match on the class path, return the current url index */
return index;
}
} else {
String filename = currentUrl.getFile();
String host = currentUrl.getHost();
if (host != null && host.length() > 0) {
filename = new StringBuffer(host.length() + filename.length() + name.length() + 2).append("//").append(host).append(filename).append(name).toString();
} else {
filename = new StringBuffer(filename.length() + name.length()).append(filename).append(name).toString();
}
File file = new File(filename);
// Don't throw exceptions for speed
if (file.exists()) {
if (metaDataArray[index] == null) {
java.security.cert.Certificate[] certs = null;
CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
metaData.manifest = null;
metaData.codeSource = new CodeSource(currentUrl, certs);
metaDataArray[index] = metaData;
}
return index;
}
}
}
return classAtEntry;
}
use of java.net.JarURLConnection in project openj9 by eclipse.
the class CustomURLClassLoaderNoAPIUse method locateClass.
private int locateClass(String className) {
int classAtEntry = -1;
String name = className.replace('.', '/').concat(".class");
for (int index = 0; index < urls.length; index++) {
URL currentUrl = urls[index];
if (currentUrl.getProtocol().equals("jar")) {
JarEntry entry = null;
JarFile jf = (JarFile) jarCache.get(currentUrl);
if (jf == null) {
/* First time we have encountered this jar.
* Lets cache its metaData.
*/
try {
URL jarFileUrl = ((JarURLConnection) currentUrl.openConnection()).getJarFileURL();
JarURLConnection jarUrlConnection = (JarURLConnection) new URL("jar", "", jarFileUrl.toExternalForm() + "!/").openConnection();
try {
jf = jarUrlConnection.getJarFile();
} catch (Exception e) {
}
if (jf != null) {
jarCache.put(currentUrl, jf);
}
} catch (Exception e) {
e.printStackTrace();
}
if (jf != null) {
Manifest manifest = null;
java.security.cert.Certificate[] certs = null;
URL csUrl = currentUrl;
CodeSource codeSource;
try {
manifest = jf.getManifest();
} catch (Exception e) {
e.printStackTrace();
}
entry = jf.getJarEntry(name);
if (entry != null) {
certs = entry.getCertificates();
}
codeSource = new CodeSource(csUrl, certs);
CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
metaData.manifest = manifest;
metaData.codeSource = codeSource;
metaDataArray[index] = metaData;
}
}
if (entry == null && jf != null) {
entry = jf.getJarEntry(name);
}
if (entry != null) {
/* We have the first match on the class path, return the current url index */
return index;
}
} else {
String filename = currentUrl.getFile();
String host = currentUrl.getHost();
if (host != null && host.length() > 0) {
filename = new StringBuffer(host.length() + filename.length() + name.length() + 2).append("//").append(host).append(filename).append(name).toString();
} else {
filename = new StringBuffer(filename.length() + name.length()).append(filename).append(name).toString();
}
File file = new File(filename);
// Don't throw exceptions for speed
if (file.exists()) {
if (metaDataArray[index] == null) {
java.security.cert.Certificate[] certs = null;
CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
metaData.manifest = null;
metaData.codeSource = new CodeSource(currentUrl, certs);
metaDataArray[index] = metaData;
}
return index;
}
}
}
return classAtEntry;
}
use of java.net.JarURLConnection in project openj9 by eclipse.
the class CustomURLClassLoaderNonConfirming method locateClass.
private int locateClass(String className) {
int classAtEntry = -1;
String name = className.replace('.', '/').concat(".class");
for (int index = 0; index < urls.length; index++) {
URL currentUrl = urls[index];
if (currentUrl.getProtocol().equals("jar")) {
JarEntry entry = null;
JarFile jf = (JarFile) jarCache.get(currentUrl);
if (jf == null) {
/* First time we have encountered this jar.
* Lets cache its metaData.
*/
try {
URL jarFileUrl = ((JarURLConnection) currentUrl.openConnection()).getJarFileURL();
JarURLConnection jarUrlConnection = (JarURLConnection) new URL("jar", "", jarFileUrl.toExternalForm() + "!/").openConnection();
try {
jf = jarUrlConnection.getJarFile();
} catch (Exception e) {
}
if (jf != null) {
jarCache.put(currentUrl, jf);
}
} catch (Exception e) {
e.printStackTrace();
}
if (jf != null) {
Manifest manifest = null;
java.security.cert.Certificate[] certs = null;
URL csUrl = currentUrl;
CodeSource codeSource;
try {
manifest = jf.getManifest();
} catch (Exception e) {
e.printStackTrace();
}
entry = jf.getJarEntry(name);
if (entry != null) {
certs = entry.getCertificates();
}
codeSource = new CodeSource(csUrl, certs);
CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
metaData.manifest = manifest;
metaData.codeSource = codeSource;
metaDataArray[index] = metaData;
}
}
if (entry == null && jf != null) {
entry = jf.getJarEntry(name);
}
if (entry != null) {
/* We have the first match on the class path, return the current url index */
return index;
}
} else {
String filename = currentUrl.getFile();
String host = currentUrl.getHost();
if (host != null && host.length() > 0) {
filename = new StringBuffer(host.length() + filename.length() + name.length() + 2).append("//").append(host).append(filename).append(name).toString();
} else {
filename = new StringBuffer(filename.length() + name.length()).append(filename).append(name).toString();
}
File file = new File(filename);
// Don't throw exceptions for speed
if (file.exists()) {
if (metaDataArray[index] == null) {
java.security.cert.Certificate[] certs = null;
CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
metaData.manifest = null;
metaData.codeSource = new CodeSource(currentUrl, certs);
metaDataArray[index] = metaData;
}
return index;
}
}
}
return classAtEntry;
}
Aggregations