KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > beans > metadata > plugins > AbstractBeanMetaData


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;
23
24 import java.util.*;
25
26 import org.jboss.beans.metadata.spi.*;
27 import org.jboss.dependency.plugins.AbstractDependencyItem;
28 import org.jboss.dependency.spi.ControllerContext;
29 import org.jboss.dependency.spi.ControllerMode;
30 import org.jboss.dependency.spi.ControllerState;
31 import org.jboss.dependency.spi.DependencyItem;
32 import org.jboss.kernel.spi.dependency.KernelController;
33 import org.jboss.kernel.spi.dependency.KernelControllerContext;
34 import org.jboss.reflect.spi.TypeInfo;
35 import org.jboss.util.JBossObject;
36 import org.jboss.util.JBossStringBuilder;
37
38 /**
39  * Metadata for a bean.
40  *
41  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 57321 $
43  */

44 public class AbstractBeanMetaData extends AbstractFeatureMetaData implements BeanMetaData, BeanMetaDataFactory
45 {
46    /** The bean fully qualified class name */
47    protected String JavaDoc bean;
48
49    /** The name of this instance */
50    protected String JavaDoc name;
51
52    /** The mode */
53    protected ControllerMode mode;
54
55    /** The properties configuration Set<PropertyMetaData> */
56    private Set<PropertyMetaData> properties;
57
58    /** The bean ClassLoader */
59    protected ClassLoaderMetaData classLoader;
60
61    /** The constructor */
62    protected ConstructorMetaData constructor;
63
64    /** The create lifecycle */
65    protected LifecycleMetaData create;
66
67    /** The start lifecycle */
68    protected LifecycleMetaData start;
69
70    /** The stop lifecycle */
71    protected LifecycleMetaData stop;
72
73    /** The destroy lifecycle */
74    protected LifecycleMetaData destroy;
75
76    /** What the bean demands Set<DemandMetaData> */
77    protected Set<DemandMetaData> demands;
78
79    /** What the bean supplies Set<SupplyMetaData> */
80    protected Set<SupplyMetaData> supplies;
81
82    /** What the bean dependencies Set<DependencyMetaData> */
83    protected Set<DependencyMetaData> depends;
84
85    /** The install operations List<InstallMetaData> */
86    protected List<InstallMetaData> installs;
87
88    /** The uninstall operations List<InstallMetaData> */
89    protected List<InstallMetaData> uninstalls;
90
91    /** The kernel controller */
92    protected KernelController controller;
93
94    /**
95     * Create a new bean meta data
96     */

97    public AbstractBeanMetaData()
98    {
99       super();
100    }
101
102    /**
103     * Create a new bean meta data
104     *
105     * @param bean the bean class name
106     */

107    public AbstractBeanMetaData(String JavaDoc bean)
108    {
109       this.bean = bean;
110    }
111    /**
112     * Create a new bean meta data
113     *
114     * @param name the name
115     * @param bean the bean class name
116     */

117    public AbstractBeanMetaData(String JavaDoc name, String JavaDoc bean)
118    {
119       this.name = name;
120       this.bean = bean;
121    }
122
123    public List<BeanMetaData> getBeans()
124    {
125       List<BeanMetaData> nestedBeans = findNestedBeans();
126       if (nestedBeans.isEmpty())
127       {
128          return Collections.singletonList((BeanMetaData)this);
129       }
130       else
131       {
132          nestedBeans.add(this);
133          return nestedBeans;
134       }
135    }
136
137    protected List<BeanMetaData> findNestedBeans()
138    {
139       List<BeanMetaData> allBeans = new ArrayList<BeanMetaData>();
140       addBeans(this, allBeans);
141       return allBeans;
142    }
143
144    protected void addBeans(MetaDataVisitorNode current, List<BeanMetaData> list)
145    {
146       for(Iterator<? extends MetaDataVisitorNode> children = current.getChildren(); children != null && children.hasNext();)
147       {
148          MetaDataVisitorNode next = children.next();
149          if (next instanceof BeanMetaDataFactory)
150          {
151             list.addAll(((BeanMetaDataFactory) next).getBeans());
152          }
153          else
154          {
155             addBeans(next, list);
156             if (next instanceof BeanMetaData)
157             {
158                list.add((BeanMetaData) current);
159             }
160          }
161       }
162    }
163
164    /**
165     * Get the bean class name.
166     * @return the fully qualified bean class name.
167     */

168    public String JavaDoc getBean()
169    {
170       return bean;
171    }
172
173    /**
174     * Set the bean class name and flush the object cache.
175     *
176     * @param bean The bean class name to set.
177     */

178    public void setBean(String JavaDoc bean)
179    {
180       this.bean = bean;
181       flushJBossObjectCache();
182    }
183
184    /**
185     * Get a property
186     *
187     * @param name the name
188     * @return the property name
189     */

190    public PropertyMetaData getProperty(String JavaDoc name)
191    {
192       if (name == null)
193          throw new IllegalArgumentException JavaDoc("Null name");
194       if (properties != null && properties.size() > 0)
195       {
196          for (Iterator i = properties.iterator(); i.hasNext();)
197          {
198             AbstractPropertyMetaData prop = (AbstractPropertyMetaData) i.next();
199             if (name.equals(prop.getName()))
200                return prop;
201          }
202       }
203       return null;
204    }
205
206    /**
207     * Add a property
208     *
209     * @param property the property
210     */

211    public void addProperty(PropertyMetaData property)
212    {
213       if (property == null)
214          throw new IllegalArgumentException JavaDoc("Null property");
215       if (properties == null)
216          properties = new HashSet<PropertyMetaData>();
217       properties.add(property);
218       flushJBossObjectCache();
219    }
220
221    /**
222     * Set the propertiess.
223     *
224     * @param properties Set<PropertyMetaData>
225     */

226    public void setProperties(Set<PropertyMetaData> properties)
227    {
228       this.properties = properties;
229       flushJBossObjectCache();
230    }
231
232    public ClassLoaderMetaData getClassLoader()
233    {
234       return classLoader;
235    }
236
237    public void setClassLoader(ClassLoaderMetaData classLoader)
238    {
239       this.classLoader = classLoader;
240    }
241
242    /**
243     * Set the constructor
244     *
245     * @param constructor the constructor metadata
246     */

247    public void setConstructor(ConstructorMetaData constructor)
248    {
249       this.constructor = constructor;
250    }
251
252    /**
253     * Set what the bean demands.
254     *
255     * @param demands Set<DemandMetaData>
256     */

257    public void setDemands(Set<DemandMetaData> demands)
258    {
259       this.demands = demands;
260       flushJBossObjectCache();
261    }
262
263    /**
264     * Set what the bean supplies.
265     *
266     * @param supplies Set<SupplyMetaData>
267     */

268    public void setSupplies(Set<SupplyMetaData> supplies)
269    {
270       this.supplies = supplies;
271       flushJBossObjectCache();
272    }
273
274    /**
275     * Set what the bean depends.
276     *
277     * @param depends Set<DependencyMetaData>
278     */

279    public void setDepends(Set<DependencyMetaData> depends)
280    {
281       this.depends = depends;
282       flushJBossObjectCache();
283    }
284
285    public String JavaDoc getName()
286    {
287       return name;
288    }
289
290    /**
291     * Set the name.
292     *
293     * @param name The name to set.
294     */

295    public void setName(String JavaDoc name)
296    {
297       this.name = name;
298       flushJBossObjectCache();
299    }
300
301    public ControllerMode getMode()
302    {
303       return mode;
304    }
305
306    public void setMode(ControllerMode mode)
307    {
308       this.mode = mode;
309       flushJBossObjectCache();
310    }
311
312    public Set<PropertyMetaData> getProperties()
313    {
314       return properties;
315    }
316
317    public ConstructorMetaData getConstructor()
318    {
319       return constructor;
320    }
321
322    public LifecycleMetaData getCreate()
323    {
324       return create;
325    }
326
327    /**
328     * Set the lifecycle metadata
329     *
330     * @param lifecycle the lifecycle metadata
331     */

332    public void setCreate(LifecycleMetaData lifecycle)
333    {
334       lifecycle.setState(ControllerState.CREATE);
335       this.create = lifecycle;
336    }
337
338    public LifecycleMetaData getStart()
339    {
340       return start;
341    }
342
343    /**
344     * Set the start metadata
345     *
346     * @param lifecycle the lifecycle metadata
347     */

348    public void setStart(LifecycleMetaData lifecycle)
349    {
350       lifecycle.setState(ControllerState.START);
351       this.start = lifecycle;
352    }
353
354    public LifecycleMetaData getStop()
355    {
356       return stop;
357    }
358
359    /**
360     * Set the stop metadata
361     *
362     * @param lifecycle the lifecycle metadata
363     */

364    public void setStop(LifecycleMetaData lifecycle)
365    {
366       lifecycle.setState(ControllerState.START);
367       this.stop = lifecycle;
368    }
369
370    public LifecycleMetaData getDestroy()
371    {
372       return destroy;
373    }
374
375    /**
376     * Set the destroy metadata
377     *
378     * @param lifecycle the lifecycle metadata
379     */

380    public void setDestroy(LifecycleMetaData lifecycle)
381    {
382       lifecycle.setState(ControllerState.CREATE);
383       this.destroy = lifecycle;
384    }
385
386    public Set<DemandMetaData> getDemands()
387    {
388       return demands;
389    }
390
391    public Set<SupplyMetaData> getSupplies()
392    {
393       return supplies;
394    }
395
396    public Set<DependencyMetaData> getDepends()
397    {
398       return depends;
399    }
400
401    public List<InstallMetaData> getInstalls()
402    {
403       return installs;
404    }
405
406    /**
407     * Set the installs
408     *
409     * @param installs List<InstallMetaData>
410     */

411    public void setInstalls(List<InstallMetaData> installs)
412    {
413       this.installs = installs;
414       flushJBossObjectCache();
415    }
416
417    public List<InstallMetaData> getUninstalls()
418    {
419       return uninstalls;
420    }
421
422    /**
423     * Set the uninstalls
424     *
425     * @param uninstalls List<InstallMetaData>
426     */

427    public void setUninstalls(List<InstallMetaData> uninstalls)
428    {
429       this.uninstalls = uninstalls;
430       flushJBossObjectCache();
431    }
432
433    public void initialVisit(MetaDataVisitor visitor)
434    {
435       if (visitor.visitorNodeStack().isEmpty() == false || (classLoader != null && classLoader.getClassLoader() != this))
436       {
437          KernelControllerContext controllerContext = visitor.getControllerContext();
438          controller = (KernelController) controllerContext.getController();
439          Object JavaDoc name = controllerContext.getName();
440          Object JavaDoc iDependOn = getUnderlyingValue();
441          if (name.equals(iDependOn) == false)
442          {
443             ControllerState whenRequired = visitor.getContextState();
444             DependencyItem di = new AbstractDependencyItem(name, iDependOn, whenRequired, ControllerState.INSTALLED);
445             visitor.addDependency(di);
446          }
447       }
448       super.initialVisit(visitor);
449    }
450
451    public void setController(KernelController controller)
452    {
453       this.controller = controller;
454    }
455
456    protected void addChildren(Set<MetaDataVisitorNode> children)
457    {
458       super.addChildren(children);
459       if (classLoader != null && classLoader.getClassLoader() != this)
460          children.add(classLoader);
461       if (constructor != null)
462          children.add(constructor);
463       if (properties != null)
464          children.addAll(properties);
465       if (create != null)
466          children.add(create);
467       if (start != null)
468          children.add(start);
469       if (stop != null)
470          children.add(stop);
471       if (destroy != null)
472          children.add(destroy);
473       if (demands != null)
474          children.addAll(demands);
475       if (supplies != null)
476          children.addAll(supplies);
477       if (depends != null)
478          children.addAll(depends);
479       if (installs != null)
480          children.addAll(installs);
481       if (uninstalls != null)
482          children.addAll(uninstalls);
483    }
484
485    public Class JavaDoc getType(MetaDataVisitor visitor, MetaDataVisitorNode previous) throws Throwable JavaDoc
486    {
487       throw new IllegalArgumentException JavaDoc("Cannot determine inject class type: " + this);
488    }
489
490    public Object JavaDoc getUnderlyingValue()
491    {
492       return name;
493    }
494
495    @SuppressWarnings JavaDoc("unchecked")
496    public Object JavaDoc getValue(TypeInfo info, ClassLoader JavaDoc cl) throws Throwable JavaDoc
497    {
498       ControllerContext context = controller.getInstalledContext(name);
499       if (context == null || context.getTarget() == null)
500       {
501          // possible call for classloader
502
if (info == null && classLoader != null && classLoader.getClassLoader() == this)
503          {
504             return cl;
505          }
506          throw new IllegalArgumentException JavaDoc("Bean not yet installed: " + name);
507       }
508       Object JavaDoc target = context.getTarget();
509       if (info != null && info.getType().isAssignableFrom(target.getClass()) == false)
510       {
511          throw new ClassCastException JavaDoc(target + " is not a " + info);
512       }
513       return target;
514    }
515
516    public void toString(JBossStringBuilder buffer)
517    {
518       buffer.append("name=").append(name);
519       buffer.append(" bean=").append(bean);
520       buffer.append(" properties=");
521       JBossObject.list(buffer, properties);
522       if (classLoader != null && classLoader.getClassLoader() != this)
523          buffer.append(" classLoader=").append(classLoader);
524       buffer.append(" constructor=").append(constructor);
525       if (create != null)
526          buffer.append(" create=").append(create);
527       if (start != null)
528          buffer.append(" start=").append(start);
529       if (stop != null)
530          buffer.append(" stop=").append(stop);
531       if (destroy != null)
532          buffer.append(" destroy=").append(destroy);
533       if (demands != null)
534       {
535          buffer.append(" demands=");
536          JBossObject.list(buffer, demands);
537       }
538       super.toString(buffer);
539       if (supplies != null)
540       {
541          buffer.append(" supplies=");
542          JBossObject.list(buffer, supplies);
543       }
544       if (depends != null)
545       {
546          buffer.append(" depends=");
547          JBossObject.list(buffer, depends);
548       }
549       if (installs != null)
550       {
551          buffer.append(" installs=");
552          JBossObject.list(buffer, installs);
553       }
554       if (uninstalls != null)
555       {
556          buffer.append(" uninstalls=");
557          JBossObject.list(buffer, uninstalls);
558       }
559    }
560
561    public void toShortString(JBossStringBuilder buffer)
562    {
563       buffer.append(bean);
564       buffer.append('/');
565       buffer.append(name);
566    }
567 }
568
Popular Tags