org.backsource.utils.resource
Class URIFactory

java.lang.Object
  extended byorg.backsource.utils.resource.URIFactory

public class URIFactory
extends java.lang.Object

A factory that produces URI:s that translates URI:s to URL with the help of URLFactories.

The factory do not only produce URI:s but is also the callback or factory used by URI:s to translate URI:s to URL. A URI created without any prior knowledge of the URIFactory will get a factory through the help of the URIFactoryContext wich is a helper class that may be used to create different type of singletons.

The translation of a URI to a URL is dependant on the setup of the URI factory. The standard setup is that URI:s that resolves to URL:s are delegated as normal URL. URI:s that are not URL are delegated to other URLFactories, which is goverened by the schema part of the url. All schemaless URI:s and URI:s whose schma does to have a registered URLFactory is delegated to be loaded by the default URLFactory (which is configured in the ClassContextURLFactory.)

Here are some typical usecases:

 URI uri = new URI("http://my.site");
 URL url = uri.getURL();//Will get the url to the site
 
 URI uri = new URI("my.properties");
 URL url = uri.getURL();//Will search the classpath and return an URL it.
 
 // Preregistered is the "class:"
 // Registere different handler
 URIFactory f = new URIFactory();
 // Make a default wich searched first the servlet context then the
 // context classloader
 f.register("default:", new ServletFirstURLFactory( servletContext));
 //Registere a specific one for the servlet schema
 f.register("servlet:", new ServletURLFactory(servletContext));
 // Register an URLFactory that searches for resources backwards, i.e starts
 // in the leaf classloader.
 f.register("class", new ClassContextUrlFactory( new ReverseClassLoader(contextClassLoader)));
 
 // keep the URLFactory around programatically and use it in these three ways:
 
 // Use URI constructor
 URI uri = new URI("servlet:/WEB-INF/my.properties", f);
 URL url = uri.getURL();
 
 //User getURL method in URI
 URI uri = new URI("class:META-INF/descr.xml");
 URL url = uri.getURL(f);
 
 // Use factory direct
 URL url = f.getURL(uri);
 

The URIFactory context is a classic singleton. It is used by the URI when the URIFactory is not know to URI. It has one static get method that returns a URIFactory. If no set method has been used it returns a default unconfigured URIFactory, which is available in the global JVM.

URIFactoryContext however has several set-methods that may be use to localize the context and make a particular URIFactory available only in a particular context in the JVM. This gives great flexibility at the cost of some performance, since the algoritm will have to search bottom up to guarantee that the most localized version will be used. There are four ways to localize a factory currently:

What good is this? Say for example that you have to war-files (webapplications). Both want's to use the servlet context resource loading. Each webaplication then have to configure a URIFactory with its particular servlet context. With a normal singleton this is not possible. But by using for example setClassLocal it will be possible to do this. In the init method of a central servlet this could be done:

 ServletURLFactory sf = new ServletURLFactory( servletConfig.getServletContext() );
 URIFactory f = new URIFactory();
 f.register("default", sf);
 
 URIFactoryContext.setClassLocal(f);
 

If, and this is important, the servlet engine uses a particular context class loader, it will be possible to use URI in any class used by all the servlets and jsp pages in the particular webapplication and the URI will automatically use the registered URIFactory and get resources from the servlet context. Created: Fri Feb 1 09:52:20 2002

Version:
$Id: URIFactory.java,v 1.1.1.1 2004/05/19 12:07:31 pra Exp $
Author:
Peter Antman

Constructor Summary
URIFactory()
          Create a URIFactory with a ClassURLFactory as default handler.
URIFactory(URLFactory defaultFactory)
          Create an URIFactory with sepcifyed default URLFactory.
 
Method Summary
 java.net.URL getURL(java.lang.String uri)
          Same as with URI as parameter.
 java.net.URL getURL(URI uri)
          Get an URL from URI (without using URI.getURL()).
 void register(java.lang.String schema, URLFactory factory)
          Register an URLFactory for a schema.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

URIFactory

public URIFactory()
Create a URIFactory with a ClassURLFactory as default handler.


URIFactory

public URIFactory(URLFactory defaultFactory)
Create an URIFactory with sepcifyed default URLFactory.

Method Detail

getURL

public java.net.URL getURL(URI uri)
                    throws java.net.MalformedURLException
Get an URL from URI (without using URI.getURL()). If no schema is given or no URLFactory is found for the given schema the default URLFactory will be used, except uri:s that are valid URL:s in them self where an URL is constructed if no URLFactory was registered for the schema/protocol.

Parameters:
uri - an URI object. Its URLFactoryContext will not be used!
Throws:
java.net.MalformedURLException - if no URL was possible to constuct.

getURL

public java.net.URL getURL(java.lang.String uri)
                    throws java.net.MalformedURLException
Same as with URI as parameter.

Parameters:
uri - a string wich may be parsed into an URI.
Throws:
java.net.MalformedURLException

register

public void register(java.lang.String schema,
                     URLFactory factory)
Register an URLFactory for a schema. The schema name default will set the default URLFactory.

Parameters:
schema - an URI schema.
factory - an URLFactory handling the schema.


Copyright © 2000-2004 Backsource. All Rights Reserved.