no.sesat.search.site.config
Class AbstractResourceLoader

java.lang.Object
  extended by no.sesat.search.site.config.AbstractResourceLoader
All Implemented Interfaces:
Runnable, BytecodeLoader, DocumentLoader, PropertiesLoader, ResourceLoader
Direct Known Subclasses:
UrlResourceLoader

public abstract class AbstractResourceLoader
extends Object
implements Runnable, DocumentLoader, PropertiesLoader, BytecodeLoader

Utility class to handle loading different types of resources in a background thread. This avoids the problem of having to order loading of applications in the container because of static initialisers using resources from the search-front-config application.
Because the loading is backgrounded it is important to wait until it is finished. This is done by calling abut()

Example usecases:
(1)

 // load custom.properties into props in skin's WEB-INF/classes/
 Site site = ...;
 PropertiesContext context = ...;
 Properties props = new Properties();
 PropertiesLoader loader = context.newPropertiesLoader(site.getSiteContext(), "custom.properties", props);
 loader.abut();
 props.getProperty(...);
 
 (2)
 // create document from my.xml located in skin's WEB-INF/classes/
 Site site = ...;
 DocumentContext context = ...;
 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 factory.setValidating(false);
 final DocumentBuilder builder = factory.newDocumentBuilder();
 loader = context.newDocumentLoader(site.getSiteContext(), "my.xml", builder);
 loader.abut();
 Document doc = loader.getDocument();
 
(3)
 // read a class file from a jar file located in skin's WEB-INF/lib/
 Site site = ...;
 BytecodeContext context = ...;
 BytecodeLoader loader= context.newBytecodeLoader(site.getSiteContext(),"Example.class", "Example.jar");
 loader.abut()
 byte[] bytes = loader.getBytecode();
 

Version:
$Id: AbstractResourceLoader.java 7225 2009-04-09 00:32:20Z ssmiweve $

Field Summary
protected  DocumentBuilder builder
          DocumentBuilder builder.
protected  Document document
          Document.
protected  String jarFileName
          Name of jar to load classes from
protected  Properties props
          the properties resource holder.
 
Constructor Summary
protected AbstractResourceLoader(SiteContext cxt)
          Creates a new instance of AbstractResourceLoader.
 
Method Summary
 void abut()
          Simple thread.join() wrapper to avoid dealing with InterruptedException.
 byte[] getBytecode()
          Returns byte code for the class.
protected  SiteContext getContext()
          Get the SiteContext.
 Document getDocument()
          get the Document.
protected abstract  InputStream getInputStreamFor(URL resource)
           
 Properties getProperties()
          get the properties.
protected  String getResource()
          Get the resource name/path this class is responsible for retrieving.
protected abstract  URL getResource(Site site)
           
 void init(String resource, DocumentBuilder builder)
          initialise this resource loader with the resource name/path and the builder used to create the dom.
 void init(String resource, Properties props)
          initialise this resource loader with the resource name/path and the resource it will go into.
 void initBytecodeLoader(String className, String jarFileName)
          Prepares and loads byte code for a class.
protected  String readResourceDebug(URL url)
           
 void run()
           
abstract  boolean urlExists(URL url)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

props

protected Properties props
the properties resource holder.


builder

protected DocumentBuilder builder
DocumentBuilder builder.


document

protected Document document
Document.


jarFileName

protected String jarFileName
Name of jar to load classes from

Constructor Detail

AbstractResourceLoader

protected AbstractResourceLoader(SiteContext cxt)
Creates a new instance of AbstractResourceLoader.

Parameters:
cxt - the context that we supply us with which site we are dealing with.
Method Detail

urlExists

public abstract boolean urlExists(URL url)

getProperties

public Properties getProperties()
Description copied from interface: PropertiesLoader
get the properties.

Specified by:
getProperties in interface PropertiesLoader
Returns:
the properties.

getDocument

public Document getDocument()
Description copied from interface: DocumentLoader
get the Document.

Specified by:
getDocument in interface DocumentLoader
Returns:
the Document.

getBytecode

public byte[] getBytecode()
Description copied from interface: BytecodeLoader
Returns byte code for the class.

Specified by:
getBytecode in interface BytecodeLoader
Returns:
bytecode.

init

public void init(String resource,
                 Properties props)
Description copied from interface: PropertiesLoader
initialise this resource loader with the resource name/path and the resource it will go into. existing properties will not be overwritten.

Specified by:
init in interface PropertiesLoader
Parameters:
resource - the name/path of the resource.
props - the properties that will be used to hold the individual properties.

init

public void init(String resource,
                 DocumentBuilder builder)
Description copied from interface: DocumentLoader
initialise this resource loader with the resource name/path and the builder used to create the dom.

Specified by:
init in interface DocumentLoader
Parameters:
resource - the name/path of the resource.
builder - the document that will be used to hold the xml dom.

initBytecodeLoader

public void initBytecodeLoader(String className,
                               String jarFileName)
Description copied from interface: BytecodeLoader
Prepares and loads byte code for a class.

Specified by:
initBytecodeLoader in interface BytecodeLoader
Parameters:
className - the class to load byte code for.
jarFileName - the jar file to look in.

abut

public void abut()
Description copied from interface: ResourceLoader
Simple thread.join() wrapper to avoid dealing with InterruptedException. abut: To touch or end at one end or side; lie adjacent.

Specified by:
abut in interface ResourceLoader

run

public void run()
Specified by:
run in interface Runnable

getResource

protected abstract URL getResource(Site site)

getInputStreamFor

protected abstract InputStream getInputStreamFor(URL resource)

getContext

protected SiteContext getContext()
Get the SiteContext.

Returns:
the SiteContext.

getResource

protected String getResource()
Get the resource name/path this class is responsible for retrieving.

Returns:
the resource name/path.

readResourceDebug

protected String readResourceDebug(URL url)


Copyright © 2005-2009 Schibsted ASA. All Rights Reserved.