use of java.net.JarURLConnection in project robovm by robovm.
the class OldJarURLConnectionTest method test_getMainAttributes.
public void test_getMainAttributes() throws Exception {
URL u = createContent("lf.jar", "swt.dll");
juc = (JarURLConnection) u.openConnection();
java.util.jar.Attributes a = juc.getMainAttributes();
assertEquals("Returned incorrect Attributes", "1.0", a.get(java.util.jar.Attributes.Name.MANIFEST_VERSION));
URL invURL = createContent("InvalidJar.jar", "Test.class");
JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
try {
juConn.getMainAttributes();
fail("IOException was not thrown.");
} catch (java.io.IOException io) {
//expected
}
}
use of java.net.JarURLConnection in project robovm by robovm.
the class OldJarURLConnectionTest method test_getJarFile.
public void test_getJarFile() throws IOException {
URL url = createContent("lf.jar", "missing");
JarURLConnection connection = (JarURLConnection) url.openConnection();
try {
connection.connect();
fail("Did not throw exception on connect");
} catch (IOException e) {
// expected
}
try {
connection.getJarFile();
fail("Did not throw exception after connect");
} catch (IOException e) {
// expected
}
URL invURL = createContent("InvalidJar.jar", "Test.class");
JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
try {
juConn.getJarFile();
fail("IOException was not thrown.");
} catch (java.io.IOException io) {
//expected
}
File resources = Support_Resources.createTempFolder();
Support_Resources.copyFile(resources, null, "hyts_att.jar");
File file = new File(resources.toString() + "/hyts_att.jar");
URL fUrl1 = new URL("jar:file:" + file.getPath() + "!/");
JarURLConnection con1 = (JarURLConnection) fUrl1.openConnection();
ZipFile jf1 = con1.getJarFile();
JarURLConnection con2 = (JarURLConnection) fUrl1.openConnection();
ZipFile jf2 = con2.getJarFile();
assertTrue("file: JarFiles not the same", jf1 == jf2);
jf1.close();
assertTrue("File should exist", file.exists());
fUrl1 = createContent("lf.jar", "");
con1 = (JarURLConnection) fUrl1.openConnection();
jf1 = con1.getJarFile();
con2 = (JarURLConnection) fUrl1.openConnection();
jf2 = con2.getJarFile();
assertTrue("http: JarFiles not the same", jf1 == jf2);
jf1.close();
}
use of java.net.JarURLConnection in project robovm by robovm.
the class URLConnectionTest method test_addRequestPropertyLjava_lang_StringLjava_lang_String.
/**
* @throws URISyntaxException
* @throws ClassNotFoundException
* {@link java.net.URLConnection#addRequestProperty(java.lang.String,java.lang.String)}
*/
public void test_addRequestPropertyLjava_lang_StringLjava_lang_String() throws IOException, ClassNotFoundException, URISyntaxException {
uc.setRequestProperty("prop", "yo");
uc.setRequestProperty("prop", "yo2");
assertEquals("yo2", uc.getRequestProperty("prop"));
Map<String, List<String>> map = uc.getRequestProperties();
List<String> props = uc.getRequestProperties().get("prop");
assertEquals(1, props.size());
try {
// the map should be unmodifiable
map.put("hi", Arrays.asList(new String[] { "bye" }));
fail("could modify map");
} catch (UnsupportedOperationException e) {
// Expected
}
try {
// the list should be unmodifiable
props.add("hi");
fail("could modify list");
} catch (UnsupportedOperationException e) {
// Expected
}
JarURLConnection con1 = openJarURLConnection();
map = con1.getRequestProperties();
assertNotNull(map);
assertEquals(0, map.size());
try {
// the map should be unmodifiable
map.put("hi", Arrays.asList(new String[] { "bye" }));
fail();
} catch (UnsupportedOperationException e) {
// Expected
}
}
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:")) {
// 获取jar
JarFile jarFile = ((JarURLConnection) url.openConnection()).getJarFile();
// 从此jar包 得到一个枚举类
Enumeration<JarEntry> eje = jarFile.entries();
// 同样的进行循环迭代
while (eje.hasMoreElements()) {
// 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件
JarEntry entry = eje.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);
// 添加到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) {
LOGGER.error("The scan error when the user to define the view from a jar package file.", e);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return classes;
}
use of java.net.JarURLConnection in project felix by apache.
the class PojoSR method buildRevision.
private Revision buildRevision(BundleDescriptor desc) throws IOException {
Revision r;
URL url = new URL(desc.getUrl());
URL u = new URL(desc.getUrl() + "META-INF/MANIFEST.MF");
String extF = u.toExternalForm();
if (extF.startsWith("file:")) {
File root = new File(URLDecoder.decode(url.getFile(), "UTF-8"));
r = new DirRevision(root);
} else {
URLConnection uc = u.openConnection();
if (uc instanceof JarURLConnection) {
String target = ((JarURLConnection) uc).getJarFileURL().toExternalForm();
String prefix = null;
if (!("jar:" + target + "!/").equals(desc.getUrl()) && desc.getUrl().startsWith("jar:" + target + "!/")) {
System.out.println(desc.getUrl() + " " + target);
prefix = desc.getUrl().substring(("jar:" + target + "!/").length());
}
r = new JarRevision(((JarURLConnection) uc).getJarFile(), ((JarURLConnection) uc).getJarFileURL(), prefix, uc.getLastModified());
} else if (m_hasVFS && extF.startsWith("vfs")) {
r = new VFSRevision(url, url.openConnection().getLastModified());
} else {
r = new URLRevision(url, url.openConnection().getLastModified());
}
}
return r;
}
Aggregations