KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
25
26 import org.jboss.beans.metadata.spi.DemandMetaData;
27 import org.jboss.beans.metadata.spi.MetaDataVisitor;
28 import org.jboss.beans.metadata.spi.MetaDataVisitorNode;
29 import org.jboss.dependency.plugins.AbstractDependencyItem;
30 import org.jboss.dependency.spi.Controller;
31 import org.jboss.dependency.spi.ControllerContext;
32 import org.jboss.dependency.spi.ControllerState;
33 import org.jboss.dependency.spi.DependencyItem;
34 import org.jboss.kernel.spi.dependency.KernelControllerContext;
35 import org.jboss.util.JBossObject;
36 import org.jboss.util.JBossStringBuilder;
37
38 /**
39  * A demand.
40  *
41  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 57321 $
43  */

44 public class AbstractDemandMetaData extends JBossObject implements DemandMetaData
45 {
46    /** The demand */
47    protected Object JavaDoc demand;
48    
49    /** When the dependency is required */
50    protected ControllerState whenRequired = ControllerState.DESCRIBED;
51    
52    /** The dependency delegate */
53    protected AbstractDependencyItem dependencyItem;
54
55    /**
56     * Create a new demand
57     */

58    public AbstractDemandMetaData()
59    {
60    }
61
62    /**
63     * Create a new demand
64     *
65     * @param demand the demand
66     */

67    public AbstractDemandMetaData(Object JavaDoc demand)
68    {
69       this.demand = demand;
70    }
71    
72    /**
73     * Set the required state of the dependency
74     *
75     * @param whenRequired when the dependecy is required
76     */

77    public void setWhenRequired(ControllerState whenRequired)
78    {
79       this.whenRequired = whenRequired;
80       flushJBossObjectCache();
81    }
82    
83    /**
84     * Set the demand
85     *
86     * @param demand the demand
87     */

88    public void setDemand(Object JavaDoc demand)
89    {
90       this.demand = demand;
91       flushJBossObjectCache();
92    }
93
94    public Object JavaDoc getDemand()
95    {
96       return demand;
97    }
98
99    public ControllerState getWhenRequired()
100    {
101       return whenRequired;
102    }
103
104    public void initialVisit(MetaDataVisitor visitor)
105    {
106       KernelControllerContext context = visitor.getControllerContext();
107       DependencyItem item = new DemandDependencyItem(context.getName());
108       visitor.addDependency(item);
109       visitor.initialVisit(this);
110    }
111
112    public void describeVisit(MetaDataVisitor vistor)
113    {
114       vistor.describeVisit(this);
115    }
116
117    public Iterator JavaDoc<? extends MetaDataVisitorNode> getChildren()
118    {
119       return null;
120    }
121    
122    public void toString(JBossStringBuilder buffer)
123    {
124       buffer.append("demand=").append(demand);
125       if (whenRequired != null)
126          buffer.append(" whenRequired").append(whenRequired.getStateString());
127    }
128    
129    public void toShortString(JBossStringBuilder buffer)
130    {
131       buffer.append(demand);
132    }
133
134    /**
135     * Information about a demand dependency.
136     */

137    public class DemandDependencyItem extends AbstractDependencyItem
138    {
139       /**
140        * Create a new demand dependecy
141        *
142        * @param name my name
143        */

144       public DemandDependencyItem(Object JavaDoc name)
145       {
146          super(name, null, whenRequired, null);
147       }
148       
149       public boolean resolve(Controller controller)
150       {
151          ControllerContext context = controller.getInstalledContext(demand);
152          if (context != null)
153          {
154             setIDependOn(context.getName());
155             addDependsOnMe(controller, context);
156             setResolved(true);
157          }
158          else
159          {
160             setResolved(false);
161          }
162          return isResolved();
163       }
164
165       public void unresolved()
166       {
167          setIDependOn(null);
168          setResolved(false);
169       }
170       
171       public void toString(JBossStringBuilder buffer)
172       {
173          super.toString(buffer);
174          buffer.append(" demand=").append(demand);
175       }
176       
177       public void toShortString(JBossStringBuilder buffer)
178       {
179          buffer.append(getName()).append(" demands ").append(demand);
180       }
181    }
182 }
183
Popular Tags