jetspeedtemplatelocatorservice.java

来自「jetspeed源代码」· Java 代码 · 共 902 行 · 第 1/2 页

JAVA
902
字号
/*
 * Copyright 2000-2001,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jetspeed.services.template;

// java.io
import java.io.File;

// 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;

import javax.servlet.ServletConfig;

// turbine.util
import org.apache.turbine.util.RunData;

// turbine.services
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.jsp.JspService;
import org.apache.turbine.services.resources.TurbineResources;
import org.apache.turbine.services.template.TurbineTemplate;
import org.apache.turbine.services.velocity.VelocityService;
import org.apache.turbine.services.localization.LocalizationService;

// jetspeed.capability
import org.apache.commons.configuration.Configuration;
import org.apache.jetspeed.capability.CapabilityMap;

// jetspeed.services
import org.apache.jetspeed.services.resources.JetspeedResources;
import org.apache.jetspeed.services.rundata.JetspeedRunData;
import org.apache.jetspeed.services.Profiler;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.customlocalization.CustomLocalizationService;
import org.apache.jetspeed.util.ServiceUtil;
/**
 * <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>
 * @author <a href="mailto:paulsp@apache.org">Paul Spener</a>
 * @author <a href="mailto:kimptoc_mail@yahoo.com">Chris Kimpton</a>
 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
 * @version $Id: JetspeedTemplateLocatorService.java,v 1.22 2004/02/23 03:38:54 jford Exp $
 */

public class JetspeedTemplateLocatorService
    extends TurbineBaseService
    implements TemplateLocatorService
{
    /**
     * Static initialization of the logger for this class
     */    
    private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedTemplateLocatorService.class.getName());
    
    private final static String CONFIG_TEMPLATE_ROOT = ".templateRoot";
    private final static String CONFIG_PORTLET_GLOBAL_SEARCH = ".portlet.global.search";
    private final static String CONFIG_HOT_DEPLOY = ".hot.deploy";
    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 final static String DIR_NAVIGATIONS = "/navigations";
    private final static String DIR_PARAMETERS = "/parameters";
    private final static String DIR_EMAILS = "/emails";
    private static final String PATH_SEPARATOR = "/";

    // messages
    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;

    // check the file system if template not found in name cache
    private boolean hotDeploy = false;

    // template name cache
    private Map templateMap = null;

    // include screens when searching for portlet template
    private boolean useGlobalPortletSearch = false;

    /**
     * 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
    {
        logger.info("Late init for JetspeedTemplateLocatorService called");
        while (!getInit())
        {
            //Not yet...
            try
            {
                Thread.sleep(100);
                logger.info("Waiting for init of JetspeedTemplateLocatorService...");
            }
            catch (InterruptedException ie)
            {
                logger.error("Exception", 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)
    {
        List templatePaths = localizeTemplateName(data);
        Iterator i = templatePaths.iterator();
        String located = null;

        while (i.hasNext())
        {
            String path = (String) i.next();
            located = locateTemplate(data, DIR_SCREENS, path, template);

            if (null != located)
            {
                return located;
            }
        }

        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())
            {
                String path = (String) i.next();
                located = locateTemplate(data, DIR_SCREENS, path, template);

                if (null != located)
                {
                    return located;
                }
            }
        }

        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)
    {
        List templatePaths = localizeTemplateName(data);
        Iterator i = templatePaths.iterator();
        String located = null;

        while (i.hasNext())
        {
            String path = (String) i.next();
            located = locateTemplate(data, DIR_LAYOUTS, path, template);

            if (null != located)
            {
                return located;
            }
        }

        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);
            template = getTemplateLayout(getTemplateExtension(template));
            while (i.hasNext())
            {
                String path = (String) i.next();
                located = locateTemplate(data, DIR_LAYOUTS, path, template);

                if (null != located)
                {
                    return located;
                }
            }
        }

        return located;
    }

    /**
     * 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 locateNavigationTemplate(RunData data, String template)
    {
        List templatePaths = localizeTemplateName(data);
        Iterator i = templatePaths.iterator();

        while (i.hasNext())
        {
            String path = (String) i.next();
            String located = locateTemplate(data, DIR_NAVIGATIONS, path, template);

            if (null != located)
            {
                return located;
            }
        }

        return null;
    }

    /**
     * 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)
    {
        List templatePaths = localizeTemplateName(data);
        Iterator i = templatePaths.iterator();

        while (i.hasNext())
        {
            String path = (String) i.next();
            String located = locateTemplate(data, DIR_PORTLETS, path, template);

            if (null != located)
            {
                return DIR_PORTLETS + located;
            }
        }

        // Use "wide" search when required
        if (useGlobalPortletSearch == true)
        {
            String located = locateScreenTemplate(data, template);
            if (located != null)
            {
                return DIR_SCREENS + 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)
    {
        List templatePaths = localizeTemplateName(data);
        Iterator i = templatePaths.iterator();

        while (i.hasNext())
        {
            String path = (String) i.next();
            String located = locateTemplate(data, DIR_CONTROLS, path, template);

            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)
    {
        List templatePaths = localizeTemplateName(data);
        Iterator i = templatePaths.iterator();

        while (i.hasNext())
        {
            String path = (String) i.next();
            String located = locateTemplate(data, DIR_CONTROLLERS, path, template);

            if (null != located)
            {
                return DIR_CONTROLLERS + located;
            }
        }

        return null;
    }

    /**
     * Locate an email 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 emails directory for the requested email template,
     *          or null if not found.
     */
    public String locateEmailTemplate(RunData data, String template)
    {
        CustomLocalizationService locService = (CustomLocalizationService) ServiceUtil.getServiceByName(
            LocalizationService.SERVICE_NAME);
        return locateEmailTemplate(data, template, locService.getLocale(data));
    }

    /**
     * Locate an email template using Jetspeed template location algorithm, searching by
     * mediatype and language.
     *
     * @param data The rundata for the request.
     * @param template The name of the template.
     * @param locale The name of the language.
     *
     * @return The path relative to the emails directory for the requested email template,
     *          or null if not found.
     */
    public String locateEmailTemplate(RunData data, String template, Locale locale)
    {
        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)
            {
                return DIR_EMAILS + located;
            }
        }

        return null;
    }

    /**
     * Locate a parameter style 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,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?