KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.beans.metadata.spi.MetaDataVisitor;
25 import org.jboss.beans.metadata.spi.MetaDataVisitorNode;
26 import org.jboss.dependency.spi.ControllerContext;
27 import org.jboss.dependency.spi.DependencyItem;
28 import org.jboss.kernel.plugins.dependency.ClassContextDependencyItem;
29 import org.jboss.kernel.spi.dependency.KernelController;
30 import org.jboss.kernel.spi.dependency.KernelControllerContext;
31 import org.jboss.reflect.spi.TypeInfo;
32 import org.jboss.util.JBossStringBuilder;
33
34 /**
35  * Injection value.
36  *
37  * @author <a HREF="ales.justin@gmail.com">Ales Justin</a>
38  */

39 public class AbstractInjectionValueMetaData extends AbstractDependencyValueMetaData
40 {
41    protected InjectionType injectionType = InjectionType.BY_CLASS;
42
43    /**
44     * Simplyifies things with InjectionType.BY_NAME
45     */

46    protected AbstractPropertyMetaData propertyMetaData;
47
48    /**
49     * Create a new injection value
50     */

51    public AbstractInjectionValueMetaData()
52    {
53    }
54
55    /**
56     * Create a new injection value
57     *
58     * @param value the value
59     */

60    public AbstractInjectionValueMetaData(Object JavaDoc value)
61    {
62       super(value);
63    }
64
65    /**
66     * Create a new injection value
67     *
68     * @param value the value
69     * @param property the property
70     */

71    public AbstractInjectionValueMetaData(Object JavaDoc value, String JavaDoc property)
72    {
73       super(value, property);
74    }
75
76    public InjectionType getInjectionType()
77    {
78       return injectionType;
79    }
80
81    public void setInjectionType(InjectionType injectionType)
82    {
83       this.injectionType = injectionType;
84    }
85
86    public AbstractPropertyMetaData getPropertyMetaData()
87    {
88       return propertyMetaData;
89    }
90
91    public void setPropertyMetaData(AbstractPropertyMetaData propertyMetaData)
92    {
93       this.propertyMetaData = propertyMetaData;
94    }
95
96    public Object JavaDoc getValue(TypeInfo info, ClassLoader JavaDoc cl) throws Throwable JavaDoc
97    {
98       if (value == null)
99       {
100          ControllerContext context = controller.getInstalledContext(info.getType());
101          if (context == null)
102          {
103             throw new IllegalArgumentException JavaDoc("Possible multiple matching beans, see log for info.");
104          }
105          return context.getTarget();
106       }
107       return super.getValue(info, cl);
108    }
109
110    public void initialVisit(MetaDataVisitor visitor)
111    {
112       if (getUnderlyingValue() == null)
113       {
114          // check for property
115
if (property != null)
116          {
117             property = null;
118             log.warn("Ignoring property - contextual injection: " + this);
119          }
120
121          if (InjectionType.BY_NAME.equals(injectionType))
122          {
123             if (propertyMetaData == null)
124             {
125                throw new IllegalArgumentException JavaDoc("Illegal usage of type ByName - injection not used with property = " + this);
126             }
127             setValue(propertyMetaData.getName());
128          }
129
130          visitor.initialVisit(this);
131       }
132       // check if was maybe set with by_name
133
if (getUnderlyingValue() != null)
134       {
135          super.initialVisit(visitor);
136       }
137    }
138
139    public void describeVisit(MetaDataVisitor visitor)
140    {
141       if (getUnderlyingValue() == null)
142       {
143          if (InjectionType.BY_CLASS.equals(injectionType))
144          {
145             KernelControllerContext context = visitor.getControllerContext();
146             controller = (KernelController) context.getController(); // set controller
147

148             // we pop it so that parent node has the same semantics as this one
149
// meaning that his current peek is also his parent
150
// and all other nodes that cannot determine type follow the same
151
// contract - popping and pushing
152
// maybe the whole thing can be rewritten to LinkedList
153
// or simply using the fact that Stack is also a Vector?
154
MetaDataVisitorNode node = visitor.visitorNodeStack().pop();
155             try
156             {
157                if (node instanceof TypeProvider)
158                {
159                   TypeProvider typeProvider = (TypeProvider) node;
160                   DependencyItem item = new ClassContextDependencyItem(
161                         context.getName(),
162                         typeProvider.getType(visitor, this),
163                         visitor.getContextState(),
164                         dependentState);
165                   visitor.addDependency(item);
166                }
167                else
168                {
169                   throw new Error JavaDoc(TypeProvider.ERROR_MSG);
170                }
171             }
172             catch (Error JavaDoc error)
173             {
174                throw error;
175             }
176             catch (Throwable JavaDoc throwable)
177             {
178                throw new Error JavaDoc(throwable);
179             }
180             finally
181             {
182                visitor.visitorNodeStack().push(node);
183             }
184          }
185          else
186          {
187             throw new IllegalArgumentException JavaDoc("Unknown injection type=" + injectionType);
188          }
189       }
190       super.describeVisit(visitor);
191    }
192
193    public void toString(JBossStringBuilder buffer)
194    {
195       super.toString(buffer);
196       if (injectionType != null)
197          buffer.append(" injectionType=").append(injectionType);
198       if (propertyMetaData != null)
199          buffer.append(" propertyMetaData=").append(propertyMetaData.getName()); //else overflow - indefinite recursion
200
}
201
202 }
203
Popular Tags