Class NestedJarURLConnection

java.lang.Object
java.net.URLConnection
com.portal.pfc.util.nestedjars.NestedJarURLConnection

public class NestedJarURLConnection extends URLConnection
NestedJarURLConnection is a subclass of the URLConnection. It supports opening a connection to a file that is packaged in a JAR file, which in turn is inside another JAR file. There is support for a single level of nesting.

It is expected that the outer JAR is available on the local filesystem and accessible via standard java.io.File operations.

This class knows how to handle URLs using a custom nestedjar protocol. The URL should be of the form:

nestedjar://file:/C:/TEMP/apps.jar!/innerapps.jar!/myapp/nested.gif
The different parts of the URL are:
nestedjar:// Custom protocol for loading resources from nested jar files.
/C:/TEMP/ Path to the toplevel jar file. Should be accessible via standard java.io.File.
apps.jar Outer jar file. This jar file is on the classpath. It contains nested jar file(s).
innerapps.jar Jar file nested inside the outer jar file. This Jar file contains the actual resources.
myapp/nested.gif Resource in the inner jar file that is to be loaded
!/ Separator. Separates a jar file and the components it contains.

Author:
Max Spivak
  • Constructor Details

    • NestedJarURLConnection

      public NestedJarURLConnection(URL url)
      Constructor. See class comment regarding URL structure this class knows how to handle.
  • Method Details

    • connect

      public void connect() throws IOException
      Opens a communications link to the resource referenced by this URL, if such a connection has not already been established. More specifically, this method will attempt to open the outer jar file and locate the inner, nested jar file as specified by the URL.

      If the connect method is called when the connection has already been opened the call is ignored.

      Specified by:
      connect in class URLConnection
      Throws:
      IOException - if an I/O error occurs while opening the connection.
    • getInputStream

      public InputStream getInputStream() throws IOException
      Returns an input stream that reads from this open connection.
      Overrides:
      getInputStream in class URLConnection
      Returns:
      an input stream that reads from this open connection.
      Throws:
      IOException - if an I/O error occurs while creating the input stream.
      UnknownServiceException - if the protocol does not support input.
    • getContentType

      public String getContentType()
      Determine the content type of the resource
      Overrides:
      getContentType in class URLConnection