FileDocCategorySizeDatePackage
RepositoryManager.javaAPI DocApache James 2.3.18415Fri Jan 12 12:56:24 GMT 2007org.apache.james.mailrepository.filepair

RepositoryManager

public class RepositoryManager extends org.apache.avalon.framework.logger.AbstractLogEnabled implements org.apache.avalon.framework.service.Serviceable, org.apache.avalon.framework.context.Contextualizable, org.apache.avalon.cornerstone.services.store.Store, org.apache.avalon.framework.configuration.Configurable
phoenix:block
phoenix:service
name="org.apache.avalon.cornerstone.services.store.Store"

Fields Summary
private static final String
REPOSITORY_NAME
private static long
id
protected HashMap
m_repositories
protected HashMap
m_models
protected HashMap
m_classes
protected org.apache.avalon.framework.service.ServiceManager
m_componentManager
protected org.apache.avalon.framework.context.Context
m_context
Constructors Summary
Methods Summary
public voidconfigure(org.apache.avalon.framework.configuration.Configuration configuration)

        final Configuration[] registeredClasses =
            configuration.getChild( "repositories" ).getChildren( "repository" );

        for( int i = 0; i < registeredClasses.length; i++ )
        {
            registerRepository( registeredClasses[ i ] );
        }
    
public voidcontextualize(org.apache.avalon.framework.context.Context context)


          
    
        m_context = context;
    
public static final java.lang.StringgetName()

        return REPOSITORY_NAME;
    
public booleanisSelectable(java.lang.Object hint)

        if( hint instanceof Configuration )
            return true;
        else
            return false;
    
public voidregisterRepository(org.apache.avalon.framework.configuration.Configuration repConf)

        final String className = repConf.getAttribute( "class" );
        getLogger().info( "Registering Repository " + className );

        final Configuration[] protocols =
            repConf.getChild( "protocols" ).getChildren( "protocol" );
        final Configuration[] types = repConf.getChild( "types" ).getChildren( "type" );
        final Configuration[] modelIterator =
            repConf.getChild( "models" ).getChildren( "model" );

        for( int i = 0; i < protocols.length; i++ )
        {
            final String protocol = protocols[ i ].getValue();

            for( int j = 0; j < types.length; j++ )
            {
                final String type = types[ j ].getValue();

                for( int k = 0; k < modelIterator.length; k++ )
                {
                    final String model = modelIterator[ k ].getValue();
                    m_classes.put( protocol + type + model, className );
                    getLogger().info( "   for " + protocol + "," + type + "," + model );
                }
            }
        }
    
public voidrelease(java.lang.Object component)

    
public java.lang.Objectselect(java.lang.Object hint)

        Configuration repConf = null;
        try
        {
            repConf = (Configuration)hint;
        }
        catch( final ClassCastException cce )
        {
            throw new ServiceException("", "Hint is of the wrong type. " +
                                          "Must be a Configuration", cce );
        }

        URL destination = null;
        try
        {
            destination = new URL( repConf.getAttribute( "destinationURL" ) );
        }
        catch( final ConfigurationException ce )
        {
            throw new ServiceException("","Malformed configuration has no " +
                                          "destinationURL attribute", ce );
        }
        catch( final MalformedURLException mue )
        {
            throw new ServiceException("", "destination is malformed. " +
                                          "Must be a valid URL", mue );
        }

        try
        {
            final String type = repConf.getAttribute( "type" );
            final String repID = destination + type;
            Repository reply = (Repository)m_repositories.get( repID );
            final String model = (String)repConf.getAttribute( "model" );

            if( null != reply )
            {
                if( m_models.get( repID ).equals( model ) )
                {
                    return reply;
                }
                else
                {
                    final String message = "There is already another repository with the " +
                        "same destination and type but with different model";
                    throw new ServiceException("", message );
                }
            }
            else
            {
                final String protocol = destination.getProtocol();
                final String repClass = (String)m_classes.get( protocol + type + model );

                getLogger().debug( "Need instance of " + repClass + " to handle: " +
                                   protocol + type + model );

                try
                {
                    reply = (Repository)Class.forName( repClass ).newInstance();
                    setupLogger( reply, "repository" );

                    ContainerUtil.contextualize(reply,m_context);
                    ContainerUtil.service(reply,m_componentManager);

                    if (reply instanceof Composable) {
                        final String error = "no implementation in place to support Composable";
                        getLogger().error( error );
                        throw new IllegalArgumentException( error );
                    }

                    ContainerUtil.configure(reply,repConf);
                    ContainerUtil.initialize(reply);

                    m_repositories.put( repID, reply );
                    m_models.put( repID, model );
                    getLogger().info( "New instance of " + repClass + " created for " +
                                      destination );
                    return reply;
                }
                catch( final Exception e )
                {
                    final String message = "Cannot find or init repository: " + e.getMessage();
                    getLogger().warn( message, e );

                    throw new ServiceException("", message, e );
                }
            }
        }
        catch( final ConfigurationException ce )
        {
            throw new ServiceException("", "Malformed configuration", ce );
        }
    
public voidservice(org.apache.avalon.framework.service.ServiceManager componentManager)

        m_componentManager = componentManager;