1 21 package com.jaspersoft.jasperserver.api.engine.jasperreports.util; 22 23 import java.io.IOException ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import java.net.URLStreamHandler ; 28 29 import org.apache.commons.collections.ReferenceMap; 30 31 import com.jaspersoft.jasperserver.api.JSException; 32 import com.jaspersoft.jasperserver.api.JSExceptionWrapper; 33 34 38 public class JarURLStreamHandler extends URLStreamHandler { 39 40 private static final String PROTOCOL = "jsjar"; 41 private static final String URL_PREFIX = PROTOCOL + ":"; 42 private static final int URL_PREFIX_LENGHT = URL_PREFIX.length(); 43 private static final String SEPARATOR = "!"; 44 private static final int SEPARATOR_LENGHT = SEPARATOR.length(); 45 46 private final ReferenceMap urlStreams; 47 48 protected void parseURL(URL u, String spec, int start, int limit) { 49 if (!spec.startsWith(URL_PREFIX)) { 50 throw new JSException("Malformed jar URL \"" + spec + "\""); 51 } 52 int sep = spec.indexOf(SEPARATOR); 53 if (sep < 0) { 54 throw new JSException("Malformed jar URL \"" + spec + "\""); 55 } 56 String jar = spec.substring(URL_PREFIX_LENGHT, sep); 57 String res = spec.substring(sep + SEPARATOR_LENGHT, spec.length()); 58 setURL(u, PROTOCOL, null, -1, null, null, jar, null, res); 59 } 60 61 public JarURLStreamHandler() { 62 urlStreams = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD); 63 } 64 65 protected URLConnection openConnection(URL u) throws IOException { 66 JarFileEntry entry = getURLEntry(u); 67 if (entry == null) { 68 throw new JSException("Jar entry not found for URL " + u); 69 } 70 return new JarConnection(u, entry); 71 } 72 73 public URL createURL(JarFileEntry jarEntry) { 74 try { 75 String spec = URL_PREFIX + jarEntry.getJarFile().getName() 76 + SEPARATOR + jarEntry.getEntry().getName(); 77 URL url = new URL (null, spec, this); 78 putURL(url, jarEntry); 79 return url; 80 } catch (MalformedURLException e) { 81 throw new JSExceptionWrapper(e); 82 } 83 } 84 85 public synchronized void putURL(URL u, JarFileEntry jarEntry) { 86 urlStreams.put(u, jarEntry); 87 } 88 89 protected synchronized JarFileEntry getURLEntry(URL u) { 90 return (JarFileEntry) urlStreams.get(u); 91 } 92 93 protected boolean equals(URL u1, URL u2) { 94 return u1 == u2; 95 } 96 97 protected int hashCode(URL u) { 98 return System.identityHashCode(u); 99 } 100 101 protected String toExternalForm(URL u) { 102 return URL_PREFIX + u.getPath() + SEPARATOR + u.getRef(); 103 } 104 } 105 | Popular Tags |