jetspeedprofilerservice.java

来自「jetspeed源代码」· Java 代码 · 共 1,107 行 · 第 1/3 页

JAVA
1,107
字号
                doc = PsmlManager.getDocument( locator );
                if (null != doc)
                    return doc;
            }
            else if (null != locator.getRole())
            {
                locator.setRole(null);
                doc = PsmlManager.getDocument( locator );
                if (null != doc)
                    return doc;
            }
            else if (null != locator.getUser())
            {
                locator.setUser(null);
                doc = PsmlManager.getDocument( locator );
                if (null != doc)
                    return doc;
            }
        }
        return doc;

    }

    /**
     * Lookup the media type from the CapabilitMap.
     * First the RunData is checked for an explicit media-type request.
     *
     * @param cm The <code>CapabilityMap</code> of the current requesting device.
     * @param rundata, The <code>RunData</code> turbine request context information.
     * @return a String, the unique name of the media type.
     */
    protected String getMediaType(RunData rundata, CapabilityMap cm)
    {
        String paramMediaType;
        String media = null;

        if (null != rundata)
        {
            paramMediaType = rundata.getParameters().getString( Profiler.PARAM_MEDIA_TYPE );
            if (null != paramMediaType)
            {
                return paramMediaType;
            }
        }

        if (cm != null)
        {
            media = cm.getPreferredMediaType();
        }

        return media;
    }

    /**
     * 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
    {
        profileClass = ServiceHelper.loadModelClass(this, "profile.impl");
        locatorClass = ServiceHelper.loadModelClass(this, "locator.impl");
        
        // get configuration parameters from Jetspeed Resources
        ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance())
                                                     .getResources(ProfilerService.SERVICE_NAME);

        resourceDefault = serviceConf.getString( CONFIG_RESOURCE_DEFAULT, DEFAULT_CONFIG_RESOURCE_DEFAULT );

        resourceExt = serviceConf.getString( CONFIG_RESOURCE_EXT, DEFAULT_CONFIG_RESOURCE_EXT );
        if (-1 == resourceExt.indexOf(PATH_EXTENSION_DELIMITER))
        {
            resourceExt = PATH_EXTENSION_DELIMITER + resourceExt;
        }
        
        useSecurity = serviceConf.getBoolean( CONFIG_SECURITY, DEFAULT_CONFIG_SECURITY );

        useRoleFallback = serviceConf.getBoolean( CONFIG_ROLE_FALLBACK, DEFAULT_CONFIG_ROLE_FALLBACK );

        newUserTemplate = serviceConf.getString( CONFIG_NEWUSER_TEMPLATE, DEFAULT_CONFIG_NEWUSER_TEMPLATE );

        useFallbackToRoot = serviceConf.getBoolean( CONFIG_FALLBACK_TO_ROOT, useFallbackToRoot );

        useFallbackLanguage = serviceConf.getBoolean( CONFIG_FALLBACK_LANGUAGE, useFallbackLanguage );

        useRoleMerge = serviceConf.getBoolean( CONFIG_ROLE_MERGE, useRoleMerge );

        rolemergeControl = serviceConf.getString( CONFIG_ROLE_MERGE_CONTROL, DEFAULT_CONFIG_ROLE_MERGE_CONTROL );

        rolemergeController = serviceConf.getString( CONFIG_ROLE_MERGE_CONTROLLER, DEFAULT_CONFIG_ROLE_MERGE_CONTROLLER );

        if (useFallbackLanguage == false)
        {
            useFallbackCountry = false;
        }
        else
        {
            useFallbackCountry = serviceConf.getBoolean( CONFIG_FALLBACK_COUNTRY, useFallbackCountry );
        }

        try
        {
            mediaTypes = serviceConf.getStringArray(CONFIG_NEWUSER_MEDIA);
        }
        catch (Exception e)
        {
            logger.error( "Error getting media types", e );
        }

        if (null == mediaTypes || mediaTypes.length == 0)
        {
            mediaTypes = DEFAULT_CONFIG_NEWUSER_MEDIA;
        }
    }

   /**
    * Builds a dynamic URI based on the current profiler group/role/page
    *
    * @param data The rundata object for the current request.
    * @param locator The description of the profile.
    * @return A new dynamic URI representing all profile parameters from the locator.
    */
    public DynamicURI makeDynamicURI( RunData data, ProfileLocator locator )
        throws ProfileException
    {
        DynamicURI uri = new DynamicURI( data );

       // check mediatype to add to the uri
        String mtype = locator.getMediaType();
        if (null != mtype)
        {
            uri.addPathInfo(Profiler.PARAM_MEDIA_TYPE, mtype);
        }

       // check language to add to the uri
        String language = locator.getLanguage();
        if (null != language)
        {
            uri.addPathInfo(Profiler.PARAM_LANGUAGE, language);
        }

       // check language to add to the uri
        String country = locator.getCountry();
        if (null != country)
        {
            uri.addPathInfo(Profiler.PARAM_COUNTRY, country);
        }

        // check User, Group or Role to add to the uri
        JetspeedUser user = locator.getUser();
        if (null != user)
        {
            if (user.getUserName() != null)
                uri.addPathInfo(Profiler.PARAM_USER, user.getUserName());
        }
        else
        {
            Group group = locator.getGroup();
            if (null != group)
            {
                uri.addPathInfo(Profiler.PARAM_GROUP, group.getName());
            }
            else
            {
                Role role = locator.getRole();
                if (null != role)
                {
                    uri.addPathInfo(Profiler.PARAM_ROLE, role.getName());
                }
            }
        }

        // check Page to add to the uri
        String page = locator.getName();
        if (null != page)
        {
            uri.addPathInfo(Profiler.PARAM_PAGE, page);
        }

        return uri;
    }

    /**
     * Creates a new Profile object that can be successfully managed by
     * the current Profiler implementation
     *
     * @return A new Profile object
     */
    public Profile createProfile()
    {
        return (Profile)ServiceHelper.createObject(this.profileClass);
    }

    /**
     * Creates a new Profile object for a specific locator.
     *
     * @param locator The description of the profile.
     * @return A new Profile object
     */
    public Profile createProfile(ProfileLocator locator)
    {
        Profile profile = (Profile)ServiceHelper.createObject(this.profileClass);
        profile.init(locator);
        return profile;
    }

    /**
     * Creates a new ProfileLocator object that can be successfully managed by
     * the current Profiler implementation
     *
     * @return A new ProfileLocator object
     */
    public ProfileLocator createLocator()
    {
        return (ProfileLocator)ServiceHelper.createObject(this.locatorClass);
    }

    /**
     * Create a new profile given a profile locator
     * 
     * This method assumes that you have cloned and regenerated the
     * portlet ids if the portlets come from another profile.
     *
     * @param locator The description of the new profile to be created.
     * @param portlets The PSML tree
     */

    public Profile createProfile(ProfileLocator locator, Portlets portlets)
            throws ProfileException
    {
        if (portlets == null)
        {
            portlets = new PsmlPortlets();
        }
        
        Profile profile = createProfile(locator);
        PSMLDocument doc = new BasePSMLDocument(null, portlets);
        profile.setDocument(doc);
        doc = PsmlManager.createDocument(profile);
        profile.setDocument(doc);
        return profile;
    }

    /**
     * Create a new profile.
     * The profile parameter's document will be cloned.
     *
     * @param rundata The rundata object for the current request.
     * @param profile The description of the new profile to be created.
     * @param contentType create a profile for the specific contentType
     * @param from create a profile by cloning the profile from the specific user (if null - turbine is used)
     * @return The newly created profile.
     * -----------------------------------------------------------
     * Andreas Kempf, Siemens ICM S CP PE, Munich
     */

    /**
     * This methode creates a wml profile and a html profile
     * for a new user
     */

    public Profile createProfile( RunData data, Profile profile, String contentType, String from )
            throws ProfileException
    {
      if ((contentType == null) || (contentType.length() < 2))
        contentType = "html";

      if ((from == null) || (from.length() < 2))
        from = "turbine";


        if ((null == profile.getDocument()) || (!profile.getMediaType().equalsIgnoreCase (contentType)))
        {
            // locate the default resource

            // TODO: make this configurable

            try
            {
                ProfileLocator locator = createLocator();
                locator.setUser( JetspeedSecurity.getUser(from) );

                locator.setMediaType(contentType);
                PSMLDocument doc = fallback(locator);

                if (doc != null)
                {
                    PSMLDocument clonedDoc = (PSMLDocument) SerializationUtils.clone(doc);
                    org.apache.jetspeed.util.PortletUtils.regenerateIds(clonedDoc.getPortlets());
                    profile.setDocument(clonedDoc);
                }

                profile.setName( resourceDefault + resourceExt );

            }
            catch (Exception e)
            {
                logger.error( "Error creating profile", e );
                throw new ProfileException(e.toString());
            }
        }

        try
        {
            profile.setMediaType(contentType);

            PSMLDocument doc = PsmlManager.createDocument(profile);
            Profile newProfile = (Profile)profile.clone();
            newProfile.setDocument(doc);

            return newProfile;
        }
        catch (CloneNotSupportedException e)
        {
            logger.error("Could not clone profile locator object: ", e);
        }
        return null;

    }

   /** Create a new profile.
     *
     * @deprecated Should be removed when old customizer is removed.
     *
     * @param rundata The rundata object for the current request.
     * @param profile The description of the new profile to be created.
     * @param mt The specific mime type, which is converted to a mediatype.
     * @return The newly created profile.
     */
    public Profile createProfile( RunData data, Profile profile, MimeType mt )
        throws ProfileException
    {
        CapabilityMap cm = CapabilityMapFactory.getCapabilityMap(mt.getContentType());
        profile.setMediaType( getMediaType(data, cm) );
        return createProfile(data, profile);
    }

   /**
     *  Removes a profile.
     *
     * @param locator The profile locator criteria.
     */
    public void removeProfile( ProfileLocator locator )
    {
        PsmlManager.removeDocument(locator);
    }

    /** Query for a collection of profiles given a profile locator criteria.
     *
     * @param locator The profile locator criteria.
     * @return The list of profiles matching the locator criteria.
     */
    public Iterator query( QueryLocator locator )
    {
        return PsmlManager.query( locator );
    }

    /**
     * @see org.apache.jetspeed.services.profiler.ProfilerService#useRoleProfileMerging
     */
    public boolean useRoleProfileMerging()
    {
        return this.useRoleFallback && this.useRoleMerge;
    }
}

⌨️ 快捷键说明

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