PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/atlassian-plugins-core/src/main/java/org/codehaus/classworlds/uberjar/protocol/jar/NonLockingJarUrlConnection.java

https://bitbucket.org/purewind/atlassian-plugins
Java | 329 lines | 155 code | 51 blank | 123 comment | 24 complexity | 6caf2c5cd7a907a8fe96801aa5369d06 MD5 | raw file
  1. package org.codehaus.classworlds.uberjar.protocol.jar;
  2. /*
  3. $Id: JarUrlConnection.java 78 2004-07-01 13:59:13Z jvanzyl $
  4. Copyright 2002 (C) The Werken Company. All Rights Reserved.
  5. Redistribution and use of this software and associated documentation
  6. ("Software"), with or without modification, are permitted provided
  7. that the following conditions are met:
  8. 1. Redistributions of source code must retain copyright
  9. statements and notices. Redistributions must also contain a
  10. copy of this document.
  11. 2. Redistributions in binary form must reproduce the
  12. above copyright notice, this list of conditions and the
  13. following disclaimer in the documentation and/or other
  14. materials provided with the distribution.
  15. 3. The name "classworlds" must not be used to endorse or promote
  16. products derived from this Software without prior written
  17. permission of The Werken Company. For written permission,
  18. please contact bob@werken.com.
  19. 4. Products derived from this Software may not be called "classworlds"
  20. nor may "classworlds" appear in their names without prior written
  21. permission of The Werken Company. "classworlds" is a registered
  22. trademark of The Werken Company.
  23. 5. Due credit should be given to The Werken Company.
  24. (http://classworlds.werken.com/).
  25. THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
  26. ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
  27. NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  28. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  29. THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  30. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  36. OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. import java.io.IOException;
  39. import java.io.InputStream;
  40. import java.net.JarURLConnection;
  41. import java.net.MalformedURLException;
  42. import java.net.URL;
  43. import java.net.URLDecoder;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. import java.util.StringTokenizer;
  47. import java.util.jar.JarEntry;
  48. import java.util.jar.JarFile;
  49. import java.util.jar.JarInputStream;
  50. import java.util.zip.ZipEntry;
  51. /**
  52. * This is copied from Classwords 1.1 org.codehaus.classworlds.uberjar.protocol.jar.JarURLConnection
  53. * so that an additional dependency does not need to be added to plugins. The formatting is left as is to reduce
  54. * the diff.
  55. * <p/>
  56. * The setupPathedInputStream() method has been modified to improve the speed of resource lookups. It now
  57. * uses a ZipEntry to get random access to entries in the JAR.
  58. * <p/>
  59. * This change removes the ability for this connection class to load resources from JARs nested inside the outer
  60. * JAR. This is not used in atlassian-plugin because the inner JAR loading is handled by
  61. * {@link com.atlassian.plugin.classloader.PluginClassLoader}.
  62. */
  63. public class NonLockingJarUrlConnection
  64. extends JarURLConnection {
  65. // ----------------------------------------------------------------------
  66. // Instance members
  67. // ----------------------------------------------------------------------
  68. /**
  69. * Base resource.
  70. */
  71. private URL baseResource;
  72. /**
  73. * Additional nested segments.
  74. */
  75. private String[] segments;
  76. /**
  77. * Terminal input-stream.
  78. */
  79. private InputStream in;
  80. // ----------------------------------------------------------------------
  81. // Constructors
  82. // ----------------------------------------------------------------------
  83. /**
  84. * Construct.
  85. *
  86. * @param url Target URL of the connections.
  87. * @throws java.io.IOException If an error occurs while attempting to initialize
  88. * the connection.
  89. */
  90. NonLockingJarUrlConnection(URL url)
  91. throws IOException {
  92. super(url = normaliseURL(url));
  93. String baseText = url.getPath();
  94. int bangLoc = baseText.indexOf("!");
  95. String baseResourceText = baseText.substring(0, bangLoc);
  96. String extraText = "";
  97. if (bangLoc <= (baseText.length() - 2)
  98. &&
  99. baseText.charAt(bangLoc + 1) == '/') {
  100. if (bangLoc + 2 == baseText.length()) {
  101. extraText = "";
  102. } else {
  103. extraText = baseText.substring(bangLoc + 1);
  104. }
  105. } else {
  106. throw new MalformedURLException("No !/ in url: " + url.toExternalForm());
  107. }
  108. List segments = new ArrayList();
  109. StringTokenizer tokens = new StringTokenizer(extraText, "!");
  110. while (tokens.hasMoreTokens()) {
  111. segments.add(tokens.nextToken());
  112. }
  113. this.segments = (String[]) segments.toArray(new String[segments.size()]);
  114. this.baseResource = new URL(baseResourceText);
  115. }
  116. protected static URL normaliseURL(URL url) throws MalformedURLException {
  117. String text = normalizeUrlPath(url.toString());
  118. if (!text.startsWith("jar:")) {
  119. text = "jar:" + text;
  120. }
  121. if (text.indexOf('!') < 0) {
  122. text = text + "!/";
  123. }
  124. return new URL(text);
  125. }
  126. // ----------------------------------------------------------------------
  127. // Instance methods
  128. // ----------------------------------------------------------------------
  129. /**
  130. * Retrieve the nesting path segments.
  131. *
  132. * @return The segments.
  133. */
  134. protected String[] getSegments() {
  135. return this.segments;
  136. }
  137. /**
  138. * Retrieve the base resource <code>URL</code>.
  139. *
  140. * @return The base resource url.
  141. */
  142. protected URL getBaseResource() {
  143. return this.baseResource;
  144. }
  145. /**
  146. * @see java.net.URLConnection
  147. */
  148. public void connect()
  149. throws IOException {
  150. if (this.segments.length == 0) {
  151. setupBaseResourceInputStream();
  152. } else {
  153. setupPathedInputStream();
  154. }
  155. }
  156. /**
  157. * Setup the <code>InputStream</code> purely from the base resource.
  158. *
  159. * @throws java.io.IOException If an I/O error occurs.
  160. */
  161. protected void setupBaseResourceInputStream()
  162. throws IOException {
  163. this.in = getBaseResource().openStream();
  164. }
  165. /**
  166. * Setup the <code>InputStream</code> for URL with nested segments.
  167. *
  168. * @throws java.io.IOException If an I/O error occurs.
  169. */
  170. protected void setupPathedInputStream()
  171. throws IOException {
  172. final JarFile jar = getJarFile();
  173. String entryName = segments[0].substring(1); // remove leading slash
  174. final ZipEntry zipEntry = jar.getEntry(entryName);
  175. if (zipEntry == null) {
  176. throw new IOException("Unable to locate entry: " + entryName + ", in JAR file: " + jar.getName());
  177. }
  178. final InputStream delegate = jar.getInputStream(zipEntry);
  179. this.in = new InputStream() {
  180. public int read() throws IOException {
  181. return delegate.read();
  182. }
  183. public int read(byte b[]) throws IOException {
  184. return delegate.read(b);
  185. }
  186. public int read(byte b[], int off, int len) throws IOException {
  187. return delegate.read(b, off, len);
  188. }
  189. public long skip(long n) throws IOException {
  190. return delegate.skip(n);
  191. }
  192. public int available() throws IOException {
  193. return delegate.available();
  194. }
  195. public void close() throws IOException {
  196. // close the stream and the plugin JAR file
  197. delegate.close();
  198. jar.close();
  199. }
  200. public synchronized void mark(int readlimit) {
  201. delegate.mark(readlimit);
  202. }
  203. public synchronized void reset() throws IOException {
  204. delegate.reset();
  205. }
  206. public boolean markSupported() {
  207. return delegate.markSupported();
  208. }
  209. };
  210. }
  211. /**
  212. * Retrieve the <code>InputStream</code> for the nesting
  213. * segment relative to a base <code>InputStream</code>.
  214. *
  215. * @param baseIn The base input-stream.
  216. * @param segment The nesting segment path.
  217. * @return The input-stream to the segment.
  218. * @throws java.io.IOException If an I/O error occurs.
  219. */
  220. protected InputStream getSegmentInputStream(InputStream baseIn,
  221. String segment)
  222. throws IOException {
  223. JarInputStream jarIn = new JarInputStream(baseIn);
  224. JarEntry entry = null;
  225. while (jarIn.available() != 0) {
  226. entry = jarIn.getNextJarEntry();
  227. if (entry == null) {
  228. break;
  229. }
  230. if (("/" + entry.getName()).equals(segment)) {
  231. return jarIn;
  232. }
  233. }
  234. throw new IOException("unable to locate segment: " + segment);
  235. }
  236. /**
  237. * @see java.net.URLConnection
  238. */
  239. public InputStream getInputStream()
  240. throws IOException {
  241. if (this.in == null) {
  242. connect();
  243. }
  244. return this.in;
  245. }
  246. /**
  247. * @return JarFile
  248. * @throws java.io.IOException
  249. * @see java.net.JarURLConnection#getJarFile()
  250. */
  251. public JarFile getJarFile() throws IOException {
  252. String url = baseResource.toExternalForm();
  253. if (url.startsWith("file:")) {
  254. url = url.substring(5);
  255. }
  256. return new JarFile(URLDecoder.decode(url, "UTF-8"));
  257. }
  258. private static String normalizeUrlPath(String name) {
  259. if (name.startsWith("/")) {
  260. name = name.substring(1);
  261. }
  262. int i = name.indexOf("/..");
  263. if (i > 0) {
  264. int j = name.lastIndexOf("/", i - 1);
  265. name = name.substring(0, j) + name.substring(i + 3);
  266. }
  267. return name;
  268. }
  269. }