org.apache.turbine.services.velocity
Class TurbineVelocityService

java.lang.Object
  |
  +--org.apache.turbine.services.BaseInitable
        |
        +--org.apache.turbine.services.BaseService
              |
              +--org.apache.turbine.services.TurbineBaseService
                    |
                    +--org.apache.turbine.services.template.BaseTemplateEngineService
                          |
                          +--org.apache.turbine.services.velocity.TurbineVelocityService
All Implemented Interfaces:
Initable, Service, TemplateEngineService, VelocityService

public class TurbineVelocityService
extends BaseTemplateEngineService
implements VelocityService

This is a Service that can process Velocity templates from within a Turbine Screen. Here's an example of how you might use it from a screen:
Context context = TurbineVelocity.getContext(data);
context.put("message", "Hello from Turbine!");
String results = TurbineVelocity.handleRequest(context,"helloWorld.vm");
data.getPage().getBody().addElement(results);

Version:
$Id: TurbineVelocityService.java,v 1.6 2002/03/13 19:43:35 mpoeschl Exp $
Author:
Dave Bryson, Rafal Krzewski, Jason van Zyl, Sean Legassick, Daniel Rall

Field Summary
private static java.lang.String ABSOLUTE_PREFIX
          The prefix used for URIs which are of type absolute.
private static java.lang.String DEFAULT_CHAR_SET
          Default character set to use if not specified in the RunData object.
private  org.apache.velocity.context.Context globalContext
          The context used to the store the context containing the global application tools.
private static java.lang.String JAR_PREFIX
          The prefix used for URIs which are of type jar.
private  boolean pullModelActive
          Is the pullModelActive.
private  boolean refreshToolsPerRequest
          Should we refresh the tools on a per request basis.
private static java.lang.String RESOURCE_LOADER_PATH
          The generic resource loader path property in velocity.
 
Fields inherited from class org.apache.turbine.services.template.BaseTemplateEngineService
configuration
 
Fields inherited from class org.apache.turbine.services.BaseService
name, properties, serviceBroker
 
Fields inherited from class org.apache.turbine.services.BaseInitable
initableBroker, isInitialized
 
Fields inherited from interface org.apache.turbine.services.velocity.VelocityService
CONTEXT, SERVICE_NAME
 
Fields inherited from interface org.apache.turbine.services.template.TemplateEngineService
DEFAULT_ERROR_SCREEN, DEFAULT_LAYOUT, DEFAULT_LAYOUT_TEMPLATE, DEFAULT_NAVIGATION, DEFAULT_PAGE, DEFAULT_SCREEN, DEFAULT_TEMPLATE_EXTENSION, TEMPLATE_EXTENSIONS
 
Constructor Summary
TurbineVelocityService()
           
 
Method Summary
private  java.lang.String decodeRequest(org.apache.velocity.context.Context context, java.lang.String filename, java.io.OutputStream output)
          Process the request and fill in the template with the values you set in the Context.
private  void decodeRequest(org.apache.velocity.context.Context context, java.lang.String filename, java.lang.String encoding, java.io.Writer writer)
          Process the request and fill in the template with the values you set in the Context.
private  java.lang.String getCharSet(org.apache.velocity.context.Context context)
          Retrieve the required charset from the Turbine RunData in the context
 org.apache.velocity.context.Context getContext()
          Create a Context object that also contains the globalContext.
 org.apache.velocity.context.Context getContext(RunData data)
          Create a Context from the RunData object.
private  java.lang.String getEncoding(org.apache.velocity.context.Context context)
          Retrieve the required encoding from the Turbine RunData in the context
 java.lang.String handleRequest(org.apache.velocity.context.Context context, java.lang.String filename)
          Process the request and fill in the template with the values you set in the Context.
 void handleRequest(org.apache.velocity.context.Context context, java.lang.String filename, java.io.OutputStream output)
          Process the request and fill in the template with the values you set in the Context.
 void handleRequest(org.apache.velocity.context.Context context, java.lang.String filename, java.io.Writer writer)
          Process the request and fill in the template with the values you set in the Context.
 void init(javax.servlet.ServletConfig config)
          Performs early initialization of this Turbine service.
private  void initVelocity()
          Setup the velocity runtime by using a subset of the Turbine configuration which relates to velocity.
private static void renderingError(java.lang.String filename, java.lang.Exception e)
          Macro to handle rendering errors.
 void requestFinished(org.apache.velocity.context.Context context)
          Performs post-request actions (releases context tools back to the object pool).
 boolean templateExists(java.lang.String template)
          Find out if a given template exists.
 
Methods inherited from class org.apache.turbine.services.template.BaseTemplateEngineService
getAssociatedFileExtensions, getTemplateEngineServiceConfiguration, initConfiguration, registerConfiguration
 
Methods inherited from class org.apache.turbine.services.TurbineBaseService
init, init, init, shutdown
 
Methods inherited from class org.apache.turbine.services.BaseService
getConfiguration, getName, getProperties, getServiceBroker, setName, setServiceBroker
 
Methods inherited from class org.apache.turbine.services.BaseInitable
getInit, getInitableBroker, setInit, setInitableBroker
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 
Methods inherited from interface org.apache.turbine.services.Service
getConfiguration, getName, getProperties, setName, setServiceBroker
 
Methods inherited from interface org.apache.turbine.services.Initable
getInit, init, init, setInitableBroker, shutdown
 

Field Detail

RESOURCE_LOADER_PATH

private static final java.lang.String RESOURCE_LOADER_PATH
The generic resource loader path property in velocity.

DEFAULT_CHAR_SET

private static final java.lang.String DEFAULT_CHAR_SET
Default character set to use if not specified in the RunData object.

JAR_PREFIX

private static final java.lang.String JAR_PREFIX
The prefix used for URIs which are of type jar.

ABSOLUTE_PREFIX

private static final java.lang.String ABSOLUTE_PREFIX
The prefix used for URIs which are of type absolute.

globalContext

private org.apache.velocity.context.Context globalContext
The context used to the store the context containing the global application tools.

pullModelActive

private boolean pullModelActive
Is the pullModelActive.

refreshToolsPerRequest

private boolean refreshToolsPerRequest
Should we refresh the tools on a per request basis. This is used for development.
Constructor Detail

TurbineVelocityService

public TurbineVelocityService()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws InitializationException
Performs early initialization of this Turbine service.
Overrides:
init in class TurbineBaseService
Following copied from class: org.apache.turbine.services.TurbineBaseService
Parameters:
config - A ServletConfing to use for initialization activities.
Throws:
InitializationException, - if initialization of this class was not successful.

getContext

public org.apache.velocity.context.Context getContext()
Create a Context object that also contains the globalContext.
Specified by:
getContext in interface VelocityService
Returns:
A Context object.

getContext

public org.apache.velocity.context.Context getContext(RunData data)
Create a Context from the RunData object. Adds a pointer to the RunData object to the WC so that RunData is available in the templates.
Specified by:
getContext in interface VelocityService
Parameters:
data - The Turbine RunData object.
Returns:
A clone of the WebContext needed by Velocity.

handleRequest

public java.lang.String handleRequest(org.apache.velocity.context.Context context,
                                      java.lang.String filename)
                               throws TurbineException
Process the request and fill in the template with the values you set in the Context.
Specified by:
handleRequest in interface VelocityService
Parameters:
context - The populated context.
filename - The file name of the template.
Returns:
The process template as a String.
Throws:
TurbineException - Any exception trown while processing will be wrapped into a TurbineException and rethrown.

handleRequest

public void handleRequest(org.apache.velocity.context.Context context,
                          java.lang.String filename,
                          java.io.OutputStream output)
                   throws TurbineException
Process the request and fill in the template with the values you set in the Context.
Specified by:
handleRequest in interface VelocityService
Parameters:
context - A Context.
filename - A String with the filename of the template.
out - A OutputStream where we will write the process template as a String.
Throws:
TurbineException - Any exception trown while processing will be wrapped into a TurbineException and rethrown.

handleRequest

public void handleRequest(org.apache.velocity.context.Context context,
                          java.lang.String filename,
                          java.io.Writer writer)
                   throws TurbineException
Process the request and fill in the template with the values you set in the Context.
Specified by:
handleRequest in interface VelocityService
Parameters:
context - A Context.
filename - A String with the filename of the template.
writer - A Writer where we will write the process template as a String.
Throws:
TurbineException - Any exception trown while processing will be wrapped into a TurbineException and rethrown.

decodeRequest

private java.lang.String decodeRequest(org.apache.velocity.context.Context context,
                                       java.lang.String filename,
                                       java.io.OutputStream output)
                                throws TurbineException
Process the request and fill in the template with the values you set in the Context. Apply the character and template encodings from RunData to the result.
Parameters:
context - A Context.
filename - A String with the filename of the template.
out - A OutputStream where we will write the process template as a String.
Returns:
The character encoding applied to the resulting String.
Throws:
TurbineException - Any exception trown while processing will be wrapped into a TurbineException and rethrown.

getCharSet

private java.lang.String getCharSet(org.apache.velocity.context.Context context)
Retrieve the required charset from the Turbine RunData in the context
Parameters:
context - A Context.
Returns:
The character set applied to the resulting String.

getEncoding

private java.lang.String getEncoding(org.apache.velocity.context.Context context)
Retrieve the required encoding from the Turbine RunData in the context
Parameters:
context - A Context.
Returns:
The encoding applied to the resulting String.

decodeRequest

private void decodeRequest(org.apache.velocity.context.Context context,
                           java.lang.String filename,
                           java.lang.String encoding,
                           java.io.Writer writer)
                    throws TurbineException
Process the request and fill in the template with the values you set in the Context.
Parameters:
context - A Context.
filename - A String with the filename of the template.
encoding - The encoding to use with the template
writer - A Writer where we will write the process template as a String. This writer charset should be compatible with the selected encoding
Throws:
TurbineException - Any exception trown while processing will be wrapped into a TurbineException and rethrown.

renderingError

private static final void renderingError(java.lang.String filename,
                                         java.lang.Exception e)
                                  throws TurbineException
Macro to handle rendering errors.
Parameters:
filename - The file name of the unrenderable template.
e - The error.
Throws:
TurbineException - Thrown every time. Adds additional information to e.

initVelocity

private void initVelocity()
                   throws InitializationException
Setup the velocity runtime by using a subset of the Turbine configuration which relates to velocity.
Throws:
InitializationException - For any errors during initialization.

templateExists

public boolean templateExists(java.lang.String template)
Find out if a given template exists. Velocity will do its own searching to determine whether a template exists or not.
Overrides:
templateExists in class BaseTemplateEngineService
Parameters:
String - template to search for
Returns:
boolean

requestFinished

public void requestFinished(org.apache.velocity.context.Context context)
Performs post-request actions (releases context tools back to the object pool).
Specified by:
requestFinished in interface VelocityService
Parameters:
context - a Velocity Context


Copyright © 2000-2002 Apache Software Foundation. All Rights Reserved.