portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tay...@apache.org
Subject cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/services/template JetspeedTemplateLocatorService.java TemplateLocatorService.java
Date Sat, 07 Jul 2001 21:06:23 GMT
taylor      01/07/07 14:06:22

  Modified:    src/java/org/apache/jetspeed/services Profiler.java
               src/java/org/apache/jetspeed/services/profiler
                        JetspeedProfilerService.java ProfilerService.java
  Added:       src/java/org/apache/jetspeed/services TemplateLocator.java
               src/java/org/apache/jetspeed/services/template
                        JetspeedTemplateLocatorService.java
                        TemplateLocatorService.java
  Log:
  - Created new TemplateLocatorService interface and Jetspeed implementation (JetspeedTemplateLocatorService.java), decoupling template location from profiler
  - Removed all template location methods from Profiler interfaces and implementations
  
  Revision  Changes    Path
  1.8       +3 -52     jakarta-jetspeed/src/java/org/apache/jetspeed/services/Profiler.java
  
  Index: Profiler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/Profiler.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Profiler.java	2001/07/02 07:40:43	1.7
  +++ Profiler.java	2001/07/07 21:06:17	1.8
  @@ -67,12 +67,12 @@
   
   /**
    * <P>This is a commodity static accessor class around the 
  - * <code>ProfilerService</code> implementing the ProfilerService interface</P>
  + * <code>ProfilerService</code> interface</P>
    * 
    * @see org.apache.jetspeed.services.profiler.Profiler
    * @see org.apache.jetspeed.services.profiler.ProfilerService
  - * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
  - * @version $Id: Profiler.java,v 1.7 2001/07/02 07:40:43 taylor Exp $
  + * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
  + * @version $Id: Profiler.java,v 1.8 2001/07/07 21:06:17 taylor Exp $
    */
   public class Profiler
   {
  @@ -223,54 +223,5 @@
       {
           return getService().getResourceNameCaching();
       }
  -
  -    /**
  -     * @see ProfilerService#locateTemplate
  -     */
  -    public static String locateTemplate(RunData data, String resourceType, String path, String template)
  -    {
  -        return getService().locateTemplate(data, resourceType, path, template);
  -    }
  -
  -    /**
  -     * @see ProfilerService#locateScreenTemplate
  -     */
  -    public static String locateScreenTemplate(RunData data, String template)
  -    {
  -        return getService().locateScreenTemplate(data, template);
  -    }
  -
  -    /**
  -     * @see ProfilerService#locateLayoutTemplate
  -     */
  -    public static String locateLayoutTemplate(RunData data, String template)
  -    {
  -        return getService().locateLayoutTemplate(data, template);
  -    }
  -
  -    /**
  -     * @see ProfilerService#locatePortletTemplate
  -     */
  -    public static String locatePortletTemplate(RunData data, String template)
  -    {
  -        return getService().locatePortletTemplate(data, template);
  -    }
  -
  -    /**
  -     * @see ProfilerService#locateControlTemplate
  -     */
  -    public static String locateControlTemplate(RunData data, String template)
  -    {
  -        return getService().locateControlTemplate(data, template);
  -    }
  -
  -    /**
  -     * @see ProfilerService#locateControllerTemplate
  -     */
  -    public static String locateControllerTemplate(RunData data, String template)
  -    {
  -        return getService().locateControllerTemplate(data, template);
  -    }
  -
   
   }
  
  
  
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/services/TemplateLocator.java
  
  Index: TemplateLocator.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *     "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache" or
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.jetspeed.services;
  
  import org.apache.jetspeed.services.template.TemplateLocatorService;
  import org.apache.turbine.services.TurbineServices;
  import org.apache.turbine.util.RunData;
  
  /**
   * <P>This is a commodity static accessor class around the 
   * <code>TemplateLocatorService</code> interface</P>
   * 
   * @see org.apache.jetspeed.services.template.TemplateLocatorService
   *
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
   * @version $Id: TemplateLocator.java,v 1.1 2001/07/07 21:06:18 taylor Exp $
   */
  public class TemplateLocator
  {
      /** 
       * Commodity method for getting a reference to the service
       * singleton
       */
      private static TemplateLocatorService getService()
      {
          return (TemplateLocatorService)TurbineServices
                  .getInstance()
                  .getService(TemplateLocatorService.SERVICE_NAME);
      }
  
      /**
       * @see TemplateLocatorService#locateScreenTemplate
       */
      public static String locateScreenTemplate(RunData data, String template)
      {
          return getService().locateScreenTemplate(data, template);
      }
  
      /**
       * @see TemplateLocatorService#locateLayoutTemplate
       */
      public static String locateLayoutTemplate(RunData data, String template)
      {
          return getService().locateLayoutTemplate(data, template);
      }
  
      /**
       * @see TemplateLocatorService#locatePortletTemplate
       */
      public static String locatePortletTemplate(RunData data, String template)
      {
          return getService().locatePortletTemplate(data, template);
      }
  
      /**
       * @see TemplateLocatorService#locateControlTemplate
       */
      public static String locateControlTemplate(RunData data, String template)
      {
          return getService().locateControlTemplate(data, template);
      }
  
      /**
       * @see TemplateLocatorService#locateControllerTemplate
       */
      public static String locateControllerTemplate(RunData data, String template)
      {
          return getService().locateControllerTemplate(data, template);
      }
  
  
  }
  
  
  
  1.9       +45 -294   jakarta-jetspeed/src/java/org/apache/jetspeed/services/profiler/JetspeedProfilerService.java
  
  Index: JetspeedProfilerService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/profiler/JetspeedProfilerService.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- JetspeedProfilerService.java	2001/07/02 07:40:45	1.8
  +++ JetspeedProfilerService.java	2001/07/07 21:06:19	1.9
  @@ -54,43 +54,61 @@
   
   package org.apache.jetspeed.services.profiler;
   
  -//jetspeed stuff
  -import org.apache.jetspeed.om.profile.*;
  -import org.apache.jetspeed.services.Profiler;
  -import org.apache.jetspeed.capability.CapabilityMap;
  -import org.apache.jetspeed.capability.CapabilityMapFactory;
  -import org.apache.jetspeed.services.resources.JetspeedResources;
  -import org.apache.jetspeed.services.rundata.JetspeedRunData;
  -import org.apache.jetspeed.util.MimeType;
  -import org.apache.jetspeed.services.Registry;
  -import org.apache.jetspeed.om.newregistry.RegistryException;
  -import org.apache.jetspeed.om.newregistry.MediaTypeEntry;
  -import org.apache.jetspeed.util.FileCopy;
  -import org.apache.jetspeed.util.DirectoryUtils;
  -
  -//turbine stuff
  -import org.apache.turbine.util.Log;
  -import org.apache.turbine.services.*;
  -import org.apache.turbine.util.RunData;
  -import org.apache.turbine.util.DynamicURI;
  -import org.apache.turbine.om.security.User;
  -import org.apache.turbine.services.servlet.TurbineServlet;
  -import org.apache.turbine.services.localization.LocaleDetector;
  -import org.apache.turbine.services.resources.TurbineResources;
  -import org.apache.turbine.services.template.TurbineTemplate;
  -
  -//java stuff
  +//java.util
   import java.util.Map;
   import java.util.HashMap;
   import java.util.Enumeration;
   import java.util.Locale;
  +
  +// java.io
   import java.io.File;
   import java.io.IOException;
  +
  +// java.net
   import java.net.URL;
   import java.net.MalformedURLException;
   import java.lang.reflect.Method;
   import javax.servlet.ServletConfig;
   
  +//turbine.util
  +import org.apache.turbine.util.Log;
  +import org.apache.turbine.util.RunData;
  +import org.apache.turbine.util.DynamicURI;
  +
  +// turbine.services
  +import org.apache.turbine.services.Service;
  +import org.apache.turbine.services.TurbineBaseService;
  +import org.apache.turbine.services.TurbineServices;
  +import org.apache.turbine.services.InitializationException;
  +import org.apache.turbine.services.servlet.TurbineServlet;
  +import org.apache.turbine.services.localization.LocaleDetector;
  +import org.apache.turbine.services.resources.TurbineResources;
  +import org.apache.turbine.services.template.TurbineTemplate;
  +
  +// turbine.om
  +import org.apache.turbine.om.security.User;
  +
  +// jetspeed.om
  +import org.apache.jetspeed.om.profile.*;
  +import org.apache.jetspeed.om.newregistry.RegistryException;
  +import org.apache.jetspeed.om.newregistry.MediaTypeEntry;
  +
  +// jetspeed.capability
  +import org.apache.jetspeed.capability.CapabilityMap;
  +import org.apache.jetspeed.capability.CapabilityMapFactory;
  +
  +// jetspeed.services
  +import org.apache.jetspeed.services.Profiler;
  +import org.apache.jetspeed.services.resources.JetspeedResources;
  +import org.apache.jetspeed.services.rundata.JetspeedRunData;
  +import org.apache.jetspeed.services.Registry;
  +
  +// jetspeed.util
  +import org.apache.jetspeed.util.MimeType;
  +import org.apache.jetspeed.util.FileCopy;
  +import org.apache.jetspeed.util.DirectoryUtils;
  +
  +
   /**
    * <p>This is an implementation of the <code>Profiler</code> interface.
    *
  @@ -111,7 +129,7 @@
    *
    * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
    * @author <a href="mailto:sgala@hisitech.com">Santiago Gala</a>
  - * @version $Id: JetspeedProfilerService.java,v 1.8 2001/07/02 07:40:45 taylor Exp $
  + * @version $Id: JetspeedProfilerService.java,v 1.9 2001/07/07 21:06:19 taylor Exp $
    */
   
   public class JetspeedProfilerService  extends TurbineBaseService
  @@ -126,7 +144,6 @@
       public final static String TEMP_USER                = "temp_user";
       // configuration keys
       private final static String CONFIG_ROOT             = ".root";
  -    private final static String CONFIG_TEMPLATE_ROOT    = ".templateRoot";
       private final static String CONFIG_RESOURCE_DEFAULT = ".resource.default";
       private final static String CONFIG_RESOURCE_EXT     = ".resource.ext";
       private final static String CONFIG_SECURITY         = ".security";
  @@ -151,7 +168,6 @@
   
       // configuration parameters
        String root;                   // the root psml resource directory
  -     String templateRoot;
        String resourceDefault;        // the default name for a resource
        String resourceExt;            // the default extension for a resource
        boolean useSecurity = false;   // use security features
  @@ -166,7 +182,6 @@
   
       // fallback map of resources
       private Map resourceMap = null;
  -    private Map templateMap = null;
   
       /**
        * This is the early initialization method called by the 
  @@ -817,16 +832,6 @@
                                                  + ProfilerService.SERVICE_NAME
                                                  + CONFIG_NAMECACHE,
                                                  true);
  -
  -        templateRoot = JetspeedResources.getString(TurbineServices.SERVICE_PREFIX
  -                                               + ProfilerService.SERVICE_NAME
  -                                               + CONFIG_TEMPLATE_ROOT);
  -        if (templateRoot == null)
  -            throw new InitializationException(
  -                MSG_MISSING_PARAMETER  + CONFIG_TEMPLATE_ROOT);
  -        if (!templateRoot.endsWith(PATH_SEPARATOR))
  -            templateRoot = templateRoot + PATH_SEPARATOR;
  -
       }
   
       /**
  @@ -846,11 +851,6 @@
           if (null != realRootPath)
               traverseResources( realRootPath, "", resourceMap );
   
  -        templateMap = new HashMap();
  -        // traverse starting from the root template directory and add resources
  -        realRootPath = TurbineServlet.getRealPath( templateRoot );
  -        if (null != realRootPath)
  -            traverseResources( realRootPath, "", templateMap );
       }
   
       /**
  @@ -927,255 +927,6 @@
               uri.addPathInfo(PARAM_PAGE, page);
   
           return uri;
  -    }
  -
  -    /**
  -     * Locate a screen template using the Profiler's fallback algorithm
  -     *
  -     * @param data the rundata for the request.
  -     * @param template the name of the template
  -     *
  -     * @return the exact path to the screen template, or null if not found
  -     */
  -    public String locateScreenTemplate(RunData data, String template)
  -    {
  -        String path = localizeTemplateName(data);
  -
  -        String located = locateTemplate(data, "/screens", path, template);
  -        if (null == located) 
  -        {
  -            // FIXME: need to get default.layout.template from TR.p
  -            template = "/default."+getTemplateExtension(template);
  -            located = locateTemplate(data, "/screens", path, template);
  -        }
  -
  -        return located;
  -    }
  -
  -    /**
  -     * Locate a layout template using the Profiler's fallback algorithm
  -     *
  -     * @param data the rundata for the request.
  -     * @param template the name of the template
  -     *
  -     * @return the exact path to the layout template, or null if not found
  -     */
  -    public String locateLayoutTemplate(RunData data, String template)
  -    {
  -        String path = localizeTemplateName(data);
  -
  -        String located = locateTemplate(data, "/layouts", path, template);
  -        if (null == located) 
  -        {
  -            // FIXME: need to get default.layout.template from TR.p
  -            template = "/default."+getTemplateExtension(template);
  -            located = locateTemplate(data, "/layouts", path, template);
  -        }
  -
  -        return located;
  -    }
  -
  -    /**
  -     * Locate a portlet template using the Profiler's fallback algorithm
  -     *
  -     * @param data the rundata for the request.
  -     * @param template the name of the template
  -     *
  -     * @return the exact path to the portlet template, or null if not found
  -     */
  -    public String locatePortletTemplate(RunData data, String template)
  -    {
  -        String path = localizeTemplateName(data);
  -
  -        path = locateTemplate(data, "/portlets", path, template);
  -        
  -        return "/portlets"+path;
  -    }
  -
  -    /**
  -     * Locate a controller template using the Profiler's fallback algorithm
  -     *
  -     * @param data the rundata for the request.
  -     * @param template the name of the template
  -     *
  -     * @return the exact path to the portlet template, or null if not found
  -     */
  -    public String locateControlTemplate(RunData data, String template)
  -    {
  -        String path = localizeTemplateName(data);
  -
  -        path = locateTemplate(data, "/controls", path, template);
  -        
  -        return "/controls"+path;
  -    }
  -
  -    /**
  -     * Locate a controller template using the Profiler's fallback algorithm
  -     *
  -     * @param data the rundata for the request.
  -     * @param template the name of the template
  -     *
  -     * @return the exact path to the portlet template, or null if not found
  -     */
  -    public String locateControllerTemplate(RunData data, String template)
  -    {
  -        String path = localizeTemplateName(data);
  -
  -        path = locateTemplate(data, "/controllers", path, template);
  -        
  -        return "/controllers"+path;
  -    }
  -
  -    /**
  -     * Locate a template using the Profiler's fallback algorithm
  -     *
  -     * @param resourceType the path specific to the resource type sought (eg /screens)
  -     * @param path the fullest path to the template based on simple NLS/mediatype directory
  -     * @param template the name of the template
  -     *
  -     * @return the exact path to the template, or null if not found
  -     */
  -    public String locateTemplate(RunData data, String resourceType, String path, String template)
  -    {
  -        String finalPath;
  -
  -        // make sure resourceType doesn't end with "/" but starts with "/"
  -        if (resourceType.endsWith(PATH_SEPARATOR))
  -        {
  -            resourceType = resourceType.substring(0, resourceType.length()-1);
  -        }
  -        if (!resourceType.startsWith(PATH_SEPARATOR))
  -        {
  -            resourceType = PATH_SEPARATOR + resourceType;
  -        }
  -        // make sure path doesn't end with "/" but starts with "/"
  -        if (path.endsWith(PATH_SEPARATOR))
  -        {
  -            path = path.substring(0, path.length()-1);
  -        }
  -        if (!path.startsWith(PATH_SEPARATOR))
  -        {
  -            path = PATH_SEPARATOR + path;
  -        }
  -        // make sure template starts with "/"
  -        if (!template.startsWith(PATH_SEPARATOR))
  -        {
  -            template = PATH_SEPARATOR + template;
  -        }
  -        
  -        StringBuffer fullPath = new StringBuffer( templateRoot );
  -
  -        if (!templateRoot.endsWith(PATH_SEPARATOR))
  -            fullPath.append(PATH_SEPARATOR);
  -        fullPath.append(getTemplateExtension(template));
  -
  -        fullPath.append(resourceType);
  -        
  -        String basePath = fullPath.toString();
  -        String realPath = null;
  -        String workingPath = null;
  -
  -        do
  -        {
  -            workingPath = path+template;
  -            realPath = TurbineServlet.getRealPath( basePath + workingPath );
  -
  -            // the current template exists, return the corresponding path
  -            if (templateExists(realPath))
  -            {
  -                Log.debug("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 )
  -            {
  -                path = path.substring(0,pt);
  -            }
  -            else
  -            {
  -                path = null;
  -            }
  -        }
  -        while (path != null);
  -        
  -        return null;
  -    }
  -
  -    /**
  -     * helper function for template locator to find a localized (NLS) resource
  -     *
  -     * @param path the fullest path to the template based on simple NLS/mediatype directory
  -     * @param template the name of the template
  -     *
  -     * @return the exact path to the template, or null if not found
  -     */
  -    private String localizeTemplateName(RunData data)
  -    {
  -        // Get the locale store it in the user object
  -        Locale tmplocale = LocaleDetector.getLocale(data);
  -        if (tmplocale == null) {
  -            tmplocale = new Locale(
  -                     TurbineResources.getString("locale.default.language", "en"),
  -                     TurbineResources.getString("locale.default.country", "US"));
  -        }
  -
  -        data.getUser().setTemp("locale", tmplocale);
  -
  -        StringBuffer templatePath = new StringBuffer();
  -
  -        // set the content type (including charset)
  -        CapabilityMap cm = CapabilityMapFactory.getCapabilityMap( data );
  -        String type = MimeType.getCode( cm.getPreferredType() );
  -        data.setContentType( cm.getPreferredType().toString() );  
  -        if ((type != null) && (type.length() > 0)) 
  -            templatePath.append(PATH_SEPARATOR).append(type);
  -        
  -        // Grab the Locale from the temporary storage in the User object
  -        Locale locale = (Locale)data.getUser().getTemp("locale");
  -        String language = locale.getLanguage();
  -        String country = locale.getCountry();
  -        if ((language != null) && (language.length() > 0)) 
  -            templatePath.append(PATH_SEPARATOR).append(language);
  -        if ((country != null) && (country.length() > 0)) 
  -            templatePath.append(PATH_SEPARATOR).append(country);
  -
  -        return  templatePath.toString();
  -    }
  -
  -    /** 
  -     * Returns the extension for the specified template
  -     *
  -     * @param template the template name to scan for an extension
  -     * @return the template extension if it exists or the default 
  -     * template extension
  -     */
  -    private String getTemplateExtension(String template)
  -    {
  -        String ext = TurbineTemplate.getDefaultExtension();
  -        
  -        int idx = template.lastIndexOf(".");
  -        
  -        if (idx > 0)
  -        {
  -            ext = template.substring(idx+1);
  -        }
  -        
  -        return ext;
  -    }
  -    
  -    /**
  -     * checks for the existence of a template resource given a key
  -     *
  -     * @parameter key the resource name.
  -     */
  -    public boolean templateExists(String templateKey)
  -    {
  -        if (useNameCache == true)
  -        {
  -            return templateMap.containsKey(templateKey);
  -        }
  -        return (new File(templateKey).exists());
       }
   
      /**
  
  
  
  1.7       +1 -16     jakarta-jetspeed/src/java/org/apache/jetspeed/services/profiler/ProfilerService.java
  
  Index: ProfilerService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/profiler/ProfilerService.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ProfilerService.java	2001/07/02 07:40:46	1.6
  +++ ProfilerService.java	2001/07/07 21:06:20	1.7
  @@ -69,7 +69,7 @@
    * 
    * @see org.apache.jetspeed.om.profile.Profile
    * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
  - * @version $Id: ProfilerService.java,v 1.6 2001/07/02 07:40:46 taylor Exp $
  + * @version $Id: ProfilerService.java,v 1.7 2001/07/07 21:06:20 taylor Exp $
    */
   
   public interface ProfilerService extends Service
  @@ -211,20 +211,5 @@
        * @return the resource caching status: true in on, false is off
        */
       public boolean getResourceNameCaching();
  -
  -    /**
  -     * Locate a template using the Profiler's fallback algorithm
  -     *
  -     * @param resourceType the path specific to the resource type sought (eg /screens)
  -     * @param path the fullest path to the template based on simple NLS/mediatype directory
  -     * @param template the name of the template
  -     * @return the exact path to the found template
  -     */
  -    public String locateTemplate(RunData data, String resourceType, String path, String template);
  -    public String locateScreenTemplate(RunData data, String template);
  -    public String locateLayoutTemplate(RunData data, String template);
  -    public String locatePortletTemplate(RunData data, String template);
  -    public String locateControlTemplate(RunData data, String template);
  -    public String locateControllerTemplate(RunData data, String template);
   
   }
  
  
  
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/services/template/JetspeedTemplateLocatorService.java
  
  Index: JetspeedTemplateLocatorService.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *     "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache" or
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.jetspeed.services.template;
  
  // java.io
  import java.io.File;
  import java.io.IOException;
  
  // java.util
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Locale;
  
  import javax.servlet.ServletConfig;
  
  // turbine.util
  import org.apache.turbine.util.Log;
  import org.apache.turbine.util.RunData;
  import org.apache.turbine.util.DynamicURI;
  
  // turbine.om
  import org.apache.turbine.om.security.User;
  
  // turbine.services
  import org.apache.turbine.services.Service;
  import org.apache.turbine.services.TurbineBaseService;
  import org.apache.turbine.services.TurbineServices;
  import org.apache.turbine.services.InitializationException;
  import org.apache.turbine.services.servlet.TurbineServlet;
  import org.apache.turbine.services.localization.LocaleDetector;
  import org.apache.turbine.services.resources.TurbineResources;
  import org.apache.turbine.services.template.TurbineTemplate;
  
  // jetspeed.capability
  import org.apache.jetspeed.capability.CapabilityMap;
  import org.apache.jetspeed.capability.CapabilityMapFactory;
  
  // jetspeed.util
  import org.apache.jetspeed.util.MimeType;
  
  // jetspeed.services
  import org.apache.jetspeed.services.resources.JetspeedResources;
  import org.apache.jetspeed.services.rundata.JetspeedRunData;
  
  /**
   * <p>Implements all template location related operations.
   * Template location algorithms are different from the Velocity template location,
   * since Jetspeed has a specialized template directory structure.
   * This is a fix to get us through unti the TurbineTemplateService can locate
   * resources by NLS and mediatype. Then it can be removed</p>
   *
   * <p>The directory structure is currently layout out in the following order:
   *    /templateType/mediaType/LanguageCode/CountryCode</p>
   * <p>Example: /screens/html/en/US/resource.vm</p>
   *
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
   * @author <a href="mailto:rapahel@apache.org">Raphael Luta</a>
   * @version $Id: JetspeedTemplateLocatorService.java,v 1.1 2001/07/07 21:06:22 taylor Exp $
   */
  
  public class JetspeedTemplateLocatorService   extends TurbineBaseService
      implements TemplateLocatorService
  {
      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";
      private final static String DIR_CONTROLS = "/controls";
      private final static String DIR_CONTROLLERS = "/controllers";
      private static final String PATH_SEPARATOR = "/";
  
      // messages
      private final static String MSG_MISSING_PARAMETER =
          "JetspeedTemplateLocatorService initialization failed. Missing parameter:";
  
      // the template root directory, webapp relative
      private String templateRoot;
      // the absolute path to the template root directory
      private String templateRootPath;
      // use the name cache when looking up a template
      private boolean useNameCache = true;
  
      // template name cache
      private Map templateMap = null;
  
      /**
       * This is the early initialization method called by the 
       * Turbine <code>Service</code> framework
       * @param conf The <code>ServletConfig</code>
       * @exception throws a <code>InitializationException</code> if the service
       * fails to initialize
       */
      public synchronized void init(ServletConfig conf) throws InitializationException {
  
          // already initialized
          if (getInit()) return;
  
          initConfiguration();        
  
          // initialization done
          setInit(true);
  
       }
  
      public void init() throws InitializationException {
          Log.note( "Late init for JetspeedTemplateLocatorService called" );
          while( !getInit() ) {
              //Not yet...
              try {
                  Thread.sleep( 100 );
                  Log.note( "Waiting for init of JetspeedTemplateLocatorService..." );
              } catch (InterruptedException ie ) {
                  Log.error( ie );
              }
          }
      }
  
      /**
       * This is the shutdown method called by the 
       * Turbine <code>Service</code> framework
       */
      public void shutdown() {
      }
  
      /**
       * Locate a screen template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the screens directory for the requested screen template,
       *          or null if not found.
       */
      public String locateScreenTemplate(RunData data, String template)
      {
          String path = localizeTemplateName(data);
  
          String located = locateTemplate(data, DIR_SCREENS, path, template);
          if (null == located) 
          {
              // Try to locate it directly on file system, perhaps it was recently added
              useNameCache = false;
              located = locateTemplate(data, DIR_SCREENS, path, template);
              if (null != located)
              {
                  // add it to the map
                  templateMap.put( located, null );
              }
              else
              {
                  template = "/default." + getTemplateExtension(template);
                  located = locateTemplate(data, DIR_SCREENS, path, template);
              }
              useNameCache = true;
          }
  
          return located;
      }
  
      /**
       * Locate a layout template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the layouts directory for the requested layout template,
       *          or null if not found.
       */
      public String locateLayoutTemplate(RunData data, String template)
      {
          String path = localizeTemplateName(data);
  
          String located = locateTemplate(data, DIR_LAYOUTS, path, template);
          if (null == located) 
          {
              // Try to locate it directly on file system, perhaps it was recently added
              useNameCache = false;
              located = locateTemplate(data, DIR_LAYOUTS, path, template);
              if (null != located)
              {
                  // add it to the map
                  templateMap.put( located, null );
              }
              else
              {
                  template = "/default." + getTemplateExtension(template);
                  located = locateTemplate(data, DIR_LAYOUTS, path, template);                
              }
              useNameCache = true;
          }
  
          return located;
      }
  
      /**
       * Locate a portlet template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the portlets directory for the requested portlet template,
       *          or null if not found.
       */
      public String locatePortletTemplate(RunData data, String template)
      {
          String path = localizeTemplateName(data);
  
          String located = locateTemplate(data, DIR_PORTLETS, path, template);
          if (null == located)
          {
              // Try to locate it directly on file system, perhaps it was recently added
              useNameCache = false;
              located = locateTemplate(data, DIR_PORTLETS, path, template);
              if (null != located)
              {
                  // add it to the map
                  templateMap.put( located, null );
              }
              useNameCache = true;
          }
  
          if (null != located)
              return DIR_PORTLETS + located;
          return null;
      }
  
      /**
       * Locate a control template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the controls directory for the requested control template,
       *          or null if not found.
       */
      public String locateControlTemplate(RunData data, String template)
      {
          String path = localizeTemplateName(data);
  
          String located = locateTemplate(data, DIR_CONTROLS, path, template);
          if (null == located)
          {
              // Try to locate it directly on file system, perhaps it was recently added
              useNameCache = false;
              located = locateTemplate(data, DIR_CONTROLS, path, template);
              if (null != located)
              {
                  // add it to the map
                  templateMap.put( located, null );
              }
              useNameCache = true;
          }
  
          if (null != located)
              return DIR_CONTROLS + located;
          return null;
      }
  
      /**
       * Locate a controller template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the controllers directory for the requested controller template,
       *          or null if not found.
       */
      public String locateControllerTemplate(RunData data, String template)
      {
          String path = localizeTemplateName(data);
  
          String located = locateTemplate(data, DIR_CONTROLLERS, path, template);
          if (null == located)
          {
              // Try to locate it directly on file system, perhaps it was recently added
              useNameCache = false;
              located = locateTemplate(data, DIR_CONTROLLERS, path, template);
              if (null != located)
              {
                  // add it to the map
                  templateMap.put( located, null );
              }
              useNameCache = true;
          }
          if (null != located)
              return DIR_CONTROLLERS + located;
          return null;
      }
  
      /**
       * General template location algorithm. Starts with the most specific resource,
       * including mediatype + nls specification, and fallsback to least specific.
       *
       * @param data The rundata for the request.
       * @param resourceType The path specific to the resource type sought (eg /screens).
       * @param path The fullest path to the template based on simple NLS/mediatype directory.
       * @param template The name of the template.
       *
       * @return the exact path to the template, or null if not found.
       */
      private String locateTemplate(RunData data, String resourceType, String path, String template)
      {
          String finalPath;
  
          // make sure resourceType doesn't end with "/" but starts with "/"
          if (resourceType.endsWith(PATH_SEPARATOR))
          {
              resourceType = resourceType.substring(0, resourceType.length()-1);
          }
          if (!resourceType.startsWith(PATH_SEPARATOR))
          {
              resourceType = PATH_SEPARATOR + resourceType;
          }
          // make sure path doesn't end with "/" but starts with "/"
          if (path.endsWith(PATH_SEPARATOR))
          {
              path = path.substring(0, path.length()-1);
          }
          if (!path.startsWith(PATH_SEPARATOR))
          {
              path = PATH_SEPARATOR + path;
          }
          // make sure template starts with "/"
          if (!template.startsWith(PATH_SEPARATOR))
          {
              template = PATH_SEPARATOR + template;
          }
          
          StringBuffer fullPath = new StringBuffer( templateRoot );
  
          if (!templateRoot.endsWith(PATH_SEPARATOR))
              fullPath.append(PATH_SEPARATOR);
          fullPath.append(getTemplateExtension(template));
  
          fullPath.append(resourceType);
          
          String basePath = fullPath.toString();
          String realPath = null;
          String workingPath = null;
  
          do
          {
              workingPath = path+template;
              realPath = TurbineServlet.getRealPath( basePath + workingPath );
  
              // the current template exists, return the corresponding path
              if (templateExists(realPath))
              {
                  Log.debug("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 )
              {
                  path = path.substring(0,pt);
              }
              else
              {
                  path = null;
              }
          }
          while (path != null);
          
          return null;
      }
  
      /**
       * Helper function for template locator to find a localized (NLS) resource.
       * Considers both language and country resources.
       *
       * @param data The rundata for the request.
       *
       * @return The possible path to a localized template.
       */
      private String localizeTemplateName(RunData data)
      {
          // Get the locale store it in the user object
          Locale tmplocale = LocaleDetector.getLocale(data);
          if (tmplocale == null) {
              tmplocale = new Locale(
                       TurbineResources.getString("locale.default.language", "en"),
                       TurbineResources.getString("locale.default.country", "US"));
          }
  
          data.getUser().setTemp("locale", tmplocale);
  
          StringBuffer templatePath = new StringBuffer();
  
          // set the content type (including charset)
          CapabilityMap cm = CapabilityMapFactory.getCapabilityMap( data );
          String type = MimeType.getCode( cm.getPreferredType() );
          data.setContentType( cm.getPreferredType().toString() );  
          if ((type != null) && (type.length() > 0)) 
              templatePath.append(PATH_SEPARATOR).append(type);
          
          // Grab the Locale from the temporary storage in the User object
          Locale locale = (Locale)data.getUser().getTemp("locale");
          String language = locale.getLanguage();
          String country = locale.getCountry();
          if ((language != null) && (language.length() > 0)) 
              templatePath.append(PATH_SEPARATOR).append(language);
          if ((country != null) && (country.length() > 0)) 
              templatePath.append(PATH_SEPARATOR).append(country);
  
          return  templatePath.toString();
      }
  
      /** 
       * Returns the extension for the specified template
       *
       * @param template the template name to scan for an extension
       * @return the template extension if it exists or the default 
       * template extension
       */
      private String getTemplateExtension(String template)
      {
          String ext = TurbineTemplate.getDefaultExtension();
          
          int idx = template.lastIndexOf(".");
          
          if (idx > 0)
          {
              ext = template.substring(idx+1);
          }
          
          return ext;
      }
      
      /**
       * Checks for the existence of a template resource given a key.
       * The key are absolute paths to the templates, and are cached
       * in a template cache for performance.
       *
       * @param key The absolute path to the template resource.
       *
       * @return True when the template is found, otherwise false.
       */
      public boolean templateExists(String templateKey)
      {
          if (useNameCache == true)
          {
              return templateMap.containsKey(templateKey);
          }
          return (new File(templateKey).exists());
      }
  
      /**
       * Loads the configuration parameters for this service from the
       * JetspeedResources.properties file.
       *
       * @exception throws a <code>InitializationException</code> if the service
       * fails to initialize
       */
      private void initConfiguration() throws InitializationException
      {
  
          templateRoot = JetspeedResources.getString(TurbineServices.SERVICE_PREFIX
                                                 + TemplateLocatorService.SERVICE_NAME
                                                 + CONFIG_TEMPLATE_ROOT);
          if (templateRoot == null)
              throw new InitializationException(
                  MSG_MISSING_PARAMETER  + CONFIG_TEMPLATE_ROOT);
          if (!templateRoot.endsWith(PATH_SEPARATOR))
              templateRoot = templateRoot + PATH_SEPARATOR;
  
          templateMap = new HashMap();
          // traverse starting from the root template directory and add resources
          templateRootPath = TurbineServlet.getRealPath( templateRoot );
          if (null != templateRootPath)
              loadNameCache( templateRootPath, "" );
  
      }
  
      /**
       * Loads the template name cache map to accelerate template searches.
       *
       * @parameter path The template
       * @parameter name just the name of the resource
       */
      private void loadNameCache(String path, String name)
      {
          File file = new File(path);
          if(file.isFile()) {
  
              // add it to the map
              templateMap.put( path, null );
  
          } else if(file.isDirectory()) {
  
              if(!path.endsWith(File.separator))
                  path += File.separator;
  
              String list[] = file.list();
  
              // Process all files recursivly
              for(int ix = 0; list != null && ix < list.length; ix++)
                  loadNameCache(path + list[ix], list[ix]);
          }
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/services/template/TemplateLocatorService.java
  
  Index: TemplateLocatorService.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *     "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache" or
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.jetspeed.services.template;
  
  import org.apache.jetspeed.om.profile.*;
  import org.apache.jetspeed.services.Profiler;
  import org.apache.jetspeed.capability.CapabilityMap;
  import org.apache.turbine.om.security.User;
  import org.apache.turbine.services.Service;
  import org.apache.turbine.util.RunData;
  import org.apache.turbine.util.DynamicURI;
  import org.apache.jetspeed.util.MimeType;
  import java.io.IOException;
  
  /**
   * <p>This interface is a facade for all template location related operations.
   * Template location algorithms are different from the Velocity template location,
   * since Jetspeed has a specialized template directory structure.
   * This is a fix to get us through unti the TurbineTemplateService can locate
   * resources by NLS and mediatype. Then it can be removed</p>
   *
   * <p>The directory structure is currently layout out in the following order:
   *    /templateType/mediaType/LanguageCode/CountryCode</p>
   * <p>Example: /screens/html/en/US/resource.vm</p>
   *
   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
   * @version $Id: TemplateLocatorService.java,v 1.1 2001/07/07 21:06:22 taylor Exp $
   */
  
  public interface TemplateLocatorService extends Service
  {
   
      /** The name of this service */
      public String SERVICE_NAME = "TemplateLocator";
  
      /**
       * Locate a screen template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the screens directory for the requested screen template,
       *          or null if not found.
       */
      public String locateScreenTemplate(RunData data, String template);
  
      /**
       * Locate a layout template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the layouts directory for the requested layout template,
       *          or null if not found.
       */
      public String locateLayoutTemplate(RunData data, String template);
  
      /**
       * Locate a portlet template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the portlets directory for the requested portlet template,
       *          or null if not found.
       */
      public String locatePortletTemplate(RunData data, String template);
  
      /**
       * Locate a control template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the controls directory for the requested control template,
       *          or null if not found.
       */
      public String locateControlTemplate(RunData data, String template);
  
      /**
       * Locate a controller template using Jetspeed template location algorithm, searching by
       * mediatype and language criteria extracted from the request state in rundata.
       *
       * @param data The rundata for the request.
       * @param template The name of the template.
       *
       * @return The path relative to the controllers directory for the requested controller template,
       *          or null if not found.
       */
      public String locateControllerTemplate(RunData data, String template);
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org


Mime
View raw message