org.norther.tammi.spray.engine
Class AbstractTemplateEngine<K,V>

java.lang.Object
  extended by org.norther.tammi.core.base.Adaptee
      extended by org.norther.tammi.core.config.DefaultConfigurable
          extended by org.norther.tammi.spray.engine.AbstractTemplateEngine<K,V>
Type Parameters:
K - the cache key type.
V - the cache value type.
All Implemented Interfaces:
Serializable, EventListener, NotificationBroadcaster, NotificationEmitter, TraceListener, Manageable, MBeanDelegate, Refreshable, Startable, CacheClient<K,V>, Configurable, TemplateEngine
Direct Known Subclasses:
DefaultForwardEngine, DefaultFreemarkerEngine, DefaultVelocityEngine

public abstract class AbstractTemplateEngine<K,V>
extends DefaultConfigurable
implements TemplateEngine, CacheClient<K,V>

An abstract implementation of TemplateEngine.

Based on BaseTemplateEngineService in the Apache Jakarta Turbine project.

Version:
$Id: AbstractTemplateEngine.java,v 1.11 2009/11/13 16:31:45 cvsimp Exp $
Author:
Jason van Zyl, Daniel Rall, Ilkka Priha
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class org.norther.tammi.core.config.DefaultConfigurable
DefaultConfigurable.Modified
 
Field Summary
 
Fields inherited from class org.norther.tammi.core.base.Adaptee
ADAPTEE_NOTIF_DESCRIPTION, ADAPTEE_NOTIFICATIONS
 
Constructor Summary
AbstractTemplateEngine()
          Constructs a new engine.
 
Method Summary
 void clearCached()
          Clears all cached entries without notifying.
 CharSequence evaluate(CharSequence content, TemplateContext context, String template)
          Evaluates the given run-time content.
 CharSequence evaluate(TemplateContext context, String template)
          Evaluates the specified template.
 CharSequence evaluate(TemplateContext context, String template, String encoding)
          Evaluates the specified template using the given encoding.
 void execute(CharSequence content, TemplateContext context, String template, Writer writer)
          Executes the given run-time content.
 void execute(TemplateContext context, String template, String encoding, Writer writer)
          Executes the specified template using the given encoding.
 void execute(TemplateContext context, String template, Writer writer)
          Executes the specified template.
 void expireCached(long timestamp)
          Expires all cached entries notifying them about expiration.
 void expireFraction(long timestamp, float fraction)
          Expires the specified fraction of cached entries notifying them about expiration.
 ConcurrentMap<K,V> getCache(boolean... create)
          Gets and optionally creates the cache.
 String getDefaultEncoding()
          Gets the default encoding to apply when not explicitly specified.
 long getRefreshed()
          Gets the last refresh timestamp.
 ObjectName getSharedCacheName()
          Gets the the shared cache of this client.
protected  String getTemplateEncoding(String template)
          Gets the template encoding for the speficied template.
 boolean isCachingDisabled()
          Checks whether the caching is disabled.
 boolean isStarted()
          Checks whether the startable has been started.
protected abstract  void process(CharSequence content, TemplateContext context, String template, String encoding, Writer writer)
          Subclasses implement this method to process the specified run-time content or target template.
 void refresh()
          Refreshes the MBean.
 void removeCache()
          Removes the cache.
 String resolveLocalizedTemplate(String template, Locale locale)
          Resolves a localized template name.
 void setCachingDisabled(boolean flag)
          Sets the caching disabled flag.
 void setDefaultEncoding(String encoding)
          Sets the default encoding to apply when not explicitly specified.
 void setSharedCacheName(ObjectName cache)
          Sets the shared cache of this client.
 void start()
          Starts the MBean.
 void stop()
          Stops the MBean.
abstract  boolean templateExists(String template)
          Checks whether a specific template exists.
 
Methods inherited from class org.norther.tammi.core.config.DefaultConfigurable
addProperty, addProperty, addPropertyFilePath, addPropertyFilePath, checkConfiguration, clearProperties, containsProperty, getConfigKey, getConfiguration, getModifiedState, getProperties, getProperty, getPropertyFilePath, getPropertyFilePaths, indexOfProperty, loadConfiguration, loadConfiguration, loadConfiguration, modified, propertyMap, propertyMap, removeProperty, removeProperty, removePropertyFilePath, setConfigKey, setModifiedState, setProperties, setProperty, setPropertyFilePath, setPropertyFilePath, setPropertyFilePaths, setPropertyFilePaths, storeProperties, validateConfiguration
 
Methods inherited from class org.norther.tammi.core.base.Adaptee
addAdaptee, addNotificationListener, getAttributeSupport, getBroker, getCanonicalName, getDomain, getFactory, getLoader, getLog, getLog, getMBean, getMBeanServer, getNotificationInfo, getObjectName, getRegistrationTime, getSequenceNumber, hasListeners, isRegistered, postmanaged, premanaged, removeNotificationListener, removeNotificationListener, sendNotification, sendNotification, sendNotification, sendNotification, unmanaged, unregister
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.norther.tammi.core.config.Configurable
addProperty, addProperty, addPropertyFilePath, addPropertyFilePath, clearProperties, containsProperty, getConfigKey, getProperties, getProperty, getPropertyFilePath, getPropertyFilePaths, indexOfProperty, propertyMap, propertyMap, removeProperty, removeProperty, removePropertyFilePath, setConfigKey, setProperties, setProperty, setPropertyFilePath, setPropertyFilePath, setPropertyFilePaths, setPropertyFilePaths, storeProperties
 

Constructor Detail

AbstractTemplateEngine

public AbstractTemplateEngine()
Constructs a new engine.

Method Detail

isCachingDisabled

public boolean isCachingDisabled()
Description copied from interface: CacheClient
Checks whether the caching is disabled.

Specified by:
isCachingDisabled in interface CacheClient<K,V>
Returns:
true if the caching is disabled, true otherwise.

setCachingDisabled

public void setCachingDisabled(boolean flag)
Description copied from interface: CacheClient
Sets the caching disabled flag.

Specified by:
setCachingDisabled in interface CacheClient<K,V>
Parameters:
flag - the caching disabled flag.

expireCached

public void expireCached(long timestamp)
Description copied from interface: CacheClient
Expires all cached entries notifying them about expiration.

Specified by:
expireCached in interface CacheClient<K,V>
Parameters:
timestamp - the timestamp of the expiration.

expireFraction

public void expireFraction(long timestamp,
                           float fraction)
Description copied from interface: CacheClient
Expires the specified fraction of cached entries notifying them about expiration.

Specified by:
expireFraction in interface CacheClient<K,V>
Parameters:
timestamp - the timestamp of the expiration.
fraction - the fraction of entries to expire (0.0..1.0).

clearCached

public void clearCached()
Description copied from interface: CacheClient
Clears all cached entries without notifying.

Specified by:
clearCached in interface CacheClient<K,V>

getCache

public ConcurrentMap<K,V> getCache(boolean... create)
Description copied from interface: CacheClient
Gets and optionally creates the cache.

Specified by:
getCache in interface CacheClient<K,V>
Parameters:
create - true to create a missing cache.
Returns:
the cache or null if disabled.

removeCache

public void removeCache()
Description copied from interface: CacheClient
Removes the cache.

Specified by:
removeCache in interface CacheClient<K,V>

getSharedCacheName

public ObjectName getSharedCacheName()
Description copied from interface: CacheClient
Gets the the shared cache of this client.

Specified by:
getSharedCacheName in interface CacheClient<K,V>
Returns:
the object name of the shared cache or a query.

setSharedCacheName

public void setSharedCacheName(ObjectName cache)
Description copied from interface: CacheClient
Sets the shared cache of this client. If it is not set, the default one will be used.

Specified by:
setSharedCacheName in interface CacheClient<K,V>
Parameters:
cache - the object name of the shared cache or a query.

refresh

public void refresh()
Description copied from interface: Refreshable
Refreshes the MBean.

Specified by:
refresh in interface Refreshable

getRefreshed

public long getRefreshed()
Description copied from interface: Refreshable
Gets the last refresh timestamp.

Specified by:
getRefreshed in interface Refreshable
Returns:
the timestamp of the last refresh.

isStarted

public boolean isStarted()
Description copied from interface: Startable
Checks whether the startable has been started.

Specified by:
isStarted in interface Startable
Returns:
true if started, otherwise false.

start

public void start()
           throws Exception
Description copied from interface: Startable
Starts the MBean.

Specified by:
start in interface Startable
Throws:
Exception - if start fails.

stop

public void stop()
Description copied from interface: Startable
Stops the MBean.

Specified by:
stop in interface Startable

getDefaultEncoding

public String getDefaultEncoding()
Description copied from interface: TemplateEngine
Gets the default encoding to apply when not explicitly specified. If not set, the locale specific encoding of the template is used.

Specified by:
getDefaultEncoding in interface TemplateEngine
Returns:
the default encoding.

setDefaultEncoding

public void setDefaultEncoding(String encoding)
Description copied from interface: TemplateEngine
Sets the default encoding to apply when not explicitly specified. If not set, the locale specific encoding of the template is applied.

Specified by:
setDefaultEncoding in interface TemplateEngine
Parameters:
encoding - the default encoding.

templateExists

public abstract boolean templateExists(String template)
Description copied from interface: TemplateEngine
Checks whether a specific template exists.

Specified by:
templateExists in interface TemplateEngine
Parameters:
template - the name of the template.
Returns:
true, if the template is available.

resolveLocalizedTemplate

public String resolveLocalizedTemplate(String template,
                                       Locale locale)
Description copied from interface: TemplateEngine
Resolves a localized template name. It searches for a template having the given locale, or any part of it, embedded into its name. The best match will be returned. If a localized template is not found, the method searches for the plain template. The found name or null is returned.

Specified by:
resolveLocalizedTemplate in interface TemplateEngine
Parameters:
template - the name of the template.
locale - the locale to apply.
Returns:
the localized name, the plain name or null.

evaluate

public CharSequence evaluate(CharSequence content,
                             TemplateContext context,
                             String template)
                      throws TemplateException
Description copied from interface: TemplateEngine
Evaluates the given run-time content.

Specified by:
evaluate in interface TemplateEngine
Parameters:
content - the content.
context - the context.
template - a virtual template for identifying the content.
Returns:
the evaluated content.
Throws:
TemplateException - if processing fails.

evaluate

public CharSequence evaluate(TemplateContext context,
                             String template)
                      throws TemplateException
Description copied from interface: TemplateEngine
Evaluates the specified template.

Specified by:
evaluate in interface TemplateEngine
Parameters:
context - the context.
template - the name of the template.
Returns:
the evaluated template.
Throws:
TemplateException - if processing fails.

evaluate

public CharSequence evaluate(TemplateContext context,
                             String template,
                             String encoding)
                      throws TemplateException
Description copied from interface: TemplateEngine
Evaluates the specified template using the given encoding.

Specified by:
evaluate in interface TemplateEngine
Parameters:
context - the context.
template - the name of the template.
encoding - an optional template encoding.
Returns:
the evaluated template.
Throws:
TemplateException - if processing fails.

execute

public void execute(CharSequence content,
                    TemplateContext context,
                    String template,
                    Writer writer)
             throws TemplateException
Description copied from interface: TemplateEngine
Executes the given run-time content.

Specified by:
execute in interface TemplateEngine
Parameters:
content - the content.
context - the context.
template - a virtual template for identifying the content.
writer - a writer for the result.
Throws:
TemplateException - if processing fails.

execute

public void execute(TemplateContext context,
                    String template,
                    Writer writer)
             throws TemplateException
Description copied from interface: TemplateEngine
Executes the specified template.

Specified by:
execute in interface TemplateEngine
Parameters:
context - the context.
template - the name of the template.
writer - a writer for the result.
Throws:
TemplateException - if processing fails.

execute

public void execute(TemplateContext context,
                    String template,
                    String encoding,
                    Writer writer)
             throws TemplateException
Description copied from interface: TemplateEngine
Executes the specified template using the given encoding.

Specified by:
execute in interface TemplateEngine
Parameters:
context - the context.
template - the name of the template.
encoding - an optional template encoding.
writer - a writer for the result.
Throws:
TemplateException - if processing fails.

process

protected abstract void process(CharSequence content,
                                TemplateContext context,
                                String template,
                                String encoding,
                                Writer writer)
                         throws TemplateException
Subclasses implement this method to process the specified run-time content or target template.

Note that implementations should catch TemplateBreakExceptions.

Parameters:
content - optional run-time content.
context - the template context.
template - the target template.
encoding - the template encoding.
writer - the content writer.
Throws:
TemplateException - on errors.

getTemplateEncoding

protected String getTemplateEncoding(String template)
Gets the template encoding for the speficied template.

Parameters:
template - the (localized) name of the template.
Returns:
the template encoding.


Copyright © 2004 The Norther Organization. All rights reserved.