Search in sources :

Example 11 with JarURLConnection

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
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) Attributes(java.util.jar.Attributes) URL(java.net.URL)

Example 12 with JarURLConnection

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();
}
Also used : ZipFile(java.util.zip.ZipFile) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) URL(java.net.URL)

Example 13 with JarURLConnection

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
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) List(java.util.List)

Example 14 with JarURLConnection

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;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 15 with JarURLConnection

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;
}
Also used : BundleRevision(org.osgi.framework.wiring.BundleRevision) JarURLConnection(java.net.JarURLConnection) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Aggregations

JarURLConnection (java.net.JarURLConnection)222 URL (java.net.URL)160 JarFile (java.util.jar.JarFile)129 IOException (java.io.IOException)120 JarEntry (java.util.jar.JarEntry)105 File (java.io.File)92 URLConnection (java.net.URLConnection)89 ArrayList (java.util.ArrayList)32 InputStream (java.io.InputStream)27 MalformedURLException (java.net.MalformedURLException)25 URISyntaxException (java.net.URISyntaxException)21 Enumeration (java.util.Enumeration)17 Manifest (java.util.jar.Manifest)16 FileInputStream (java.io.FileInputStream)12 CodeSource (java.security.CodeSource)12 LinkedHashSet (java.util.LinkedHashSet)11 URI (java.net.URI)10 Attributes (java.util.jar.Attributes)10 ZipEntry (java.util.zip.ZipEntry)9 FileNotFoundException (java.io.FileNotFoundException)8