weaver 2003/03/27 08:54:24 Modified: src/java/org/apache/jetspeed/services/template JetspeedTemplateLocatorService.java Log: Bug ID: 18304 Fixes the template locator ignoring "default.layout.template" property of the asscoaite TemplateService when locating a templates associated layout. Ran Checkstyle, lots of tab removal and bracket additions to single line "if" constructs Revision Changes Path 1.16 +156 -71 jakarta-jetspeed/src/java/org/apache/jetspeed/services/template/JetspeedTemplateLocatorService.java Index: JetspeedTemplateLocatorService.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/template/JetspeedTemplateLocatorService.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- JetspeedTemplateLocatorService.java 4 Mar 2003 00:05:13 -0000 1.15 +++ JetspeedTemplateLocatorService.java 27 Mar 2003 16:54:24 -0000 1.16 @@ -58,6 +58,7 @@ import java.io.File; // java.util +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.ArrayList; @@ -76,11 +77,16 @@ import org.apache.turbine.services.TurbineServices; import org.apache.turbine.services.InitializationException; import org.apache.turbine.services.servlet.TurbineServlet; +import org.apache.turbine.services.jsp.JspService; import org.apache.turbine.services.localization.LocaleDetector; import org.apache.turbine.services.resources.TurbineResources; +import org.apache.turbine.services.template.BaseTemplateEngineService; import org.apache.turbine.services.template.TurbineTemplate; +import org.apache.turbine.services.velocity.TurbineVelocity; +import org.apache.turbine.services.velocity.VelocityService; // jetspeed.capability +import org.apache.commons.configuration.Configuration; import org.apache.jetspeed.capability.CapabilityMap; // jetspeed.services @@ -102,13 +108,15 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Raphael Luta</a> * @author <a href="mailto:[EMAIL PROTECTED]">Paul Spener</a> * @author <a href="mailto:[EMAIL PROTECTED]">Chris Kimpton</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Scott T. Weaver</a> * @version $Id$ */ -public class JetspeedTemplateLocatorService extends TurbineBaseService +public class JetspeedTemplateLocatorService + extends TurbineBaseService implements TemplateLocatorService { - private final static String CONFIG_TEMPLATE_ROOT = ".templateRoot"; + private final static String CONFIG_TEMPLATE_ROOT = ".templateRoot"; private final static String DIR_SCREENS = "/screens"; private final static String DIR_LAYOUTS = "/layouts"; private final static String DIR_PORTLETS = "/portlets"; @@ -123,6 +131,15 @@ private final static String MSG_MISSING_PARAMETER = "JetspeedTemplateLocatorService initialization failed. Missing parameter:"; + // Template Service Constants + private static final String TEMPLATE_EXTENSION = "template.extension"; + private static final String DEFAULT_LAYOUT = "default.layout.template"; + + // Template services + private static VelocityService velocityService; + + private static JspService jspService; + // the template root directories, webapp relative private String[] templateRoots; @@ -142,28 +159,31 @@ public synchronized void init(ServletConfig conf) throws InitializationException { // already initialized - if (getInit()) return; + if (getInit()) + { + return; + } initConfiguration(); // initialization done setInit(true); - } + } public void init() throws InitializationException { - Log.info( "Late init for JetspeedTemplateLocatorService called" ); - while( !getInit() ) + Log.info("Late init for JetspeedTemplateLocatorService called"); + while (!getInit()) { //Not yet... try { - Thread.sleep( 100 ); - Log.info( "Waiting for init of JetspeedTemplateLocatorService..." ); + Thread.sleep(100); + Log.info("Waiting for init of JetspeedTemplateLocatorService..."); } - catch (InterruptedException ie ) + catch (InterruptedException ie) { - Log.error( ie ); + Log.error(ie); } } } @@ -194,7 +214,7 @@ while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); located = locateTemplate(data, DIR_SCREENS, path, template); if (null == located) @@ -206,7 +226,7 @@ if (null != located) { // add it to the map - templateMap.put( located, null ); + templateMap.put(located, null); } useNameCache = true; @@ -227,7 +247,7 @@ template = "/default." + getTemplateExtension(template); while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); located = locateTemplate(data, DIR_SCREENS, path, template); if (null != located) @@ -258,7 +278,7 @@ while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); located = locateTemplate(data, DIR_LAYOUTS, path, template); if (null == located) @@ -270,7 +290,7 @@ if (null != located) { // add it to the map - templateMap.put( located, null ); + templateMap.put(located, null); } useNameCache = true; @@ -288,17 +308,19 @@ //something, search for the default layout i = templatePaths.iterator(); - template = "/default." + getTemplateExtension(template); + + //template = "/default." + getTemplateExtension(template); + template = getTemplateLayout(getTemplateExtension(template)); while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); located = locateTemplate(data, DIR_LAYOUTS, path, template); - if (null != located) - { - return located; - } - } + if (null != located) + { + return located; + } + } } return located; @@ -321,7 +343,7 @@ while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); String located = locateTemplate(data, DIR_NAVIGATIONS, path, template); if (null == located) @@ -333,7 +355,7 @@ if (null != located) { // add it to the map - templateMap.put( located, null ); + templateMap.put(located, null); } useNameCache = true; @@ -365,7 +387,7 @@ while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); String located = locateTemplate(data, DIR_PORTLETS, path, template); if (null == located) @@ -377,7 +399,7 @@ if (null != located) { // add it to the map - templateMap.put( located, null ); + templateMap.put(located, null); } useNameCache = true; @@ -409,7 +431,7 @@ while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); String located = locateTemplate(data, DIR_CONTROLS, path, template); if (null == located) @@ -421,7 +443,7 @@ if (null != located) { // add it to the map - templateMap.put( located, null ); + templateMap.put(located, null); } useNameCache = true; @@ -453,7 +475,7 @@ while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); String located = locateTemplate(data, DIR_CONTROLLERS, path, template); if (null == located) @@ -465,7 +487,7 @@ if (null != located) { // add it to the map - templateMap.put( located, null ); + templateMap.put(located, null); } useNameCache = true; @@ -492,7 +514,7 @@ */ public String locateEmailTemplate(RunData data, String template) { - return locateEmailTemplate( data, template, LocaleDetector.getLocale(data)); + return locateEmailTemplate(data, template, LocaleDetector.getLocale(data)); } /** @@ -508,12 +530,12 @@ */ public String locateEmailTemplate(RunData data, String template, Locale locale) { - List templatePaths = localizeTemplateName(data,locale); + List templatePaths = localizeTemplateName(data, locale); Iterator i = templatePaths.iterator(); while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); String located = locateTemplate(data, DIR_EMAILS, path, template); if (null == located) @@ -525,7 +547,7 @@ if (null != located) { // add it to the map - templateMap.put( located, null ); + templateMap.put(located, null); } useNameCache = true; @@ -538,7 +560,7 @@ } return null; - } + } /** * Locate a parameter style template using Jetspeed template location algorithm, searching by @@ -557,7 +579,7 @@ while (i.hasNext()) { - String path = (String)i.next(); + String path = (String) i.next(); String located = locateTemplate(data, DIR_PARAMETERS, path, template); if (null == located) @@ -568,7 +590,7 @@ if (null != located) { // add it to the map - templateMap.put( located, null ); + templateMap.put(located, null); } useNameCache = true; } @@ -598,9 +620,13 @@ String located = null; // Iterate through each of the template roots - for (int i = 0; i < templateRoots.length; i++) { + for (int i = 0; i < templateRoots.length; i++) + { located = locateTemplate(data, resourceType, path, template, templateRoots[i]); - if (located != null) break; + if (located != null) + { + break; + } } return located; @@ -618,14 +644,19 @@ * * @return the exact path to the template, or null if not found. */ - private String locateTemplate(RunData data, String resourceType, String path, String template, String templateRoot) + private String locateTemplate( + RunData data, + String resourceType, + String path, + String template, + String templateRoot) { String finalPath; // make sure resourceType doesn't end with "/" but starts with "/" if (resourceType.endsWith(PATH_SEPARATOR)) { - resourceType = resourceType.substring(0, resourceType.length()-1); + resourceType = resourceType.substring(0, resourceType.length() - 1); } if (!resourceType.startsWith(PATH_SEPARATOR)) { @@ -634,7 +665,7 @@ // make sure path doesn't end with "/" but starts with "/" if (path.endsWith(PATH_SEPARATOR)) { - path = path.substring(0, path.length()-1); + path = path.substring(0, path.length() - 1); } if (!path.startsWith(PATH_SEPARATOR)) { @@ -646,7 +677,7 @@ template = PATH_SEPARATOR + template; } - StringBuffer fullPath = new StringBuffer( templateRoot ); + StringBuffer fullPath = new StringBuffer(templateRoot); if (!templateRoot.endsWith(PATH_SEPARATOR)) { @@ -662,24 +693,28 @@ do { - workingPath = path+template; - realPath = TurbineServlet.getRealPath( basePath + workingPath ); + workingPath = path + template; + realPath = TurbineServlet.getRealPath(basePath + workingPath); // the current template exists, return the corresponding path if (templateExists(realPath)) { if (Log.getLogger().isDebugEnabled()) { - Log.debug("TemplateLocator: template exists: "+realPath+" returning "+workingPath); + Log.debug( + "TemplateLocator: template exists: " + + realPath + + " returning " + + workingPath); } return workingPath; } // else strip path of one of its components and loop int pt = path.lastIndexOf(PATH_SEPARATOR); - if ( pt > -1 ) + if (pt > -1) { - path = path.substring(0,pt); + path = path.substring(0, pt); } else { @@ -724,19 +759,20 @@ if (inLocale != null) { - tmplocale = inLocale; + tmplocale = inLocale; } else { - tmplocale = LocaleDetector.getLocale(data); + tmplocale = LocaleDetector.getLocale(data); } // Get the locale store it in the user object if (tmplocale == null) { - tmplocale = new Locale( - TurbineResources.getString("locale.default.language", "en"), - TurbineResources.getString("locale.default.country", "US")); + tmplocale = + new Locale( + TurbineResources.getString("locale.default.language", "en"), + TurbineResources.getString("locale.default.country", "US")); } data.getUser().setTemp("locale", tmplocale); @@ -746,10 +782,10 @@ // retrieve all the possible media types String type = data.getParameters().getString(Profiler.PARAM_MEDIA_TYPE, null); List types = new ArrayList(); - CapabilityMap cm = ((JetspeedRunData)data).getCapability(); + CapabilityMap cm = ((JetspeedRunData) data).getCapability(); // Grab the Locale from the temporary storage in the User object - Locale locale = (Locale)data.getUser().getTemp("locale"); + Locale locale = (Locale) data.getUser().getTemp("locale"); String language = locale.getLanguage(); String country = locale.getCountry(); @@ -760,7 +796,7 @@ else { Iterator i = cm.listMediaTypes(); - while(i.hasNext()) + while (i.hasNext()) { types.add(i.next()); } @@ -768,9 +804,9 @@ Iterator typeIterator = types.iterator(); - while(typeIterator.hasNext()) + while (typeIterator.hasNext()) { - type = (String)typeIterator.next(); + type = (String) typeIterator.next(); if ((type != null) && (type.length() > 0)) { @@ -808,7 +844,7 @@ if (idx > 0) { - ext = template.substring(idx+1); + ext = template.substring(idx + 1); } return ext; @@ -825,7 +861,10 @@ */ public boolean templateExists(String templateKey) { - if (null == templateKey) return false; + if (null == templateKey) + { + return false; + } if (useNameCache == true) { return templateMap.containsKey(templateKey); @@ -843,14 +882,15 @@ private void initConfiguration() throws InitializationException { - templateRoots = JetspeedResources.getStringArray(TurbineServices.SERVICE_PREFIX - + TemplateLocatorService.SERVICE_NAME - + CONFIG_TEMPLATE_ROOT); + templateRoots = + JetspeedResources.getStringArray( + TurbineServices.SERVICE_PREFIX + + TemplateLocatorService.SERVICE_NAME + + CONFIG_TEMPLATE_ROOT); if ((templateRoots == null) || (templateRoots.length == 0)) { - throw new InitializationException( - MSG_MISSING_PARAMETER + CONFIG_TEMPLATE_ROOT); + throw new InitializationException(MSG_MISSING_PARAMETER + CONFIG_TEMPLATE_ROOT); } templateMap = new HashMap(); @@ -866,16 +906,24 @@ if (Log.getLogger().isDebugEnabled()) { - Log.debug("TemplateLocator: Adding templateRoot:"+templateRoot); + Log.debug("TemplateLocator: Adding templateRoot:" + templateRoot); } // traverse starting from the root template directory and add resources - String templateRootPath = TurbineServlet.getRealPath( templateRoot ); + String templateRootPath = TurbineServlet.getRealPath(templateRoot); if (null != templateRootPath) { - loadNameCache( templateRootPath, "" ); + loadNameCache(templateRootPath, ""); } } + + + velocityService = + (VelocityService) TurbineServices.getInstance().getService( + VelocityService.SERVICE_NAME); + + jspService = (JspService) TurbineServices.getInstance().getService(JspService.SERVICE_NAME); + } /** @@ -890,7 +938,7 @@ if (file.isFile()) { // add it to the map - templateMap.put( path, null ); + templateMap.put(path, null); } else { @@ -904,11 +952,48 @@ String list[] = file.list(); // Process all files recursivly - for(int ix = 0; list != null && ix < list.length; ix++) + for (int ix = 0; list != null && ix < list.length; ix++) { loadNameCache(path + list[ix], list[ix]); } } + } + } + + /** + * Correctly locate the default layout based on the + * default.layout.template property of the appropriate + * template service. + * @author <a href="mailto:[EMAIL PROTECTED]">Scott Weaver</a> + */ + private String getTemplateLayout(String extension) + { + String dftLayout = "/default." + extension; + + Configuration velocityCfg = null; + Configuration jspCfg = null; + if (velocityService != null) + { + velocityCfg = velocityService.getConfiguration(); + } + + if (jspService != null) + { + jspCfg = jspService.getConfiguration(); + } + + if (velocityCfg != null + && velocityCfg.getString(TEMPLATE_EXTENSION).indexOf(extension) > -1) + { + return velocityCfg.getString(DEFAULT_LAYOUT, dftLayout); + } + else if (jspCfg != null && jspCfg.getString(TEMPLATE_EXTENSION).indexOf(extension) > -1) + { + return jspCfg.getString(DEFAULT_LAYOUT, dftLayout); + } + else + { + return dftLayout; } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]