View Javadoc
1   // license-header java merge-point
2   //
3   // Attention: Generated code! Do not modify by hand!
4   // Generated by: SpringBeanLocator.java.vsl in andromda-spring-cartridge.
5   //
6   package fr.ifremer.quadrige2.core.dao;
7   
8   /*-
9    * #%L
10   * Quadrige2 Core :: Server API
11   * %%
12   * Copyright (C) 2017 Ifremer
13   * %%
14   * This program is free software: you can redistribute it and/or modify
15   * it under the terms of the GNU Affero General Public License as published by
16   * the Free Software Foundation, either version 3 of the License, or
17   * (at your option) any later version.
18   * 
19   * This program is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22   * GNU General Public License for more details.
23   * 
24   * You should have received a copy of the GNU Affero General Public License
25   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26   * #L%
27   */
28  
29  import org.springframework.beans.factory.access.BeanFactoryLocator;
30  import org.springframework.beans.factory.access.BeanFactoryReference;
31  import org.springframework.context.ApplicationContext;
32  import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
33  import org.springframework.context.support.AbstractApplicationContext;
34  
35  /**
36   * Provides lookup for Spring beans
37   */
38  public class BeanLocator
39  {
40      private BeanLocator()
41      {
42          // shouldn't be instantiated
43      }
44  
45      /**
46       * The prefix used for all bean ids.
47       */
48      public static final String BEAN_PREFIX = "";
49  
50      /**
51       * The shared instance of this BeanLocator.
52       */
53      private static final BeanLocator instance = new BeanLocator();
54  
55      /**
56       * Gets the shared instance of this Class
57       *
58       * @return the shared bean locator instance.
59       */
60      public static final BeanLocator instance()
61      {
62          return instance;
63      }
64  
65      /**
66       * The bean factory reference instance.
67       */
68      private BeanFactoryReference beanFactoryReference;
69  
70      /**
71       * The bean factory reference location.
72       */
73      private String beanFactoryReferenceLocation;
74  
75      /**
76       * The bean factory reference id.
77       */
78      private String beanRefFactoryReferenceId;
79  
80      /**
81       * Initializes the Spring application context from the default settings.
82       */
83      public synchronized void init()
84      {
85          this.getContext();
86      }
87  
88      /**
89       * Initializes the Spring application context from
90       * the given <code>beanFactoryReferenceLocation</code>.  If <code>null</code>
91       * is specified for the <code>beanFactoryReferenceLocation</code>
92       * then the default application context will be used.
93       *
94       * @param beanFactoryReferenceLocationIn the location of the beanRefFactory reference.
95       * @param beanRefFactoryReferenceIdIn the id of the beanRefFactory reference.
96       */
97      public synchronized void init(final String beanFactoryReferenceLocationIn, final String beanRefFactoryReferenceIdIn)
98      {
99          this.beanFactoryReferenceLocation = beanFactoryReferenceLocationIn;
100         this.beanRefFactoryReferenceId = beanRefFactoryReferenceIdIn;
101         this.beanFactoryReference = null;
102         // - initialize the context
103         this.getContext();
104     }
105 
106     /**
107      * Initializes the Spring application context from
108      * the given <code>beanFactoryReferenceLocation</code>.  If <code>null</code>
109      * is specified for the <code>beanFactoryReferenceLocation</code>
110      * then the default application context will be used.
111      *
112      * @param beanFactoryReferenceLocationIn the location of the beanRefFactory reference.
113      */
114     public synchronized void init(final String beanFactoryReferenceLocationIn)
115     {
116         this.beanFactoryReferenceLocation = beanFactoryReferenceLocationIn;
117         this.beanFactoryReference = null;
118         // - initialize the context
119         this.getContext();
120     }
121 
122     /**
123      * The default bean reference factory location.
124      */
125     private final String DEFAULT_BEAN_REFERENCE_LOCATION = "beanRefFactory.xml";
126 
127     /**
128      * The default bean reference factory ID.
129      */
130     private final String DEFAULT_BEAN_REFERENCE_ID = "beanRefFactory";
131 
132     /**
133      * Gets the Spring ApplicationContext.
134      * @return beanFactoryReference.getFactory()
135      */
136     public synchronized ApplicationContext getContext()
137     {
138         if (this.beanFactoryReference == null)
139         {
140             if (this.beanFactoryReferenceLocation == null)
141             {
142                 this.beanFactoryReferenceLocation = this.DEFAULT_BEAN_REFERENCE_LOCATION;
143             }
144             if (this.beanRefFactoryReferenceId == null)
145             {
146                 this.beanRefFactoryReferenceId = this.DEFAULT_BEAN_REFERENCE_ID;
147             }
148             BeanFactoryLocator beanFactoryLocator =
149                 ContextSingletonBeanFactoryLocator.getInstance(
150                     this.beanFactoryReferenceLocation);
151             this.beanFactoryReference = beanFactoryLocator.useBeanFactory(this.beanRefFactoryReferenceId);
152         }
153         return (ApplicationContext)this.beanFactoryReference.getFactory();
154     }
155 
156     /**
157      * Shuts down the BeanLocator and releases any used resources.
158      */
159     public synchronized void shutdown()
160     {
161         ((AbstractApplicationContext)this.getContext()).close();
162         if (this.beanFactoryReference != null)
163         {
164             this.beanFactoryReference.release();
165             this.beanFactoryReference = null;
166         }
167     }
168 
169     /**
170      * Retrieve the bean with the given <code>name</code>.
171      * @param name
172      * @return getContext().getBean(BEAN_PREFIX + name)
173      */
174     public Object getBean(final String name)
175     {
176         return this.getContext().getBean(BEAN_PREFIX + name);
177     }
178 }