Package net.sf.jportlet.impl

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

/*
* Created on Mar 8, 2003
*/
package net.sf.jportlet.impl;

import java.io.IOException;
import java.io.InputStream;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.Enumeration;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

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

import net.sf.jportlet.portlet.Client;
import net.sf.jportlet.portlet.Portlet;
import net.sf.jportlet.portlet.PortletContext;
import net.sf.jportlet.portlet.PortletException;
import net.sf.jportlet.portlet.PortletLog;
import net.sf.jportlet.portlet.PortletRequest;
import net.sf.jportlet.portlet.PortletResponse;
import net.sf.jportlet.portlet.application.PortletApplication;
import net.sf.jportlet.portlet.application.PortletProxy;
import net.sf.jportlet.portlet.descriptor.PortletDescriptor;
import net.sf.jportlet.portlet.event.MessageEvent;
import net.sf.jportlet.service.PortletService;
import net.sf.jportlet.service.PortletServiceNotFoundException;
import net.sf.jportlet.service.PortletServiceUnavailableException;
import net.sf.jportlet.service.ssi.SSIFactoryService;
import net.sf.jportlet.util.ClasspathResourceLoader;
import net.sf.jportlet.util.WebResourceLoader;


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

    private static final Log __log = LogFactory.getLog( PortletContextImpl.class );

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

    private PortletProxy            _proxy;
    private PortletDescriptor       _descriptor;
    private PortletLog              _log;
    private PortletApplication      _application;
    private ServletContext          _servletContext;
    private WebResourceLoader       _webResourceLoader;
    private ClasspathResourceLoader _classResourceLoader;

    //~ Constructors -----------------------------------------------------------

    public PortletContextImpl( PortletProxy       proxy,
                               PortletApplication application,
                               ServletContext     servletContext )
    {
        _proxy               = proxy;
        _descriptor          = proxy.getDescriptor(  );
        _application         = application;
        _servletContext      = servletContext;
        _log                 = new PortletLogImpl( Portlet.class.getName(  ) + ":" + proxy.getDescriptor(  ).getName(  ) );
        _webResourceLoader   = new WebResourceLoader( _descriptor.getContextPath(  ), servletContext );
        _classResourceLoader = new ClasspathResourceLoader( _descriptor.getPortletClass(  ) );
    }

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

    /**
     * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
     */
    public Object getAttribute( String name )
    {
        return getApplication(  ).getAttribute( name );
    }

    /**
     * @see javax.servlet.ServletContext#getAttributeNames()
     */
    public Enumeration getAttributeNames(  )
    {
        return getApplication(  ).getAttributeNames(  );
    }

    /**
     * Not supported function. This method throws a <code>RuntimeException</code>
     * @throws RuntimeException
     *
     * @see javax.servlet.ServletContext#getContext(java.lang.String)
     */
    public ServletContext getContext( String name )
    {
        throw new RuntimeException( "not supported" );
    }

    /**
     * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
     */
    public String getInitParameter( String name )
    {
        return getApplication(  ).getInitParameter( name );
    }

    /**
     * @see javax.servlet.ServletContext#getInitParameterNames()
     */
    public Enumeration getInitParameterNames(  )
    {
        return getApplication(  ).getInitParameterNames(  );
    }

    /**
     * @see net.sf.jportlet.portlet.PortletContext#getLog()
     */
    public PortletLog getLog(  )
    {
        return _log;
    }

    /**
     * @see javax.servlet.ServletContext#getMajorVersion()
     */
    public int getMajorVersion(  )
    {
        return PortletApplication.MAJOR_VERSION;
    }

    /**
     * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
     */
    public String getMimeType( String file )
    {
        return _servletContext.getMimeType( file );
    }

    /**
     * @see javax.servlet.ServletContext#getMinorVersion()
     */
    public int getMinorVersion(  )
    {
        return PortletApplication.MINOR_VERSION;
    }

    /**
     * Not supported function. This method throws a <code>RuntimeException</code>
     * @throws RuntimeException
     *
     * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
     */
    public RequestDispatcher getNamedDispatcher( String name )
    {
        throw new RuntimeException( "not supported" );
    }

    /**
     * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
     */
    public String getRealPath( String path )
    {
        return _servletContext.getRealPath( _descriptor.getContextPath(  ) + path );
    }

    /**
     * Not supported function. This method throws a <code>RuntimeException</code>
     * @throws RuntimeException
     *
     * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
     */
    public RequestDispatcher getRequestDispatcher( String path )
    {
        throw new RuntimeException( "not supported" );
    }

    /**
     * @see javax.servlet.ServletContext#getResource(java.lang.String)
     */
    public URL getResource( String path )
        throws MalformedURLException
    {
        URL url = _classResourceLoader.getResource( path );
        return ( url != null )
               ? url
               : _webResourceLoader.getResource( path );
    }

    /**
     * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
     */
    public InputStream getResourceAsStream( String path )
    {
        InputStream in = _classResourceLoader.getResourceAsStream( path );
        return ( in != null )
               ? in
               : _webResourceLoader.getResourceAsStream( path );
    }

    /**
     * @see net.sf.jportlet.portlet.PortletContext#getResourceAsStream(java.lang.String, net.sf.jportlet.portlet.Client)
     */
    public InputStream getResourceAsStream( String path,
                                            Client client )
    {
        String      markup = client.getMarkup(  ).toString(  );
        InputStream in = _classResourceLoader.getResourceAsStream( path, markup );
        return ( in != null )
               ? in
               : _webResourceLoader.getResourceAsStream( path, markup );
    }

    /**
     * @see net.sf.jportlet.portlet.PortletContext#getResourceAsStream(java.lang.String, net.sf.jportlet.portlet.Client, java.util.Locale)
     */
    public InputStream getResourceAsStream( String path,
                                            Client client,
                                            Locale locale )
    {
        String      markup = client.getMarkup(  ).toString(  );
        InputStream in = _classResourceLoader.getResourceAsStream( path, markup, locale );
        return ( in != null )
               ? in
               : _webResourceLoader.getResourceAsStream( path, markup, locale );
    }

    /**
     * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
     */
    public Set getResourcePaths( String path )
    {
        return _servletContext.getResourcePaths( _descriptor.getContextPath(  ) + path );
    }

    /**
     * @see javax.servlet.ServletContext#getServerInfo()
     */
    public String getServerInfo(  )
    {
        return PortletApplication.INFOS;
    }

    /**
     * @see net.sf.jportlet.portlet.PortletContext#getService(java.lang.String)
     */
    public PortletService getService( String name )
        throws PortletServiceUnavailableException,
                   PortletServiceNotFoundException
    {
        return _application.getPortletServiceFactory(  ).getPortletService( name );
    }

    /**
     * @see javax.servlet.ServletContext#getServlet(java.lang.String)
     */
    public Servlet getServlet( String arg0 )
        throws ServletException
    {
        throw new RuntimeException( "not supported" );
    }

    /**
     * @see javax.servlet.ServletContext#getServletContextName()
     */
    public String getServletContextName(  )
    {
        return _descriptor.getContextName(  );
    }

    /**
     * @see javax.servlet.ServletContext#getServletNames()
     */
    public Enumeration getServletNames(  )
    {
        throw new RuntimeException( "deprecated" );
    }

    /**
     * @see javax.servlet.ServletContext#getServlets()
     */
    public Enumeration getServlets(  )
    {
        throw new RuntimeException( "deprecated" );
    }

    /**
     * @see net.sf.jportlet.portlet.PortletContext#getText(java.lang.String, java.util.Locale)
     */
    public String getText( String key,
                           Locale locale )
    {
        try
        {
            return _descriptor.getBundle( locale ).getString( key );
        }
        catch ( MissingResourceException m )
        {
            return "???" + key + "???";
        }
    }

    /**
     * @see net.sf.jportlet.portlet.PortletContext#include(java.lang.String, net.sf.jportlet.portlet.PortletRequest, net.sf.jportlet.portlet.PortletResponse)
     */
    public void include( String          path,
                         PortletRequest  request,
                         PortletResponse response )
        throws PortletException,
                   IOException
    {
        if ( __log.isDebugEnabled(  ) )
        {
            __log.debug( "include(" + path + ",...)" );
        }

        SSIFactoryService factory = ( SSIFactoryService ) getService( SSIFactoryService.NAME );
        factory.getSSI( path ).include( path, this, request, response );
    }

    /**
     * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String)
     */
    public void log( Exception cause,
                     String    message )
    {
        throw new RuntimeException( "deprecated" );
    }

    /**
     * @see javax.servlet.ServletContext#log(java.lang.String)
     */
    public void log( String message )
    {
        _log.info( message );
    }

    /**
     * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable)
     */
    public void log( String    message,
                     Throwable cause )
    {
        _log.error( message, cause );
    }

    /**
     * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
     */
    public void removeAttribute( String name )
    {
        getApplication(  ).removeAttribute( name );
    }

    /**
     * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
     */
    public void setAttribute( String name,
                              Object value )
    {
        getApplication(  ).setAttribute( name, value );
    }

    /**
     *
     * @see net.sf.jportlet.portlet.PortletContext#send(String, Object, PortletRequest, PortletResponse)
     */
    public void send( String          portletName,
                      Object          message,
                      PortletRequest  request,
                      PortletResponse response )
        throws PortletException
    {
        PortletProxy       proxy;
        PortletApplication container = getApplication(  );
        MessageEventImpl   event = new MessageEventImpl( message, ( PortletRequestImpl ) request, ( PortletResponseImpl ) response );

        if ( portletName != null )
        {
            proxy = ( PortletProxy ) container.getPortlet( portletName );
            send( event, proxy );
        }
        else
        {
            Enumeration names = container.getPortletNames(  );

            while ( names.hasMoreElements(  ) )
            {
                portletName = names.nextElement(  ).toString(  );
                proxy       = ( PortletProxy ) container.getPortlet( portletName );
                send( event, proxy );
            }
        }
    }

    private void send( MessageEvent event,
                       PortletProxy proxy )
        throws PortletException
    {
        proxy.messageReceived( event );
    }

    public PortletApplication getApplication(  )
    {
        return _application;
    }

    /**
     * @return ServletContext
     */
    public ServletContext getServletContext(  )
    {
        return _servletContext;
    }

    /**
     * @see net.sf.jportlet.portlet.PortletContext#getResourcePath(java.lang.String, net.sf.jportlet.portlet.Client, java.util.Locale)
     */
    public String getResourcePath( String path,
                                   Client client,
                                   Locale locale )
    {
        String markup = client.getMarkup(  ).toString(  );
        String xpath = _classResourceLoader.getResourcePath( path, markup, locale );
        return ( xpath != null )
               ? xpath
               : _webResourceLoader.getResourcePath( path, markup, locale );
    }

    /**
     * @see net.sf.jportlet.portlet.PortletContext#getResourcePath(java.lang.String, net.sf.jportlet.portlet.Client)
     */
    public String getResourcePath( String path,
                                   Client client )
    {
        String markup = client.getMarkup(  ).toString(  );
        String xpath = _classResourceLoader.getResourcePath( path, markup );
        return ( xpath != null )
               ? xpath
               : _webResourceLoader.getResourcePath( path, markup );
    }
}
TOP

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

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.