KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.PrintWriter JavaDoc;
25 import java.io.StringWriter JavaDoc;
26
27 import org.jboss.dependency.spi.Controller;
28 import org.jboss.dependency.spi.ControllerContext;
29 import org.jboss.dependency.spi.ControllerContextActions;
30 import org.jboss.dependency.spi.ControllerMode;
31 import org.jboss.dependency.spi.ControllerState;
32 import org.jboss.dependency.spi.DependencyInfo;
33 import org.jboss.util.JBossObject;
34 import org.jboss.util.JBossStringBuilder;
35
36 /**
37  * A ControllerContext.
38  *
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 37459 $
41  */

42 public class AbstractControllerContext extends JBossObject implements ControllerContext
43 {
44    /** The name */
45    private Object JavaDoc name;
46
47    /** The target */
48    private Object JavaDoc target;
49
50    /** The controller */
51    private Controller controller;
52
53    /** The state */
54    private ControllerState state = ControllerState.ERROR;
55
56    /** The required state */
57    private ControllerState requiredState = ControllerState.NOT_INSTALLED;
58
59    /** The mdoe */
60    private ControllerMode mode = ControllerMode.AUTOMATIC;
61    
62    /** The actions */
63    private ControllerContextActions actions;
64    
65    /** The dependencies */
66    private DependencyInfo dependencies;
67
68    /** Any error */
69    private Throwable JavaDoc error;
70
71    /**
72     * Create a new AbstractControllerContext.
73     *
74     * @param name the name
75     * @param actions the actions
76     */

77    public AbstractControllerContext(Object JavaDoc name, ControllerContextActions actions)
78    {
79       this(name, actions, null, null);
80    }
81
82    /**
83     * Create a new AbstractControllerContext.
84     *
85     * @param name the name
86     * @param actions the actions
87     * @param dependencies the dependencies
88     */

89    public AbstractControllerContext(Object JavaDoc name, ControllerContextActions actions, DependencyInfo dependencies)
90    {
91       this(name, actions, dependencies, null);
92    }
93
94    /**
95     * Create a new AbstractControllerContext.
96     *
97     * @param name the name
98     * @param actions the actions
99     * @param dependencies the dependencies
100     * @param target the target
101     */

102    public AbstractControllerContext(Object JavaDoc name, ControllerContextActions actions, DependencyInfo dependencies, Object JavaDoc target)
103    {
104       if (name == null)
105          throw new IllegalArgumentException JavaDoc("Null name");
106       if (actions == null)
107          throw new IllegalArgumentException JavaDoc("Null actions");
108
109       this.name = name;
110       this.actions = actions;
111       if (dependencies == null)
112          this.dependencies = new AbstractDependencyInfo();
113       else
114         this.dependencies = dependencies;
115       this.target = target;
116    }
117
118    /**
119     * Create a new AbstractControllerContext.
120     *
121     * @param name the name
122     * @param target the target
123     */

124    public AbstractControllerContext(Object JavaDoc name, Object JavaDoc target)
125    {
126       if (name == null)
127          throw new IllegalArgumentException JavaDoc("Null name");
128
129       this.name = name;
130       this.target = target;
131    }
132    
133    public Object JavaDoc getName()
134    {
135       return name;
136    }
137
138    /**
139     * Set the name
140     *
141     * @param name the name
142     */

143    public void setName(Object JavaDoc name)
144    {
145       this.name = name;
146    }
147    
148    public ControllerState getState()
149    {
150       return state;
151    }
152    
153    public ControllerState getRequiredState()
154    {
155       return requiredState;
156    }
157    
158    public void setRequiredState(ControllerState state)
159    {
160       this.requiredState = state;
161    }
162    
163    public ControllerMode getMode()
164    {
165       return mode;
166    }
167    
168    public void setMode(ControllerMode mode)
169    {
170       this.mode = mode;
171       flushJBossObjectCache();
172    }
173
174    /**
175     * Get the controller
176     *
177     * @return the controller
178     */

179    public Controller getController()
180    {
181       return controller;
182    }
183    
184    public void setController(Controller controller)
185    {
186       this.controller = controller;
187       flushJBossObjectCache();
188    }
189
190    public DependencyInfo getDependencyInfo()
191    {
192       return dependencies;
193    }
194
195    public Object JavaDoc getTarget()
196    {
197       return target;
198    }
199
200    /**
201     * Set the target
202     *
203     * @param target the target
204     */

205    public void setTarget(Object JavaDoc target)
206    {
207       this.target = target;
208       flushJBossObjectCache();
209    }
210    
211    public Throwable JavaDoc getError()
212    {
213       return error;
214    }
215
216    public void setError(Throwable JavaDoc error)
217    {
218       this.error = error;
219       state = ControllerState.ERROR;
220       flushJBossObjectCache();
221    }
222
223    public void install(ControllerState fromState, ControllerState toState) throws Throwable JavaDoc
224    {
225       this.error = null;
226       actions.install(this, fromState, toState);
227       this.state = toState;
228       flushJBossObjectCache();
229    }
230
231    public void uninstall(ControllerState fromState, ControllerState toState)
232    {
233       this.error = null;
234       this.state = toState;
235       flushJBossObjectCache();
236       actions.uninstall(this, fromState, toState);
237    }
238
239    public void toString(JBossStringBuilder buffer)
240    {
241       buffer.append("name=").append(name);
242       buffer.append(" target=").append(target);
243       if (error != null || state.equals(ControllerState.ERROR) == false)
244          buffer.append(" state=").append(state.getStateString());
245       if (ControllerMode.AUTOMATIC.equals(mode) == false)
246       {
247          buffer.append(" mode=").append(mode.getModeString());
248          buffer.append(" requiredState=").append(requiredState.getStateString());
249       }
250       if (dependencies != null)
251          buffer.append(" depends=").append(dependencies);
252       if (error != null)
253       {
254          StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
255          PrintWriter JavaDoc writer = new PrintWriter JavaDoc(stringWriter);
256          error.printStackTrace(writer);
257          writer.flush();
258          buffer.append(" error=").append(stringWriter.getBuffer());
259       }
260    }
261
262    public void toShortString(JBossStringBuilder buffer)
263    {
264       buffer.append("name=").append(name);
265       if (error != null || state.equals(ControllerState.ERROR) == false)
266          buffer.append(" state=").append(state.getStateString());
267       if (ControllerMode.AUTOMATIC.equals(mode) == false)
268       {
269          buffer.append(" mode=").append(mode.getModeString());
270          buffer.append(" requiredState=").append(requiredState.getStateString());
271       }
272       if (error != null)
273          buffer.append(" error=").append(error.getClass().getName()).append(": ").append(error.getMessage());
274    }
275 }
276
Popular Tags