BeanLocator.java

// license-header java merge-point
//
// Attention: Generated code! Do not modify by hand!
// Generated by: SpringBeanLocator.java.vsl in andromda-spring-cartridge.
//
package org.andromda.spring.howto9;

import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.context.ApplicationContext;
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
import org.springframework.context.support.AbstractApplicationContext;

/**
 * Provides lookup for Spring beans
 */
public class BeanLocator
{
    private BeanLocator()
    {
        // shouldn't be instantiated
    }

    /**
     * The prefix used for all bean ids.
     */
    public static final String BEAN_PREFIX = "";

    /**
     * The shared instance of this BeanLocator.
     */
    private static final BeanLocator instance = new BeanLocator();

    /**
     * Gets the shared instance of this Class
     *
     * @return the shared bean locator instance.
     */
    public static final BeanLocator instance()
    {
        return instance;
    }

    /**
     * The bean factory reference instance.
     */
    private BeanFactoryReference beanFactoryReference;

    /**
     * The bean factory reference location.
     */
    private String beanFactoryReferenceLocation;

    /**
     * The bean factory reference id.
     */
    private String beanRefFactoryReferenceId;

    /**
     * Initializes the Spring application context from the default settings.
     */
    public synchronized void init()
    {
        this.getContext();
    }

    /**
     * Initializes the Spring application context from
     * the given <code>beanFactoryReferenceLocation</code>.  If <code>null</code>
     * is specified for the <code>beanFactoryReferenceLocation</code>
     * then the default application context will be used.
     *
     * @param beanFactoryReferenceLocationIn the location of the beanRefFactory reference.
     * @param beanRefFactoryReferenceIdIn the id of the beanRefFactory reference.
     */
    public synchronized void init(final String beanFactoryReferenceLocationIn, final String beanRefFactoryReferenceIdIn)
    {
        this.beanFactoryReferenceLocation = beanFactoryReferenceLocationIn;
        this.beanRefFactoryReferenceId = beanRefFactoryReferenceIdIn;
        this.beanFactoryReference = null;
        // - initialize the context
        this.getContext();
    }

    /**
     * Initializes the Spring application context from
     * the given <code>beanFactoryReferenceLocation</code>.  If <code>null</code>
     * is specified for the <code>beanFactoryReferenceLocation</code>
     * then the default application context will be used.
     *
     * @param beanFactoryReferenceLocationIn the location of the beanRefFactory reference.
     */
    public synchronized void init(final String beanFactoryReferenceLocationIn)
    {
        this.beanFactoryReferenceLocation = beanFactoryReferenceLocationIn;
        this.beanFactoryReference = null;
        // - initialize the context
        this.getContext();
    }

    /**
     * The default bean reference factory location.
     */
    private final String DEFAULT_BEAN_REFERENCE_LOCATION = "beanRefFactory.xml";

    /**
     * The default bean reference factory ID.
     */
    private final String DEFAULT_BEAN_REFERENCE_ID = "beanRefFactory";

    /**
     * Gets the Spring ApplicationContext.
     * @return beanFactoryReference.getFactory()
     */
    public synchronized ApplicationContext getContext()
    {
        if (this.beanFactoryReference == null)
        {
            if (this.beanFactoryReferenceLocation == null)
            {
                this.beanFactoryReferenceLocation = this.DEFAULT_BEAN_REFERENCE_LOCATION;
            }
            if (this.beanRefFactoryReferenceId == null)
            {
                this.beanRefFactoryReferenceId = this.DEFAULT_BEAN_REFERENCE_ID;
            }
            BeanFactoryLocator beanFactoryLocator =
                ContextSingletonBeanFactoryLocator.getInstance(
                    this.beanFactoryReferenceLocation);
            this.beanFactoryReference = beanFactoryLocator.useBeanFactory(this.beanRefFactoryReferenceId);
        }
        return (ApplicationContext)this.beanFactoryReference.getFactory();
    }

    /**
     * Shuts down the BeanLocator and releases any used resources.
     */
    public synchronized void shutdown()
    {
        ((AbstractApplicationContext)this.getContext()).close();
        if (this.beanFactoryReference != null)
        {
            this.beanFactoryReference.release();
            this.beanFactoryReference = null;
        }
    }

    /**
     * Retrieve the bean with the given <code>name</code>.
     * @param name
     * @return getContext().getBean(BEAN_PREFIX + name)
     */
    public Object getBean(final String name)
    {
        return this.getContext().getBean(BEAN_PREFIX + name);
    }
}