1 22 package org.jboss.net.protocol.njar; 23 24 import java.net.URL ; 25 import java.net.URLConnection ; 26 import java.net.URLStreamHandler ; 27 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 import org.jboss.logging.Logger; 38 39 import org.jboss.util.stream.Streams; 40 import org.jboss.util.ThrowableHandler; 41 42 69 public class Handler 70 extends URLStreamHandler 71 { 72 public static final String PROTOCOL = "njar"; 74 public static final String NJAR_SEPARATOR = "^/"; 75 public static final String JAR_SEPARATOR = "!/"; 76 77 private static final Logger log = Logger.getLogger(Handler.class); 78 79 protected Map savedJars = new HashMap (); 80 81 public URLConnection openConnection(final URL url) 82 throws IOException 83 { 84 String file = url.getFile(); 85 String embeddedURL = file; 86 String jarPath = ""; 87 88 boolean trace = log.isTraceEnabled(); 89 90 int pos = file.lastIndexOf(NJAR_SEPARATOR); 91 if (pos >= 0) 92 { 93 embeddedURL = file.substring(0, pos); 94 if (file.length() > pos + NJAR_SEPARATOR.length()) 95 jarPath = file.substring(pos + NJAR_SEPARATOR.length()); 96 } 97 98 if (embeddedURL.startsWith(PROTOCOL)) 99 { 100 if (trace) log.trace("Opening next nested jar: " + embeddedURL); 101 File tempJar = (File ) savedJars.get(embeddedURL); 102 if (tempJar == null) 103 { 104 URLConnection embededDataConnection = new URL (embeddedURL).openConnection(); 105 if (trace) log.trace("Content length: " + embededDataConnection.getContentLength()); 106 107 InputStream embededData = embededDataConnection.getInputStream(); 108 tempJar = File.createTempFile("nested-", ".jar"); 109 tempJar.deleteOnExit(); 110 111 if (trace) log.trace("temp file location : " + tempJar); 112 OutputStream output = new FileOutputStream (tempJar); 113 114 try { 115 long bytes = Streams.copyb(embededData, output); 117 if (trace) log.trace("copied " + bytes + " bytes"); 118 } 119 finally { 120 Streams.flush(output); 121 122 Streams.close(embededData); 124 Streams.close(output); 125 } 126 127 savedJars.put(embeddedURL, tempJar); 128 } 129 130 String t = tempJar.getCanonicalFile().toURL().toExternalForm(); 131 if (trace) log.trace("file URL : " + t); 132 133 t = "njar:" + t + NJAR_SEPARATOR + jarPath; 134 if (trace) log.trace("Opening saved jar: " + t); 135 136 URL u = new URL (t); 137 if (trace) log.trace("Using URL: " + u); 138 139 return u.openConnection(); 140 } 141 else 142 { 143 if (trace) log.trace("Opening final nested jar: " + embeddedURL); 144 145 URL u = new URL ("jar:" + embeddedURL + JAR_SEPARATOR + jarPath); 146 if (trace) log.trace("Using URL: " + u); 147 148 return u.openConnection(); 149 } 150 } 151 152 public static URL njarToFile(URL url) 153 { 154 if (url.getProtocol().equals(PROTOCOL)) 155 { 156 try 157 { 158 URL dummy=new URL (PROTOCOL+":"+url.toString()+NJAR_SEPARATOR+"dummy.jar"); 161 String tmp=dummy.openConnection().getURL().toString(); 162 tmp=tmp.substring("jar:".length()); 163 tmp=tmp.substring(0, tmp.length()-(JAR_SEPARATOR+"dummy.jar").length()); 164 return new URL (tmp); 165 } 166 catch (Exception ignore) 167 { 168 ThrowableHandler.addWarning(ignore); 169 } 170 } 171 172 return url; 173 } 174 } 175 176 | Popular Tags |