KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > dependency > plugins > AbstractDependencyItem


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.dependency.plugins;
23
24 import org.jboss.dependency.spi.Controller;
25 import org.jboss.dependency.spi.ControllerContext;
26 import org.jboss.dependency.spi.ControllerMode;
27 import org.jboss.dependency.spi.ControllerState;
28 import org.jboss.dependency.spi.DependencyInfo;
29 import org.jboss.dependency.spi.DependencyItem;
30 import org.jboss.logging.Logger;
31 import org.jboss.util.JBossObject;
32 import org.jboss.util.JBossStringBuilder;
33
34 /**
35  * A DependencyItem.
36  *
37  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
38  * @version $Revision: 57321 $
39  */

40 public class AbstractDependencyItem extends JBossObject implements DependencyItem
41 {
42    /** The log */
43    private static final Logger log = Logger.getLogger(AbstractDependencyItem.class);
44    
45    /** What I depend on */
46    private Object JavaDoc iDependOn;
47
48    /** My name */
49    private Object JavaDoc name;
50
51    /** When the dependency is required */
52    private ControllerState whenRequired = ControllerState.DESCRIBED;
53
54    /** The state of the dependency */
55    private ControllerState dependentState;
56    
57    /** Whether we are resolved */
58    private boolean resolved;
59
60    /**
61     * Create a new dependency item
62     */

63    public AbstractDependencyItem()
64    {
65    }
66
67    /**
68     * Create a new dependency item
69     *
70     * @param name my name
71     * @param iDependOn what I depend on
72     * @param whenRequired when the dependency is required
73     * @param dependentState the required state of the dependent
74     */

75    public AbstractDependencyItem(Object JavaDoc name, Object JavaDoc iDependOn, ControllerState whenRequired, ControllerState dependentState)
76    {
77       this.name = name;
78       this.iDependOn = iDependOn;
79       this.whenRequired = whenRequired;
80       this.dependentState = dependentState;
81    }
82
83    public Object JavaDoc getName()
84    {
85       return name;
86    }
87
88    public Object JavaDoc getIDependOn()
89    {
90       return iDependOn;
91    }
92
93    public ControllerState getWhenRequired()
94    {
95       return whenRequired;
96    }
97
98    public ControllerState getDependentState()
99    {
100       return dependentState;
101    }
102
103    public boolean isResolved()
104    {
105       return resolved;
106    }
107
108    public boolean resolve(Controller controller)
109    {
110       boolean previous = resolved;
111       ControllerContext context;
112
113       if (dependentState == null)
114          context = controller.getInstalledContext(iDependOn);
115       else
116       {
117          context = controller.getContext(iDependOn, dependentState);
118          if (context == null)
119          {
120             if (dependentState == ControllerState.INSTALLED)
121                context = controller.getInstalledContext(iDependOn);
122          }
123       }
124
125       if (context == null)
126       {
127          resolved = false;
128          ControllerContext unresolvedContext = controller.getContext(iDependOn, null);
129          if (unresolvedContext != null && ControllerMode.ON_DEMAND.equals(unresolvedContext.getMode()))
130          {
131             try
132             {
133                controller.enableOnDemand(unresolvedContext);
134             }
135             catch (Throwable JavaDoc ignored)
136             {
137                if (log.isTraceEnabled())
138                   log.trace("Unexpected error", ignored);
139             }
140          }
141       }
142       else
143       {
144          addDependsOnMe(controller, context);
145          resolved = true;
146       }
147
148       if (previous != resolved)
149       {
150          flushJBossObjectCache();
151          if (log.isTraceEnabled())
152          {
153             if (resolved)
154                log.trace("Resolved " + this);
155             else
156                log.trace("Unresolved " + this);
157          }
158       }
159       return resolved;
160    }
161
162    public void unresolved(Controller controller)
163    {
164       if (resolved)
165       {
166          resolved = false;
167          flushJBossObjectCache();
168          log.trace("Forced unresolved " + this);
169       }
170    }
171    
172    public void toString(JBossStringBuilder buffer)
173    {
174       buffer.append("name=").append(name);
175       buffer.append(" dependsOn=").append(iDependOn);
176       if (whenRequired != null)
177          buffer.append(" whenRequired=").append(whenRequired.getStateString());
178       if (dependentState != null)
179          buffer.append(" dependentState=").append(dependentState.getStateString());
180       buffer.append(" resolved=").append(resolved);
181    }
182    
183    public void toShortString(JBossStringBuilder buffer)
184    {
185       buffer.append(name).append(" dependsOn ").append(iDependOn);
186    }
187
188    /**
189     * Register a dependency with another context
190     *
191     * @param controller the controller
192     * @param context the other context
193     */

194    protected void addDependsOnMe(Controller controller, ControllerContext context)
195    {
196       DependencyInfo info = context.getDependencyInfo();
197       if (info != null)
198          info.addDependsOnMe(this);
199    }
200
201    /**
202     * Set what I depend upon
203     *
204     * @param iDependOn what I depend upon
205     */

206    protected void setIDependOn(Object JavaDoc iDependOn)
207    {
208       this.iDependOn = iDependOn;
209       flushJBossObjectCache();
210    }
211
212    /**
213     * Set the resolved state
214     *
215     * @param resolved the new resolved state
216     */

217    protected void setResolved(boolean resolved)
218    {
219       this.resolved = resolved;
220       flushJBossObjectCache();
221    }
222 }
223
Popular Tags