001// license-header java merge-point
002//
003// Attention: Generated code! Do not modify by hand!
004// Generated by: SpringBeanLocator.java.vsl in andromda-spring-cartridge.
005//
006package fr.ifremer.adagio.core.dao;
007
008/*
009 * #%L
010 * SIH-Adagio :: Core
011 * $Id:$
012 * $HeadURL:$
013 * %%
014 * Copyright (C) 2012 - 2014 Ifremer
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Affero General Public License as published by
018 * the Free Software Foundation, either version 3 of the License, or
019 * (at your option) any later version.
020 * 
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Affero General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * #L%
029 */
030
031import org.springframework.beans.factory.access.BeanFactoryLocator;
032import org.springframework.beans.factory.access.BeanFactoryReference;
033import org.springframework.context.ApplicationContext;
034import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
035import org.springframework.context.support.AbstractApplicationContext;
036
037/**
038 * Provides lookup for Spring beans
039 */
040public class BeanLocator
041{
042    private BeanLocator()
043    {
044        // shouldn't be instantiated
045    }
046
047    /**
048     * The prefix used for all bean ids.
049     */
050    public static final String BEAN_PREFIX = "";
051
052    /**
053     * The shared instance of this BeanLocator.
054     */
055    private static final BeanLocator instance = new BeanLocator();
056
057    /**
058     * Gets the shared instance of this Class
059     *
060     * @return the shared bean locator instance.
061     */
062    public static final BeanLocator instance()
063    {
064        return instance;
065    }
066
067    /**
068     * The bean factory reference instance.
069     */
070    private BeanFactoryReference beanFactoryReference;
071
072    /**
073     * The bean factory reference location.
074     */
075    private String beanFactoryReferenceLocation;
076
077    /**
078     * The bean factory reference id.
079     */
080    private String beanRefFactoryReferenceId;
081
082    /**
083     * Initializes the Spring application context from the default settings.
084     */
085    public synchronized void init()
086    {
087        this.getContext();
088    }
089
090    /**
091     * Initializes the Spring application context from
092     * the given <code>beanFactoryReferenceLocation</code>.  If <code>null</code>
093     * is specified for the <code>beanFactoryReferenceLocation</code>
094     * then the default application context will be used.
095     *
096     * @param beanFactoryReferenceLocationIn the location of the beanRefFactory reference.
097     * @param beanRefFactoryReferenceIdIn the id of the beanRefFactory reference.
098     */
099    public synchronized void init(final String beanFactoryReferenceLocationIn, final String beanRefFactoryReferenceIdIn)
100    {
101        this.beanFactoryReferenceLocation = beanFactoryReferenceLocationIn;
102        this.beanRefFactoryReferenceId = beanRefFactoryReferenceIdIn;
103        this.beanFactoryReference = null;
104        // - initialize the context
105        this.getContext();
106    }
107
108    /**
109     * Initializes the Spring application context from
110     * the given <code>beanFactoryReferenceLocation</code>.  If <code>null</code>
111     * is specified for the <code>beanFactoryReferenceLocation</code>
112     * then the default application context will be used.
113     *
114     * @param beanFactoryReferenceLocationIn the location of the beanRefFactory reference.
115     */
116    public synchronized void init(final String beanFactoryReferenceLocationIn)
117    {
118        this.beanFactoryReferenceLocation = beanFactoryReferenceLocationIn;
119        this.beanFactoryReference = null;
120        // - initialize the context
121        this.getContext();
122    }
123
124    /**
125     * The default bean reference factory location.
126     */
127    private final String DEFAULT_BEAN_REFERENCE_LOCATION = "beanRefFactory.xml";
128
129    /**
130     * The default bean reference factory ID.
131     */
132    private final String DEFAULT_BEAN_REFERENCE_ID = "beanRefFactory";
133
134    /**
135     * Gets the Spring ApplicationContext.
136     * @return beanFactoryReference.getFactory()
137     */
138    public synchronized ApplicationContext getContext()
139    {
140        if (this.beanFactoryReference == null)
141        {
142            if (this.beanFactoryReferenceLocation == null)
143            {
144                this.beanFactoryReferenceLocation = this.DEFAULT_BEAN_REFERENCE_LOCATION;
145            }
146            if (this.beanRefFactoryReferenceId == null)
147            {
148                this.beanRefFactoryReferenceId = this.DEFAULT_BEAN_REFERENCE_ID;
149            }
150            BeanFactoryLocator beanFactoryLocator =
151                ContextSingletonBeanFactoryLocator.getInstance(
152                    this.beanFactoryReferenceLocation);
153            this.beanFactoryReference = beanFactoryLocator.useBeanFactory(this.beanRefFactoryReferenceId);
154        }
155        return (ApplicationContext)this.beanFactoryReference.getFactory();
156    }
157
158    /**
159     * Shuts down the BeanLocator and releases any used resources.
160     */
161    public synchronized void shutdown()
162    {
163        ((AbstractApplicationContext)this.getContext()).close();
164        if (this.beanFactoryReference != null)
165        {
166            this.beanFactoryReference.release();
167            this.beanFactoryReference = null;
168        }
169    }
170
171    /**
172     * Retrieve the bean with the given <code>name</code>.
173     * @param name
174     * @return getContext().getBean(BEAN_PREFIX + name)
175     */
176    public Object getBean(final String name)
177    {
178        return this.getContext().getBean(BEAN_PREFIX + name);
179    }
180}