jetspeedportaltoolkitservice.java

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

JAVA
908
字号
/*
 * 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.portaltoolkit;

//jetspeed stuff
import org.apache.jetspeed.portal.PortletControl;
import org.apache.jetspeed.portal.PortletController;
import org.apache.jetspeed.portal.PortletSkin;
import org.apache.jetspeed.portal.PortletSet;
import org.apache.jetspeed.portal.Portlet;
import org.apache.jetspeed.portal.PortletConfig;
import org.apache.jetspeed.portal.BasePortletConfig;
import org.apache.jetspeed.portal.PortletControlConfig;
import org.apache.jetspeed.portal.BasePortletControlConfig;
import org.apache.jetspeed.portal.PortletControllerConfig;
import org.apache.jetspeed.portal.BasePortletControllerConfig;
import org.apache.jetspeed.portal.BasePortletSkin;
import org.apache.jetspeed.portal.BasePortletSet;
import org.apache.jetspeed.om.profile.Control;
import org.apache.jetspeed.om.profile.Controller;
import org.apache.jetspeed.om.profile.Skin;
import org.apache.jetspeed.om.profile.Portlets;
import org.apache.jetspeed.om.profile.Layout;
import org.apache.jetspeed.om.profile.Profile;
import org.apache.jetspeed.om.profile.Parameter;
import org.apache.jetspeed.om.profile.MetaInfo;
import org.apache.jetspeed.om.profile.Entry;
import org.apache.jetspeed.om.profile.ProfileLocator;
import org.apache.jetspeed.om.profile.PSMLDocument;
import org.apache.jetspeed.services.Profiler;
import org.apache.jetspeed.services.rundata.JetspeedRunData;
import org.apache.jetspeed.services.rundata.JetspeedRunDataService;

import org.apache.jetspeed.services.Registry;
import org.apache.jetspeed.services.PortletFactory;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.om.registry.PortletEntry;
import org.apache.jetspeed.om.registry.PortletControlEntry;
import org.apache.jetspeed.om.registry.PortletControllerEntry;
import org.apache.jetspeed.om.registry.SkinEntry;
import org.apache.jetspeed.util.MetaData;
import org.apache.jetspeed.util.JetspeedException;
import org.apache.jetspeed.util.ServiceUtil;
import org.apache.jetspeed.om.BaseSecurityReference;
import org.apache.jetspeed.om.SecurityReference;
import org.apache.jetspeed.om.registry.SecurityEntry;

import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.resources.ResourceService;
import org.apache.turbine.services.rundata.RunDataService;

import java.util.Iterator;

import java.util.Hashtable;
import java.util.Map;
import javax.servlet.ServletConfig;

/**
 * Simple implementation of the PortalFactoryService.
 *
 * @author <a href="mailto:raphael@apache.org">Rapha雔 Luta</a>
 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a> 
 *
 * @version $Id: JetspeedPortalToolkitService.java,v 1.33 2004/03/29 21:02:29 taylor Exp $
 */
public class JetspeedPortalToolkitService
    extends TurbineBaseService
    implements PortalToolkitService
{
    /**
     * Static initialization of the logger for this class
     */    
    private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedPortalToolkitService.class.getName());    

    /** The default control to use when none is specified */
    private String defaultControl = null;

    /** The default controller to use when none is specified */
    private String defaultController = null;

    /** The default skin to use when none is specified */
    private String defaultSkin = null;

    /** The default user security ref to use when none is specified */
    private String defaultUserSecurityRef = null;

    /** The default anonymous user security ref to use when none is specified */
    private String defaultAnonSecurityRef = null;

    /** The default role security ref to use when none is specified */
    private String defaultRoleSecurityRef = null;

    /** The default group security ref to use when none is specified */
    private String defaultGroupSecurityRef = null;

    /**
     * This is the early initialization method called by the
     * Turbine <code>Service</code> framework
     */
    public void init(ServletConfig conf) throws InitializationException
    {

        ResourceService serviceConf =
            ((TurbineServices) TurbineServices.getInstance()).getResources(
                PortalToolkitService.SERVICE_NAME);

        this.defaultControl = serviceConf.getString("default.control");
        this.defaultController = serviceConf.getString("default.controller");
        this.defaultSkin = serviceConf.getString("default.skin");
        this.defaultUserSecurityRef = serviceConf.getString("default.user.security.ref");
        this.defaultAnonSecurityRef = serviceConf.getString("default.anon.security.ref");
        this.defaultRoleSecurityRef = serviceConf.getString("default.role.security.ref");
        this.defaultGroupSecurityRef = serviceConf.getString("default.group.security.ref");
        setInit(true);

    }

    /**
     * Instanciates a PortletControl based on a Registry entry, if available
     * or directly from a classname.
     *
     * @param name a PortletControl name available in the registry or a classname
     * @return the created PortletControl
     */
    public PortletControl getControl(String name)
    {
        PortletControl pc = null;
        PortletControlEntry entry = null;

        if (name != null)
        {
            entry = (PortletControlEntry) Registry.getEntry(Registry.PORTLET_CONTROL, name);
        }

        Map params = null;

        try
        {
            if (entry == null)
            {
                if (name != null)
                {
                    pc = (PortletControl) Class.forName(name).newInstance();
                    params = new Hashtable();
                }
            }
            else
            {
                pc = (PortletControl) Class.forName(entry.getClassname()).newInstance();
                params = entry.getParameterMap();
            }
        }
        catch (Exception e)
        {
            logger.error("Unable to instanciate control " + name + ", using default", e);
        }

        if ((pc == null) && (defaultControl != null) && (!defaultControl.equals(name)))
        {
            return getControl(defaultControl);
        }

        PortletControlConfig pcConf = new BasePortletControlConfig();
        pcConf.setName(name);
        pcConf.setInitParameters(params);
        pc.setConfig(pcConf);

        return pc;
    }

    /**
     * Instanciates a PortletControl based on a PSML Control object
     *
     * @param control the PSML control object
     * @return the created PortletControl
     */
    public PortletControl getControl(Control control)
    {
        PortletControl pc = null;

        if (control != null)
        {
            pc = getControl(control.getName());
            pc.getConfig().getInitParameters().putAll(getParameters(control));
        }
        else
        {
            if (defaultControl != null)
            {
                pc = getControl(this.defaultControl);
            }
        }

        return pc;
    }

    protected PortletControl getControl(Control control, PortletEntry entry)
    {
        PortletControl pc = null;

        if (control != null)
        {
            pc = getControl(control.getName());
            pc.getConfig().getInitParameters().putAll(getParameters(control));
        }
        else
        {
            org.apache.jetspeed.om.registry.Parameter dftPortletCtrl =
                entry.getParameter("_control");

            if (dftPortletCtrl != null)
            {
                pc = getControl(dftPortletCtrl.getValue());
            }
            else if (defaultControl != null)
            {
                pc = getControl(this.defaultControl);
            }
        }

        return pc;
    }

    /**
     * Instanciates a PortletController based on a Registry entry, if available
     * or directly from a classname.
     *
     * @param name a PortletController name available in the registry or a classname
     * @return the created PortletController
     */
    public PortletController getController(String name)
    {
        PortletController pc = null;
        PortletControllerEntry entry = null;

        if (name != null)
        {
            entry = (PortletControllerEntry) Registry.getEntry(Registry.PORTLET_CONTROLLER, name);
        }

        Map params = null;

        try
        {
            if (entry == null)
            {
                if (name != null)
                {
                    pc = (PortletController) Class.forName(name).newInstance();
                    params = new Hashtable();
                }
            }
            else
            {
                pc = (PortletController) Class.forName(entry.getClassname()).newInstance();
                params = entry.getParameterMap();
            }
        }
        catch (Exception e)
        {
            logger.error("Unable to instanciate controller " + name + ", using default");
        }

        if ((pc == null) && (defaultController != null) && (!defaultController.equals(name)))
        {
            return getController(defaultController);
        }

        PortletControllerConfig pcConf = new BasePortletControllerConfig();
        pcConf.setName(name);
        pcConf.setInitParameters(params);
        pc.setConfig(pcConf);
        pc.init();

        return pc;
    }

    /**
     * Instantiates a PortletController based on a PSML Controller object
     *
     * @param controller the PSML controller object
     * @return the created PortletController
     */
    public PortletController getController(Controller controller)
    {

        PortletController pc = null;

        if (controller != null)
        {
            pc = getController(controller.getName());
            pc.getConfig().getInitParameters().putAll(getParameters(controller));
        }
        else
        {
            if (defaultController != null)
            {
                pc = getController(this.defaultController);
            }
        }

        pc.init();

        return pc;
    }

    /**
     * Create a PortletSkin object based on a Registry skin name
     *
     * @param name the registry SkinEntry name
     * @return the new PortletSkin object
     */
    public PortletSkin getSkin(String name)
    {
        BasePortletSkin result = new BasePortletSkin();

        SkinEntry entry = null;

        if (name != null)
        {
            entry = (SkinEntry) Registry.getEntry(Registry.SKIN, name);
        }

        // either we don't have any skin defined, the skin reference is null
        // or the skin reference is invalid, in all case, retrieve the default
        // skin entry
        if (entry == null)
        {
            entry = (SkinEntry) Registry.getEntry(Registry.SKIN, this.defaultSkin);
        }

        if (entry != null)
        {
            // build the PortletSkin object
            result.setName(entry.getName());
            result.putAll(entry.getParameterMap());
        }

        // Make the skin aware of what the user agent is capable of.
        JetspeedRunDataService jrds =
            (JetspeedRunDataService) ServiceUtil.getServiceByName(RunDataService.SERVICE_NAME);
        JetspeedRunData jData = jrds.getCurrentRunData();
        if(jData != null)
        {
        	result.setCapabilityMap(jData.getCapability()); 
        }
        return result;
    }

    /**
     * Create a PortletSkin object based on PSML skin description
     *
     * @param skin the PSML Skin object
     * @return the new PortletSkin object
     */
    public PortletSkin getSkin(Skin skin)
    {
        PortletSkin result = null;
        String name = null;

        if (skin != null)
        {
            name = skin.getName();

            // create the PortletSkin corresponding to this entry
            result = getSkin(name);

            // override the values with the locally defined properties
            result.putAll(getParameters(skin));

        }

        return result;
    }

    /**
     * Creates a PortletSet from a PSML portlets description
     *
     * @param portlets the PSML portlet set description
     * @return a new instance of PortletSet
     */
    public PortletSet getSet(Portlets portlets)
    {
        VariableInteger lastID = new VariableInteger(0);
        return getSet(portlets, new VariableInteger(0));
    }

    /**
     * Creates a PortletSet from a PSML portlets description, updating
     * the portletset name based on its position within the tree
     *
     * @param portlets the PSML portlet set description
     * @param count the portletset number within the complete tree
     * @return a new instance of PortletSet
     */
    protected PortletSet getSet(Portlets portlets, VariableInteger theCount)
    {
        // Create a new BasePortletSet to handle the portlets
        BasePortletSet set = new BasePortletSet();
        PortletController controller = getController(portlets.getController());
        set.setController(controller);
        String name = portlets.getName();
        if (name != null)
        {
            set.setName(name);
        }
        else
            set.setName(String.valueOf(theCount.getValue()));

        set.setID(portlets.getId());

        theCount.setValue(theCount.getValue() + 1);

        //FIXME: this sucks ! we should either associate the portlet set
        //with its portlets peer or set the porpoerties directly on the portlet
        //set object
        //Unfortunately, this would change the API too drastically for now...
        set.setPortletConfig(getPortletConfig(portlets));

        // Add all sub portlet sets in the main set
        //        Portlets[] subsets = portlets.getPortlets();
        //        for (int i=0; i < subsets.length; i++ )

        for (Iterator it = portlets.getPortletsIterator(); it.hasNext();)
        {
            Portlets subset = (Portlets) it.next();
            // Set this subset's parent Portlets collection.          
            subset.setParentPortlets(portlets);

            Map constraints = getParameters(subset.getLayout());
            int position = getPosition(subset.getLayout());
            set.addPortlet(
                getSet(subset, theCount),
                controller.getConstraints(constraints),
                position);
        }

⌨️ 快捷键说明

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