jicarilla-sandbox/platform/container-integration/src/java/org/jicarilla/container/integration/pico JicarillaBasedPicoComponentAdapter.java,NONE,1.1 JicarillaBasedPicoContainer.java,NONE,1.1 PicoAssignabilityException.java,NONE,1.1 PicoBasedJicarillaComponentAdapter.java,NONE,1.1 PicoBasedJicarillaComponentFactory.java,NONE,1.1 PicoBasedKeyAwareComponentAdapter.java,NONE,1.1 PicoClassNotFoundException.java,NONE,1.1 PicoComponentAdapterBasedSelector.java,NONE,1.1 PicoContainerBasedSelector.java,NONE,1.1 PicoDuplicateComponentKeyRegistrationException.java,NONE,1.1 PicoIllegalAccessException.java,NONE,1.1 PicoInstantationException.java,NONE,1.1 PicoInvocationTargetException.java,NONE,1.1 ResolverBasedPicoContainer.java,NONE,1.1

[email protected]
Newsgroups gmane.comp.java.jicarilla.cvs
Message-ID <[email protected]>
Update of /cvsroot/jicarilla/jicarilla-sandbox/platform/container-integration/src/java/org/jicarilla/container/integration/pico
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19900/container-integration/src/java/org/jicarilla/container/integration/pico

Added Files:
	JicarillaBasedPicoComponentAdapter.java 
	JicarillaBasedPicoContainer.java 
	PicoAssignabilityException.java 
	PicoBasedJicarillaComponentAdapter.java 
	PicoBasedJicarillaComponentFactory.java 
	PicoBasedKeyAwareComponentAdapter.java 
	PicoClassNotFoundException.java 
	PicoComponentAdapterBasedSelector.java 
	PicoContainerBasedSelector.java 
	PicoDuplicateComponentKeyRegistrationException.java 
	PicoIllegalAccessException.java PicoInstantationException.java 
	PicoInvocationTargetException.java 
	ResolverBasedPicoContainer.java 
Log Message:
split out integration code from the main container project

--- NEW FILE: JicarillaBasedPicoComponentAdapter.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.jicarilla.container.Adapter;
import org.jicarilla.container.JicarillaClassNotFoundException;
import org.jicarilla.container.JicarillaIllegalAccessException;
import org.jicarilla.container.JicarillaInstantiationException;
import org.jicarilla.container.JicarillaInvocationTargetException;
import org.jicarilla.framework.CriterionExposingSelector;
import org.jicarilla.framework.OrSelector;
import org.jicarilla.framework.Selector;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.PicoException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: JicarillaBasedPicoComponentAdapter.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class JicarillaBasedPicoComponentAdapter implements org.picocontainer.ComponentAdapter
{
    final Adapter m_delegate;
    final Object m_componentKey;

    public JicarillaBasedPicoComponentAdapter( final Adapter delegate,
            final Object componentKey )
    {
        m_delegate = delegate;
        m_componentKey = componentKey;
    }

    public Object getComponentKey()
    {
        if(     m_componentKey == null &&
                m_delegate instanceof CriterionExposingSelector )
        {
            if(m_delegate instanceof OrSelector)
            {
                CriterionExposingSelector selector =
                        ((CriterionExposingSelector)m_delegate);

                final Selector[] selectors = (Selector[])selector.getCriterion();
                if( selectors[0] instanceof CriterionExposingSelector )
                    selector = ((CriterionExposingSelector)selectors[0]);

                return selector.getCriterion();
            }
            else
            {
                return ((CriterionExposingSelector)m_delegate).getCriterion();
            }
        }

        return m_componentKey;
    }

    public Class getComponentImplementation()
    {
        try
        {
            return m_delegate.getInstance().getClass();
        }
        catch( JicarillaIllegalAccessException e )
        {
            throw new PicoIllegalAccessException( e );
        }
        catch( JicarillaInvocationTargetException e )
        {
            throw new PicoInvocationTargetException( e );
        }
        catch( JicarillaInstantiationException e )
        {
            throw new PicoInstantationException( e );
        }
        catch( JicarillaClassNotFoundException e )
        {
            throw new PicoClassNotFoundException( e );
        }
    }

    public Object getComponentInstance(
            final MutablePicoContainer dependencyContainer )
            throws PicoException
    {
        try
        {
            return m_delegate.getInstance();
        }
        catch( JicarillaIllegalAccessException e )
        {
            throw new PicoIllegalAccessException( e );
        }
        catch( JicarillaInvocationTargetException e )
        {
            throw new PicoInvocationTargetException( e );
        }
        catch( JicarillaInstantiationException e )
        {
            throw new PicoInstantationException( e );
        }
        catch( JicarillaClassNotFoundException e )
        {
            throw new PicoClassNotFoundException( e );
        }
    }

    public void verify( final org.picocontainer.PicoContainer picoContainer )
    {
        /*throw new UnsupportedOperationException(
                "The PicoContainer verification feature is not supported." );*/
    }
}

--- NEW FILE: JicarillaBasedPicoContainer.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.jicarilla.container.DefaultContainer;
import org.jicarilla.container.Factory;
import org.jicarilla.container.JicarillaClassNotFoundException;
import org.jicarilla.container.JicarillaIllegalAccessException;
import org.jicarilla.container.JicarillaInstantiationException;
import org.jicarilla.container.JicarillaInvocationTargetException;
import org.jicarilla.container.adapters.SingletonAdapter;
import org.jicarilla.container.factories.ManualComponentFactory;
import org.jicarilla.container.factories.Type3ComponentFactory;
import org.jicarilla.container.selectors.ClassSelector;
import org.jicarilla.framework.Assert;
import org.jicarilla.framework.EquivalenceSelector;
import org.jicarilla.framework.OrSelector;
import org.jicarilla.framework.Selector;
import org.jicarilla.framework.Switch;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.PicoException;
import org.picocontainer.PicoRegistrationException;
import org.picocontainer.PicoVerificationException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: JicarillaBasedPicoContainer.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class JicarillaBasedPicoContainer extends DefaultContainer
        implements org.picocontainer.PicoContainer, MutablePicoContainer
{
    protected final Map m_unmanagedInstances = new HashMap();

    // ----------------------------------------------------------------------
    //  PicoContainer Interface
    // ----------------------------------------------------------------------

    public Object getComponentInstance( final Object componentKey )
            throws PicoException
    {
        try
        {
            return super.getResolver().get( componentKey );
        }
        catch( JicarillaIllegalAccessException e )
        {
            throw new PicoIllegalAccessException( e );
        }
        catch( JicarillaInvocationTargetException e )
        {
            throw new PicoInvocationTargetException( e );
        }
        catch( JicarillaInstantiationException e )
        {
            throw new PicoInstantationException( e );
        }
        catch( JicarillaClassNotFoundException e )
        {
            throw new PicoClassNotFoundException( e );
        }
    }

    public List getComponentInstances() throws PicoException
    {
        final List instances = new ArrayList();
        final Iterator it = super.m_switch.values().iterator();
        while( it.hasNext() )
        {
            final org.jicarilla.container.Adapter adapter =
                 (org.jicarilla.container.Adapter)it.next();

            final Object instance;
            try
            {
                instance = adapter.getInstance();
            }
            catch( JicarillaIllegalAccessException e )
            {
                throw new PicoIllegalAccessException( e );
            }
            catch( JicarillaInvocationTargetException e )
            {
                throw new PicoInvocationTargetException( e );
            }
            catch( JicarillaInstantiationException e )
            {
                throw new PicoInstantationException( e );
            }
            catch( JicarillaClassNotFoundException e )
            {
                throw new PicoClassNotFoundException( e );
            }

            instances.add( instance );
        }
        return instances;
    }

    public boolean hasComponent( final Object componentKey )
    {
        return super.getResolver().contains( componentKey );
    }

    public Object getComponentMulticaster( final boolean callInInstantiationOrder,
            final boolean callUnmanagedComponents ) throws PicoException
    {
        throw new UnsupportedOperationException(
                "The PicoContainer multicaster feature is not supported." );
    }

    public Object getComponentMulticaster() throws PicoException
    {
        throw new UnsupportedOperationException(
                "The PicoContainer multicaster feature is not supported." );
    }

    public Collection getComponentKeys()
    {
        throw new UnsupportedOperationException(
                "The Jicarilla container cannot export all of its component " +
                "keys." );
    }

    public Collection getChildContainers()
    {
        throw new UnsupportedOperationException(
                "The PicoContainer hierarchy feature is not supported." );
    }

    public List getParentContainers()
    {
        throw new UnsupportedOperationException(
                "The PicoContainer hierarchy feature is not supported." );
    }

    public ComponentAdapter findComponentAdapter( final Object componentKey )
    {
        if( !super.getResolver().contains( componentKey ) )
            return null;

        final Switch.Entry entry =
                (Switch.Entry)super.m_switch.get( componentKey );
final org.jicarilla.container.Adapter adapter =
        (org.jicarilla.container.Adapter)entry.getValue();
        return new JicarillaBasedPicoComponentAdapter(
                adapter, componentKey );
    }

    public void verify() throws PicoVerificationException
    {
        /*throw new UnsupportedOperationException(
                "The PicoContainer verification feature is not supported." );*/
    }

    public List getUnmanagedComponentInstances() throws PicoException
    {
        final Collection values = m_unmanagedInstances.values();
        final List list = new ArrayList();
        list.addAll( values );
        return Collections.unmodifiableList( list );
    }

    // ----------------------------------------------------------------------
    //  MutablePicoContainer Interface
    // ----------------------------------------------------------------------

    public ComponentAdapter registerComponentImplementation( final Object componentKey,
            final Class componentImplementation )
    {
        final Selector selector = getSelector(
                componentKey, componentImplementation );

        final Factory factory = new Type3ComponentFactory(
                super.getResolver(),
                componentImplementation.getName() );
        final SingletonAdapter adapter = new SingletonAdapter(
                factory );

        super.registerAdapter( selector, adapter );

        //cacheInstance( componentKey );

        return new JicarillaBasedPicoComponentAdapter( adapter, componentKey );
    }

    public ComponentAdapter registerComponentImplementation( final Object componentKey,
            final Class componentImplementation, final Parameter[] parameters )
            throws PicoRegistrationException
    {
        final Selector selector = getSelector(
                componentKey, componentImplementation );

        final Factory factory = new PicoBasedJicarillaComponentFactory(
                this, componentImplementation.getName(), parameters );
        final SingletonAdapter adapter = new SingletonAdapter(
                factory );

        super.registerAdapter( selector, adapter );

        //cacheInstance( componentKey );

        return new JicarillaBasedPicoComponentAdapter( adapter, componentKey );
    }

    public ComponentAdapter registerComponentImplementation(
            final Class componentImplementation ) throws PicoRegistrationException
    {
        final ClassSelector selector =
                new ClassSelector( componentImplementation );
        final Type3ComponentFactory factory = new Type3ComponentFactory(
                super.getResolver(),
                componentImplementation.getName() );
        final SingletonAdapter adapter =
                new SingletonAdapter( factory );

        super.registerAdapter( selector, adapter );

        //cacheInstance( componentImplementation );

        return new JicarillaBasedPicoComponentAdapter(
                adapter, componentImplementation );
    }

    public ComponentAdapter registerComponentInstance( final Object componentInstance )
            throws PicoRegistrationException
    {
        final Class clazz = componentInstance.getClass();
        final ClassSelector selector = new ClassSelector( clazz );
        final ManualComponentFactory factory = new ManualComponentFactory(
                componentInstance );

        final SingletonAdapter adapter = new SingletonAdapter(
                factory );

        super.registerAdapter( selector, adapter );

        cacheInstance( clazz );

        m_unmanagedInstances.put(
                clazz, componentInstance );

        return new JicarillaBasedPicoComponentAdapter(
                adapter, clazz );
    }

    public ComponentAdapter registerComponentInstance( final Object componentKey,
            final Object componentInstance ) throws PicoRegistrationException
    {
        final Class clazz = componentInstance.getClass();
        final Selector selector = getSelector( componentKey, clazz );

        final ManualComponentFactory factory = new ManualComponentFactory(
                componentInstance );
        final SingletonAdapter adapter = new SingletonAdapter(
                factory );

        super.registerAdapter( selector, adapter );

        cacheInstance( componentKey );

        m_unmanagedInstances.put( componentKey, componentInstance );

        return new JicarillaBasedPicoComponentAdapter(
                adapter, componentKey );
    }

    public ComponentAdapter unregisterComponent( final Object componentKey )
    {
        final org.jicarilla.container.Adapter adapter =
             (org.jicarilla.container.Adapter)super.m_switch
             .remove( componentKey );

        if( m_unmanagedInstances.containsKey( componentKey ) )
        {
            m_unmanagedInstances.remove( componentKey );
        }

        if( adapter != null )
            return new JicarillaBasedPicoComponentAdapter(
                    adapter, componentKey );

        return null;
    }

    public void addOrderedComponentAdapter( final ComponentAdapter componentAdapter )
    {
        /* not needed in practice;
           pico is wrong... Selector selector = getSelector(
                componentAdapter.getComponentKey(),
                componentAdapter.getComponentImplementation() );
        super.addAdapter(
                selector,
                new PicoBasedJicarillaComponentAdapter(
                        componentAdapter, this ) );*/
    }

    public boolean addChild( final MutablePicoContainer child )
    {
        throw new UnsupportedOperationException(
                "The PicoContainer hierarchy feature is not supported." );
    }

    public boolean addParent( final MutablePicoContainer parent )
    {
        throw new UnsupportedOperationException(
                "The PicoContainer hierarchy feature is not supported." );
    }

    public boolean removeChild( final MutablePicoContainer child )
    {
        throw new UnsupportedOperationException(
                "The PicoContainer hierarchy feature is not supported." );
    }

    public boolean removeParent( final MutablePicoContainer parent )
    {
        throw new UnsupportedOperationException(
                "The PicoContainer hierarchy feature is not supported." );
    }

    public void registerOrderedComponentAdapter(
            final ComponentAdapter componentAdapter )
    {
        addOrderedComponentAdapter( componentAdapter );
    }

    public void registerComponent( final ComponentAdapter componentAdapter )
            throws PicoRegistrationException
    {
        addOrderedComponentAdapter( componentAdapter );
    }

    // ----------------------------------------------------------------------
    //  Helper methods
    // ----------------------------------------------------------------------

    protected void cacheInstance( final Object key )
    {
        // make the container cache the instance
        try
        {
            Assert.assertNotNull(super.getResolver().get( key ));
        }
        catch( JicarillaIllegalAccessException e )
        {
            throw new PicoIllegalAccessException( e );
        }
        catch( JicarillaInvocationTargetException e )
        {
            throw new PicoInvocationTargetException( e );
        }
        catch( JicarillaInstantiationException e )
        {
            throw new PicoInstantationException( e );
        }
        catch( JicarillaClassNotFoundException e )
        {
            throw new PicoClassNotFoundException( e );
        }
    }

    protected Selector getSelector( final Object componentKey, final Class componentClass )
    {
        if( componentKey instanceof Class )
        {
            final Class key = (Class)componentKey;
            if( !key.isAssignableFrom( componentClass ) )
            {
                throw new PicoAssignabilityException( key, componentClass );
            }
        }

        if( super.getResolver().contains( componentKey ) )
            throw new PicoDuplicateComponentKeyRegistrationException(
                    componentKey );

        final Selector keySelector;
        if( componentKey instanceof Class )
            keySelector = new ClassSelector( (Class)componentKey );
        else
            keySelector = new EquivalenceSelector( componentKey );

        // add as class as well
        final Selector classSelector = new ClassSelector( componentClass );

        final Selector aggregrate =
                new OrSelector( new Selector[] {
                    keySelector, classSelector } );

        return aggregrate;
    }
}

--- NEW FILE: PicoAssignabilityException.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.picocontainer.PicoException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PicoAssignabilityException.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoAssignabilityException extends PicoException
{
    protected final Class m_key;
    protected final Class m_implementation;

    public PicoAssignabilityException( final Class key, final Class implementation )
    {
        m_key = key;
        m_implementation = implementation;
    }
    public String getMessage()
    {
        return "Cannot register the " + m_key.getName() +
                " class as a key for the class " +
                m_implementation.getName() +
                " because the latter does not implement the former.";
    }
}

--- NEW FILE: PicoBasedJicarillaComponentAdapter.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.jicarilla.container.Adapter;
import org.picocontainer.MutablePicoContainer;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PicoBasedJicarillaComponentAdapter.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoBasedJicarillaComponentAdapter implements Adapter
{
    protected final org.picocontainer.ComponentAdapter m_delegate;
    protected final MutablePicoContainer m_dependencyContainer;

    public PicoBasedJicarillaComponentAdapter(
            final org.picocontainer.ComponentAdapter delegate,
            final MutablePicoContainer dependencyContainer )
    {
        m_delegate = delegate;
        m_dependencyContainer = dependencyContainer;
    }

    public Object getInstance()
    {
        return m_delegate.getComponentInstance( m_dependencyContainer );
    }

    public void releaseInstance( final Object component ) throws Exception
    {
        // do nothing
    }
}

--- NEW FILE: PicoBasedJicarillaComponentFactory.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.jicarilla.container.Adapter;
import org.jicarilla.container.Container;
import org.jicarilla.container.JicarillaClassNotFoundException;
import org.jicarilla.container.NoPublicConstructorAvailableException;
import org.jicarilla.container.NoSatisfiableConstructorAvailableException;
import org.jicarilla.container.Resolver;
import org.jicarilla.container.factories.AbstractComponentFactory;
import org.jicarilla.container.util.ReflectionUtil;
import org.jicarilla.container.util.Type3Util;
import org.jicarilla.framework.Assert;
import org.jicarilla.framework.Selector;
import org.picocontainer.Parameter;
import org.picocontainer.PicoException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Set;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PicoBasedJicarillaComponentFactory.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoBasedJicarillaComponentFactory extends AbstractComponentFactory
{
    protected Constructor m_constructor;
    protected final Parameter[] m_parameters;
    protected final JicarillaBasedPicoContainer m_picoContainer;

    public PicoBasedJicarillaComponentFactory( final JicarillaBasedPicoContainer container,
            final String className, final Parameter[] parameters )
    {
        super( container.getResolver(), className );
        m_parameters = parameters;
        //m_resolver = new PicoComponentFactoryContainer(
        //        container, parameters );
        m_picoContainer = container;
    }

    protected Object doNewInstance() throws IllegalAccessException,
            InvocationTargetException, InstantiationException
    {
        if( m_clazz == null )
            try
            {
                m_clazz = ReflectionUtil.loadClass( m_className );
            }
            catch( ClassNotFoundException e )
            {
                throw new JicarillaClassNotFoundException( e );
            }
        if( m_constructor == null )
        {
            m_constructor = getGreediestSatisfiableConstructor(
                    m_clazz, m_picoContainer, m_parameters );
        }

        final Class[] parameterTypes = m_constructor.getParameterTypes();
        super.checkCyclicDependency( parameterTypes );
        super.enableCyclicDependencyChecking();

        final Object[] parameters = new Object[parameterTypes.length];

        if( parameterTypes.length > 0 )
        {
            for( int i = 0; i < parameters.length; i++ )
            {
                if( i < m_parameters.length && m_parameters[i] != null )
                    parameters[i] = m_parameters[i]
                            .resolveAdapter( m_picoContainer )
                            .getComponentInstance( m_picoContainer );
                else
                    // this should not throw an exception, since
                    // the container said it contained the parameter type!
                    parameters[i] = m_resolver.get( parameterTypes[i] );
            }
            return m_constructor.newInstance( parameters );
        }
        else
        {
            return m_clazz.newInstance();
        }
    }

    public class PicoComponentFactoryContainer implements Container
    {
        protected final JicarillaBasedPicoContainer m_delegate;
        protected final Parameter[] m_parameters;
        protected final Resolver m_resolver;

        public PicoComponentFactoryContainer( final JicarillaBasedPicoContainer delegate,
                final Parameter[] parameters )
        {
            m_delegate = delegate;
            m_parameters = parameters;
            m_resolver =
                    new PicoComponentFactoryResolver(
                            m_delegate.getResolver(), delegate );
        }
        
        public Container registerAdapter(
                final Selector selector, final Adapter adapter )
        {
            m_delegate.registerAdapter( selector, adapter );
            return this;
        }

        public Container registerAdapter(
                final Object key, final Adapter adapter )
        {
            m_delegate.registerAdapter( key, adapter );
            return this;
        }

        public Resolver getResolver()
        {
            return m_resolver;
        }
    }
    protected class PicoComponentFactoryResolver implements Resolver
    {
        protected final Resolver m_delegate;
        private final JicarillaBasedPicoContainer m_container;

        public PicoComponentFactoryResolver( final Resolver delegate,
                final JicarillaBasedPicoContainer container )
        {
            m_delegate = delegate;
            m_container = container;
        }

        public Object get( final Object key )
        {
            // try and fetch the dependency from the parameter set
            for( int i = 0; i < m_parameters.length; i++ )
            {
                if( m_parameters[i] == null )
                    continue;

                try
                {
                    final org.picocontainer.ComponentAdapter adapter =
                            m_parameters[i].resolveAdapter( m_container );
                    final Object currentKey = adapter.getComponentKey();
                    if( currentKey.equals( key ) )
                    {
                        final Object instance =
                                adapter.getComponentInstance( m_container );
                        // use a parameter only once!
                        m_parameters[i] = null;
                        return instance;
                    }
                }
                catch( PicoException pe )
                {
                    // continue
                }
            }

            // that didn't work, so do the 'regular type 3' thing
            return m_delegate.get( key );
        }

        public Object[] getAll( final Object key )
        {
            return m_delegate.getAll( key );
        }

        public boolean contains( final Object key )
        {
            return m_delegate.contains( key );
        }

        public void releaseInstance( final Object component ) throws Exception
        {
            m_delegate.releaseInstance( component );
        }

    }

    public static Constructor getGreediestSatisfiableConstructor( final Class clazz,
            final JicarillaBasedPicoContainer dependencyProvider, final Parameter[] parameters )
    {
        // this is the picocontainer algorithm, modified
        // to support specified parameters
        Assert.assertNotNull( "clazz argument may not be null", clazz );
        final Constructor[] constructors = clazz.getConstructors();

        final Class[] providedParameterTypes = new Class[parameters.length];
        for( int i = 0; i < parameters.length; i++ )
        {
            final Parameter p = parameters[i];
            final Class c = p.resolveAdapter( dependencyProvider )
                    .getComponentImplementation();
            providedParameterTypes[i] = c;
        }

        if( constructors.length == 0 )
            throw new NoPublicConstructorAvailableException( clazz );

        Type3Util.sortConstructorsByGreediness( constructors );

        final Set unsatisfiedDependencies = new HashSet();
        for( int i = 0; i < constructors.length; i++ )
        {
            final Constructor constructor = constructors[i];
            final Class[] parameterTypes = constructor.getParameterTypes();

            boolean found = true;
            for( int j = 0; j < parameterTypes.length; j++ )
            {
                final Class parameterType = parameterTypes[j];

                boolean alreadyFound = false;
                for( int k = 0; k < providedParameterTypes.length; k++ )
                {
                    final Class type = providedParameterTypes[k];
                    if( type != null &&
                            parameterType.isAssignableFrom(type) )
                    {
                        alreadyFound = true;
                        providedParameterTypes[k] = null; // use only once
                        break;
                    }
                }
                if(alreadyFound)
                    continue;

                if( !dependencyProvider.getResolver().contains(parameterType) )
                {
                    unsatisfiedDependencies.add( parameterType );
                    found = false;
                    // get all of them -- break;
                }
            }
            if(!found)
                continue;

            return constructor;
        }

        throw new NoSatisfiableConstructorAvailableException(
                clazz, constructors[constructors.length-1],
                unsatisfiedDependencies );
    }

}

--- NEW FILE: PicoBasedKeyAwareComponentAdapter.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.jicarilla.container.KeyAwareAdapter;
import org.jicarilla.framework.Assert;
import org.picocontainer.PicoContainer;

/**
 * 
 *
 * @author <a href="mail at leosimons dot com">Leo Simons</a>
 * @version $Id: PicoBasedKeyAwareComponentAdapter.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoBasedKeyAwareComponentAdapter
        implements KeyAwareAdapter
{
    protected final PicoContainer m_container;

    public PicoBasedKeyAwareComponentAdapter( final PicoContainer container )
    {
        Assert.assertNotNull(
                "container argument may not be null", container );
        m_container = container;
    }

    public Object getInstance( final Object key )
    {
        return m_container.getComponentInstance( key );
    }

    public void releaseInstance( final Object component ) throws Exception
    {
    }
}

--- NEW FILE: PicoClassNotFoundException.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.picocontainer.PicoException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PicoClassNotFoundException.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoClassNotFoundException extends PicoException
{
    public PicoClassNotFoundException( final Throwable cause )
    {
        super( cause );
    }
}

--- NEW FILE: PicoComponentAdapterBasedSelector.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.jicarilla.framework.Selector;
import org.picocontainer.ComponentAdapter;

/**
 *
 *
 * @author <a href="mail at leosimons dot com">Leo Simons</a>
 * @version $Id: PicoComponentAdapterBasedSelector.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoComponentAdapterBasedSelector implements Selector
{
    protected final ComponentAdapter m_delegate;

    public PicoComponentAdapterBasedSelector(
            final ComponentAdapter delegate )
    {
        m_delegate = delegate;
    }

    public boolean select( final Object object )
    {
        if( m_delegate.getComponentKey() == null )
            return object == null;

        return m_delegate.getComponentKey().equals( object );
    }

}

--- NEW FILE: PicoContainerBasedSelector.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.jicarilla.framework.Assert;
import org.jicarilla.framework.CriterionExposingSelector;
import org.picocontainer.PicoContainer;

/**
 * 
 *
 * @author <a href="mail at leosimons dot com">Leo Simons</a>
 * @version $Id: PicoContainerBasedSelector.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoContainerBasedSelector
        implements CriterionExposingSelector
{
    protected final PicoContainer m_container;

    public PicoContainerBasedSelector( final PicoContainer container )
    {
        Assert.assertNotNull(
                "container argument may not be null", container );
        m_container = container;
    }

    public boolean select( final Object object )
    {
        return m_container.hasComponent( object );
    }

    public Object getCriterion()
    {
        return m_container;
    }

    public boolean equals( final Object o )
    {
        if( this == o )
            return true;
        if( !(o instanceof PicoContainerBasedSelector) )
            return false;

        final PicoContainerBasedSelector picoContainerBasedSelector =
                (PicoContainerBasedSelector)o;

        if( !m_container.equals( picoContainerBasedSelector.m_container ) )
            return false;

        return true;
    }

    public int hashCode()
    {
        return m_container.hashCode();
    }
}

--- NEW FILE: PicoDuplicateComponentKeyRegistrationException.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.picocontainer.PicoRegistrationException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PicoDuplicateComponentKeyRegistrationException.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoDuplicateComponentKeyRegistrationException extends
        PicoRegistrationException
{
    protected final Object m_componentKey;

    public PicoDuplicateComponentKeyRegistrationException(
            final Object componentKey )
    {
        m_componentKey = componentKey;
    }

    public Object getComponentKey()
    {
        return m_componentKey;
    }

    public String getMessage()
    {
        return "Attempted to register with key " + m_componentKey +
                " which was already registered.";
    }
}

--- NEW FILE: PicoIllegalAccessException.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.picocontainer.PicoException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PicoIllegalAccessException.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoIllegalAccessException extends PicoException
{
    public PicoIllegalAccessException( final Throwable cause )
    {
        super( cause );
    }
}

--- NEW FILE: PicoInstantationException.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.picocontainer.PicoException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PicoInstantationException.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoInstantationException extends PicoException
{
    public PicoInstantationException( final Throwable cause )
    {
        super( cause );
    }
}

--- NEW FILE: PicoInvocationTargetException.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.picocontainer.PicoException;

/**
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: PicoInvocationTargetException.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class PicoInvocationTargetException extends PicoException
{
    public PicoInvocationTargetException( final Throwable cause )
    {
        super( cause );
    }
}

--- NEW FILE: ResolverBasedPicoContainer.java ---
/* ====================================================================
The Jicarilla Software License

Copyright (c) 2003 Leo Simons.
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
==================================================================== */
package org.jicarilla.container.integration.pico;

import org.jicarilla.container.JicarillaClassNotFoundException;
import org.jicarilla.container.JicarillaIllegalAccessException;
import org.jicarilla.container.JicarillaInstantiationException;
import org.jicarilla.container.JicarillaInvocationTargetException;
import org.jicarilla.container.Resolver;
import org.jicarilla.container.UnsatisfiableDependencyException;
import org.jicarilla.framework.Assert;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.PicoException;
import org.picocontainer.PicoIntrospectionException;
import org.picocontainer.PicoRegistrationException;
import org.picocontainer.PicoVerificationException;
import org.picocontainer.defaults.InstanceComponentAdapter;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 
 *
 * @author <a href="mail at leosimons dot com">Leo Simons</a>
 * @version $Id: ResolverBasedPicoContainer.java,v 1.1 2004/03/08 21:05:25 lsimons Exp $
 */
public class ResolverBasedPicoContainer implements MutablePicoContainer
{
    protected final Resolver m_delegate;

    public ResolverBasedPicoContainer( final Resolver delegate )
    {
        Assert.assertNotNull( "delegate argument may not be null", delegate );
        m_delegate = delegate;
    }

    public ComponentAdapter registerComponentImplementation(
            final Object componentKey, final Class componentImplementation )
            throws PicoRegistrationException
    {
        throw new UnsupportedOperationException();
    }

    public ComponentAdapter registerComponentImplementation(
            final Object componentKey, final Class componentImplementation,
            final Parameter[] parameters ) throws PicoRegistrationException
    {
        throw new UnsupportedOperationException();
    }

    public ComponentAdapter registerComponentImplementation(
            final Class componentImplementation ) throws PicoRegistrationException
    {
        throw new UnsupportedOperationException();
    }

    public ComponentAdapter registerComponentInstance(
            final Object componentInstance ) throws PicoRegistrationException
    {
        throw new UnsupportedOperationException();
    }

    public ComponentAdapter registerComponentInstance( final Object componentKey,
            final Object componentInstance ) throws PicoRegistrationException
    {
        throw new UnsupportedOperationException();
    }

    public final void registerComponent( final ComponentAdapter componentAdapter )
            throws PicoRegistrationException
    {
        throw new UnsupportedOperationException();
    }

    public ComponentAdapter unregisterComponent( final Object componentKey )
    {
        throw new UnsupportedOperationException();
    }

    public void registerOrderedComponentAdapter(
            final ComponentAdapter componentAdapter )
    {
        //throw new UnsupportedOperationException();
    }

    public void addOrderedComponentAdapter( final ComponentAdapter componentAdapter )
    {
        /*throw new UnsupportedOperationException();*/
    }

    public boolean addChild( final MutablePicoContainer child )
    {
        throw new UnsupportedOperationException();
    }

    public boolean addParent( final MutablePicoContainer parent )
    {
        throw new UnsupportedOperationException();
    }

    public boolean removeChild( final MutablePicoContainer child )
    {
        throw new UnsupportedOperationException();
    }

    public boolean removeParent( final MutablePicoContainer parent )
    {
        throw new UnsupportedOperationException();
    }

    public Object getComponentInstance( final Object componentKey )
            throws PicoException
    {
        try
        {
            return m_delegate.get( componentKey );
        }
        catch( JicarillaIllegalAccessException e )
        {
            throw new PicoIllegalAccessException( e );
        }
        catch( JicarillaInvocationTargetException e )
        {
            throw new PicoInvocationTargetException( e );
        }
        catch( JicarillaInstantiationException e )
        {
            throw new PicoInstantationException( e );
        }
        catch( JicarillaClassNotFoundException e )
        {
            throw new PicoClassNotFoundException( e );
        }
    }

    public List getComponentInstances() throws PicoException
    {
        throw new UnsupportedOperationException();
    }

    public List getUnmanagedComponentInstances() throws PicoException
    {
        throw new UnsupportedOperationException();
    }

    public boolean hasComponent( final Object componentKey )
    {
        return m_delegate.contains( componentKey );
    }

    public Collection getComponentKeys()
    {
        throw new UnsupportedOperationException();
    }

    public Collection getChildContainers()
    {
        throw new UnsupportedOperationException();
    }

    public List getParentContainers()
    {
        throw new UnsupportedOperationException();
    }

    public ComponentAdapter findComponentAdapter( final Object componentKey )
            throws PicoIntrospectionException
    {
        Object instance = m_delegate.get( componentKey );
        if( instance == null )
        {
            Set unsatisfiedDeps = new HashSet();
            unsatisfiedDeps.add( componentKey );

            throw new UnsatisfiableDependencyException(
                    Resolver.class, unsatisfiedDeps );
        }
        return new InstanceComponentAdapter( componentKey, instance );
    }

    public void verify() throws PicoVerificationException
    {
        /*throw new UnsupportedOperationException();*/
    }
}



-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.