Package net.sf.jportlet.impl

Source Code of net.sf.jportlet.impl.PortletApplicationImpl

/*
* Created on Apr 1, 2003
*/
package net.sf.jportlet.impl;

import java.io.InputStream;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.jportlet.portlet.Portlet;
import net.sf.jportlet.portlet.PortletException;
import net.sf.jportlet.portlet.application.PortletApplication;
import net.sf.jportlet.portlet.application.PortletNotFoundException;
import net.sf.jportlet.portlet.application.PortletProxy;
import net.sf.jportlet.portlet.descriptor.ApplicationDescriptor;
import net.sf.jportlet.portlet.descriptor.ApplicationDescriptorLoaderXml;
import net.sf.jportlet.portlet.descriptor.PortletDescriptor;
import net.sf.jportlet.service.PortletServiceFactory;


/**
* Implementation of {@link PortletApplication}
*
* @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
*/
public class PortletApplicationImpl
    implements PortletApplication
{
    //~ Static fields/initializers ---------------------------------------------

    private static final Log   __log = LogFactory.getLog( PortletApplicationImpl.class );
    public static final String SERVICE_DD = "WEB-INF/jportlet.properties";
    public static final String PORTLET_DD = "WEB-INF/portlet.xml";

    //~ Instance fields --------------------------------------------------------

    private HashMap                   _attributes = new HashMap(  );
    private ApplicationDescriptor     _descriptor;
    private HashMap                   _portlets = new HashMap(  );
    private PortletServiceFactoryImpl _serviceFactory;

    //~ Methods ----------------------------------------------------------------

    public void init( ServletConfig servletConfig )
        throws PortletException
    {
        InputStream in;
        boolean     info = __log.isInfoEnabled(  );

        if ( info )
        {
            __log.info( "Initializing" );
        }

        ServletContext ctx = servletConfig.getServletContext(  );

        /* Initializing the ServiceFactory */
        in = ctx.getResourceAsStream( SERVICE_DD );

        if ( in == null )
        {
            throw new PortletException( "Resource not found: " + SERVICE_DD );
        }

        _serviceFactory = new PortletServiceFactoryImpl( this );
        _serviceFactory.init( in, servletConfig );

        /* Initialize the portlets */
        in = ctx.getResourceAsStream( PORTLET_DD );

        if ( in == null )
        {
            throw new PortletException( "Resource not found: " + PORTLET_DD );
        }

        _descriptor = new ApplicationDescriptorLoaderXml(  ).load( in );

        Iterator it = _descriptor.getPortletDescriptors(  ).iterator(  );

        while ( it.hasNext(  ) )
        {
            PortletDescriptor descr = ( PortletDescriptor ) it.next(  );
            PortletProxy      proxy = new PortletProxy( descr );
            String            name = descr.getName(  );

            if ( info )
            {
                __log.info( "Initializing Portlet: " + name );
            }

            proxy.init( new PortletConfigImpl( proxy, servletConfig, this ) );
            _portlets.put( name, proxy );
        }
    }

    public void destroy(  )
    {
        boolean info = __log.isInfoEnabled(  );

        if ( info )
        {
            __log.info( "Destroying" );
        }

        /* Destroy the services */
        _serviceFactory.destroy(  );

        /* Destroy the portlets */
        Iterator it = _portlets.values(  ).iterator(  );

        while ( it.hasNext(  ) )
        {
            Portlet portlet = ( Portlet ) it.next(  );

            if ( info )
            {
                __log.info( "Destroying Portlet: " + portlet.getPortletConfig(  ).getPortletName(  ) );
            }

            portlet.destroy(  );
        }
    }

    /**
     * @see net.sf.jportlet.portlet.application.PortletApplication#getAttribute(java.lang.String)
     */
    public Object getAttribute( String name )
    {
        return _attributes.get( name );
    }

    /**
     * @see net.sf.jportlet.portlet.application.PortletApplication#getAttributeNames()
     */
    public Enumeration getAttributeNames(  )
    {
        return Collections.enumeration( _attributes.keySet(  ) );
    }

    /**
     * @see net.sf.jportlet.portlet.application.PortletApplication#getInitParameter(java.lang.String)
     */
    public String getInitParameter( String name )
    {
        return _descriptor.getContextParameter( name );
    }

    /**
     * @see net.sf.jportlet.portlet.application.PortletApplication#getInitParameterNames()
     */
    public Enumeration getInitParameterNames(  )
    {
        return _descriptor.getContextParameterNames(  );
    }

    /**
     * @see net.sf.jportlet.portlet.application.PortletApplication#getPortlet(java.lang.String)
     */
    public Portlet getPortlet( String name )
        throws PortletNotFoundException
    {
        Portlet portlet = ( Portlet ) _portlets.get( name );
        if ( portlet == null )
        {
          throw new PortletNotFoundException( name );
        }
        return portlet;
    }

    /**
     * @see net.sf.jportlet.portlet.application.PortletApplication#getPortletNames()
     */
    public Enumeration getPortletNames(  )
    {
        return Collections.enumeration( _portlets.keySet(  ) );
    }

    /**
     * @see net.sf.jportlet.portlet.application.PortletApplication#removeAttribute(java.lang.String)
     */
    public void removeAttribute( String name )
    {
        _attributes.remove( name );
    }

    /**
     * @see net.sf.jportlet.portlet.application.PortletApplication#setAttribute(java.lang.String, java.lang.Object)
     */
    public void setAttribute( String name,
                              Object value )
    {
        _attributes.put( name, value );
    }

    /**
     * @see net.sf.jportlet.portlet.application.PortletApplication#getPortletServiceFactory()
     */
    public PortletServiceFactory getPortletServiceFactory(  )
    {
        return _serviceFactory;
    }
}
TOP

Related Classes of net.sf.jportlet.impl.PortletApplicationImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.