jicarilla-sandbox/platform/container/impl/src/java/org/jicarilla/container/factories FactoryUtil.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/impl/src/java/org/jicarilla/container/factories
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6371/platform/container/impl/src/java/org/jicarilla/container/factories

Added Files:
	FactoryUtil.java 
Log Message:
Small utility class that simplifies use of Type35Factory logic for unmanaged classes.

--- NEW FILE: FactoryUtil.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.factories;

import org.jicarilla.container.Resolver;
import org.jicarilla.container.Factory;
import org.jicarilla.framework.Assert;

/**
 * <p>A small utility class for instantiation of and disposing of instances
 * that need to be created using dependencies from a resolver, but that
 * should not live inside the container. Example usage:</p>
 *
 * <pre>
 * Resolver resolver =
 *   DefaultBuilder.newInstance()
 *     .addComponent( HomerImpl.class )
 *     .addComponent( MargeImpl.class )
 *     .create();
 * Script unmangedScript = (Script)FactoryUtil.newInstance( resolver,
 *      SomeScriptImpl.class );
 * // ...
 * FactoryUtil.releaseInstance( unmanagedScript );
 * </pre>
 *
 * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
 * @version $Id: FactoryUtil.java,v 1.1 2004/03/17 15:56:57 lsimons Exp $
 */
public class FactoryUtil
{
    public static Object newInstance( Resolver resolver, Class clazz )
    {
        final Factory f = new Type35Factory( resolver, clazz.getName() );
        return f.newInstance();
    }

    public static void releaseInstance( Object instance ) throws Exception
    {
        Assert.assertNotNull( "instance argument may not be null", instance );

        final Resolver r = new Resolver()
        {
            public Object get( Object key )
            {
                return null;
            }

            public Object[] getAll( Object key )
            {
                return new Object[0];
            }

            public boolean contains( Object key )
            {
                return false;
            }
            public void releaseInstance( Object instance ) throws Exception
            {
            }
        };
        final Factory f = new Type35Factory(
                r, instance.getClass().getName() );

        f.releaseInstance( instance );
    }
}



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