Hi david... I knew we could.
I've implemented a custom URLStreamHandlerFactory so that we can handle
nested jars!! With it you can use the new "njar" protocol (n for nested).
To use just do:
NestedURLHandlerFactory.start();
URL u = new URL("njar:njar:file:c:/test1.zip!/test2.zip!/hello.txt");
u.openStream();
I'm going to attach the sources to this e-mail. Please review and if it
help you fix your problem, let me know.
Regards,
Hiram
> > >I hoped you could use multiple jar: prefixes, but
> > this doesn't work when I
> > >try it at least for accessing the contents :-(((( If
> > it did we wouldn't
> > >have to unpack anything.
> > >
> > >jar:jar:jar:file:/uxr/jboss/deploy/outside.sar!/middl
> > .ear!/inside.rar!/rarcode.jar
> > >
> > >david jencks
> > >
> >
> > Could we handle this if we created a custom
> > URLFactory thingy???
> > I think we could.
> >
> > Regards,
> > Hiram
> >
_________________________________________________________________
Join the world�s largest e-mail service with MSN Hotmail.
http://www.hotmail.com
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import java.util.HashMap;
import java.util.Map;
/**
*
* This is class allows you to use the njar: URL protocol. It is very
* similar to it's jar: cusin. The difference being that jars can be
* nested.
*
* An example of how to use this class is:
* <code>
*
* NestedURLHandlerFactory.start();
* URL u = new URL("njar:njar:file:c:/test1.zip!/test2.zip!/hello.txt");
* u.openStream();
*
* </code>
*
* Please be aware that the njar protocol caches it's jar in temporary
storage
* when connections are opened into them. So for the above example, 2 files
would
* cached a temp files names similar to nested-xxxx.jar
*
* TODO: Add accessors so that the cache can be flushed.
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Hiram Chirino</a>
*
*/
public class NestedURLHandlerFactory implements URLStreamHandlerFactory
{
public class NestedJarURLHandler extends URLStreamHandler
{
Map savedJars = new HashMap();
// URL protocol designations
public static final String PROTOCOL = "njar";
public static final String JAR_SEPARATOR = "!/";
/*
* @see URLStreamHandler#openConnection(URL)
*/
protected URLConnection openConnection(URL u) throws IOException
{
String file = u.getFile();
String enbededURL = file;
String jarPath = "";
int pos = file.lastIndexOf(JAR_SEPARATOR);
if (pos >= 0)
{
enbededURL = file.substring(0, pos);
if (file.length() > pos + JAR_SEPARATOR.length())
jarPath = file.substring(pos + JAR_SEPARATOR.length());
}
if (enbededURL.startsWith(PROTOCOL))
{
System.out.println("Opening next nested jar: " + enbededURL);
File tempJar = (File) savedJars.get(enbededURL);
if (tempJar == null)
{
InputStream embededData = new URL(enbededURL).openStream();
tempJar = File.createTempFile("nested-", ".jar");
storeJar(embededData, new FileOutputStream(tempJar));
savedJars.put(enbededURL, tempJar);
}
String t = tempJar.getCanonicalFile().toURL().toExternalForm();
System.out.println("file URL : " + t);
t = "jar:" + t + JAR_SEPARATOR + jarPath;
System.out.println("Opening saved jar: " + t);
return new URL(t).openConnection();
}
else
{
System.out.println("Opening final nested jar: " + enbededURL);
return new URL(enbededURL).openConnection();
}
}
}
NestedJarURLHandler handler = new NestedJarURLHandler();
public URLStreamHandler createURLStreamHandler(String protocol)
{
if (protocol.equals(handler.PROTOCOL))
return handler;
return null;
}
public static void start()
{
URL.setURLStreamHandlerFactory(new NestedURLHandlerFactory());
}
protected void storeJar(InputStream in, OutputStream out) throws
IOException
{
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
bis = new BufferedInputStream(in);
bos = new BufferedOutputStream(out);
byte data[] = new byte[512];
int c;
while ((c = bis.read(data)) >= 0)
{
bos.write(data, 0, c);
}
}
finally
{
try
{
bis.close();
}
catch (IOException ignore)
{
}
try
{
bos.close();
}
catch (IOException ignore)
{
}
}
}
public static void main(String[] arguments) throws Exception
{
NestedURLHandlerFactory.start();
URL u = new URL("njar:njar:file:c:/test1.zip!/test2.zip!/hello.txt");
String data = new DataInputStream(u.openStream()).readLine();
System.out.println("The file said: " + data);
}
}