1 package org.codehaus.classworlds.uberjar.protocol.jar;
2
3 /*
4 $Id: Handler.java 115 2005-07-03 15:23:59Z jvanzyl $
5
6 Copyright 2002 (C) The Werken Company. All Rights Reserved.
7
8 Redistribution and use of this software and associated documentation
9 ("Software"), with or without modification, are permitted provided
10 that the following conditions are met:
11
12 1. Redistributions of source code must retain copyright
13 statements and notices. Redistributions must also contain a
14 copy of this document.
15
16 2. Redistributions in binary form must reproduce the
17 above copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
20
21 3. The name "classworlds" must not be used to endorse or promote
22 products derived from this Software without prior written
23 permission of The Werken Company. For written permission,
24 please contact bob@werken.com.
25
26 4. Products derived from this Software may not be called "classworlds"
27 nor may "classworlds" appear in their names without prior written
28 permission of The Werken Company. "classworlds" is a registered
29 trademark of The Werken Company.
30
31 5. Due credit should be given to The Werken Company.
32 (http://classworlds.werken.com/).
33
34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 */
48
49 import org.codehaus.classworlds.uberjar.protocol.jar.NonLockingJarUrlConnection;
50
51 import java.io.IOException;
52 import java.net.URL;
53 import java.net.URLConnection;
54 import java.net.URLStreamHandler;
55
56 /**
57 * This is copied from Classwords 1.1 org.codehaus.classworlds.uberjar.protocol.jar.Handler
58 * so that an additional dependency does not need to be added to plugins. The formatting is left as is to reduce
59 * the diff.
60 */
61 public class NonLockingJarHandler
62 extends URLStreamHandler
63 {
64 // ----------------------------------------------------------------------
65 // Class members
66 // ----------------------------------------------------------------------
67
68 /**
69 * Singleton instance.
70 */
71 private static final NonLockingJarHandler INSTANCE = new NonLockingJarHandler();
72
73 // ----------------------------------------------------------------------
74 // Class methods
75 // ----------------------------------------------------------------------
76
77 /**
78 * Retrieve the singleton instance.
79 *
80 * @return The singleton instance.
81 */
82 public static NonLockingJarHandler getInstance()
83 {
84 return INSTANCE;
85 }
86
87 // ----------------------------------------------------------------------
88 // Constructors
89 // ----------------------------------------------------------------------
90
91
92 /**
93 * Construct.
94 */
95 public NonLockingJarHandler()
96 {
97 // intentionally left blank
98 }
99
100 // ----------------------------------------------------------------------
101 // Instance methods
102 // ----------------------------------------------------------------------
103
104 /**
105 * @see java.net.URLStreamHandler
106 */
107 public URLConnection openConnection( URL url )
108 throws IOException
109 {
110 return new NonLockingJarUrlConnection(url);
111 }
112
113 /**
114 * @see java.net.URLStreamHandler
115 */
116 public void parseURL( URL url,
117 String spec,
118 int start,
119 int limit )
120 {
121 String specPath = spec.substring( start,
122 limit );
123
124 String urlPath = null;
125
126 if ( specPath.charAt( 0 ) == '/' )
127 {
128 urlPath = specPath;
129 }
130 else if ( specPath.charAt( 0 ) == '!' )
131 {
132 String relPath = url.getFile();
133
134 int bangLoc = relPath.lastIndexOf( "!" );
135
136 if ( bangLoc < 0 )
137 {
138 urlPath = relPath + specPath;
139 }
140 else
141 {
142 urlPath = relPath.substring( 0,
143 bangLoc ) + specPath;
144 }
145 }
146 else
147 {
148 String relPath = url.getFile();
149
150 if ( relPath != null )
151 {
152 int lastSlashLoc = relPath.lastIndexOf( "/" );
153
154 if ( lastSlashLoc < 0 )
155 {
156 urlPath = "/" + specPath;
157 }
158 else
159 {
160 urlPath = relPath.substring( 0,
161 lastSlashLoc + 1 ) + specPath;
162 }
163 }
164 else
165 {
166 urlPath = specPath;
167 }
168 }
169
170 setURL( url,
171 "jar",
172 "",
173 0,
174 null,
175 null,
176 urlPath,
177 null,
178 null );
179 }
180 }