KickJava   Java API By Example, From Geeks To Geeks.

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


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.info.spi.BeanInfo;
25 import org.jboss.beans.metadata.spi.MetaDataVisitor;
26 import org.jboss.dependency.plugins.AbstractDependencyItem;
27 import org.jboss.dependency.spi.ControllerContext;
28 import org.jboss.dependency.spi.ControllerState;
29 import org.jboss.dependency.spi.DependencyItem;
30 import org.jboss.joinpoint.spi.TargettedJoinpoint;
31 import org.jboss.kernel.spi.config.KernelConfigurator;
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.JBossStringBuilder;
36
37 /**
38  * Dependency value.
39  *
40  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
41  * @version $Revision: 57133 $
42  */

43 public class AbstractDependencyValueMetaData extends AbstractValueMetaData
44 {
45    /** The controller */
46    protected KernelController controller;
47    
48    /** The property name */
49    protected String JavaDoc property;
50
51    /** The when required state of the dependency or null to use current context state */
52    protected ControllerState whenRequiredState;
53
54    /** The required state of the dependency or null to look in the registry */
55    protected ControllerState dependentState = ControllerState.INSTALLED;
56
57    /**
58     * Create a new dependency value
59     */

60    public AbstractDependencyValueMetaData()
61    {
62    }
63
64    /**
65     * Create a new dependency value
66     *
67     * @param value the value
68     */

69    public AbstractDependencyValueMetaData(Object JavaDoc value)
70    {
71       super(value);
72    }
73
74    /**
75     * Create a new dependency value
76     *
77     * @param value the value
78     * @param property the property
79     */

80    public AbstractDependencyValueMetaData(Object JavaDoc value, String JavaDoc property)
81    {
82       super(value);
83       this.property = property;
84    }
85
86    /**
87     * Get the property
88     *
89     * @return the property
90     */

91    public String JavaDoc getProperty()
92    {
93       return property;
94    }
95    
96    /**
97     * Set the property
98     *
99     * @param property the property name
100     */

101    public void setProperty(String JavaDoc property)
102    {
103       this.property = property;
104    }
105    
106    /**
107     * Set the when required state of the dependency
108     *
109     * @param whenRequiredState the when required state or null if it uses current context state
110     */

111    public void setWhenRequiredState(ControllerState whenRequiredState)
112    {
113       this.whenRequiredState = whenRequiredState;
114       flushJBossObjectCache();
115    }
116
117    public ControllerState getWhenRequiredState()
118    {
119       return whenRequiredState;
120    }
121    
122    /**
123     * Set the required state of the dependency
124     *
125     * @param dependentState the required state or null if it must be in the registry
126     */

127    public void setDependentState(ControllerState dependentState)
128    {
129       this.dependentState = dependentState;
130       flushJBossObjectCache();
131    }
132
133    public ControllerState getDependentState()
134    {
135       return dependentState;
136    }
137
138    public Object JavaDoc getValue(TypeInfo info, ClassLoader JavaDoc cl) throws Throwable JavaDoc
139    {
140       ControllerState state = dependentState;
141       if (state == null)
142          state = ControllerState.INSTALLED;
143       ControllerContext context = controller.getContext(value, state);
144       if (context == null)
145          throw new Error JavaDoc("Should not be here - dependency failed! " + this);
146       Object JavaDoc result = context.getTarget();
147       if (result != null && property != null)
148       {
149          KernelConfigurator configurator = controller.getKernel().getConfigurator();
150          BeanInfo beanInfo = configurator.getBeanInfo(result.getClass());
151          TargettedJoinpoint joinpoint = configurator.getPropertyGetterJoinPoint(beanInfo, property);
152          joinpoint.setTarget(result);
153          return joinpoint.dispatch();
154       }
155       return result;
156    }
157
158    public void initialVisit(MetaDataVisitor visitor)
159    {
160       KernelControllerContext controllerContext = visitor.getControllerContext();
161       controller = (KernelController) controllerContext.getController();
162       Object JavaDoc name = controllerContext.getName();
163       Object JavaDoc iDependOn = getUnderlyingValue();
164       ControllerState whenRequired = whenRequiredState;
165       if (whenRequired == null)
166       {
167          whenRequired = visitor.getContextState();
168       }
169
170       DependencyItem item = new AbstractDependencyItem(name, iDependOn, whenRequired, dependentState);
171       visitor.addDependency(item);
172
173       super.initialVisit(visitor);
174    }
175       
176    public void toString(JBossStringBuilder buffer)
177    {
178       super.toString(buffer);
179       if (property != null)
180          buffer.append(" property=").append(property);
181       if (whenRequiredState != null)
182          buffer.append(" whenRequiredState=").append(whenRequiredState.getStateString());
183       if (dependentState != null)
184          buffer.append(" dependentState=").append(dependentState.getStateString());
185    }
186 }
187
Popular Tags