1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.di.impl;
17
18 import java.util.Set;
19
20 import net.sf.jguiraffe.di.BeanContext;
21 import net.sf.jguiraffe.di.BeanCreationListener;
22 import net.sf.jguiraffe.di.BeanProvider;
23 import net.sf.jguiraffe.di.BeanStore;
24 import net.sf.jguiraffe.di.ClassLoaderProvider;
25
26 /**
27 * <p>
28 * A simple wrapper implementation of the <code>BeanContext</code> interface.
29 * </p>
30 * <p>
31 * The main purpose of this class is to override the default
32 * <code>BeanStore</code> of a bean context. It is initialized with the
33 * <code>BeanContext</code> object to be wrapped and the default
34 * <code>BeanStore</code> to be used. All calls to methods defined by the
35 * <code>BeanContext</code> interface are delegated to the wrapped bean context
36 * object. If they address the default bean store (i.e. no bean store is
37 * explicitly passed to the method), the bean store set for this instance is
38 * used. That way an already configured bean context can be used in a different
39 * context (which means, using another bean store as default entry in the look
40 * up mechanism) without manipulating its state.
41 * </p>
42 *
43 * @author Oliver Heger
44 * @version $Id: BeanContextWrapper.java 205 2012-01-29 18:29:57Z oheger $
45 */
46 public class BeanContextWrapper implements BeanContext
47 {
48 /** Stores the wrapped bean context. */
49 private final BeanContext context;
50
51 /** Stores the default bean store to be used. */
52 private BeanStore defaultBeanStore;
53
54 /** A helper object for managing bean creation listeners. */
55 private final BeanCreationListenerSupport beanCreationListeners;
56
57 /**
58 * Creates a new instance of <code>BeanContextWrapper</code> and sets the
59 * wrapped context.
60 *
61 * @param wrappedContext the bean context to be wrapped (must not be
62 * <b>null</b>)
63 * @throws IllegalArgumentException if the bean context is <b>null</b>
64 */
65 public BeanContextWrapper(BeanContext wrappedContext)
66 {
67 this(wrappedContext, null);
68 }
69
70 /**
71 * Creates a new instance of <code>BeanContextWrapper</code> and sets the
72 * wrapped context and the default bean store.
73 *
74 * @param wrappedContext the bean context to be wrapped (must not be
75 * <b>null</b>)
76 * @param defaultStore the default bean store ton be used
77 * @throws IllegalArgumentException if the bean context is <b>null</b>
78 */
79 public BeanContextWrapper(BeanContext wrappedContext, BeanStore defaultStore)
80 {
81 if (wrappedContext == null)
82 {
83 throw new IllegalArgumentException(
84 "Underlying context must not be null!");
85 }
86
87 context = wrappedContext;
88 setDefaultBeanStore(defaultStore);
89 beanCreationListeners = new BeanCreationListenerSupport(this);
90 initWrappedContext(wrappedContext, beanCreationListeners);
91 }
92
93 /**
94 * Returns the wrapped bean context.
95 *
96 * @return a reference to the original bean context
97 */
98 public BeanContext getWrappedContext()
99 {
100 return context;
101 }
102
103 /**
104 * Returns a set of the classes of the beans available in this bean
105 * context's default bean store. This implementation delegates to the
106 * wrapped context.
107 *
108 * @return a set with the classes of the defined beans
109 */
110 public Set<Class<?>> beanClasses()
111 {
112 return getWrappedContext().beanClasses(getDefaultBeanStore());
113 }
114
115 /**
116 * Returns a set of the classes of the beans available in the specified bean
117 * store. This implementation delegates to the wrapped context.
118 *
119 * @param store the bean store
120 * @return a set with the classes of the defined beans
121 */
122 public Set<Class<?>> beanClasses(BeanStore store)
123 {
124 return getWrappedContext().beanClasses(store);
125 }
126
127 /**
128 * Returns a set with the names of the beans defined in this context's
129 * default bean store. This implementation delegates to the wrapped context.
130 *
131 * @return a set with the names of the defined beans
132 */
133 public Set<String> beanNames()
134 {
135 return getWrappedContext().beanNames(getDefaultBeanStore());
136 }
137
138 /**
139 * Returns a set with the names of the beans defined in the given bean
140 * store. This implementation delegates to the wrapped context.
141 *
142 * @param store the bean store
143 * @return a set with the names of the defined beans
144 */
145 public Set<String> beanNames(BeanStore store)
146 {
147 return getWrappedContext().beanNames(store);
148 }
149
150 /**
151 * Checks whether a bean with the given name can be found in the default
152 * bean store of this bean context. This implementation delegates to the
153 * wrapped context.
154 *
155 * @param name the name of the searched bean
156 * @return a flag whether this bean can be found
157 */
158 public boolean containsBean(String name)
159 {
160 return getWrappedContext().containsBean(name, getDefaultBeanStore());
161 }
162
163 /**
164 * Checks whether a bean with the given name can be found in the specified
165 * bean store. This implementation delegates to the wrapped context.
166 *
167 * @param name the name of the bean
168 * @param store the bean store
169 * @return a flag whether this bean can be found
170 */
171 public boolean containsBean(String name, BeanStore store)
172 {
173 return getWrappedContext().containsBean(name, store);
174 }
175
176 /**
177 * Checks whether a bean with the given class can be found in this bean
178 * context's default bean store. This implementation delegates to the
179 * wrapped context.
180 *
181 * @param beanClass the class of the bean
182 * @return a flag whether this bean can be found
183 */
184 public boolean containsBean(Class<?> beanClass)
185 {
186 return getWrappedContext().containsBean(beanClass,
187 getDefaultBeanStore());
188 }
189
190 /**
191 * Checks whether a bean with the given class can be found in the specified
192 * bean store. This implementation delegates to the wrapped context.
193 *
194 * @param beanClass the class of the bean
195 * @param store the bean store
196 * @return a flag whether this bean can be found
197 */
198 public boolean containsBean(Class<?> beanClass, BeanStore store)
199 {
200 return getWrappedContext().containsBean(beanClass, store);
201 }
202
203 /**
204 * Returns the bean with the given name starting the search with the default
205 * bean store. This implementation delegates to the wrapped context.
206 *
207 * @param name the name of the desired bean
208 * @return the bean
209 */
210 public Object getBean(String name)
211 {
212 return getWrappedContext().getBean(name, getDefaultBeanStore());
213 }
214
215 /**
216 * Returns the bean with the given name starting the search with the
217 * specified bean store. This implementation delegates to the wrapped
218 * context.
219 *
220 * @param name the name of the desired bean
221 * @param store the bean store to start with
222 * @return the bean
223 */
224 public Object getBean(String name, BeanStore store)
225 {
226 return getWrappedContext().getBean(name, store);
227 }
228
229 /**
230 * Returns the bean with the given class starting the search with the
231 * default bean store. This implementation delegates to the wrapped context.
232 *
233 * @param <T> the type of the bean
234 * @param beanCls the class of the desired bean
235 * @return the bean
236 */
237 public <T> T getBean(Class<T> beanCls)
238 {
239 return getWrappedContext().getBean(beanCls, getDefaultBeanStore());
240 }
241
242 /**
243 * Returns the bean with the given class starting the search with the
244 * specified bean store. This implementation delegates to the wrapped
245 * context.
246 *
247 * @param <T> the type of the bean
248 * @param beanCls the class of the desired bean
249 * @param store the store to start with
250 * @return the bean
251 */
252 public <T> T getBean(Class<T> beanCls, BeanStore store)
253 {
254 return getWrappedContext().getBean(beanCls, store);
255 }
256
257 /**
258 * Returns the name of the given {@code BeanProvider} starting the search
259 * with the default bean store. This implementation delegates to the wrapped
260 * context.
261 *
262 * @param beanProvider the {@code BeanProvider}
263 * @return the name of this {@code BeanProvider} or <b>null</b>
264 */
265 public String beanNameFor(BeanProvider beanProvider)
266 {
267 return getWrappedContext().beanNameFor(beanProvider,
268 getDefaultBeanStore());
269 }
270
271 /**
272 * Returns the name of the given {@code BeanProvider} starting the search
273 * with the specified bean store. This implementation delegates to the
274 * wrapped context.
275 *
276 * @param beanProvider the {@code BeanProvider}
277 * @param store the {@code BeanStore}
278 * @return the name of this {@code BeanProvider} or <b>null</b>
279 */
280 public String beanNameFor(BeanProvider beanProvider, BeanStore store)
281 {
282 return getWrappedContext().beanNameFor(beanProvider, store);
283 }
284
285 /**
286 * Returns the default bean store. This implementation returns the default
287 * store set for this wrapped context and not the one of the underlying
288 * context.
289 *
290 * @return the default bean store
291 */
292 public BeanStore getDefaultBeanStore()
293 {
294 return defaultBeanStore;
295 }
296
297 /**
298 * Sets the default bean store. This implementation only changes the default
299 * bean store of this wrapped context. The default store of the underlying
300 * context is not touched.
301 *
302 * @param store the new default bean store
303 */
304 public void setDefaultBeanStore(BeanStore store)
305 {
306 defaultBeanStore = store;
307 }
308
309 /**
310 * Adds the specified {@code BeanCreationListener} to this context. This
311 * implementation ensures that events from the wrapped context are received.
312 * However, the events are correctly transformed so that this context is set
313 * as the source context of the event.
314 *
315 * @param l the listener to be added
316 */
317 public void addBeanCreationListener(BeanCreationListener l)
318 {
319 getBeanCreationListeners().addBeanCreationListener(l);
320 }
321
322 /**
323 * Removes the specified {@code BeanCreationListener} from this context.
324 *
325 * @param l the listener to be removed
326 */
327 public void removeBeanCreationListener(BeanCreationListener l)
328 {
329 getBeanCreationListeners().removeBeanCreationListener(l);
330 }
331
332 /**
333 * Closes this {@code BeanContext}. This implementation removes the {@code
334 * BeanCreationListener} registered at the wrapped context. Note that the
335 * wrapped context will not be closed! This is because the wrapped context
336 * is typically shared.
337 */
338 public void close()
339 {
340 getWrappedContext().removeBeanCreationListener(
341 getBeanCreationListeners());
342 }
343
344 /**
345 * Returns the {@code ClassLoaderProvider} used by this context. This
346 * implementation delegates to the wrapped context.
347 *
348 * @return the {@code ClassLoaderProvider}
349 */
350 public ClassLoaderProvider getClassLoaderProvider()
351 {
352 return getWrappedContext().getClassLoaderProvider();
353 }
354
355 /**
356 * Sets the {@code ClassLoaderProvider} to be used by this context. This
357 * implementation delegates to the wrapped context.
358 *
359 * @param clp the new {@code ClassLoaderProvider}
360 */
361 public void setClassLoaderProvider(ClassLoaderProvider clp)
362 {
363 getWrappedContext().setClassLoaderProvider(clp);
364 }
365
366 /**
367 * Returns the helper object for managing bean creation listeners.
368 *
369 * @return the bean creation listeners
370 */
371 BeanCreationListenerSupport getBeanCreationListeners()
372 {
373 return beanCreationListeners;
374 }
375
376 /**
377 * Initializes the wrapped context. This method is called by the
378 * constructor. It registers the support object as bean creation listener at
379 * the wrapped context, so that the correct source context can be set.
380 *
381 * @param wrappedContext the wrapped context
382 * @param support the support object
383 */
384 void initWrappedContext(BeanContext wrappedContext,
385 BeanCreationListenerSupport support)
386 {
387 wrappedContext.addBeanCreationListener(support);
388 }
389 }