1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.jjar; |
18 | |
|
19 | |
import java.lang.Package; |
20 | |
|
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.Collection; |
24 | |
import java.util.Map; |
25 | |
import java.util.List; |
26 | |
import java.util.StringTokenizer; |
27 | |
import java.util.Enumeration; |
28 | |
|
29 | |
import java.util.jar.JarFile; |
30 | |
import java.util.jar.Manifest; |
31 | |
import java.util.jar.Attributes; |
32 | |
import java.util.jar.JarInputStream; |
33 | |
import java.util.jar.JarOutputStream; |
34 | |
import java.util.jar.JarEntry; |
35 | |
|
36 | |
import java.io.File; |
37 | |
import java.io.FileOutputStream; |
38 | |
import java.io.BufferedInputStream; |
39 | |
import java.io.BufferedOutputStream; |
40 | |
import java.io.InputStream; |
41 | |
import java.io.ByteArrayOutputStream; |
42 | |
|
43 | |
import java.net.JarURLConnection; |
44 | |
import java.net.URLConnection; |
45 | |
import java.net.URL; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | public class ClasspathUtil |
56 | |
{ |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public static String getDefaultRepository() |
62 | |
{ |
63 | 0 | return "http://jakarta.apache.org/jjar/"; |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public static void listJars( String path ) |
70 | |
{ |
71 | 0 | List jars = getJarList( path ); |
72 | |
|
73 | 0 | Iterator i = jars.iterator(); |
74 | |
|
75 | 0 | while( i.hasNext() ) |
76 | |
{ |
77 | 0 | String jar = (String) i.next(); |
78 | 0 | System.out.println(" inspecting : " + jar ); |
79 | 0 | } |
80 | |
|
81 | 0 | return; |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public static boolean containsVersionedJar( String path, String pkg, String version) |
89 | |
{ |
90 | 0 | List jars = getJarList( path ); |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 0 | Iterator i = jars.iterator(); |
98 | |
|
99 | 0 | while( i.hasNext() ) |
100 | |
{ |
101 | 0 | String jar = (String) i.next(); |
102 | |
|
103 | |
try |
104 | |
{ |
105 | 0 | URL url = new URL("jar:file:" + jar + "!/"); |
106 | 0 | JarURLConnection conn = (JarURLConnection) url.openConnection(); |
107 | |
|
108 | 0 | if (packageMatch( conn, pkg, version ) ) |
109 | |
{ |
110 | |
|
111 | |
|
112 | 0 | return true; |
113 | |
} |
114 | |
} |
115 | 0 | catch( Exception e ) |
116 | |
{ |
117 | |
|
118 | 0 | } |
119 | 0 | } |
120 | |
|
121 | 0 | return false; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public static JJARPackage getPackage( String jarname ) |
128 | |
throws Exception |
129 | |
{ |
130 | 0 | URL url = new URL("jar:file:" + jarname + "!/"); |
131 | 0 | JarURLConnection conn = (JarURLConnection) url.openConnection(); |
132 | |
|
133 | 0 | return getPackage( conn ); |
134 | |
} |
135 | |
|
136 | |
public static JJARPackage getPackage( JarURLConnection jarConn ) |
137 | |
throws Exception |
138 | |
{ |
139 | 0 | if (jarConn == null) |
140 | 0 | return null; |
141 | |
|
142 | 0 | Manifest mfst = jarConn.getManifest(); |
143 | |
|
144 | 0 | if (mfst == null) |
145 | |
{ |
146 | 0 | return null; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | 0 | Attributes att = mfst.getMainAttributes(); |
154 | |
|
155 | 0 | String jarpkg = att.getValue( Attributes.Name.IMPLEMENTATION_TITLE ); |
156 | 0 | String jarver = att.getValue( Attributes.Name.IMPLEMENTATION_VERSION ); |
157 | |
|
158 | 0 | if (jarpkg == null || jarver == null) |
159 | |
{ |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | 0 | return null; |
165 | |
} |
166 | |
|
167 | 0 | return new JJARPackage( jarpkg, jarver ); |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public static boolean packageMatch( JarURLConnection jarConn, String pkg, String version ) |
174 | |
throws Exception |
175 | |
{ |
176 | 0 | JJARPackage jjarp = getPackage( jarConn ); |
177 | |
|
178 | 0 | if ( jjarp == null) |
179 | 0 | return false; |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | 0 | return jjarp.equals( pkg, version ); |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public static List getJarList( String path ) |
194 | |
{ |
195 | 0 | if (path == null) |
196 | |
{ |
197 | 0 | path = "java.class.path"; |
198 | |
} |
199 | |
|
200 | 0 | String classpath = System.getProperty( path ); |
201 | 0 | String pathSep = System.getProperty("path.separator"); |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | 0 | StringTokenizer st = new StringTokenizer( classpath, pathSep ); |
208 | |
|
209 | 0 | ArrayList jars = new ArrayList(); |
210 | |
|
211 | 0 | while( st.hasMoreTokens() ) |
212 | |
{ |
213 | 0 | String foo = st.nextToken(); |
214 | |
|
215 | 0 | if ( foo.indexOf("jar") != -1 ) |
216 | 0 | jars.add(foo); |
217 | 0 | } |
218 | |
|
219 | 0 | return jars; |
220 | |
} |
221 | |
|
222 | |
public static void main( String args[] ) |
223 | |
{ |
224 | |
|
225 | |
|
226 | |
|
227 | 0 | if ( ClasspathUtil.containsVersionedJar( null, "commons-beanutils", "0.1") ) |
228 | 0 | System.out.println("Found commons-beanutils v0.1"); |
229 | |
else |
230 | |
{ |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
try |
236 | |
{ |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | 0 | URL url = new URL("jar:file:/home/gmj/jakarta/jakarta-commons/beanutils/dist/commons-beanutils.jar!/"); |
242 | 0 | JarURLConnection conn = (JarURLConnection) url.openConnection(); |
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | 0 | if (!packageMatch( conn, "commons-beanutils", "0.1" )) |
249 | |
{ |
250 | 0 | System.out.println("repository doesn't have it"); |
251 | 0 | return; |
252 | |
} |
253 | |
|
254 | 0 | System.out.println("jar in repository correct. Fetching"); |
255 | |
|
256 | 0 | String jar = "commons-beanutils-0.1.jar"; |
257 | |
|
258 | 0 | url = new URL("file:/home/gmj/jakarta/jakarta-commons/beanutils/dist/commons-beanutils.jar"); |
259 | 0 | URLConnection uconn = (URLConnection) url.openConnection(); |
260 | |
|
261 | 0 | Transport.fetchJar( uconn, jar ); |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | 0 | String classpath = System.getProperty( "java.class.path" ); |
268 | 0 | String pathSep = System.getProperty("path.separator"); |
269 | |
|
270 | 0 | classpath = classpath + pathSep + jar; |
271 | |
|
272 | 0 | System.setProperty("java.class.path", classpath); |
273 | |
|
274 | 0 | if ( ClasspathUtil.containsVersionedJar( null, "commons-beanutils", "0.1") ) |
275 | 0 | System.out.println("Found commons-beanutils v0.1"); |
276 | |
|
277 | |
} |
278 | 0 | catch( Exception e ) |
279 | |
{ |
280 | 0 | System.out.println("exception " + e ); |
281 | 0 | } |
282 | |
} |
283 | |
|
284 | 0 | } |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
public static boolean markJar( String jarname, String packagename, String version ) |
290 | |
throws Exception |
291 | |
{ |
292 | 0 | JarFile jf = new JarFile( jarname ); |
293 | |
|
294 | 0 | Manifest mfst = jf.getManifest(); |
295 | |
|
296 | 0 | if (mfst == null) |
297 | |
{ |
298 | 0 | return false; |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | 0 | Attributes att = mfst.getMainAttributes(); |
306 | |
|
307 | 0 | Object a = att.put( Attributes.Name.IMPLEMENTATION_TITLE, packagename ); |
308 | 0 | Object b = att.put( Attributes.Name.IMPLEMENTATION_VERSION, version ); |
309 | |
|
310 | 0 | System.out.println(" Object a " + a ); |
311 | 0 | System.out.println(" Object b " + b ); |
312 | |
|
313 | 0 | JarOutputStream jos = new JarOutputStream( new FileOutputStream( new File( jarname + "-jjar"))); |
314 | 0 | byte[] buffer = new byte[1024]; |
315 | |
int bytesRead; |
316 | |
|
317 | 0 | for( Enumeration e = jf.entries(); e.hasMoreElements(); ) |
318 | |
{ |
319 | 0 | JarEntry entry = (JarEntry) e.nextElement(); |
320 | |
|
321 | 0 | if( entry.getName().equals("META-INF/MANIFEST.MF")) |
322 | |
{ |
323 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
324 | |
|
325 | 0 | mfst.write( baos ); |
326 | |
|
327 | 0 | JarEntry ee = new JarEntry( entry ); |
328 | |
|
329 | 0 | ee.setSize(baos.size() ); |
330 | |
|
331 | 0 | jos.putNextEntry(ee ); |
332 | 0 | mfst.write( jos ); |
333 | 0 | continue; |
334 | |
} |
335 | |
|
336 | 0 | System.out.println(" Name : " + entry.getName() ); |
337 | |
|
338 | 0 | InputStream entryStream = jf.getInputStream( entry); |
339 | |
|
340 | 0 | jos.putNextEntry(entry ); |
341 | |
|
342 | 0 | while ((bytesRead = entryStream.read(buffer)) != -1) |
343 | |
{ |
344 | 0 | jos.write(buffer, 0, bytesRead); |
345 | |
} |
346 | 0 | } |
347 | |
|
348 | 0 | jos.close(); |
349 | 0 | jf.close(); |
350 | |
|
351 | 0 | return true; |
352 | |
} |
353 | |
} |
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|