jicarilla-sandbox/platform/container-integration/src/java/org/jicarilla/container/integration/dna DnaFactory.java,NONE,1.1 DnaUtil.java,NONE,1.1 JicarillaBasedDnaResourceLocator.java,NONE,1.1

Leo Simons <[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/dna
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20590/container-integration/src/java/org/jicarilla/container/integration/dna

Added Files:
	DnaFactory.java DnaUtil.java 
	JicarillaBasedDnaResourceLocator.java 
Log Message:
Initial support for dna.

--- NEW FILE: JicarillaBasedDnaResourceLocator.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.dna;

import org.jcontainer.dna.ResourceLocator;
import org.jcontainer.dna.MissingResourceException;
import org.jicarilla.container.Resolver;

/**
 * 
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: JicarillaBasedDnaResourceLocator.java,v 1.1 2004/04/03 12:09:21 lsimons Exp $
 */
public class JicarillaBasedDnaResourceLocator implements ResourceLocator
{
    protected Resolver m_container;
    
    public JicarillaBasedDnaResourceLocator( Resolver container )
    {
        m_container = container;
    }

    public Object lookup( String key ) throws MissingResourceException
    {
        if( key == null )
            throw new MissingResourceException(
                    key, "No value exists for key 'null'" );

        final Object result;
        try
        {
            result = m_container.get( key );
        }
        catch( Exception e )
        {
            throw new MissingResourceException( key,
                    "Unable to get " + key + "from the ResourceLocator", e );
        }

        if( result == null )
            throw new MissingResourceException(
                    key, "No value exists for key " + key.toString() );

        return result;
    }

    public boolean contains( String key )
    {
        return m_container.contains( key );
    }
}

--- NEW FILE: DnaUtil.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.dna;

import org.jicarilla.container.Resolver;
import org.jicarilla.collections.NodeUtil;
import org.jcontainer.dna.impl.ContainerUtil;
import org.jcontainer.dna.impl.ConsoleLogger;
import org.jcontainer.dna.impl.ConfigurationUtil;
import org.jcontainer.dna.impl.DefaultParameters;
import org.jcontainer.dna.Logger;
import org.jcontainer.dna.ResourceLocator;
import org.jcontainer.dna.Configurable;
import org.jcontainer.dna.Parameterizable;
import org.jcontainer.dna.Configuration;
import org.jcontainer.dna.Parameters;
import org.jcontainer.dna.ConfigurationException;
import org.xml.sax.InputSource;

import java.io.Reader;
import java.io.StringReader;
import java.util.Properties;
import java.util.Iterator;

import com.thoughtworks.xstream.XStream;

/**
 * 
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: DnaUtil.java,v 1.1 2004/04/03 12:09:21 lsimons Exp $
 */
public class DnaUtil
{
    public final static String CONFIGURATION_POSTFIX = "Configuration";

    public final static ConsoleLogger FallbackLogger = new ConsoleLogger(
            ConsoleLogger.LEVEL_WARN );

    public static void startDnaLifecycle( final Object instance,
            final Resolver container ) throws InstantiationException
    {
        try
        {
            ContainerUtil.enableLogging( instance,
                    getLogger( container, instance.getClass().getName() ) );
            ContainerUtil.compose( instance, getResourceLocator( container ) );
            
            if( instance instanceof Configurable ||
                instance instanceof Parameterizable )
            {
                final Configuration configuration =
                        getConfiguration( container,
                                instance.getClass().getName() );
                ContainerUtil.configure( instance, configuration  );
                
                if( instance instanceof Parameterizable )
                {
                    Parameters parameters =
                            configurationToParameters( configuration );
                    ContainerUtil.parameterize( instance, parameters );
                }
            }
            ContainerUtil.initialize( instance );
        }
        catch( Exception e )
        {
            final InstantiationException ex = new InstantiationException();
            ex.initCause(e);
            throw ex;
        }
    }

    protected static Logger getLogger( final Resolver container,
            final String fallbackLoggerName )
    {
        Logger logger = null;

        logger = (Logger)container.get( Logger.class );

        if( logger == null )
            logger = FallbackLogger.getChildLogger( fallbackLoggerName );

        return logger;
    }

    protected static ResourceLocator getResourceLocator(
            final Resolver container )
    {
        return new JicarillaBasedDnaResourceLocator( container );
    }

    public static Configuration getConfiguration(
            final Resolver container, final String className )
            throws Exception
    {
        final Object obj = container.get( className + CONFIGURATION_POSTFIX );

        if(obj == null)
            throw new ConfigurationException(
                    "No configuration can be found for '" +
                    className + "'", null, null );

        if(obj instanceof Configuration )
            return (Configuration)obj;

        return beanToConfiguration( obj );
    }

    protected static Configuration beanToConfiguration( final Object obj )
            throws Exception
    {
        final XStream xstream = new XStream();
        final String configString = xstream.toXML(obj);
        
        final Reader reader = new StringReader( configString );
        final InputSource source = new InputSource( reader );
        
        final Configuration configuration =
                ConfigurationUtil.buildFromXML( source );
        
        return configuration;
    }

    protected static Parameters configurationToParameters(
            final Configuration configuration )
    {
        final Properties properties = NodeUtil.toProperties(
            NodeUtil.fromElement( 
                ConfigurationUtil.toElement( configuration )
            )
        );
        final DefaultParameters parameters =
                new DefaultParameters();
        Iterator it = properties.keySet().iterator();
        while( it.hasNext() )
        {
            Object key = it.next();
            if( key == null ) continue;
            String keyString = key.toString();
            parameters.setParameter( keyString,
                    properties.getProperty( keyString ) );
        }
        parameters.makeReadOnly();
        return parameters;
    }
}

--- NEW FILE: DnaFactory.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.dna;

import org.jicarilla.container.factories.AbstractFactory;
import org.jicarilla.container.Resolver;
import org.jcontainer.dna.impl.ContainerUtil;

/**
 * 
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: DnaFactory.java,v 1.1 2004/04/03 12:09:21 lsimons Exp $
 */
public class DnaFactory extends AbstractFactory
{
    public DnaFactory( Resolver resolver, String className )
    {
        super( resolver, className );
    }

    protected Object doNewInstance()
            throws IllegalAccessException, InstantiationException
    {
        super.checkCyclicDependency( new Class[] { super.getClazz() } );
        super.enableCyclicDependencyChecking();

        final Object instance = super.getClazz().newInstance();
        DnaUtil.startDnaLifecycle( instance, super.getResolver() );
        return instance;
    }

    protected void doReleaseInstance( final Object o )
            throws Exception
    {
        ContainerUtil.dispose( o );
    }
}



-------------------------------------------------------
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.