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.quadrige3.core.dao;
7   
8   /*-
9    * #%L
10   * Quadrige3 Core :: Client API
11   * %%
12   * Copyright (C) 2017 - 2024 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  import org.springframework.beans.factory.access.BeanFactoryLocator;
29  import org.springframework.beans.factory.access.BeanFactoryReference;
30  import org.springframework.context.ApplicationContext;
31  import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
32  import org.springframework.context.support.AbstractApplicationContext;
33  
34  /**
35   * Provides lookup for Spring beans
36   */
37  public class BeanLocator
38  {
39      private BeanLocator()
40      {
41          // shouldn't be instantiated
42      }
43  
44      /**
45       * The prefix used for all bean ids.
46       */
47      public static final String BEAN_PREFIX = "";
48  
49      /**
50       * The shared instance of this BeanLocator.
51       */
52      private static final BeanLocator instance = new BeanLocator();
53  
54      /**
55       * Gets the shared instance of this Class
56       *
57       * @return the shared bean locator instance.
58       */
59      public static final BeanLocator instance()
60      {
61          return instance;
62      }
63  
64      /**
65       * The bean factory reference instance.
66       */
67      private BeanFactoryReference beanFactoryReference;
68  
69      /**
70       * The bean factory reference location.
71       */
72      private String beanFactoryReferenceLocation;
73  
74      /**
75       * The bean factory reference id.
76       */
77      private String beanRefFactoryReferenceId;
78  
79      /**
80       * Initializes the Spring application context from the default settings.
81       */
82      public synchronized void init()
83      {
84          this.getContext();
85      }
86  
87      /**
88       * Initializes the Spring application context from
89       * the given <code>beanFactoryReferenceLocation</code>.  If <code>null</code>
90       * is specified for the <code>beanFactoryReferenceLocation</code>
91       * then the default application context will be used.
92       *
93       * @param beanFactoryReferenceLocationIn the location of the beanRefFactory reference.
94       * @param beanRefFactoryReferenceIdIn the id of the beanRefFactory reference.
95       */
96      public synchronized void init(final String beanFactoryReferenceLocationIn, final String beanRefFactoryReferenceIdIn)
97      {
98          this.beanFactoryReferenceLocation = beanFactoryReferenceLocationIn;
99          this.beanRefFactoryReferenceId = beanRefFactoryReferenceIdIn;
100         this.beanFactoryReference = null;
101         // - initialize the context
102         this.getContext();
103     }
104 
105     /**
106      * Initializes the Spring application context from
107      * the given <code>beanFactoryReferenceLocation</code>.  If <code>null</code>
108      * is specified for the <code>beanFactoryReferenceLocation</code>
109      * then the default application context will be used.
110      *
111      * @param beanFactoryReferenceLocationIn the location of the beanRefFactory reference.
112      */
113     public synchronized void init(final String beanFactoryReferenceLocationIn)
114     {
115         this.beanFactoryReferenceLocation = beanFactoryReferenceLocationIn;
116         this.beanFactoryReference = null;
117         // - initialize the context
118         this.getContext();
119     }
120 
121     /**
122      * The default bean reference factory location.
123      */
124     private final String DEFAULT_BEAN_REFERENCE_LOCATION = "beanRefFactory.xml";
125 
126     /**
127      * The default bean reference factory ID.
128      */
129     private final String DEFAULT_BEAN_REFERENCE_ID = "beanRefFactory";
130 
131     /**
132      * Gets the Spring ApplicationContext.
133      * @return beanFactoryReference.getFactory()
134      */
135     public synchronized ApplicationContext getContext()
136     {
137         if (this.beanFactoryReference == null)
138         {
139             if (this.beanFactoryReferenceLocation == null)
140             {
141                 this.beanFactoryReferenceLocation = this.DEFAULT_BEAN_REFERENCE_LOCATION;
142             }
143             if (this.beanRefFactoryReferenceId == null)
144             {
145                 this.beanRefFactoryReferenceId = this.DEFAULT_BEAN_REFERENCE_ID;
146             }
147             BeanFactoryLocator beanFactoryLocator =
148                 ContextSingletonBeanFactoryLocator.getInstance(
149                     this.beanFactoryReferenceLocation);
150             this.beanFactoryReference = beanFactoryLocator.useBeanFactory(this.beanRefFactoryReferenceId);
151         }
152         return (ApplicationContext)this.beanFactoryReference.getFactory();
153     }
154 
155     /**
156      * Shuts down the BeanLocator and releases any used resources.
157      */
158     public synchronized void shutdown()
159     {
160         ((AbstractApplicationContext)this.getContext()).close();
161         if (this.beanFactoryReference != null)
162         {
163             this.beanFactoryReference.release();
164             this.beanFactoryReference = null;
165         }
166     }
167 
168     /**
169      * Retrieve the bean with the given <code>name</code>.
170      * @param name
171      * @return getContext().getBean(BEAN_PREFIX + name)
172      */
173     public Object getBean(final String name)
174     {
175         return this.getContext().getBean(BEAN_PREFIX + name);
176     }
177 }