org.apache.commons.jelly
Class JellyContext

java.lang.Object
  extended byorg.apache.commons.jelly.JellyContext
Direct Known Subclasses:
FunctionContext, XwingContext

public class JellyContext
extends Object

JellyContext represents the Jelly context.

Version:
$Revision: 1.1 $
Author:
James Strachan

Field Summary
protected  ClassLoader classLoader
          The class loader to use for instantiating application objects.
protected  boolean useContextClassLoader
          Do we want to use the Context ClassLoader when loading classes for instantiating new objects? Default is false.
 
Constructor Summary
JellyContext()
           
JellyContext(JellyContext parent)
           
JellyContext(JellyContext parentJellyContext, URL currentURL)
           
JellyContext(JellyContext parentJellyContext, URL rootURL, URL currentURL)
           
JellyContext(URL rootURL)
           
JellyContext(URL rootURL, URL currentURL)
           
 
Method Summary
 Script compileScript(String uri)
          Attempts to parse the script from the given uri using the getResource(java.lang.String) method then returns the compiled script.
 Script compileScript(URL url)
          Attempts to parse the script from the given URL using the getResource(java.lang.String) method then returns the compiled script.
protected  JellyContext createChildContext()
          Factory method to create a new child of this context
protected  URL createRelativeURL(URL rootURL, String relativeURI)
           
protected  XMLParser createXMLParser()
          Factory method to allow JellyContext implementations to overload how an XMLParser is created - such as to overload what the default ExpressionFactory should be.
 Object findVariable(String name)
          Finds the variable value of the given name in this context or in any other parent context.
 ClassLoader getClassLoader()
          Return the class loader to be used for instantiating application objects when required.
 URL getCurrentURL()
           
protected  URL getJellyContextURL(URL url)
          Strips off the name of a script to create a new context URL
 JellyContext getParent()
           
 URL getResource(String uri)
          Returns a URL for the given resource from the specified path.
 InputStream getResourceAsStream(String uri)
          Attempts to open an InputStream to the given resource at the specified path.
 URL getRootURL()
           
 JellyContext getScope(String name)
           
 TagLibrary getTagLibrary(String namespaceURI)
           
 boolean getUseContextClassLoader()
          Return the boolean as to whether the context classloader should be used.
 Object getVariable(String name)
           
 Object getVariable(String name, String scopeName)
           
 Iterator getVariableNames()
           
 Map getVariables()
           
protected  XMLParser getXMLParser()
           
 boolean isCacheTags()
          Returns whether caching of Tag instances, per thread, is enabled.
 boolean isExport()
           
 boolean isExportLibraries()
          Returns whether we export tag libraries to our parents context
 boolean isInherit()
           
 boolean isTagLibraryRegistered(String namespaceURI)
           
 JellyContext newJellyContext()
          A factory method to create a new child context of the current context.
 JellyContext newJellyContext(Map newVariables)
          A factory method to create a new child context of the current context.
 void registerTagLibrary(String namespaceURI, String className)
          Registers the given tag library class name against the given namespace URI.
 void registerTagLibrary(String namespaceURI, TagLibrary taglib)
          Registers the given tag library against the given namespace URI.
 void removeVariable(String name)
          Removes the given variable
 void removeVariable(String name, String scopeName)
          Removes the given variable in the specified scope.
 JellyContext runScript(File file, XMLOutput output)
          Parses the script from the given File then compiles it and runs it.
 JellyContext runScript(File file, XMLOutput output, boolean export, boolean inherit)
          Parses the script from the given file then compiles it and runs it.
 JellyContext runScript(String uri, XMLOutput output)
          Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.
 JellyContext runScript(String uri, XMLOutput output, boolean export, boolean inherit)
          Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.
 JellyContext runScript(URL url, XMLOutput output)
          Parses the script from the given URL then compiles it and runs it.
 JellyContext runScript(URL url, XMLOutput output, boolean export, boolean inherit)
          Parses the script from the given URL then compiles it and runs it.
 void setCacheTags(boolean cacheTags)
          Sets whether caching of Tag instances, per thread, is enabled.
 void setClassLoader(ClassLoader classLoader)
          Set the class loader to be used for instantiating application objects when required.
 void setCurrentURL(URL currentURL)
          Sets the current URL context of the current script that is executing.
 void setExport(boolean export)
          Sets whether we should export variable definitions to our parent context
 void setExportLibraries(boolean exportLibraries)
          Sets whether we export tag libraries to our parents context
 void setInherit(boolean inherit)
          Sets whether we should inherit variables from our parent context
protected  void setParent(JellyContext context)
          Change the parent context to the one provided
 void setRootURL(URL rootURL)
          Sets the current root context URL from which all absolute resource URIs will be relative to.
 void setUseContextClassLoader(boolean use)
          Determine whether to use the Context ClassLoader (the one found by calling Thread.currentThread().getContextClassLoader()) to resolve/load classes.
 void setVariable(String name, Object value)
          Sets the value of the given variable name
 void setVariable(String name, String scopeName, Object value)
          Sets the value of the given variable name in the given variable scope
 void setVariables(Map variables)
          Sets the Map of variables to use
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

classLoader

protected ClassLoader classLoader
The class loader to use for instantiating application objects. If not specified, the context class loader, or the class loader used to load this class itself, is used, based on the value of the useContextClassLoader variable.


useContextClassLoader

protected boolean useContextClassLoader
Do we want to use the Context ClassLoader when loading classes for instantiating new objects? Default is false.

Constructor Detail

JellyContext

public JellyContext()

JellyContext

public JellyContext(URL rootURL)

JellyContext

public JellyContext(URL rootURL,
                    URL currentURL)

JellyContext

public JellyContext(JellyContext parent)

JellyContext

public JellyContext(JellyContext parentJellyContext,
                    URL currentURL)

JellyContext

public JellyContext(JellyContext parentJellyContext,
                    URL rootURL,
                    URL currentURL)
Method Detail

getParent

public JellyContext getParent()
Returns:
the parent context for this context

getScope

public JellyContext getScope(String name)
Returns:
the scope of the given name, such as the 'parent' scope. If Jelly is used in a Servlet situation then 'request', 'session' and 'application' are other names for scopes

findVariable

public Object findVariable(String name)
Finds the variable value of the given name in this context or in any other parent context. If this context does not contain the variable, then its parent is used and then its parent and so forth until the context with no parent is found.

Returns:
the value of the variable in this or one of its descendant contexts or null if the variable could not be found.

getVariable

public Object getVariable(String name)
Returns:
the value of the given variable name

getVariable

public Object getVariable(String name,
                          String scopeName)
Parameters:
name - is the name of the variable
scopeName - is the optional scope name such as 'parent'. For servlet environments this could be 'application', 'session' or 'request'.
Returns:
the value of the given variable name in the given variable scope

setVariable

public void setVariable(String name,
                        Object value)
Sets the value of the given variable name


setVariable

public void setVariable(String name,
                        String scopeName,
                        Object value)
Sets the value of the given variable name in the given variable scope

Parameters:
name - is the name of the variable
scopeName - is the optional scope name such as 'parent'. For servlet environments this could be 'application', 'session' or 'request'.
value - is the value of the attribute

removeVariable

public void removeVariable(String name)
Removes the given variable


removeVariable

public void removeVariable(String name,
                           String scopeName)
Removes the given variable in the specified scope.

Parameters:
name - is the name of the variable
scopeName - is the optional scope name such as 'parent'. For servlet environments this could be 'application', 'session' or 'request'.

getVariableNames

public Iterator getVariableNames()
Returns:
an Iterator over the current variable names in this context

getVariables

public Map getVariables()
Returns:
the Map of variables in this scope

setVariables

public void setVariables(Map variables)
Sets the Map of variables to use


newJellyContext

public JellyContext newJellyContext(Map newVariables)
A factory method to create a new child context of the current context.


newJellyContext

public JellyContext newJellyContext()
A factory method to create a new child context of the current context.


registerTagLibrary

public void registerTagLibrary(String namespaceURI,
                               TagLibrary taglib)
Registers the given tag library against the given namespace URI. This should be called before the parser is used.


registerTagLibrary

public void registerTagLibrary(String namespaceURI,
                               String className)
Registers the given tag library class name against the given namespace URI. The class will be loaded via the given ClassLoader This should be called before the parser is used.


isTagLibraryRegistered

public boolean isTagLibraryRegistered(String namespaceURI)

getTagLibrary

public TagLibrary getTagLibrary(String namespaceURI)
Returns:
the TagLibrary for the given namespace URI or null if one could not be found

compileScript

public Script compileScript(String uri)
                     throws JellyException
Attempts to parse the script from the given uri using the getResource(java.lang.String) method then returns the compiled script.

Throws:
JellyException

compileScript

public Script compileScript(URL url)
                     throws JellyException
Attempts to parse the script from the given URL using the getResource(java.lang.String) method then returns the compiled script.

Throws:
JellyException

getXMLParser

protected XMLParser getXMLParser()
Returns:
a thread pooled XMLParser to avoid the startup overhead of the XMLParser

createXMLParser

protected XMLParser createXMLParser()
Factory method to allow JellyContext implementations to overload how an XMLParser is created - such as to overload what the default ExpressionFactory should be.


runScript

public JellyContext runScript(File file,
                              XMLOutput output)
                       throws JellyException
Parses the script from the given File then compiles it and runs it.

Returns:
the new child context that was used to run the script
Throws:
JellyException

runScript

public JellyContext runScript(URL url,
                              XMLOutput output)
                       throws JellyException
Parses the script from the given URL then compiles it and runs it.

Returns:
the new child context that was used to run the script
Throws:
JellyException

runScript

public JellyContext runScript(String uri,
                              XMLOutput output)
                       throws JellyException
Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.

Returns:
the new child context that was used to run the script
Throws:
JellyException

runScript

public JellyContext runScript(String uri,
                              XMLOutput output,
                              boolean export,
                              boolean inherit)
                       throws JellyException
Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.

Returns:
the new child context that was used to run the script
Throws:
JellyException

runScript

public JellyContext runScript(File file,
                              XMLOutput output,
                              boolean export,
                              boolean inherit)
                       throws JellyException
Parses the script from the given file then compiles it and runs it.

Returns:
the new child context that was used to run the script
Throws:
JellyException

runScript

public JellyContext runScript(URL url,
                              XMLOutput output,
                              boolean export,
                              boolean inherit)
                       throws JellyException
Parses the script from the given URL then compiles it and runs it.

Returns:
the new child context that was used to run the script
Throws:
JellyException

getResource

public URL getResource(String uri)
                throws MalformedURLException
Returns a URL for the given resource from the specified path. If the uri starts with "/" then the path is taken as relative to the current context root. If the uri is a well formed URL then it is used. If the uri is a file that exists and can be read then it is used. Otherwise the uri is interpreted as relative to the current context (the location of the current script).

Throws:
MalformedURLException

getResourceAsStream

public InputStream getResourceAsStream(String uri)
Attempts to open an InputStream to the given resource at the specified path. If the uri starts with "/" then the path is taken as relative to the current context root. If the uri is a well formed URL then it is used. Otherwise the uri is interpreted as relative to the current context (the location of the current script).

Returns:
null if this resource could not be loaded, otherwise the resources input stream is returned.

getRootURL

public URL getRootURL()
Returns:
the current root context URL from which all absolute resource URIs will be relative to. For example in a web application the root URL will map to the web directory which contains the WEB-INF directory.

setRootURL

public void setRootURL(URL rootURL)
Sets the current root context URL from which all absolute resource URIs will be relative to. For example in a web application the root URL will map to the web directory which contains the WEB-INF directory.


getCurrentURL

public URL getCurrentURL()
Returns:
the current URL context of the current script that is executing. This URL context is used to deduce relative scripts when relative URIs are used in calls to getResource(java.lang.String) to process relative scripts.

setCurrentURL

public void setCurrentURL(URL currentURL)
Sets the current URL context of the current script that is executing. This URL context is used to deduce relative scripts when relative URIs are used in calls to getResource(java.lang.String) to process relative scripts.


isCacheTags

public boolean isCacheTags()
Returns whether caching of Tag instances, per thread, is enabled. Caching Tags can boost performance, on some JVMs, by reducing the cost of object construction when running Jelly inside a multi-threaded application server such as a Servlet engine.

Returns:
whether caching of Tag instances is enabled.

setCacheTags

public void setCacheTags(boolean cacheTags)
Sets whether caching of Tag instances, per thread, is enabled. Caching Tags can boost performance, on some JVMs, by reducing the cost of object construction when running Jelly inside a multi-threaded application server such as a Servlet engine.

Parameters:
cacheTags - Whether caching should be enabled or disabled.

isExportLibraries

public boolean isExportLibraries()
Returns whether we export tag libraries to our parents context

Returns:
boolean

setExportLibraries

public void setExportLibraries(boolean exportLibraries)
Sets whether we export tag libraries to our parents context

Parameters:
exportLibraries - The exportLibraries to set

setExport

public void setExport(boolean export)
Sets whether we should export variable definitions to our parent context


isExport

public boolean isExport()

setInherit

public void setInherit(boolean inherit)
Sets whether we should inherit variables from our parent context


isInherit

public boolean isInherit()

getClassLoader

public ClassLoader getClassLoader()
Return the class loader to be used for instantiating application objects when required. This is determined based upon the following rules:


setClassLoader

public void setClassLoader(ClassLoader classLoader)
Set the class loader to be used for instantiating application objects when required.

Parameters:
classLoader - The new class loader to use, or null to revert to the standard rules

getUseContextClassLoader

public boolean getUseContextClassLoader()
Return the boolean as to whether the context classloader should be used.


setUseContextClassLoader

public void setUseContextClassLoader(boolean use)
Determine whether to use the Context ClassLoader (the one found by calling Thread.currentThread().getContextClassLoader()) to resolve/load classes. If not using Context ClassLoader, then the class-loading defaults to using the calling-class' ClassLoader.

Parameters:
use - determines whether to use JellyContext ClassLoader.

createRelativeURL

protected URL createRelativeURL(URL rootURL,
                                String relativeURI)
                         throws MalformedURLException
Parameters:
rootURL - is the root context from which the relative URI will be applied
relativeURI - is the relative URI (without a leading "/")
Returns:
a new relative URL from the given root and with the addition of the extra relative URI
Throws:
MalformedURLException - if the URL is invalid.

getJellyContextURL

protected URL getJellyContextURL(URL url)
                          throws MalformedURLException
Strips off the name of a script to create a new context URL

Throws:
MalformedURLException

createChildContext

protected JellyContext createChildContext()
Factory method to create a new child of this context


setParent

protected void setParent(JellyContext context)
Change the parent context to the one provided

Parameters:
context - the new parent context


Copyright © 2003 The Xwing Project. All Rights Reserved.