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;
17
18 import java.util.EventObject;
19
20 /**
21 * <p>
22 * An event class for reporting the creation of a bean by the dependency
23 * injection framework.
24 * </p>
25 * <p>
26 * Objects of this event class are received by {@link BeanCreationListener}
27 * implementations, which can be registered at a {@link BeanContext} object.
28 * Whenever the {@code BeanContext} is queried for a bean, and this bean has to
29 * be newly created (e.g. because of the first access of a singleton bean or
30 * because it is a factory bean), an event of this type is triggered.
31 * </p>
32 *
33 * @author Oliver Heger
34 * @version $Id: BeanCreationEvent.java 205 2012-01-29 18:29:57Z oheger $
35 */
36 public class BeanCreationEvent extends EventObject
37 {
38 /**
39 * The serial version UID.
40 */
41 private static final long serialVersionUID = 3858761676828030611L;
42
43 /** Stores the bean provider responsible for the creation. */
44 private final transient BeanProvider beanProvider;
45
46 /** The bean that was newly created. */
47 private final Object bean;
48
49 /** The dependency provider involved in the creation process. */
50 private final transient DependencyProvider dependencyProvider;
51
52 /**
53 * Creates a new instance of {@code BeanCreationEvent} and initializes it.
54 *
55 * @param source the {@code BeanContext} that caused this event
56 * @param provider the {@code BeanProvider} that created the bean
57 * @param depProvider the {@code DependencyProvider} used for creating the
58 * bean
59 * @param newBean the newly created bean
60 */
61 public BeanCreationEvent(BeanContext source, BeanProvider provider,
62 DependencyProvider depProvider, Object newBean)
63 {
64 super(source);
65 beanProvider = provider;
66 dependencyProvider = depProvider;
67 bean = newBean;
68 }
69
70 /**
71 * Returns the {@code BeanContext} that caused this event.
72 *
73 * @return the source {@code BeanContext}
74 */
75 public BeanContext getBeanContext()
76 {
77 return (BeanContext) getSource();
78 }
79
80 /**
81 * Returns the {@code BeanProvider} that created the new bean.
82 *
83 * @return the responsible {@code BeanProvider}
84 */
85 public BeanProvider getBeanProvider()
86 {
87 return beanProvider;
88 }
89
90 /**
91 * Returns the {@code DependencyProvider} involved in the bean creation
92 * process. This is the object that was passed to the {@code BeanProvider}
93 * when it created the new bean.
94 *
95 * @return the {@code DependencyProvider} involved when creating the bean
96 */
97 public DependencyProvider getDependencyProvider()
98 {
99 return dependencyProvider;
100 }
101
102 /**
103 * Returns the newly created bean.
104 *
105 * @return the bean
106 */
107 public Object getBean()
108 {
109 return bean;
110 }
111 }