KickJava   Java API By Example, From Geeks To Geeks.

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


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.Stack JavaDoc;
25
26 import org.jboss.beans.metadata.spi.BeanMetaData;
27 import org.jboss.beans.metadata.spi.MetaDataVisitor;
28 import org.jboss.beans.metadata.spi.MetaDataVisitorNode;
29 import org.jboss.beans.metadata.spi.PropertyMetaData;
30 import org.jboss.dependency.spi.ControllerState;
31 import org.jboss.joinpoint.spi.TargettedJoinpoint;
32 import org.jboss.kernel.plugins.config.Configurator;
33 import org.jboss.kernel.spi.config.KernelConfigurator;
34 import org.jboss.kernel.spi.dependency.KernelController;
35 import org.jboss.kernel.spi.dependency.KernelControllerContext;
36 import org.jboss.reflect.spi.ClassInfo;
37 import org.jboss.util.JBossStringBuilder;
38
39 /**
40  * A typed value.
41  *
42  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
43  * @version $Revision: 58417 $
44  */

45 public abstract class AbstractTypeMetaData extends AbstractValueMetaData
46 {
47    /**
48     * The type
49     */

50    protected String JavaDoc type;
51
52    /**
53     * The configurator
54     */

55    protected KernelController controller;
56
57    /**
58     * The configurator
59     */

60    protected KernelConfigurator configurator;
61
62    /**
63     * The property name
64     */

65    protected String JavaDoc propertyName;
66
67    /**
68     * The bean name
69     */

70    protected String JavaDoc beanName;
71
72    /**
73     * Create a new typed value
74     */

75    public AbstractTypeMetaData()
76    {
77    }
78
79    /**
80     * Create a new typed value
81     *
82     * @param value the value
83     */

84    public AbstractTypeMetaData(String JavaDoc value)
85    {
86       super(value);
87    }
88
89    /**
90     * Set the type
91     *
92     * @param type the type
93     */

94    public void setType(String JavaDoc type)
95    {
96       this.type = type;
97    }
98
99    public String JavaDoc getType()
100    {
101       return type;
102    }
103
104    public void initialVisit(MetaDataVisitor visitor)
105    {
106       controller = (KernelController) visitor.getControllerContext().getController();
107       configurator = visitor.getControllerContext().getKernel().getConfigurator();
108       preparePreinstantiatedLookup(visitor);
109       visitor.initialVisit(this);
110    }
111
112    private void preparePreinstantiatedLookup(MetaDataVisitor visitor)
113    {
114       Stack JavaDoc<MetaDataVisitorNode> visitorNodes = visitor.visitorNodeStack();
115       // pop it so that we can get to grand parent for more info
116
MetaDataVisitorNode parent = visitorNodes.pop();
117       try
118       {
119          if (parent instanceof PropertyMetaData)
120          {
121             PropertyMetaData pmd = (PropertyMetaData) parent;
122             propertyName = pmd.getName();
123             Object JavaDoc gp = visitorNodes.peek();
124             if (gp instanceof BeanMetaData)
125             {
126                BeanMetaData bmd = (BeanMetaData) gp;
127                beanName = bmd.getName();
128             }
129          }
130       }
131       finally
132       {
133          visitorNodes.push(parent);
134       }
135    }
136
137    @SuppressWarnings JavaDoc("unchecked")
138    protected Object JavaDoc preinstantiatedLookup(ClassLoader JavaDoc cl, Class JavaDoc expected)
139    {
140       Object JavaDoc result = null;
141       if (propertyName != null && beanName != null)
142       {
143          try
144          {
145             KernelControllerContext context = (KernelControllerContext) controller.getContext(beanName, ControllerState.INSTANTIATED);
146             TargettedJoinpoint joinpoint = configurator.getPropertyGetterJoinPoint(context.getBeanInfo(), propertyName);
147             joinpoint.setTarget(context.getTarget());
148             result = joinpoint.dispatch();
149          }
150          catch (Throwable JavaDoc t)
151          {
152             log.warn("Exception in preinstantiated lookup: " + t);
153          }
154          if (result != null && expected != null && expected.isAssignableFrom(result.getClass()) == false)
155             throw new ClassCastException JavaDoc(result.getClass() + " is not a " + expected.getName());
156       }
157       return result;
158    }
159
160    /**
161     * Set the configurator
162     *
163     * @param configurator the configurator
164     */

165    public void setConfigurator(KernelConfigurator configurator)
166    {
167       this.configurator = configurator;
168    }
169
170    public void toString(JBossStringBuilder buffer)
171    {
172       super.toString(buffer);
173       if (type != null)
174          buffer.append(" type=").append(type);
175    }
176
177    /**
178     * Get the class info for this type
179     *
180     * @param cl classloader
181     * @return the class info
182     * @throws Throwable for any error
183     */

184    protected ClassInfo getClassInfo(ClassLoader JavaDoc cl) throws Throwable JavaDoc
185    {
186       return getClassInfo(type, cl);
187    }
188
189    protected ClassInfo getClassInfo(String JavaDoc classType, ClassLoader JavaDoc cl) throws Throwable JavaDoc
190    {
191       if (classType == null)
192          return null;
193
194       return configurator.getClassInfo(classType, cl);
195    }
196
197    protected Class JavaDoc getClass(MetaDataVisitor visitor, String JavaDoc classType) throws Throwable JavaDoc
198    {
199       KernelControllerContext context = visitor.getControllerContext();
200       ClassLoader JavaDoc cl = Configurator.getClassLoader(context.getBeanMetaData());
201       return getClassInfo(classType, cl).getType();
202    }
203
204 }
205
Popular Tags