1 22 package org.jboss.dependency.plugins; 23 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 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 42 public class AbstractControllerContext extends JBossObject implements ControllerContext 43 { 44 45 private Object name; 46 47 48 private Object target; 49 50 51 private Controller controller; 52 53 54 private ControllerState state = ControllerState.ERROR; 55 56 57 private ControllerState requiredState = ControllerState.NOT_INSTALLED; 58 59 60 private ControllerMode mode = ControllerMode.AUTOMATIC; 61 62 63 private ControllerContextActions actions; 64 65 66 private DependencyInfo dependencies; 67 68 69 private Throwable error; 70 71 77 public AbstractControllerContext(Object name, ControllerContextActions actions) 78 { 79 this(name, actions, null, null); 80 } 81 82 89 public AbstractControllerContext(Object name, ControllerContextActions actions, DependencyInfo dependencies) 90 { 91 this(name, actions, dependencies, null); 92 } 93 94 102 public AbstractControllerContext(Object name, ControllerContextActions actions, DependencyInfo dependencies, Object target) 103 { 104 if (name == null) 105 throw new IllegalArgumentException ("Null name"); 106 if (actions == null) 107 throw new IllegalArgumentException ("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 124 public AbstractControllerContext(Object name, Object target) 125 { 126 if (name == null) 127 throw new IllegalArgumentException ("Null name"); 128 129 this.name = name; 130 this.target = target; 131 } 132 133 public Object getName() 134 { 135 return name; 136 } 137 138 143 public void setName(Object 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 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 getTarget() 196 { 197 return target; 198 } 199 200 205 public void setTarget(Object target) 206 { 207 this.target = target; 208 flushJBossObjectCache(); 209 } 210 211 public Throwable getError() 212 { 213 return error; 214 } 215 216 public void setError(Throwable error) 217 { 218 this.error = error; 219 state = ControllerState.ERROR; 220 flushJBossObjectCache(); 221 } 222 223 public void install(ControllerState fromState, ControllerState toState) throws Throwable 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 stringWriter = new StringWriter (); 255 PrintWriter writer = new PrintWriter (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 |