You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/12/29 22:39:41 UTC
svn commit: r491110 [2/2] - in
/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni:
internal/net/www/ internal/net/www/protocol/file/
internal/net/www/protocol/ftp/ internal/net/www/protocol/http/
internal/net/www/protoc...
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java?view=diff&rev=491110&r1=491109&r2=491110
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java Fri Dec 29 13:39:40 2006
@@ -17,18 +17,20 @@
package org.apache.harmony.luni.internal.net.www.protocol.jar;
-
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.Serializable;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
+import java.net.ContentHandler;
+import java.net.ContentHandlerFactory;
+import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
+import java.security.Permission;
import java.security.PrivilegedAction;
import java.util.Comparator;
import java.util.Date;
@@ -39,9 +41,9 @@
import java.util.jar.JarFile;
import java.util.zip.ZipFile;
+import org.apache.harmony.kernel.vm.VM;
import org.apache.harmony.luni.util.Msg;
import org.apache.harmony.luni.util.Util;
-import org.apache.harmony.kernel.vm.VM;
/**
* This subclass extends <code>URLConnection</code>.
@@ -49,363 +51,407 @@
*
* This class is responsible for connecting and retrieving resources from a Jar
* file which can be anywhere that can be refered to by an URL.
- *
*/
public class JarURLConnection extends java.net.JarURLConnection {
- static Hashtable<String,CacheEntry<? extends JarFile>> jarCache = new Hashtable<String,CacheEntry<?>>();
- InputStream jarInput;
+ static Hashtable<String, CacheEntry<? extends JarFile>> jarCache = new Hashtable<String, CacheEntry<?>>();
- private JarFile jarFile;
+ InputStream jarInput;
- private JarEntry jarEntry;
+ private JarFile jarFile;
- ReferenceQueue<JarFile> cacheQueue = new ReferenceQueue<JarFile>();
+ private JarEntry jarEntry;
- static TreeSet<LRUKey> lru = new TreeSet<LRUKey>(new LRUComparitor<LRUKey>());
+ ReferenceQueue<JarFile> cacheQueue = new ReferenceQueue<JarFile>();
- static int Limit;
- static {
- Limit = AccessController.doPrivileged(new PrivilegedAction<Integer>() {
- public Integer run() {
- return Integer.getInteger("jar.cacheSize", 500); //$NON-NLS-1$
- }
- });
- VM.closeJars();
- }
+ static TreeSet<LRUKey> lru = new TreeSet<LRUKey>(
+ new LRUComparator<LRUKey>());
- static final class CacheEntry<T extends JarFile> extends WeakReference<T>{
- Object key;
+ static int Limit;
+
+ static {
+ Limit = AccessController.doPrivileged(new PrivilegedAction<Integer>() {
+ public Integer run() {
+ return Integer.getInteger("jar.cacheSize", 500); //$NON-NLS-1$
+ }
+ });
+ VM.closeJars();
+ }
+
+ static final class CacheEntry<T extends JarFile> extends WeakReference<T> {
+ Object key;
CacheEntry(T jar, String key, ReferenceQueue<JarFile> queue) {
- super(jar, queue);
- this.key = key;
- }
- }
-
- static final class LRUKey {
- JarFile jar;
-
- long ts;
-
- LRUKey(JarFile file, long time) {
- jar = file;
- ts = time;
- }
-
- /**
- * @see java.lang.Object#equals(java.lang.Object)
- */
- public boolean equals(Object obj) {
- return obj instanceof LRUKey && jar == ((LRUKey) obj).jar;
- }
-
+ super(jar, queue);
+ this.key = key;
+ }
+ }
+
+ static final class LRUKey {
+ JarFile jar;
+
+ long ts;
+
+ LRUKey(JarFile file, long time) {
+ jar = file;
+ ts = time;
+ }
+
+ /**
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return (obj instanceof LRUKey) &&
+ (jar == ((LRUKey) obj).jar);
+ }
+
+ @Override
public int hashCode() {
return jar.hashCode();
}
- }
+ }
+
+ static final class LRUComparator<T> implements Comparator<LRUKey> {
+
+ LRUComparator() {
+ }
+
+ /**
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ public int compare(LRUKey o1, LRUKey o2) {
+ if ((o1).ts > (o2).ts) {
+ return 1;
+ }
+ return (o1).ts == (o2).ts ? 0 : -1;
+ }
+
+ /**
+ * @param o1
+ * an object to compare
+ * @param o2
+ * an object to compare
+ * @return <code>true</code> if the objects are equal,
+ * <code>false</code> otherwise.
+ */
+ public boolean equals(Object o1, Object o2) {
+ return o1.equals(o2);
+ }
+ }
+
+ /**
+ * @param url
+ * the URL of the JAR
+ * @throws java.net.MalformedURLException
+ * if the URL is malformed
+ */
+ public JarURLConnection(java.net.URL url) throws MalformedURLException {
+ super(url);
+ }
+
+ /**
+ * @see java.net.URLConnection#connect()
+ */
+ @Override
+ public void connect() throws IOException {
+ jarFileURLConnection = getJarFileURL().openConnection();
+ findJarFile(); // ensure the file can be found
+ findJarEntry(); // ensure the entry, if any, can be found
+ connected = true;
+ }
+
+ /**
+ * Answers the Jar file refered by this <code>URLConnection</code>
+ *
+ * @return the JAR file referenced by this connection
+ *
+ * @throws IOException
+ * thrown if an IO error occurs while connecting to the
+ * resource.
+ */
+ @Override
+ public JarFile getJarFile() throws IOException {
+ if (!connected) {
+ connect();
+ }
+ return jarFile;
+ }
+
+ /**
+ * Answers the Jar file refered by this <code>URLConnection</code>
+ *
+ * @throws IOException
+ * if an IO error occurs while connecting to the resource.
+ */
+ private void findJarFile() throws IOException {
+ URL jarFileURL = getJarFileURL();
+ if (jarFileURL.getProtocol().equals("file")) { //$NON-NLS-1$
+ String fileName = jarFileURL.getFile();
+ String host = jarFileURL.getHost();
+ if (host != null && host.length() > 0) {
+ fileName = "//" + host + fileName; //$NON-NLS-1$
+ }
+ jarFile = openJarFile(fileName, fileName, false);
+ return;
+ }
+
+ final String externalForm = jarFileURLConnection.getURL()
+ .toExternalForm();
+ jarFile = AccessController
+ .doPrivileged(new PrivilegedAction<JarFile>() {
+ public JarFile run() {
+ try {
+ return openJarFile(null, externalForm, false);
+ } catch (IOException e) {
+ return null;
+ }
+ }
+ });
+ if (jarFile != null) {
+ return;
+ }
+
+ // Build a temp jar file
+ final InputStream is = jarFileURLConnection.getInputStream();
+ try {
+ jarFile = AccessController
+ .doPrivileged(new PrivilegedAction<JarFile>() {
+ public JarFile run() {
+ try {
+ File tempJar = File.createTempFile("hyjar_", //$NON-NLS-1$
+ ".tmp", null); //$NON-NLS-1$
+ FileOutputStream fos = new FileOutputStream(
+ tempJar);
+ byte[] buf = new byte[4096];
+ int nbytes = 0;
+ while ((nbytes = is.read(buf)) > -1) {
+ fos.write(buf, 0, nbytes);
+ }
+ fos.close();
+ String path = tempJar.getPath();
+ return openJarFile(path, externalForm, true);
+ } catch (IOException e) {
+ return null;
+ }
+ }
+ });
+ } finally {
+ is.close();
+ }
+ if (jarFile == null) {
+ throw new IOException();
+ }
+ }
- static final class LRUComparitor<T> implements Comparator<LRUKey>, Serializable {
- LRUComparitor() {
- }
-
- /**
- * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
- */
- public int compare(LRUKey o1, LRUKey o2) {
- if (((LRUKey) o1).ts > ((LRUKey) o2).ts) {
- return 1;
- }
- return ((LRUKey) o1).ts == ((LRUKey) o2).ts ? 0 : -1;
- }
-
- /**
- * @param o1
- * an object to compare
- * @param o2
- * an object to compare
- * @return <code>true</code> if the objects are equal,
- * <code>false</code> otherwise.
- */
- public boolean equals(Object o1, Object o2) {
- return o1.equals(o2);
- }
- }
-
- /**
- * @param url
- * the URL of the JAR
- * @throws java.net.MalformedURLException
- * if the URL is malformed
- */
- public JarURLConnection(java.net.URL url)
- throws java.net.MalformedURLException {
- super(url);
- }
-
- /**
- * @see java.net.URLConnection#connect()
- */
- public void connect() throws IOException {
- jarFileURLConnection = getJarFileURL().openConnection();
- findJarFile(); // ensure the file can be found
- findJarEntry(); // ensure the entry, if any, can be found
- connected = true;
- }
-
- /**
- * Answers the Jar file refered by this <code>URLConnection</code>
- *
- * @return the JAR file referenced by this connection
- *
- * @throws IOException
- * thrown if an IO error occurs while connecting to the
- * resource.
- */
- public JarFile getJarFile() throws IOException {
- if (!connected)
- connect();
- return jarFile;
- }
-
- /**
- * Answers the Jar file refered by this <code>URLConnection</code>
- *
- * @throws IOException
- * if an IO error occurs while connecting to the resource.
- */
- private void findJarFile() throws IOException {
- URL jarFileURL = getJarFileURL();
- if (jarFileURL.getProtocol().equals("file")) { //$NON-NLS-1$
- String fileName = jarFileURL.getFile();
- String host = jarFileURL.getHost();
- if (host != null && host.length() > 0)
- fileName = "//" + host + fileName; //$NON-NLS-1$
- jarFile = openJarFile(fileName, fileName, false);
- return;
- }
-
- final String externalForm = jarFileURLConnection.getURL()
- .toExternalForm();
- jarFile = AccessController.doPrivileged(new PrivilegedAction<JarFile>() {
- public JarFile run() {
- try {
- return openJarFile(null, externalForm, false);
- } catch (IOException e) {
- return null;
- }
- }
- });
- if (jarFile != null)
- return;
-
- // Build a temp jar file
- final InputStream is = jarFileURLConnection.getInputStream();
- try {
- jarFile = AccessController.doPrivileged(new PrivilegedAction<JarFile>() {
- public JarFile run() {
- try {
- File tempJar = File.createTempFile("hyjar_", //$NON-NLS-1$
- ".tmp", null); //$NON-NLS-1$
- FileOutputStream fos = new FileOutputStream(
- tempJar);
- byte[] buf = new byte[4096];
- int nbytes = 0;
- while ((nbytes = is.read(buf)) > -1)
- fos.write(buf, 0, nbytes);
- fos.close();
- String path = tempJar.getPath();
- return openJarFile(path, externalForm, true);
- } catch (IOException e) {
- return null;
- }
- }
- });
- } finally {
- is.close();
- }
- if (jarFile == null)
- throw new IOException();
- }
-
- JarFile openJarFile(String fileString, String key, boolean temp)
- throws IOException {
-
- JarFile jar = null;
- if(useCaches){
- CacheEntry<? extends JarFile> entry;
- while ((entry = (CacheEntry<? extends JarFile>)cacheQueue.poll()) != null)
+ JarFile openJarFile(String fileString, String key, boolean temp)
+ throws IOException {
+
+ JarFile jar = null;
+ if (useCaches) {
+ CacheEntry<? extends JarFile> entry;
+ while ((entry = (CacheEntry<? extends JarFile>) cacheQueue.poll()) != null) {
jarCache.remove(entry.key);
+ }
entry = jarCache.get(key);
- if (entry != null)
- jar = (JarFile) entry.get();
+ if (entry != null) {
+ jar = entry.get();
+ }
if (jar == null && fileString != null) {
int flags = ZipFile.OPEN_READ
+ (temp ? ZipFile.OPEN_DELETE : 0);
jar = new JarFile(new File(Util.decode(fileString, false)),
true, flags);
- jarCache.put(key, new CacheEntry<JarFile>(jar, key, cacheQueue));
+ jarCache
+ .put(key, new CacheEntry<JarFile>(jar, key, cacheQueue));
} else {
SecurityManager security = System.getSecurityManager();
- if (security != null)
+ if (security != null) {
security.checkPermission(getPermission());
- if (temp)
+ }
+ if (temp) {
lru.remove(new LRUKey(jar, 0));
+ }
+ }
+ } else if (fileString != null) {
+ int flags = ZipFile.OPEN_READ + (temp ? ZipFile.OPEN_DELETE : 0);
+ jar = new JarFile(new File(Util.decode(fileString, false)), true,
+ flags);
+ }
+
+ if (temp) {
+ lru.add(new LRUKey(jar, new Date().getTime()));
+ if (lru.size() > Limit) {
+ lru.remove(lru.first());
+ }
+ }
+ return jar;
+ }
+
+ /**
+ * Answers the JarEntry of the entry referenced by this
+ * <code>URLConnection</code>.
+ *
+ * @return java.util.jar.JarEntry the JarEntry referenced
+ *
+ * @throws IOException
+ * if an IO error occurs while getting the entry
+ */
+ @Override
+ public JarEntry getJarEntry() throws IOException {
+ if (!connected) {
+ connect();
+ }
+ return jarEntry;
+
+ }
+
+ /**
+ * Look up the JarEntry of the entry referenced by this
+ * <code>URLConnection</code>.
+ */
+ private void findJarEntry() throws IOException {
+ if (getEntryName() == null) {
+ return;
+ }
+ jarEntry = jarFile.getJarEntry(getEntryName());
+ if (jarEntry == null) {
+ throw new FileNotFoundException(getEntryName());
+ }
+ }
+
+ /**
+ * Creates an input stream for reading from this URL Connection.
+ *
+ * @return the input stream
+ *
+ * @throws IOException
+ * if an IO error occurs while connecting to the resource.
+ */
+ @Override
+ public InputStream getInputStream() throws IOException {
+ if (!connected) {
+ connect();
+ }
+ if (jarInput != null) {
+ return jarInput;
+ }
+ if (jarEntry == null) {
+ throw new IOException(Msg.getString("K00fc")); //$NON-NLS-1$
+ }
+ return jarInput = new JarURLConnectionInputStream(jarFile
+ .getInputStream(jarEntry), jarFile);
+ }
+
+ /**
+ * Answers the content type of the resource. Test cases reveal that only if
+ * the URL is refering to a Jar file, that this method answers a non-null
+ * value - x-java/jar.
+ *
+ * @return the content type
+ */
+ @Override
+ public String getContentType() {
+ // it could also return "x-java/jar" which jdk returns but here, we get
+ // it from the URLConnection
+ try {
+ if (url.getFile().endsWith("!/")) { //$NON-NLS-1$
+ return getJarFileURL().openConnection().getContentType();
+ }
+ } catch (IOException ioe) {
+ // Ignore
+ }
+ // if there is an Jar Entry, get the content type from the name
+ return guessContentTypeFromName(url.getFile());
+ }
+
+ /**
+ * Answers the content length of the resource. Test cases reveal that if the
+ * URL is refering to a Jar file, this method answers a content-length
+ * returned by URLConnection. If not, it will return -1.
+ *
+ * @return the content length
+ */
+ @Override
+ public int getContentLength() {
+ try {
+ if (url.getFile().endsWith("!/")) { //$NON-NLS-1$
+ return getJarFileURL().openConnection().getContentLength();
+ }
+ } catch (IOException e) {
+ //Ignored
+ }
+ return -1;
+ }
+
+ /**
+ * Answers the object pointed by this <code>URL</code>. If this
+ * URLConnection is pointing to a Jar File (no Jar Entry), this method will
+ * return a <code>JarFile</code> If there is a Jar Entry, it will return
+ * the object corresponding to the Jar entry content type.
+ *
+ * @return a non-null object
+ *
+ * @throws IOException
+ * if an IO error occured
+ *
+ * @see ContentHandler
+ * @see ContentHandlerFactory
+ * @see java.io.IOException
+ * @see #setContentHandlerFactory(ContentHandlerFactory)
+ */
+ @Override
+ public Object getContent() throws IOException {
+ if (!connected) {
+ connect();
+ }
+ // if there is no Jar Entry, return a JarFile
+ if (jarEntry == null) {
+ return jarFile;
+ }
+ return super.getContent();
+ }
+
+ /**
+ * Answers the permission, in this case the subclass, FilePermission object
+ * which represents the permission necessary for this URLConnection to
+ * establish the connection.
+ *
+ * @return the permission required for this URLConnection.
+ *
+ * @throws IOException
+ * thrown when an IO exception occurs while creating the
+ * permission.
+ */
+ @Override
+ public Permission getPermission() throws IOException {
+ if (jarFileURLConnection != null) {
+ return jarFileURLConnection.getPermission();
+ }
+ return getJarFileURL().openConnection().getPermission();
+ }
+
+ /**
+ * Closes the cached files.
+ */
+ public static void closeCachedFiles() {
+ Enumeration<CacheEntry<? extends JarFile>> elemEnum = jarCache
+ .elements();
+ while (elemEnum.hasMoreElements()) {
+ try {
+ ZipFile zip = elemEnum.nextElement().get();
+ if (zip != null) {
+ zip.close();
+ }
+ } catch (IOException e) {
+ // Ignored
}
- }else if(fileString != null){
- int flags = ZipFile.OPEN_READ + (temp ? ZipFile.OPEN_DELETE : 0);
- jar = new JarFile(new File(Util.decode(fileString, false)), true, flags);
- }
-
- if (temp) {
- lru.add(new LRUKey(jar, new Date().getTime()));
- if (lru.size() > Limit)
- lru.remove(lru.first());
- }
- return jar;
- }
-
- /**
- * Answers the JarEntry of the entry referenced by this
- * <code>URLConnection</code>.
- *
- * @return java.util.jar.JarEntry the JarEntry referenced
- *
- * @throws IOException
- * if an IO error occurs while getting the entry
- */
- public JarEntry getJarEntry() throws IOException {
- if (!connected)
- connect();
- return jarEntry;
-
- }
-
- /**
- * Look up the JarEntry of the entry referenced by this
- * <code>URLConnection</code>.
- */
- private void findJarEntry() throws IOException {
- if (getEntryName() == null)
- return;
- jarEntry = jarFile.getJarEntry(getEntryName());
- if (jarEntry == null)
- throw new FileNotFoundException(getEntryName());
- }
-
- /**
- * Creates an input stream for reading from this URL Connection.
- *
- * @return the input stream
- *
- * @throws IOException
- * if an IO error occurs while connecting to the resource.
- */
- public InputStream getInputStream() throws IOException {
- if (!connected)
- connect();
- if (jarInput != null)
- return jarInput;
- if (jarEntry == null)
- throw new IOException(Msg.getString("K00fc")); //$NON-NLS-1$
- return jarInput = new JarURLConnectionInputStream(jarFile.getInputStream(jarEntry),jarFile);
- }
-
- /**
- * Answers the content type of the resource. Test cases reveal that only if
- * the URL is refering to a Jar file, that this method answers a non-null
- * value - x-java/jar.
- *
- * @return the content type
- */
- public String getContentType() {
- // it could also return "x-java/jar" which jdk returns but here, we get
- // it from the URLConnection
- try {
- if (url.getFile().endsWith("!/")) //$NON-NLS-1$
- return getJarFileURL().openConnection().getContentType();
- } catch (IOException ioe) {
- }
- // if there is an Jar Entry, get the content type from the name
- return guessContentTypeFromName(url.getFile());
- }
-
- /**
- * Answers the content length of the resource. Test cases reveal that if the
- * URL is refering to a Jar file, this method answers a content-length
- * returned by URLConnection. If not, it will return -1.
- *
- * @return the content length
- */
- public int getContentLength() {
- try {
- if (url.getFile().endsWith("!/")) //$NON-NLS-1$
- return getJarFileURL().openConnection().getContentLength();
- } catch (IOException e) {
- }
- return -1;
- }
-
- /**
- * Answers the object pointed by this <code>URL</code>. If this
- * URLConnection is pointing to a Jar File (no Jar Entry), this method will
- * return a <code>JarFile</code> If there is a Jar Entry, it will return
- * the object corresponding to the Jar entry content type.
- *
- * @return a non-null object
- *
- * @throws IOException
- * if an IO error occured
- *
- * @see ContentHandler
- * @see ContentHandlerFactory
- * @see java.io.IOException
- * @see #setContentHandlerFactory(ContentHandlerFactory)
- */
- public Object getContent() throws IOException {
- if (!connected)
- connect();
- // if there is no Jar Entry, return a JarFile
- if (jarEntry == null)
- return jarFile;
- return super.getContent();
- }
-
- /**
- * Answers the permission, in this case the subclass, FilePermission object
- * which represents the permission necessary for this URLConnection to
- * establish the connection.
- *
- * @return the permission required for this URLConnection.
- *
- * @throws IOException
- * thrown when an IO exception occurs while creating the
- * permission.
- */
- public java.security.Permission getPermission() throws IOException {
- if (jarFileURLConnection != null)
- return jarFileURLConnection.getPermission();
- return getJarFileURL().openConnection().getPermission();
- }
-
- /**
- * Closes the cached files.
- */
- public static void closeCachedFiles() {
- Enumeration<CacheEntry<? extends JarFile>> elemEnum = jarCache.elements();
- while (elemEnum.hasMoreElements()) {
- try {
- ZipFile zip = (ZipFile) ((CacheEntry) elemEnum.nextElement())
- .get();
- if (zip != null)
- zip.close();
- } catch (IOException e) {
- }
- }
- }
-
+ }
+ }
+
private class JarURLConnectionInputStream extends FilterInputStream {
InputStream inputStream;
+
JarFile jarFile;
protected JarURLConnectionInputStream(InputStream in, JarFile file) {
@@ -414,6 +460,7 @@
jarFile = file;
}
+ @Override
public void close() throws IOException {
super.close();
if (!useCaches) {
@@ -421,15 +468,18 @@
jarFile.close();
}
}
-
+
+ @Override
public int read() throws IOException {
return inputStream.read();
}
+ @Override
public int read(byte[] buf, int off, int nbytes) throws IOException {
return inputStream.read(buf, off, nbytes);
}
+ @Override
public long skip(long nbytes) throws IOException {
return inputStream.skip(nbytes);
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties?view=diff&rev=491110&r1=491109&r2=491110
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties Fri Dec 29 13:39:40 2006
@@ -306,6 +306,8 @@
KA011=Malformed reply from SOCKS server
KA012=No such file or directory
KA013=Number of bytes to skip cannot be negative
-KA014=Invalit UUID string
+KA014=Invalid UUID string
KA015=Incompatible class (base name)\: {0} but expected {1}
-
+KA016=Received authentication challenge is null
+KA017=Received HTTP_PROXY_AUTH (407) code while not using proxy
+KA018=Received authentication challenge is null