raphael     2003/03/02 11:40:14

  Modified:    src/java/org/apache/jetspeed/services/template
                        JetspeedTemplateLocatorService.java
  Log:
  Updated template locator code to use media types fallback.
  
  Revision  Changes    Path
  1.14      +342 -156  
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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- JetspeedTemplateLocatorService.java       8 Nov 2002 10:05:19 -0000       1.13
  +++ JetspeedTemplateLocatorService.java       2 Mar 2003 19:40:13 -0000       1.14
  @@ -61,6 +61,7 @@
   // java.util
   import java.util.Iterator;
   import java.util.List;
  +import java.util.ArrayList;
   import java.util.Map;
   import java.util.HashMap;
   import java.util.Locale;
  @@ -146,8 +147,8 @@
        * @exception throws a <code>InitializationException</code> if the service
        * fails to initialize
        */
  -    public synchronized void init(ServletConfig conf) throws 
InitializationException {
  -
  +    public synchronized void init(ServletConfig conf) throws InitializationException
  +    {
           // already initialized
           if (getInit()) return;
   
  @@ -155,17 +156,21 @@
   
           // initialization done
           setInit(true);
  -
        }
   
  -    public void init() throws InitializationException {
  +    public void init() throws InitializationException
  +    {
           Log.info( "Late init for JetspeedTemplateLocatorService called" );
  -        while( !getInit() ) {
  +        while( !getInit() )
  +        {
               //Not yet...
  -            try {
  +            try
  +            {
                   Thread.sleep( 100 );
                   Log.info( "Waiting for init of JetspeedTemplateLocatorService..." );
  -            } catch (InterruptedException ie ) {
  +            }
  +            catch (InterruptedException ie )
  +            {
                   Log.error( ie );
               }
           }
  @@ -175,7 +180,8 @@
        * This is the shutdown method called by the
        * Turbine <code>Service</code> framework
        */
  -    public void shutdown() {
  +    public void shutdown()
  +    {
       }
   
       /**
  @@ -190,25 +196,53 @@
        */
       public String locateScreenTemplate(RunData data, String template)
       {
  -        String path = localizeTemplateName(data);
  +        List templatePaths = localizeTemplateName(data);
  +        Iterator i = templatePaths.iterator();
  +        String located = null;
   
  -        String located = locateTemplate(data, DIR_SCREENS, path, template);
  -        if (null == located)
  +        while (i.hasNext())
           {
  -            // Try to locate it directly on file system, perhaps it was recently 
added
  -            useNameCache = false;
  +            String path = (String)i.next();
               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 );
  +                }
  +
  +                useNameCache = true;
  +            }
  +
               if (null != located)
               {
  -                // add it to the map
  -                templateMap.put( located, null );
  +                return located;
               }
  -            else
  +        }
  +
  +        if (null == located)
  +        {
  +            //we have not found the requested sreen but still need to return
  +            //something, search for the default screen
  +
  +            i = templatePaths.iterator();
  +            template = "/default." + getTemplateExtension(template);
  +            while (i.hasNext())
               {
  -                template = "/default." + getTemplateExtension(template);
  +                String path = (String)i.next();
                   located = locateTemplate(data, DIR_SCREENS, path, template);
  +
  +                if (null != located)
  +                {
  +                    return located;
  +                }
               }
  -            useNameCache = true;
           }
   
           return located;
  @@ -226,129 +260,191 @@
        */
       public String locateLayoutTemplate(RunData data, String template)
       {
  -        String path = localizeTemplateName(data);
  +        List templatePaths = localizeTemplateName(data);
  +        Iterator i = templatePaths.iterator();
  +        String located = null;
   
  -        String located = locateTemplate(data, DIR_LAYOUTS, path, template);
  -        if (null == located)
  +        while (i.hasNext())
           {
  -            // Try to locate it directly on file system, perhaps it was recently 
added
  -            useNameCache = false;
  +            String path = (String)i.next();
               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 );
  +                }
  +
  +                useNameCache = true;
  +            }
  +
               if (null != located)
               {
  -                // add it to the map
  -                templateMap.put( located, null );
  +                return located;
               }
  -            else
  +        }
  +
  +        if (null == located)
  +        {
  +            //we have not found the requested layout but still need to return
  +            //something, search for the default layout
  +
  +            i = templatePaths.iterator();
  +            template = "/default." + getTemplateExtension(template);
  +            while (i.hasNext())
               {
  -                template = "/default." + getTemplateExtension(template);
  +                String path = (String)i.next();
                   located = locateTemplate(data, DIR_LAYOUTS, path, template);
  -            }
  -            useNameCache = true;
  +
  +               if (null != located)
  +               {
  +                   return located;
  +               }
  +           }
           }
   
           return located;
       }
   
       /**
  -     * Locate a portlet template using Jetspeed template location algorithm, 
searching by
  +     * 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 portlets directory for the requested 
portlet template,
  +     * @return The path relative to the controllers directory for the requested 
controller template,
        *          or null if not found.
        */
  -    public String locatePortletTemplate(RunData data, String template)
  +    public String locateNavigationTemplate(RunData data, String template)
       {
  -        String path = localizeTemplateName(data);
  +        List templatePaths = localizeTemplateName(data);
  +        Iterator i = templatePaths.iterator();
   
  -        String located = locateTemplate(data, DIR_PORTLETS, path, template);
  -        if (null == located)
  +        while (i.hasNext())
           {
  -            // Try to locate it directly on file system, perhaps it was recently 
added
  -            useNameCache = false;
  -            located = locateTemplate(data, DIR_PORTLETS, path, template);
  +            String path = (String)i.next();
  +            String located = locateTemplate(data, DIR_NAVIGATIONS, path, template);
  +
  +            if (null == located)
  +            {
  +                // Try to locate it directly on file system, perhaps it was 
recently added
  +                useNameCache = false;
  +                located = locateTemplate(data, DIR_NAVIGATIONS, path, template);
  +
  +                if (null != located)
  +                {
  +                    // add it to the map
  +                    templateMap.put( located, null );
  +                }
  +
  +                useNameCache = true;
  +            }
  +
               if (null != located)
               {
  -                // add it to the map
  -                templateMap.put( located, null );
  +                return located;
               }
  -            useNameCache = true;
           }
   
  -        if (null != located)
  -            return DIR_PORTLETS + located;
           return null;
       }
   
       /**
  -     * Locate a control template using Jetspeed template location algorithm, 
searching by
  +     * 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 controls directory for the requested 
control template,
  +     * @return The path relative to the portlets directory for the requested 
portlet template,
        *          or null if not found.
        */
  -    public String locateControlTemplate(RunData data, String template)
  +    public String locatePortletTemplate(RunData data, String template)
       {
  -        String path = localizeTemplateName(data);
  +        List templatePaths = localizeTemplateName(data);
  +        Iterator i = templatePaths.iterator();
   
  -        String located = locateTemplate(data, DIR_CONTROLS, path, template);
  -        if (null == located)
  +        while (i.hasNext())
           {
  -            // Try to locate it directly on file system, perhaps it was recently 
added
  -            useNameCache = false;
  -            located = locateTemplate(data, DIR_CONTROLS, path, template);
  +            String path = (String)i.next();
  +            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)
               {
  -                // add it to the map
  -                templateMap.put( located, null );
  +                return DIR_PORTLETS + located;
               }
  -            useNameCache = true;
           }
   
  -        if (null != located)
  -            return DIR_CONTROLS + located;
           return null;
       }
   
       /**
  -     * Locate a controller template using Jetspeed template location algorithm, 
searching by
  +     * 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 controllers directory for the requested 
controller template,
  +     * @return The path relative to the controls directory for the requested 
control template,
        *          or null if not found.
        */
  -    public String locateControllerTemplate(RunData data, String template)
  +    public String locateControlTemplate(RunData data, String template)
       {
  -        String path = localizeTemplateName(data);
  +        List templatePaths = localizeTemplateName(data);
  +        Iterator i = templatePaths.iterator();
   
  -        String located = locateTemplate(data, DIR_CONTROLLERS, path, template);
  -        if (null == located)
  +        while (i.hasNext())
           {
  -            // Try to locate it directly on file system, perhaps it was recently 
added
  -            useNameCache = false;
  -            located = locateTemplate(data, DIR_CONTROLLERS, path, template);
  +            String path = (String)i.next();
  +            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)
               {
  -                // add it to the map
  -                templateMap.put( located, null );
  +                return DIR_CONTROLS + located;
               }
  -            useNameCache = true;
           }
  -        if (null != located)
  -            return DIR_CONTROLLERS + 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.
        *
  @@ -358,25 +454,37 @@
        * @return The path relative to the controllers directory for the requested 
controller template,
        *          or null if not found.
        */
  -    public String locateNavigationTemplate(RunData data, String template)
  +    public String locateControllerTemplate(RunData data, String template)
       {
  -        String path = localizeTemplateName(data);
  +        List templatePaths = localizeTemplateName(data);
  +        Iterator i = templatePaths.iterator();
   
  -        String located = locateTemplate(data, DIR_NAVIGATIONS, path, template);
  -        if (null == located)
  +        while (i.hasNext())
           {
  -            // Try to locate it directly on file system, perhaps it was recently 
added
  -            useNameCache = false;
  -            located = locateTemplate(data, DIR_NAVIGATIONS, path, template);
  +            String path = (String)i.next();
  +            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)
               {
  -                // add it to the map
  -                templateMap.put( located, null );
  +                return DIR_CONTROLLERS + located;
               }
  -            useNameCache = true;
           }
  -        if (null != located)
  -            return located;
  +
           return null;
       }
   
  @@ -392,7 +500,7 @@
        */
       public String locateEmailTemplate(RunData data, String template)
       {
  -      return locateEmailTemplate( data, template, LocaleDetector.getLocale(data));
  +        return locateEmailTemplate( data, template, LocaleDetector.getLocale(data));
       }
   
       /**
  @@ -408,23 +516,36 @@
        */
       public String locateEmailTemplate(RunData data, String template, Locale locale)
       {
  -         String path = localizeTemplateName(data, locale);
  -         String located = locateTemplate(data, DIR_EMAILS, path, template);
  -         if (null == located)
  -         {
  -             // Try to locate it directly on file system, perhaps it was recently 
added
  -             useNameCache = false;
  -             located = locateTemplate(data, DIR_EMAILS, path, template);
  -             if (null != located)
  -             {
  -                 // add it to the map
  -                 templateMap.put( located, null );
  -             }
  -             useNameCache = true;
  -         }
  -         if (null != located)
  -             return DIR_EMAILS + located;
  -         return null;
  +        List templatePaths = localizeTemplateName(data,locale);
  +        Iterator i = templatePaths.iterator();
  +
  +        while (i.hasNext())
  +        {
  +            String path = (String)i.next();
  +            String located = locateTemplate(data, DIR_EMAILS, path, template);
  +
  +            if (null == located)
  +            {
  +                // Try to locate it directly on file system, perhaps it was 
recently added
  +                useNameCache = false;
  +                located = locateTemplate(data, DIR_EMAILS, path, template);
  +
  +                if (null != located)
  +                {
  +                    // add it to the map
  +                    templateMap.put( located, null );
  +                }
  +
  +                useNameCache = true;
  +            }
  +
  +            if (null != located)
  +            {
  +                return DIR_EMAILS + located;
  +            }
  +        }
  +
  +        return null;
        }
   
       /**
  @@ -439,24 +560,33 @@
        */
       public String locateParameterTemplate(RunData data, String template)
       {
  -        String path = localizeTemplateName(data);
  +        List templatePaths = localizeTemplateName(data);
  +        Iterator i = templatePaths.iterator();
   
  -        String located = locateTemplate(data, DIR_PARAMETERS, path, template);
  -        if (null == located)
  +        while (i.hasNext())
           {
  -            // Try to locate it directly on file system, perhaps it was recently 
added
  -            useNameCache = false;
  -            located = locateTemplate(data, DIR_PARAMETERS, path, template);
  +            String path = (String)i.next();
  +            String located = locateTemplate(data, DIR_PARAMETERS, path, template);
  +
  +            if (null == located)
  +            {
  +                // Try to locate it directly on file system, perhaps it was 
recently added
  +                useNameCache = false;
  +                located = locateTemplate(data, DIR_PARAMETERS, path, template);
  +                if (null != located)
  +                {
  +                    // add it to the map
  +                    templateMap.put( located, null );
  +                }
  +                useNameCache = true;
  +            }
  +
               if (null != located)
               {
  -                // add it to the map
  -                templateMap.put( located, null );
  +                return DIR_PARAMETERS + located;
               }
  -            useNameCache = true;
           }
   
  -        if (null != located)
  -            return DIR_PARAMETERS + located;
           return null;
       }
   
  @@ -527,9 +657,11 @@
           StringBuffer fullPath = new StringBuffer( templateRoot );
   
           if (!templateRoot.endsWith(PATH_SEPARATOR))
  +        {
               fullPath.append(PATH_SEPARATOR);
  -        fullPath.append(getTemplateExtension(template));
  +        }
   
  +        fullPath.append(getTemplateExtension(template));
           fullPath.append(resourceType);
   
           String basePath = fullPath.toString();
  @@ -544,7 +676,11 @@
               // the current template exists, return the corresponding path
               if (templateExists(realPath))
               {
  -                Log.debug("template exists: "+realPath+" returning "+workingPath);
  +                if (Log.getLogger().isDebugEnabled())
  +                {
  +                    Log.debug("TemplateLocator: template exists: "+realPath+" 
returning "+workingPath);
  +                }
  +
                   return workingPath;
               }
               // else strip path of one of its components and loop
  @@ -565,29 +701,35 @@
   
       /**
        * Helper function for template locator to find a localized (NLS) resource.
  -     * Considers both language and country resources.
  +     * Considers both language and country resources as well as all the possible
  +     * media-types for the request
        *
        * @param data The rundata for the request.
        *
  -     * @return The possible path to a localized template.
  +     * @return The possible paths to a localized template ordered by
  +     * descending preference
        */
  -    private String localizeTemplateName(RunData data)
  +    private List localizeTemplateName(RunData data)
       {
  -      return localizeTemplateName(data, null);
  +        return localizeTemplateName(data, null);
       }
   
       /**
        * Helper function for template locator to find a localized (NLS) resource.
  -     * Considers both language and country resources.
  +     * Considers both language and country resources as well as all the possible
  +     * media-types for the request
        *
        * @param data The rundata for the request.
        * @param locale The locale for the request.
        *
  -     * @return The possible path to a localized template.
  +     * @return The possible paths to a localized template ordered by
  +     * descending preference
        */
  -    private String localizeTemplateName(RunData data, Locale inLocale)
  +    private List localizeTemplateName(RunData data, Locale inLocale)
       {
  +        List templates = new ArrayList();
           Locale tmplocale = null;
  +
           if (inLocale != null)
           {
             tmplocale = inLocale;
  @@ -596,8 +738,10 @@
           {
             tmplocale = LocaleDetector.getLocale(data);
           }
  +
           // Get the locale store it in the user object
  -        if (tmplocale == null) {
  +        if (tmplocale == null)
  +        {
               tmplocale = new Locale(
                        TurbineResources.getString("locale.default.language", "en"),
                        TurbineResources.getString("locale.default.country", "US"));
  @@ -607,26 +751,54 @@
   
           StringBuffer templatePath = new StringBuffer();
   
  -        // set the content type (including charset)
  +        // 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();
  -        if (null == type)
  -        {
  -            type = cm.getPreferredType().getCode();
  -        }
  -        //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();
  +
  +        if (null != type)
  +        {
  +            types.add(type);
  +        }
  +        else
  +        {
  +            Iterator i = cm.listMediaTypes();
  +            while(i.hasNext())
  +            {
  +                types.add(i.next());
  +            }
  +        }
  +
  +        Iterator typeIterator = types.iterator();
  +
  +        while(typeIterator.hasNext())
  +        {
  +            type = (String)typeIterator.next();
  +
  +            if ((type != null) && (type.length() > 0))
  +            {
  +                templatePath.append(PATH_SEPARATOR).append(type);
  +            }
  +
  +            if ((language != null) && (language.length() > 0))
  +            {
  +                templatePath.append(PATH_SEPARATOR).append(language);
  +            }
  +
  +            if ((country != null) && (country.length() > 0))
  +            {
  +                templatePath.append(PATH_SEPARATOR).append(country);
  +            }
  +
  +            templates.add(templatePath.toString());
  +        }
  +
  +        return templates;
       }
   
       /**
  @@ -682,24 +854,33 @@
           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);
  +        }
   
           templateMap = new HashMap();
   
  -        for (int i = 0; i < templateRoots.length; i++) {
  +        for (int i = 0; i < templateRoots.length; i++)
  +        {
               String templateRoot = templateRoots[i];
   
  -            if (!templateRoot.endsWith(PATH_SEPARATOR)) {
  +            if (!templateRoot.endsWith(PATH_SEPARATOR))
  +            {
                   templateRoot = templateRoot + PATH_SEPARATOR;
               }
   
  -            if (Log.getLogger().isDebugEnabled()) Log.debug("Adding 
templateRoot:"+templateRoot);
  +            if (Log.getLogger().isDebugEnabled())
  +            {
  +                Log.debug("TemplateLocator: Adding templateRoot:"+templateRoot);
  +            }
   
               // traverse starting from the root template directory and add resources
               String templateRootPath = TurbineServlet.getRealPath( templateRoot );
  -            if (null != templateRootPath) {
  +            if (null != templateRootPath)
  +            {
                   loadNameCache( templateRootPath, "" );
               }
           }
  @@ -714,23 +895,28 @@
       private void loadNameCache(String path, String name)
       {
           File file = new File(path);
  -        if(file.isFile()) {
  -
  +        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]);
  +        }
  +        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]);
  +                }
  +            }
           }
       }
  -
  -
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to