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