jetspeedtemplateservice.java
来自「jetspeed源代码」· Java 代码 · 共 722 行 · 第 1/2 页
JAVA
722 行
package org.apache.jetspeed.services.template;
/*
* Copyright 2000-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Java Core Classes
import java.util.ArrayList;
import java.util.Properties;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;
import java.io.File;
import javax.servlet.ServletConfig;
// Turbine Utility Classes
import org.apache.turbine.util.ServletUtils;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.resources.TurbineResources;
import org.apache.turbine.modules.ScreenLoader;
import org.apache.turbine.modules.NavigationLoader;
// Jetspeed classes
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
/**
* <p>This service extends the TurbineTemplateService to modify its behaviour:
* Not only layout and screen packages, but also the screen templates
* are searched in the template neames filepath, so that a fallback
* strategy is provided, that can be used for multi-language, multi-device
* and browser-specific support support.</p>
* <p>E.g: a template name "/html/en/US/IE/mytemplate" would search for
* following files (in the given order):
* <ol>
* <li>. /html/en/US/IE/mytemplate</li>
* <li>. /html/en/US/mytemplate</li>
* <li>. /html/en/mytemplate</li>
* <li>. /html/mytemplate</li>
* <li>. /mytemplate</li>
* </ol>
* </p>
* <p>
* TurbineTemplateService part:
* @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
* @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
* JetspeedTemplateService part:
* @author <a href="mailto:ingo@apache.org">Ingo Schuster</a>
* @version $Id: JetspeedTemplateService.java,v 1.11 2004/02/23 03:38:54 jford Exp $
*/
public class JetspeedTemplateService
extends TurbineBaseService
// implements TemplateService
// removed dst: 2001/06/03, TDK 2.2 integration
{
/**
* Static initialization of the logger for this class
*/
private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedTemplateService.class.getName());
/** The hashtable used to cache Screen names. */
private Hashtable screenCache = null;
/** The hashtable used to cache screen template names. */
private Hashtable templateCache = null;
/** The hashtable used to cache Navigation names. */
private Hashtable navCache = null;
/** The hashtable used to cache layout template names. */
private Hashtable layoutCache = null;
/** Flag set if cache is to be used. */
private boolean useCache = false;
/** Default extension. */
private String extension;
/** Default layout template. */
private String defaultLayoutTemplate;
/** Default Navigation module. */
private String defaultNavigation;
/** Default Screen module. */
private String defaultScreen;
/**
* The absolute paths where the appropriate template engine will
* be searching for templates.
*/
private String[] templateRoot = null;
/**
* Called the first time the Service is used.
*
* @param config A ServletConfig.
*/
public void init(ServletConfig config)
throws InitializationException
{
try
{
initTemplate(config);
setInit(true);
logger.info ("TemplateService init()....finished!");
}
catch (Exception e)
{
logger.error( "TurbineTemplateService failed to initialize", e );
throw new InitializationException("TurbineTemplateService failed to initialize", e);
}
}
/**
* TODO: Document this class.
*
* @param config A ServletConfig.
* @exception Exception, a generic exception.
*/
private void initTemplate(ServletConfig config)
throws Exception
{
useCache = TurbineResources.getBoolean("modules.cache", true);
Properties props = getProperties();
if (useCache)
{
int layoutSize = Integer
.parseInt(props.getProperty("layout.cache.size", "5"));
int navigationSize = Integer
.parseInt(props.getProperty("navigation.cache.size", "10"));
int screenSize = Integer
.parseInt(props.getProperty("screen.cache.size", "5"));
int templateSize = Integer
.parseInt(props.getProperty("screen.cache.size", "50"));
layoutCache = new Hashtable( (int)(1.25*layoutSize) + 1);
navCache = new Hashtable( (int)(1.25*navigationSize) + 1);
screenCache = new Hashtable( (int)(1.25*screenSize) + 1);
templateCache = new Hashtable( (int)(1.25*templateSize) + 1);
}
// relative to the webapp root directory
String templatePaths = props
.getProperty("template.path", "/templates");
// If possible, transform paths to be webapp root relative.
templatePaths = ServletUtils.expandRelative(config,
templatePaths);
// store the converted paths in service properties for
// Turbine based providers
props.put("template.path", templatePaths);
// tokenize the template.path property and assign to an array
String pathSep = System.getProperty("path.separator");
StringTokenizer st = new StringTokenizer(templatePaths,pathSep);
templateRoot = new String[st.countTokens()];
int pos = 0;
while(st.hasMoreTokens())
{
templateRoot[pos++] = st.nextToken();
}
// the extension that is added to layout templates (e.g.)
extension = props.getProperty("default.extension", "html");
// the default modules
defaultNavigation = props
.getProperty("default.navigation", "TemplateNavigation");
defaultScreen = props.getProperty("default.screen", "TemplateScreen");
// the default layout template
defaultLayoutTemplate = props
.getProperty("default.layout.template", "/default." + extension);
if (defaultLayoutTemplate.indexOf('.') == -1)
{
defaultLayoutTemplate = defaultLayoutTemplate + "." + extension;
}
}
/**
* Adds the object into the hashtable.
*
* @param key The String key for the object.
* @param value The Object.
* @param h The Hashtable.
*/
private void addToCache ( String key,
Object value,
Hashtable h )
{
if (useCache && value != null)
{
h.put(key, value);
}
}
/**
* Get the Screen template given in the properties file.
*
* @return A String which is the value of the TemplateService
* default.screen property.
*/
public String getDefaultScreen()
{
return defaultScreen;
}
/**
* Get the default Navigation given in the properties file.
*
* @return A String which is the value of the TemplateService
* default.navigation property.
*/
public String getDefaultNavigation()
{
return defaultNavigation;
}
/**
* Get the default layout template given in the properties file.
*
* @return A String which is the value of the TemplateService
* default.layout.template property.
*/
public String getDefaultLayoutTemplate()
{
return defaultLayoutTemplate;
}
/**
* Locate and return the name of a screen template.
*
*
* @param name A String which is the key to the template.
* @return A String with the screen template path.
* @exception Exception, a generic exception.
*/
public String getScreenTemplateName(String key)
throws Exception
{
if (name==null)
throw new Exception ("TurbineTemplateService: " +
"getLayoutTemplateName() was passed in a null value.");
String name = null;
if ( useCache && templateCache.containsKey(key) )
{
name = (String)templateCache.get(key);
}
else
{
if ( logger.isDebugEnabled() )
{
logger.debug("JetspeedTemplatePage.getLayoutTemplateName(" + key + ")");
}
String[] names = parseScreenTemplate(key);
name = names[2];
addToCache( key, names[0], screenCache );
addToCache( key, names[1], layoutCache );
addToCache( key, names[2], templateCache );
}
return name;
}
/**
* Locate and return the name of a layout template.
*
*
* @param name A String with the name of the template.
* @return A String with the layout template path.
* @exception Exception, a generic exception.
*/
public String getLayoutTemplateName(String name)
throws Exception
{
if (name==null)
throw new Exception ("TurbineTemplateService: " +
"getLayoutTemplateName() was passed in a null value.");
String layoutName = null;
if ( useCache && layoutCache.containsKey(name) )
{
layoutName = (String)layoutCache.get(name);
}
else
{
String[] names = parseScreenTemplate(name);
layoutName = names[1];
addToCache( name, names[0], screenCache );
addToCache( name, names[1], layoutCache );
addToCache( name, names[2], templateCache );
}
return layoutName;
}
/**
* Locate and return the name of a Navigation module.
*
* @param name A String with the name of the template.
* @return A String with the name of the navigation.
* @exception Exception, a generic exception.
*/
public String getNavigationName(String name)
throws Exception
{
if (name==null)
throw new Exception ("TurbineTemplateService: " +
"getNavigationName() was passed in a null value.");
String nav_name = null;
if ( useCache && navCache.containsKey(name) )
{
nav_name = (String)navCache.get(name);
}
else
{
nav_name = parseNavigationTemplate(name);
addToCache( name, nav_name, navCache );
}
return nav_name;
}
/**
* Locate and return the name of a Screen module.
*
* @param name A String with the name of the template.
* @return A String with the name of the screen.
* @exception Exception, a generic exception.
*/
public String getScreenName(String name)
throws Exception
{
if (name==null)
throw new Exception ("TurbineTemplateService: " +
"getScreenName() was passed in a null value.");
String screenName = null;
if ( useCache && screenCache.containsKey(name) )
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?