KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > beans > metadata > plugins > factory > GenericBeanFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.beans.metadata.plugins.factory;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.jboss.beans.info.spi.BeanInfo;
29 import org.jboss.beans.metadata.spi.ClassLoaderMetaData;
30 import org.jboss.beans.metadata.spi.ConstructorMetaData;
31 import org.jboss.beans.metadata.spi.LifecycleMetaData;
32 import org.jboss.beans.metadata.spi.ParameterMetaData;
33 import org.jboss.beans.metadata.spi.ValueMetaData;
34 import org.jboss.joinpoint.spi.Joinpoint;
35 import org.jboss.joinpoint.spi.JoinpointException;
36 import org.jboss.joinpoint.spi.MethodJoinpoint;
37 import org.jboss.joinpoint.spi.TargettedJoinpoint;
38 import org.jboss.kernel.plugins.config.Configurator;
39 import org.jboss.kernel.spi.config.KernelConfigurator;
40
41 /**
42  * Collection metadata.
43  *
44  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
45  * @version $Revision: 57307 $
46  */

47 public class GenericBeanFactory
48 {
49    /** The configurator */
50    protected KernelConfigurator configurator;
51    
52    /** The bean class name */
53    protected String JavaDoc bean;
54    
55    /** The classloader */
56    protected ClassLoaderMetaData classLoader;
57    
58    /** The constructor metadata */
59    protected ConstructorMetaData constructor;
60    
61    /** The properties Map<propertyName, ValueMetaData> */
62    protected Map JavaDoc properties;
63
64    /** The create lifecycle method */
65    protected LifecycleMetaData create;
66
67    /** The start lifecycle method */
68    protected LifecycleMetaData start;
69    
70    /**
71     * Create a new generic bean factory
72     *
73     * @param configurator the configurator
74     */

75    public GenericBeanFactory(KernelConfigurator configurator)
76    {
77       this.configurator = configurator;
78    }
79    
80    /**
81     * Create a new bean
82     *
83     * @return the bean
84     * @throws Throwable for any error
85     */

86    public Object JavaDoc createBean() throws Throwable JavaDoc
87    {
88       ClassLoader JavaDoc cl = Configurator.getClassLoader(classLoader);
89       BeanInfo info = configurator.getBeanInfo(bean, cl);
90       Joinpoint joinpoint = configurator.getConstructorJoinPoint(info, constructor, null);
91       Object JavaDoc result = joinpoint.dispatch();
92       if (properties != null && properties.size() > 0)
93       {
94          for (Iterator JavaDoc i = properties.entrySet().iterator(); i.hasNext();)
95          {
96             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
97             String JavaDoc property = (String JavaDoc) entry.getKey();
98             ValueMetaData vmd = (ValueMetaData) entry.getValue();
99             TargettedJoinpoint jp = configurator.getPropertySetterJoinPoint(info, property, cl, vmd);
100             jp.setTarget(result);
101             jp.dispatch();
102          }
103       }
104       invokeLifecycle("create", create, info, cl, result);
105       invokeLifecycle("start", start, info, cl, result);
106       return result;
107    }
108    
109    /**
110     * Get the bean name
111     *
112     * @return the bean
113     */

114    public String JavaDoc getBean()
115    {
116       return bean;
117    }
118    
119    /**
120     * Set the bean name
121     *
122     * @param bean the bean name
123     */

124    public void setBean(String JavaDoc bean)
125    {
126       this.bean = bean;
127    }
128    
129    /**
130     * Get the classLoader.
131     *
132     * @return the classLoader.
133     */

134    public ClassLoaderMetaData getClassLoader()
135    {
136       return classLoader;
137    }
138
139    /**
140     * Set the classLoader.
141     *
142     * @param classLoader the classLoader.
143     */

144    public void setClassLoader(ClassLoaderMetaData classLoader)
145    {
146       this.classLoader = classLoader;
147    }
148
149    /**
150     * Get the constructor metadata
151     *
152     * @return the contructor metadata
153     */

154    public ConstructorMetaData getConstructor()
155    {
156       return constructor;
157    }
158    
159    /**
160     * Set the constructor metadata
161     *
162     * @param constructor the constructor metadata
163     */

164    public void setConstructor(ConstructorMetaData constructor)
165    {
166       this.constructor = constructor;
167    }
168    
169    /**
170     * Get the properties
171     *
172     * @return the properties Map<propertyName, ValueMetaData>
173     */

174    public Map JavaDoc getProperties()
175    {
176       return properties;
177    }
178    
179    /**
180     * Set the properties
181     *
182     * @param properties the properties Map<propertyName, ValueMetaData>
183     */

184    public void setProperties(Map JavaDoc properties)
185    {
186       this.properties = properties;
187    }
188
189    /**
190     * Get the create.
191     *
192     * @return the create.
193     */

194    public LifecycleMetaData getCreate()
195    {
196       return create;
197    }
198
199    /**
200     * Set the create.
201     *
202     * @param create the create.
203     */

204    public void setCreate(LifecycleMetaData create)
205    {
206       this.create = create;
207    }
208
209    /**
210     * Get the start.
211     *
212     * @return the start.
213     */

214    public LifecycleMetaData getStart()
215    {
216       return start;
217    }
218
219    /**
220     * Set the start.
221     *
222     * @param start the start.
223     */

224    public void setStart(LifecycleMetaData start)
225    {
226       this.start = start;
227    }
228
229    /**
230     * Invoke a lifecycle method
231     *
232     * @param methodName the default method name
233     * @param lifecycle the lifecycle
234     * @param info the bean info
235     * @param cl the classloader
236     * @param target the target
237     * @throws Throwable for any error
238     */

239    protected void invokeLifecycle(String JavaDoc methodName, LifecycleMetaData lifecycle, BeanInfo info, ClassLoader JavaDoc cl, Object JavaDoc target) throws Throwable JavaDoc
240    {
241       String JavaDoc method = methodName;
242       if (lifecycle != null && lifecycle.getMethodName() != null)
243          method = lifecycle.getMethodName();
244       List JavaDoc<ParameterMetaData> parameters = null;
245       if (lifecycle != null)
246          parameters = lifecycle.getParameters();
247       MethodJoinpoint joinpoint;
248       try
249       {
250          joinpoint = configurator.getMethodJoinPoint(info, cl, method, parameters, false, true);
251       }
252       catch (JoinpointException ignored)
253       {
254          return;
255       }
256       joinpoint.setTarget(target);
257       joinpoint.dispatch();
258    }
259 }
Popular Tags