1 24 25 package org.objectweb.dream; 26 27 import java.util.Hashtable ; 28 import java.util.Map ; 29 30 import org.objectweb.dream.control.logger.Loggable; 31 import org.objectweb.dream.control.logger.LoggerControllerRegister; 32 import org.objectweb.dream.util.NullLogger; 33 import org.objectweb.fractal.api.Component; 34 import org.objectweb.fractal.api.NoSuchInterfaceException; 35 import org.objectweb.fractal.api.control.BindingController; 36 import org.objectweb.fractal.api.control.IllegalBindingException; 37 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 38 import org.objectweb.fractal.api.control.LifeCycleController; 39 import org.objectweb.fractal.julia.control.binding.ChainedIllegalBindingException; 40 import org.objectweb.util.monolog.api.BasicLevel; 41 import org.objectweb.util.monolog.api.Logger; 42 43 52 public abstract class AbstractComponent 53 implements 54 LifeCycleController, 55 BindingController, 56 Loggable 57 { 58 59 60 public Component weaveableC; 61 62 63 protected Logger logger = NullLogger.NULL_LOGGER; 64 65 69 protected Logger lifeCycleLogger = NullLogger.NULL_LOGGER; 70 71 75 protected Logger bindingLogger = NullLogger.NULL_LOGGER; 76 77 78 private Map bindings; 79 80 85 protected String fcState; 86 87 88 protected Component componentDesc; 89 90 91 protected boolean firstStart = true; 92 93 97 public AbstractComponent() 98 { 99 bindings = new Hashtable (); 100 fcState = LifeCycleController.STOPPED; 101 } 102 103 107 126 protected void initComponent(Component componentItf) 127 throws InitializationException 128 { 129 try 130 { 131 LoggerControllerRegister lcr = (LoggerControllerRegister) weaveableC 132 .getFcInterface("/logger-controller-register"); 133 lcr.register(null, this); 134 lcr.register("life-cycle", this); 135 lcr.register("binding", this); 136 logger.log(BasicLevel.DEBUG, "Component initialized."); 137 } 138 catch (NoSuchInterfaceException e) 139 { 140 } 142 } 143 144 149 protected void beforeFirstStart(Component componentItf) 150 throws IllegalLifeCycleException 151 { 152 153 } 154 155 159 162 public void setLogger(String name, Logger l) 163 { 164 if (name == null) 165 { 166 logger = l; 167 } 168 else if (name.equals("life-cycle")) 169 { 170 lifeCycleLogger = l; 171 } 172 else if (name.equals("binding")) 173 { 174 bindingLogger = l; 175 } 176 } 177 178 182 185 public String getFcState() 186 { 187 if (lifeCycleLogger.isLoggable(BasicLevel.DEBUG)) 188 { 189 lifeCycleLogger.log(BasicLevel.DEBUG, "getFcState: " + fcState); 190 } 191 return fcState; 192 } 193 194 197 public void stopFc() throws IllegalLifeCycleException 198 { 199 fcState = LifeCycleController.STOPPED; 200 lifeCycleLogger.log(BasicLevel.DEBUG, "stopped"); 201 } 202 203 206 public void startFc() throws IllegalLifeCycleException 207 { 208 if (firstStart) 209 { 210 beforeFirstStart(weaveableC); 211 firstStart = false; 212 } 213 fcState = LifeCycleController.STARTED; 214 lifeCycleLogger.log(BasicLevel.DEBUG, "started"); 215 } 216 217 221 224 public Object lookupFc(String clientItfName) throws NoSuchInterfaceException 225 { 226 synchronized (bindings) 227 { 228 if (bindingLogger.isLoggable(BasicLevel.DEBUG)) 229 { 230 bindingLogger.log(BasicLevel.DEBUG, "lookupFc on interface \"" 231 + clientItfName + "\""); 232 } 233 return bindings.get(clientItfName); 234 } 235 } 236 237 240 public void bindFc(String clientItfName, Object serverItf) 241 throws NoSuchInterfaceException, IllegalBindingException, 242 IllegalLifeCycleException 243 { 244 synchronized (bindings) 245 { 246 if (bindingLogger.isLoggable(BasicLevel.DEBUG)) 247 { 248 bindingLogger.log(BasicLevel.DEBUG, "bindFc on interface \"" 249 + clientItfName + "\""); 250 } 251 if (clientItfName.equals("component")) 252 { 253 weaveableC = (Component) serverItf; 254 try 255 { 256 initComponent(weaveableC); 257 } 258 catch (InitializationException e) 259 { 260 logger.log(BasicLevel.ERROR, 261 "An error occured while initializing the component", e); 262 throw new ChainedIllegalBindingException(e, weaveableC, null, 263 "component", null, 264 "An error occured while initializing component"); 265 } 266 } 267 else 268 { 269 bindings.put(clientItfName, serverItf); 270 } 271 } 272 } 273 274 277 public void unbindFc(String clientItfName) throws NoSuchInterfaceException, 278 IllegalBindingException, IllegalLifeCycleException 279 { 280 synchronized (bindings) 281 { 282 if (bindingLogger.isLoggable(BasicLevel.DEBUG)) 283 { 284 bindingLogger.log(BasicLevel.DEBUG, "unbindIf on interface \"" 285 + clientItfName + "\""); 286 } 287 bindings.remove(clientItfName); 288 } 289 } 290 } | Popular Tags |