baseprofilelocator.java

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

JAVA
691
字号
/*
 * Copyright 2000-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.om.profile;

import java.util.StringTokenizer;


import org.apache.jetspeed.om.security.JetspeedUser;
import org.apache.jetspeed.om.security.JetspeedUserFactory;
import org.apache.jetspeed.om.security.Role;
import org.apache.jetspeed.om.security.Group;

import org.apache.jetspeed.services.Profiler;
import org.apache.jetspeed.services.JetspeedSecurity;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;

/**
 * Interface definition for a Profile Locator.
 * Locators are used by the profiler to describe the parameters used to locate
 * a resource in the persistent configuration store.
 *
 *
 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
 * @author <a href="mailto:adambalk@cisco.com">Atul Dambalkar</a>
 * @version $Id: BaseProfileLocator.java,v 1.21 2004/02/23 03:05:01 jford Exp $
*/

public class BaseProfileLocator implements ProfileLocator
{
    // instance state
    private String name = null;
    private String mediaType = null;
    private String language = null;
    private String country = null;
    private JetspeedUser user = null;
    private Role   role = null;
    private Group  group = null;
    private boolean anonymous = false;
    
    private String roleName = null;
    private String userName = null;
    private String groupName = null;

    private static final String DELIM = "/";

    /**
     * Static initialization of the logger for this class
     */    
    private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseProfileLocator.class.getName());    
    
    /*
     * Gets the unique profile locator id, which is a combination of the params
     * This ID must follow the one of the 4 sequences below:
     *
     *   <username>/<mediaType>/<language>/<country>/<page>
     *   <group>/<mediaType>/<language>/<country>/<page>
     *   <role>/<mediaType>/<language>/<country>/<page>
     *
     *
     * @return The profile locator id
     */

    public String getId()
    {
        StringBuffer id = new StringBuffer(128);

        if (!anonymous && user != null)
        {
            id.append(Profiler.PARAM_USER).append(DELIM);
            id.append(user.getUserName());
        }
        else if (group != null)
        {
            id.append(Profiler.PARAM_GROUP).append(DELIM);
            id.append(group.getName());
        }
        else if (role != null)
        {
            id.append(Profiler.PARAM_ROLE).append(DELIM);
            id.append(role.getName());
        }
        else
        {
            id.append(Profiler.PARAM_ANON);
        }
        if (language != null)
        {
            id.append(DELIM);
            id.append(language);
        }
        if (country != null)
        {
            id.append(DELIM);
            id.append(country);
        }
        if (mediaType != null)
        {
            id.append(DELIM);
            id.append(mediaType);
        }
        if (name != null)
        {
            id.append(DELIM);
            id.append(name);
        }

        return id.toString();
    }


    /*
     * Gets the unique profile locator path, which is a combination of the name
     * value pairs. This ID must follow the one of the 4 sequences below:
     *
     *   user/<name>/media-type/<mediaType>/language/<language>
     *               /country/<country>/<page>/page
     *
     *   group/ ""
     *   role/  ""
     *
     *
     * @return The profile locator path
     */

    public String getPath()
    {
        StringBuffer id = new StringBuffer(128);

        if (!anonymous && user != null)
        {
            id.append(Profiler.PARAM_USER).append(DELIM);
            id.append(user.getUserName()).append(DELIM);
        }
        else if (group != null)
        {
            id.append(Profiler.PARAM_GROUP).append(DELIM);
            id.append(group.getName()).append(DELIM);
        }
        else if (role != null)
        {
            id.append(Profiler.PARAM_ROLE).append(DELIM);
            id.append(role.getName()).append(DELIM);
        }
        else
        {
            id.append(Profiler.PARAM_USER).append(DELIM);
            id.append(Profiler.PARAM_ANON).append(DELIM);
        }

        if (language != null)
        {
            id.append(Profiler.PARAM_LANGUAGE).append(DELIM);
            id.append(language).append(DELIM);
        }
        if (country != null)
        {
            id.append(Profiler.PARAM_COUNTRY).append(DELIM);
            id.append(country).append(DELIM);
        }
        if (mediaType != null)
        {
            id.append(Profiler.PARAM_MEDIA_TYPE).append(DELIM);
            id.append(mediaType).append(DELIM);
        }
        if (name != null)
        {
            id.append(Profiler.PARAM_PAGE).append(DELIM);
            id.append(name).append(DELIM);
        }
        id.deleteCharAt(id.length()-1);
        return id.toString();
    }


    /*
     * populates this profile locator from a given path in the format:
     *
     *   user/<name>/media-type/<mediaType>/language/<language>
     *               /country/<country>/<page>/page
     *
     *   group/ ""
     *   role/  ""
     *
     * @param path The formatted profiler path string.
     */
    public void createFromPath(String path)
    {
        StringTokenizer tok = new StringTokenizer(path, "/");
        while (tok.hasMoreTokens())
        {
            String name = (String)tok.nextToken();
            if (name.equals(Profiler.PARAM_USER) && tok.hasMoreTokens())
            {
                try
                {
                    // keep profile locator from failing if the user has been removed
                    // from security as it may still exist in the PSML structure.
                    this.userName = tok.nextToken();
                    this.setUser( JetspeedSecurity.getUser(this.userName) );
                }
                catch (Exception e)
                {
                    logger.error("ProfileLocator: Failed to set User: ", e);
                }
            }
            else if (name.equals(Profiler.PARAM_GROUP) && tok.hasMoreTokens())
            {
                try
                {
                    // keep profile locator from failing if the group has been removed
                    // from security as it may still exist in the PSML structure.
                    this.groupName = tok.nextToken();
                    this.setGroup( JetspeedSecurity.getGroup(this.groupName) );
                }
                catch (Exception e)
                {
                    logger.error("ProfileLocator: Failed to set Group: ", e);
                }
            }
            else if (name.equals(Profiler.PARAM_ROLE) && tok.hasMoreTokens())
            {
                try
                {
                    // keep profile locator from failing if the role has been removed
                    // from security as it may still exist in the PSML structure.
                    this.roleName = tok.nextToken();
                    this.setRole(JetspeedSecurity.getRole(this.roleName));
                }
                catch (Exception e)
                {
                    logger.error("ProfileLocator: Failed to set Role: ", e);
                }
            }
            else if (name.equals(Profiler.PARAM_PAGE) && tok.hasMoreTokens())
            {
                this.setName(tok.nextToken());
            }
            else if (name.equals(Profiler.PARAM_MEDIA_TYPE) && tok.hasMoreTokens())
            {
                this.setMediaType(tok.nextToken());
            }
            else if (name.equals(Profiler.PARAM_LANGUAGE) && tok.hasMoreTokens())
            {
                this.setLanguage(tok.nextToken());
            }
            else if (name.equals(Profiler.PARAM_COUNTRY) && tok.hasMoreTokens())
            {
                this.setCountry(tok.nextToken());
            }

        }
    }

    /**
     * @see Object#clone
     * @return an instance copy of this object
     */
    public Object clone() throws java.lang.CloneNotSupportedException
    {
        return super.clone();
    }

    /*
     * Gets the resource name parameter for this profile.
     *
     * @return The resource name parameter for this profile.
     */
    public String getName()
    {
        return name;
    }

    /*
     * Sets the resource name parameter for this profile.
     *
     * @param The resource name parameter for this profile.
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /*
     * Gets the anonymous user flag for this profile.
     *
     * @param The user parameter for this profile.
     */
    public boolean getAnonymous()
    {
        return this.anonymous;
    }


    /*
     * Sets the user parameter as the anonymous user
     *
     * @param anonymous True indicates this is an anonymous user.
     */
    public void setAnonymous(boolean anonymous)
    {
        try
        {
            JetspeedUser user = JetspeedUserFactory.getInstance();
            user.setUserName(JetspeedSecurity.getAnonymousUserName());
            this.setUser(user);
        }
        catch (Exception e)
        {
            logger.error("Could not get Anonymous user", e);
        }
        finally
        {
            this.anonymous = anonymous;
        }
    }

    /*
     * Gets the media type parameter for this profile.
     * Media types are values such as html, wml, xml ...
     *
     * @return The media type parameter for this profile.
     */
    public String getMediaType()
    {
        return mediaType;
    }

    /*
     * Sets the media type parameter for this profile.
     * Media types are values such as html, wml, xml ...
     *

⌨️ 快捷键说明

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