jetspeedportletfactoryservice.java

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

JAVA
558
字号

        }
        catch ( Throwable t )
        {
            logger.error("Throwable", t);
            throw new PortletException( t.getMessage() );
        }

        // save the current meta-info
        String title = null;
        String description = null;
        MetaData metainfo = pc.getMetainfo();
        
        if ( metainfo != null ) {
            title=metainfo.getTitle();
            description=metainfo.getDescription();
        }
        
        
        // init the portlet, it may override its PSML defined markup if
        // it doesn't check for it
        portlet.setID( id );
        portlet.setName( pc.getName() );
        portlet.setPortletConfig( pc );
        portlet.setCreationTime( System.currentTimeMillis() );
        portlet.init();

        //force the title and description from markup metadata
        //in case the portlet overwrote some values

        if ( metainfo != null)
        {
            if (!MetaData.DEFAULT_TITLE.equals(title) )
            {
                portlet.setTitle( title );
            }

            if (!MetaData.DEFAULT_DESCRIPTION.equals(description) )
            {
                portlet.setDescription( description );
            }
        }

        if (enableCache && (portlet instanceof Cacheable))
        {
            //place this portlet in a cache...
            ((Cacheable)portlet).setHandle( handle );
            PortletCache.addCacheable( ((Cacheable)portlet) );
            //Expiration should be added to the portlet now, so that
            //the watcher is created before file changes on disk.
            ((Cacheable)portlet).getExpire();

        }

        //now compute the time it took to instantate and log it...
        // time in millis, sugested by Thomas Schaeck (schaeck@de.ibm.com)
        long milliseconds = ( System.currentTimeMillis() - begin );

        if (logger.isDebugEnabled())
            logger.debug( "PortletFactory.getPortlet(): constructed in "
                + milliseconds + " ms - handle: " + handle );

        return PortletWrapper.wrap( portlet );

    }

    /**
     * Given a Registry Entry, get the value of what its PortletConfig would be.
     *
     * @param entry the PSML Entry containing the config
     * @param portletId the PSML entry's portlet id
     * @return the newly created PortletConfig object
     */
    protected PortletConfig getPortletConfig( PortletEntry portletEntry, String id)
    {
        Map map = new HashMap();
        map.putAll(portletEntry.getParameterMap());
        
        PortletConfig pc = new BasePortletConfig();
        pc.setName( portletEntry.getName() );
        addParentInitParameters(portletEntry, map);        
        pc.setInitParameters( map );
        pc.setMetainfo( getMetaData( portletEntry ) );
        pc.setURL( portletEntry.getURL() );
        pc.setCachedOnURL( portletEntry.isCachedOnURL() );
        //pc.setSecurityRef(portletEntry.getSecurityRef());
        pc.setSecurityRef(getSecurityReference(null, portletEntry));

        if (runDataService != null)
        {
            JetspeedRunData rundata = runDataService.getCurrentRunData();
            if (rundata != null)
            {
                Profile profile = rundata.getProfile();
                if (profile != null)
                {
                    pc.setPageId(profile.getId());
                }
            }
        }
        pc.setPortletId(id);

        return pc;
    }
    
    
    /**
     * Fetches the parameters out of a PSML Entry
     * 
     * @param entry the Entry to check for parameters
     * @return a Map containing the parameters names/values, an empty Map 
     *         is returned if there are no parameters
     */
    protected static Map getParameters( Entry entry )
    {
        Hashtable hash = new Hashtable();
        
        Parameter[] props = entry.getParameter();
        
        for(int i = 0; i < props.length; ++i)
        {
            hash.put(props[i].getName(), props[i].getValue() );
        }
        
        return hash;
    }

    /**
    Create a MetaData object from a PSML Metainfo object
    
    @param meta the Metainfo to copy

    @return the new MetaData object, empty if meta is null
    */
    protected static MetaData getMetaData(Entry entry)
    {
        MetaData data = new MetaData();
        MetaInfo meta = entry.getMetaInfo();

        if ( meta != null )
        {
            if ( meta.getTitle() != null )
                data.setTitle( meta.getTitle() );

            if ( meta.getDescription() != null )
                data.setDescription( meta.getDescription() );

            if ( meta.getImage() != null )
                data.setImage( meta.getImage() );
        }

        if ( entry.getParent() != null )
        {

            PortletEntry parent = (PortletEntry)Registry
                .getEntry( Registry.PORTLET, entry.getParent() );

            if (parent != null)
            {
                MetaData parentData = getMetaData( parent );
                parentData.merge(data);
                return parentData;
            }
            
        }

        return data;

    }

    /**
    Create a MetaData object from a registry Metainfo object
    
    @param meta the Metainfo to copy

    @return the new MetaData object, empty if meta is null
    */
    protected static MetaData getMetaData(PortletEntry entry)
    {
        MetaData data = new MetaData();

        if ( entry.getTitle() != null )
            data.setTitle( entry.getTitle() );

        if ( entry.getDescription() != null )
            data.setDescription( entry.getDescription() );
            
		if ( entry.getMetaInfo() != null && entry.getMetaInfo().getImage() != null )
			data.setImage( entry.getMetaInfo().getImage() );
            
        return data;
    }
    
    /**
     * @param Entry entry Entry whose parent we want
     * @return PortletEntry Parent of Entry
     * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
     */
    protected static PortletEntry getParentEntry(PortletEntry entry)
    {
        PortletEntry result = null;
        String parent = entry.getParent();
        if (parent != null)
        {
            result = (PortletEntry) Registry.getEntry(Registry.PORTLET, parent);
        }

        return result;
    }
    
    /**
     * Retruns the classname defined for this PortletEntry.
     * If no classname was defined, the parent is queried
     * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
     */
    protected String getClassname(PortletEntry entry)
    {
        String className = entry.getClassname();
        if (className == null)
        {
            PortletEntry parent = getParentEntry(entry);
            if (parent != null)
            {
            	// We must walk up the hierarchy just to be safe
                className = getClassname(parent);
            }
        }

        return className;
    }
    
    /**
     * Maps all parameters, not found within the <code>entry</code>, from
     * the <code>entry</code>'s parent into the entry
     * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
     */
    protected void addParentInitParameters(PortletEntry entry, Map hash)
    {
        // Now map any parameters from the parent that the child does not have
        PortletEntry parent = getParentEntry(entry);
        if (parent != null)
        {
            Map parentMap = parent.getParameterMap();
            Iterator names = parent.getParameterNames();

            while (names.hasNext())
            {
                String key = (String) names.next();
                if (!hash.containsKey(key))
                {
                    hash.put(key, parentMap.get(key));                    
                }
            }
            
            // Always make sure to get the entire inheritence chain
           addParentInitParameters(parent, hash);
        }        
    }
    
    /**
     * Figures out how to produce a security reference for
     * this portlet.
     */
    protected SecurityReference getSecurityReference(Entry entry, PortletEntry pEntry)
    {
        // If something happended during init() that prevented this
        if (runDataService == null)
        {
            this.runDataService =
                (JetspeedRunDataService) TurbineServices.getInstance().getService(
                    RunDataService.SERVICE_NAME);
        }
        JetspeedRunData rundata = runDataService.getCurrentRunData();
        
        return JetspeedSecurity.getSecurityReference(entry,  rundata);
    }

}

⌨️ 快捷键说明

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