urlclasspath.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 922 行 · 第 1/2 页
JAVA
922 行
url = new URL(base, name); } catch (MalformedURLException e) { throw new IllegalArgumentException("name"); } try { if (check) { URLClassPath.check(url); } /* * For a HTTP connection we use the HEAD method to * check if the resource exists. */ URLConnection uc = url.openConnection(); if (uc instanceof HttpURLConnection) { HttpURLConnection hconn = (HttpURLConnection)uc; hconn.setRequestMethod("HEAD"); if (hconn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) { return null; } } else { // our best guess for the other cases InputStream is = url.openStream(); is.close(); } return url; } catch (Exception e) { return null; } } Resource getResource(final String name, boolean check) { final URL url; try { url = new URL(base, name); } catch (MalformedURLException e) { throw new IllegalArgumentException("name"); } final URLConnection uc; try { if (check) { URLClassPath.check(url); } uc = url.openConnection(); InputStream in = uc.getInputStream(); } catch (Exception e) { return null; } return new Resource() { public String getName() { return name; } public URL getURL() { return url; } public URL getCodeSourceURL() { return base; } public InputStream getInputStream() throws IOException { return uc.getInputStream(); } public int getContentLength() throws IOException { return uc.getContentLength(); } }; } /* * Returns the Resource for the specified name, or null if not * found or the caller does not have the permission to get the * resource. */ Resource getResource(final String name) { return getResource(name, true); } /* * Returns the local class path for this loader, or null if none. */ URL[] getClassPath() throws IOException { return null; } } /* * Inner class used to represent a Loader of resources from a JAR URL. */ private static class JarLoader extends Loader { private JarFile jar; private URL csu; private JarIndex index; private URLStreamHandler handler; private HashMap lmap; /* * Creates a new JarLoader for the specified URL referring to * a JAR file. */ JarLoader(URL url, URLStreamHandler jarHandler, HashMap loaderMap) throws IOException { super(new URL("jar", "", -1, url + "!/", jarHandler)); jar = getJarFile(url); index = JarIndex.getJarIndex(jar); csu = url; handler = jarHandler; lmap = loaderMap; if (index != null) { String[] jarfiles = index.getJarFiles(); // Add all the dependent URLs to the lmap so that loaders // will not be created for them by URLClassPath.getLoader(int) // if the same URL occurs later on the main class path. We set // Loader to null here to avoid creating a Loader for each // URL until we actually need to try to load something from them. for(int i = 0; i < jarfiles.length; i++) { try { URL jarURL = new URL(csu, jarfiles[i]); // If a non-null loader already exists, leave it alone. if (!lmap.containsKey(jarURL)) { lmap.put(jarURL, null); } } catch (MalformedURLException e) { continue; } } } } private JarFile getJarFile(URL url) throws IOException { // Optimize case where url refers to a local jar file if ("file".equals(url.getProtocol())) { FileURLMapper p = new FileURLMapper (url); if (!p.exists()) { throw new FileNotFoundException(p.getPath()); } return new JarFile (p.getPath()); } URLConnection uc = getBaseURL().openConnection(); uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION); return ((JarURLConnection)uc).getJarFile(); } /* * Returns the index of this JarLoader if it exists. */ JarIndex getIndex() { return index; } /* * Creates the resource and if the check flag is set to true, checks if * is its okay to return the resource. */ Resource checkResource(final String name, boolean check, final JarEntry entry) { final URL url; try { url = new URL(getBaseURL(), name); if (check) { URLClassPath.check(url); } } catch (MalformedURLException e) { return null; // throw new IllegalArgumentException("name"); } catch (IOException e) { return null; } catch (AccessControlException e) { return null; } return new Resource() { public String getName() { return name; } public URL getURL() { return url; } public URL getCodeSourceURL() { return csu; } public InputStream getInputStream() throws IOException { return jar.getInputStream(entry); } public int getContentLength() { return (int)entry.getSize(); } public Manifest getManifest() throws IOException { return jar.getManifest(); }; public Certificate[] getCertificates() { return entry.getCertificates(); }; }; } /* * Returns true iff atleast one resource in the jar file has the same * package name as that of the specified resource name. */ boolean validIndex(final String name) { String packageName = name; int pos; if((pos = name.lastIndexOf("/")) != -1) { packageName = name.substring(0, pos); } String entryName; ZipEntry entry; Enumeration enum_ = jar.entries(); while (enum_.hasMoreElements()) { entry = (ZipEntry)enum_.nextElement(); entryName = entry.getName(); if((pos = entryName.lastIndexOf("/")) != -1) entryName = entryName.substring(0, pos); if (entryName.equals(packageName)) { return true; } } return false; } /* * Returns the URL for a resource with the specified name */ URL findResource(final String name, boolean check) { Resource rsc = getResource(name, check); if (rsc != null) { return rsc.getURL(); } return null; } /* * Returns the JAR Resource for the specified name. */ Resource getResource(final String name, boolean check) { final JarEntry entry = jar.getJarEntry(name); if (entry != null) return checkResource(name, check, entry); if (index == null) return null; HashSet visited = new HashSet(); return getResource(name, check, visited); } /* * Version of getResource() that tracks the jar files that have been * visited by linking through the index files. This helper method uses * a HashSet to store the URLs of jar files that have been searched and * uses it to avoid going into an infinite loop, looking for a * non-existent resource */ Resource getResource(final String name, boolean check, Set visited) { Resource res; Object[] jarFiles; int count = 0; LinkedList jarFilesList = null; /* If there no jar files in the index that can potential contain * this resource then return immediately. */ if((jarFilesList = index.get(name)) == null) return null; do { jarFiles = jarFilesList.toArray(); int size = jarFilesList.size(); /* loop through the mapped jar file list */ while(count < size) { String jarName = (String)jarFiles[count++]; JarLoader newLoader; final URL url; try{ url = new URL(csu, jarName); if ((newLoader = (JarLoader)lmap.get(url)) == null) { /* no loader has been set up for this jar file * before */ newLoader = (JarLoader) AccessController.doPrivileged( new PrivilegedExceptionAction() { public Object run() throws IOException { return new JarLoader(url, handler, lmap); } }); /* this newly opened jar file has its own index, * merge it into the parent's index, taking into * account the relative path. */ JarIndex newIndex = ((JarLoader)newLoader).getIndex(); if(newIndex != null) { int pos = jarName.lastIndexOf("/"); newIndex.merge(this.index, (pos == -1 ? null : jarName.substring(0, pos + 1))); } /* put it in the global hashtable */ lmap.put(url, newLoader); } } catch (java.security.PrivilegedActionException pae) { continue; } catch (MalformedURLException e) { continue; } /* Note that the addition of the url to the list of visited * jars incorporates a check for presence in the hashmap */ boolean visitedURL = !visited.add(url); if (!visitedURL) { final JarEntry entry = newLoader.jar.getJarEntry(name); if (entry != null) { return newLoader.checkResource(name, check, entry); } /* Verify that at least one other resource with the * same package name as the lookedup resource is * present in the new jar */ if (!newLoader.validIndex(name)) { /* the mapping is wrong */ throw new InvalidJarIndexException("Invalid index"); } } /* If newLoader is the current loader or if it is a * loader that has already been searched or if the new * loader does not have an index then skip it * and move on to the next loader. */ if (visitedURL || newLoader == this || newLoader.getIndex() == null) { continue; } /* Process the index of the new loader */ if((res = newLoader.getResource(name, check, visited)) != null) { return res; } } // Get the list of jar files again as the list could have grown // due to merging of index files. jarFilesList = index.get(name); // If the count is unchanged, we are done. } while(count < jarFilesList.size()); return null; } /* * Returns the JAR file local class path, or null if none. */ URL[] getClassPath() throws IOException { if (index != null) { return null; } parseExtensionsDependencies(); // we probably don't want SharedSecrets in J2ME //if (SharedSecrets.javaUtilJarAccess().jarFileHasClassPathAttribute(jar)) { // Only get manifest when necessary Manifest man = jar.getManifest(); if (man != null) { Attributes attr = man.getMainAttributes(); if (attr != null) { String value = attr.getValue(Name.CLASS_PATH); if (value != null) { return parseClassPath(csu, value); } } } //} return null; } /* * parse the standard extension dependencies */ private void parseExtensionsDependencies() throws IOException { ExtensionDependency.checkExtensionsDependencies(jar); } /* * Parses value of the Class-Path manifest attribute and returns * an array of URLs relative to the specified base URL. */ private URL[] parseClassPath(URL base, String value) throws MalformedURLException { StringTokenizer st = new StringTokenizer(value); URL[] urls = new URL[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) { String path = st.nextToken(); urls[i] = new URL(base, path); i++; } return urls; } } /* * Inner class used to represent a loader of classes and resources * from a file URL that refers to a directory. */ private static class FileLoader extends Loader { private File dir; FileLoader(URL url) throws IOException { super(url); if (!"file".equals(url.getProtocol())) { throw new IllegalArgumentException("url"); } String path = url.getFile().replace('/', File.separatorChar); path = ParseUtil.decode(path); dir = new File(path); } /* * Returns the URL for a resource with the specified name */ URL findResource(final String name, boolean check) { Resource rsc = getResource(name, check); if (rsc != null) { return rsc.getURL(); } return null; } Resource getResource(final String name, boolean check) { final URL url; try { URL normalizedBase = new URL(getBaseURL(), "."); url = new URL(getBaseURL(), name); if (url.getFile().startsWith(normalizedBase.getFile()) == false) { // requested resource had ../..'s in path return null; } if (check) URLClassPath.check(url); final File file = new File(dir, name.replace('/', File.separatorChar)); if (file.exists()) { return new Resource() { public String getName() { return name; }; public URL getURL() { return url; }; public URL getCodeSourceURL() { return getBaseURL(); }; public InputStream getInputStream() throws IOException { return new FileInputStream(file); }; public int getContentLength() throws IOException { return (int)file.length(); }; }; } } catch (Exception e) { return null; } return null; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?