KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > metadata > ServiceInjectionValueMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.system.metadata;
23
24 import org.jboss.beans.info.spi.BeanInfo;
25 import org.jboss.dependency.plugins.AbstractDependencyItem;
26 import org.jboss.dependency.spi.ControllerContext;
27 import org.jboss.dependency.spi.ControllerState;
28 import org.jboss.dependency.spi.DependencyItem;
29 import org.jboss.joinpoint.spi.TargettedJoinpoint;
30 import org.jboss.kernel.spi.config.KernelConfigurator;
31 import org.jboss.kernel.spi.dependency.KernelController;
32 import org.jboss.system.microcontainer.ServiceControllerContext;
33
34 /**
35  * ServiceInjectionValueMetaData.
36  *
37  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
38  * @version $Revision: 1.1 $
39  */

40 public class ServiceInjectionValueMetaData extends AbstractMetaDataVisitorNode implements ServiceValueMetaData
41 {
42    /** The dependency */
43    private Object JavaDoc dependency;
44
45    /** The property */
46    private String JavaDoc property;
47
48    /** The required state of the dependency */
49    private ControllerState dependentState = ControllerState.INSTALLED;
50
51    /**
52     * Create a new ServiceInjectionValueMetaData.
53     */

54    public ServiceInjectionValueMetaData()
55    {
56    }
57
58    /**
59     * Create a new ServiceInjectionValueMetaData.
60     *
61     * @param dependency the dependency
62     */

63    public ServiceInjectionValueMetaData(Object JavaDoc dependency)
64    {
65       this(dependency, null);
66    }
67    
68    /**
69     * Create a new ServiceInjectionValueMetaData.
70     *
71     * @param dependency the dependency
72     * @param property the property name
73     */

74    public ServiceInjectionValueMetaData(Object JavaDoc dependency, String JavaDoc property)
75    {
76       this(dependency, property, ControllerState.INSTALLED);
77    }
78    
79    /**
80     * Create a new ServiceInjectionValueMetaData.
81     *
82     * @param dependency the dependency
83     * @param property the property name
84     * @param dependentState the dependent state
85     */

86    public ServiceInjectionValueMetaData(Object JavaDoc dependency, String JavaDoc property, ControllerState dependentState)
87    {
88       setDependency(dependency);
89       setProperty(property);
90       setDependentState(dependentState);
91    }
92
93    /**
94     * Get the dependency.
95     *
96     * @return the dependency.
97     */

98    public Object JavaDoc getDependency()
99    {
100       return dependency;
101    }
102
103    /**
104     * Set the dependency.
105     *
106     * @param dependency the dependency.
107     */

108    public void setDependency(Object JavaDoc dependency)
109    {
110       if (dependency == null)
111          throw new IllegalArgumentException JavaDoc("Null dependency");
112       this.dependency = dependency;
113    }
114
115    /**
116     * Get the property.
117     *
118     * @return the property.
119     */

120    public String JavaDoc getProperty()
121    {
122       return property;
123    }
124
125    /**
126     * Set the property.
127     *
128     * @param property the property.
129     */

130    public void setProperty(String JavaDoc property)
131    {
132       this.property = property;
133    }
134
135    /**
136     * Get the dependentState.
137     *
138     * @return the dependentState.
139     */

140    public ControllerState getDependentState()
141    {
142       return dependentState;
143    }
144
145    /**
146     * Set the dependentState.
147     *
148     * @param dependentState the dependentState.
149     */

150    public void setDependentState(ControllerState dependentState)
151    {
152       this.dependentState = dependentState;
153    }
154
155    public Object JavaDoc getValue(ServiceValueContext valueContext) throws Throwable JavaDoc
156    {
157       KernelController controller = valueContext.getController();
158       
159       ControllerState state = dependentState;
160       if (state == null)
161          state = ControllerState.INSTALLED;
162
163       ControllerContext context = controller.getContext(dependency, dependentState);
164       if (context == null)
165          throw new Error JavaDoc("Should not be here - dependency failed! " + this);
166       Object JavaDoc result = context.getTarget();
167       if (result != null && property != null)
168       {
169          KernelConfigurator configurator = controller.getKernel().getConfigurator();
170          BeanInfo beanInfo = configurator.getBeanInfo(result.getClass());
171          TargettedJoinpoint joinpoint = configurator.getPropertyGetterJoinPoint(beanInfo, property);
172          joinpoint.setTarget(result);
173          return joinpoint.dispatch();
174       }
175       return result;
176    }
177
178    public void visit(ServiceMetaDataVisitor visitor)
179    {
180       ServiceControllerContext context = visitor.getControllerContext();
181       Object JavaDoc name = context.getName();
182       ControllerState whenRequired = visitor.getContextState();
183
184       DependencyItem item = new AbstractDependencyItem(name, dependency, whenRequired, dependentState);
185       visitor.addDependency(item);
186
187       visitor.visit(this);
188    }
189 }
190
Popular Tags