/*
* Created on Mar 31, 2003
*/
package net.sf.jportlet.impl;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.servlet.ServletConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jportlet.portlet.application.PortletApplication;
import net.sf.jportlet.service.PortletService;
import net.sf.jportlet.service.PortletServiceException;
import net.sf.jportlet.service.PortletServiceFactory;
import net.sf.jportlet.service.PortletServiceNotFoundException;
import net.sf.jportlet.service.PortletServiceUnavailableException;
/**
* Implementation of {@link PortletServiceFactory}
*
* @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
*/
public class PortletServiceFactoryImpl
implements PortletServiceFactory
{
//~ Static fields/initializers ---------------------------------------------
private static final Log __log = LogFactory.getLog( PortletServiceFactoryImpl.class );
public static final String JPORTLET_SERVICES = "jportlet.services";
public static final String _CLASS = ".class";
//~ Instance fields --------------------------------------------------------
private HashMap _services = new HashMap( );
private HashMap _unavailables = new HashMap( );
private PortletApplication _application;
//~ Constructors -----------------------------------------------------------
public PortletServiceFactoryImpl( PortletApplication application )
{
_application = application;
}
//~ Methods ----------------------------------------------------------------
public void init( InputStream in,
ServletConfig servletConfig )
throws PortletServiceException
{
boolean info = __log.isInfoEnabled( );
if ( info )
{
__log.info( "Initializing" );
}
try
{
Properties props = new Properties( );
props.load( in );
/* Get all the properties */
String services = props.getProperty( JPORTLET_SERVICES );
StringTokenizer tokenizer = new StringTokenizer( services, "," );
while ( tokenizer.hasMoreTokens( ) )
{
String prefix = tokenizer.nextToken( ).trim( );
String classname = props.getProperty( prefix + _CLASS );
PortletService service = ( PortletService ) Class.forName( classname ).newInstance( );
if ( info )
{
__log.info( "Initializing service: " + service.getServiceName( ) );
}
try
{
service.init( new PortletServiceConfigImpl( prefix, props, servletConfig, _application ) );
_services.put( service.getServiceName( ), service );
}
catch ( Exception e )
{
__log.error( "Unable to initialize service: " + prefix, e );
_unavailables.put( prefix, service );
}
}
}
catch ( Exception e )
{
__log.error( "Unexpected error", e );
throw new PortletServiceException( "Error while initializing PortletServiceFactory", e );
}
}
public void destroy( )
{
boolean info = __log.isInfoEnabled( );
if ( info )
{
__log.info( "Destroying" );
}
Iterator it = _services.values( ).iterator( );
while ( it.hasNext( ) )
{
PortletService service = ( PortletService ) it.next( );
if ( info )
{
__log.info( "Destroying service:" + service.getServiceName( ) );
}
service.destroy( );
}
_services.clear( );
_unavailables.clear( );
}
/**
* @see net.sf.jportlet.service.PortletServiceFactory#getPortletService(java.lang.String)
*/
public PortletService getPortletService( String name )
throws PortletServiceNotFoundException,
PortletServiceUnavailableException
{
PortletService service = ( PortletService ) _services.get( name );
if ( service == null )
{
service = ( PortletService ) _unavailables.get( name );
if ( service == null )
{
throw new PortletServiceNotFoundException( name );
}
else
{
throw new PortletServiceUnavailableException( name );
}
}
else
{
return service;
}
}
}