1
2
3
4
5
6 package fr.ifremer.quadrige2.core.dao;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
37
38 public class BeanLocator
39 {
40 private BeanLocator()
41 {
42
43 }
44
45
46
47
48 public static final String BEAN_PREFIX = "";
49
50
51
52
53 private static final BeanLocator instance = new BeanLocator();
54
55
56
57
58
59
60 public static final BeanLocator instance()
61 {
62 return instance;
63 }
64
65
66
67
68 private BeanFactoryReference beanFactoryReference;
69
70
71
72
73 private String beanFactoryReferenceLocation;
74
75
76
77
78 private String beanRefFactoryReferenceId;
79
80
81
82
83 public synchronized void init()
84 {
85 this.getContext();
86 }
87
88
89
90
91
92
93
94
95
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
103 this.getContext();
104 }
105
106
107
108
109
110
111
112
113
114 public synchronized void init(final String beanFactoryReferenceLocationIn)
115 {
116 this.beanFactoryReferenceLocation = beanFactoryReferenceLocationIn;
117 this.beanFactoryReference = null;
118
119 this.getContext();
120 }
121
122
123
124
125 private final String DEFAULT_BEAN_REFERENCE_LOCATION = "beanRefFactory.xml";
126
127
128
129
130 private final String DEFAULT_BEAN_REFERENCE_ID = "beanRefFactory";
131
132
133
134
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
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
171
172
173
174 public Object getBean(final String name)
175 {
176 return this.getContext().getBean(BEAN_PREFIX + name);
177 }
178 }