1 24 25 package org.objectweb.clif.supervisor.lib; 26 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.Iterator ; 30 import java.util.Observable ; 31 import java.io.Serializable ; 32 import org.objectweb.clif.datacollector.api.DataCollectorAdmin; 33 import org.objectweb.clif.storage.api.StorageAdmin; 34 import org.objectweb.clif.storage.api.AlarmEvent; 35 import org.objectweb.clif.supervisor.api.SupervisorInfo; 36 import org.objectweb.clif.supervisor.api.TestControl; 37 import org.objectweb.clif.supervisor.api.BladeState; 38 import org.objectweb.clif.server.api.BladeControl; 39 import org.objectweb.fractal.api.NoSuchInterfaceException; 40 import org.objectweb.fractal.api.Interface; 41 import org.objectweb.fractal.api.control.*; 42 import org.objectweb.util.monolog.api.Logger; 43 import org.objectweb.util.monolog.api.BasicLevel; 44 import org.objectweb.util.monolog.Monolog; 45 46 47 59 60 61 public class SupervisorImpl 62 extends 63 Observable 64 implements 65 TestControl, 66 SupervisorInfo, 67 BindingController, 68 LifeCycleController 69 { 70 static private Logger log = Monolog.getDefaultMonologFactory().getLogger(SupervisorImpl.class.toString()); 71 72 73 private Serializable currentTestId = null; 74 75 76 private Map bladesById; 77 78 79 private String fcState = LifeCycleController.STOPPED; 80 81 82 86 87 88 private Map bladesItf = new HashMap (); 89 90 91 private Map collectorsItf = new HashMap (); 92 93 94 private StorageAdmin storageItf; 95 96 97 private SupervisorInfo infoItf; 98 99 100 private String [] interfaceNamesCache = null; 101 102 103 107 108 public void startFc() 109 { 110 fcState = LifeCycleController.STARTED; 111 } 112 113 114 public void stopFc() 115 { 116 bladesById = null; 117 fcState = LifeCycleController.STOPPED; 118 } 119 120 121 public String getFcState() 122 { 123 return fcState; 124 } 125 126 127 131 132 public Object lookupFc(String clientItfName) 133 { 134 if (clientItfName.equals(StorageAdmin.STORAGE_ADMIN)) 135 { 136 return storageItf; 137 } 138 else if (clientItfName.startsWith(DataCollectorAdmin.DATA_COLLECTOR_ADMIN)) 139 { 140 return collectorsItf.get(clientItfName); 141 } 142 else if (clientItfName.startsWith(BladeControl.BLADE_CONTROL)) 143 { 144 return bladesItf.get(clientItfName); 145 } 146 else if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO)) 147 { 148 return infoItf; 149 } 150 else 151 { 152 return null; 153 } 154 } 155 156 157 public synchronized void bindFc(String clientItfName, Object serverItf) 158 { 159 if (clientItfName.equals(StorageAdmin.STORAGE_ADMIN)) 160 { 161 storageItf = (StorageAdmin) serverItf; 162 interfaceNamesCache = null; 163 } 164 else if (clientItfName.startsWith(DataCollectorAdmin.DATA_COLLECTOR_ADMIN)) 165 { 166 collectorsItf.put(clientItfName, serverItf); 167 interfaceNamesCache = null; 168 } 169 else if (clientItfName.startsWith(BladeControl.BLADE_CONTROL)) 170 { 171 bladesItf.put(clientItfName, serverItf); 172 interfaceNamesCache = null; 173 } 174 else if (clientItfName.equals(SupervisorInfo.SUPERVISOR_INFO)) 175 { 176 infoItf = (SupervisorInfo) serverItf; 177 interfaceNamesCache = null; 178 } 179 } 180 181 182 public synchronized void unbindFc(String clientItfName) 183 { 184 if (clientItfName.equals(StorageAdmin.STORAGE_ADMIN)) 185 { 186 storageItf = null; 187 interfaceNamesCache = null; 188 } 189 else if (clientItfName.startsWith(DataCollectorAdmin.DATA_COLLECTOR_ADMIN)) 190 { 191 collectorsItf.remove(clientItfName); 192 interfaceNamesCache = null; 193 } 194 else if (clientItfName.startsWith(BladeControl.BLADE_CONTROL)) 195 { 196 bladesItf.remove(clientItfName); 197 interfaceNamesCache = null; 198 } 199 } 200 201 202 public synchronized String [] listFc() 203 { 204 if (interfaceNamesCache == null) 205 { 206 int i = 0; 207 interfaceNamesCache = new String [ 208 (storageItf == null ? 0 : 1) 209 + (infoItf == null ? 0 : 1) 210 + bladesItf.size() 211 + collectorsItf.size()]; 212 if (storageItf != null) 213 { 214 interfaceNamesCache[i++] = StorageAdmin.STORAGE_ADMIN; 215 } 216 if (infoItf != null) 217 { 218 interfaceNamesCache[i++] = SupervisorInfo.SUPERVISOR_INFO; 219 } 220 i = fillInterfaceArray(i, interfaceNamesCache, bladesItf.keySet().iterator()); 221 i = fillInterfaceArray(i, interfaceNamesCache, collectorsItf.keySet().iterator()); 222 } 223 return interfaceNamesCache; 224 } 225 226 227 230 private int fillInterfaceArray(int i, String [] array, Iterator values) 231 { 232 while (values.hasNext()) 233 { 234 array[i++] = (String )values.next(); 235 } 236 return i; 237 } 238 239 240 244 245 250 public long[] getStats(String bladeId) 251 { 252 Interface blade = (Interface)bladesById.get(bladeId); 253 if (blade != null) 254 { 255 Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); 256 try 257 { 258 return ((DataCollectorAdmin)blade.getFcItfOwner().getFcInterface( 259 DataCollectorAdmin.DATA_COLLECTOR_ADMIN)).getStat(); 260 } 261 catch (NoSuchInterfaceException ex) 262 { 263 log.log(BasicLevel.ERROR, "Blade " + bladeId + " does not have a DataCollectorAdmin interface."); 264 } 265 } 266 return null; 267 } 268 269 270 public String [] getStatLabels(String bladeId) 271 { 272 Interface blade = (Interface)bladesById.get(bladeId); 273 if (blade != null) 274 { 275 try 276 { 277 return ((DataCollectorAdmin)blade.getFcItfOwner().getFcInterface( 278 DataCollectorAdmin.DATA_COLLECTOR_ADMIN)).getLabels(); 279 } 280 catch (NoSuchInterfaceException ex) 281 { 282 log.log(BasicLevel.ERROR, "Blade " + bladeId + " does not have a DataCollectorAdmin interface."); 283 } 284 } 285 return null; 286 } 287 288 289 292 public void collect() 293 { 294 storageItf.collect(); 295 } 296 297 298 301 public void init(Serializable testDef) 302 { 303 currentTestId = (Serializable )((Object [])testDef)[0]; 304 Map testPlan = (Map )((Object [])testDef)[1]; 305 storageItf.newTest( 306 currentTestId, 307 testPlan); 308 boolean firstInit = bladesById == null; 309 if (firstInit) 310 { 311 bladesById = new HashMap (bladesItf.size()); 312 } 313 try 314 { 315 Iterator iter = bladesItf.values().iterator(); 316 while (iter.hasNext()) 317 { 318 BladeControl bladeCtl = (BladeControl)iter.next(); 319 if (firstInit) 320 { 321 bladesById.put(bladeCtl.getId(), bladeCtl); 322 } 323 bladeCtl.init(currentTestId); 324 } 325 } 326 catch (Exception e) 327 { 328 log.log(BasicLevel.ERROR, "SupervisorImpl.init() Error ", e); 329 } 330 } 331 332 333 336 public void start() 337 { 338 log.log(BasicLevel.DEBUG, "SupervisorImpl.start()"); 339 Iterator it = bladesItf.values().iterator(); 340 while (it.hasNext()) 341 { 342 ((BladeControl)it.next()).start(); 343 } 344 } 345 346 347 350 public void stop() 351 { 352 log.log(BasicLevel.DEBUG, "SupervisorImpl.stop()"); 353 Iterator it = bladesItf.values().iterator(); 354 while (it.hasNext()) 355 { 356 ((BladeControl)it.next()).stop(); 357 } 358 } 359 360 361 364 public void suspend() 365 { 366 log.log(BasicLevel.DEBUG, "SupervisorImpl.suspend()"); 367 Iterator it = bladesItf.values().iterator(); 368 while (it.hasNext()) 369 { 370 ((BladeControl)it.next()).suspend(); 371 } 372 } 373 374 375 378 public void resume() 379 { 380 log.log(BasicLevel.DEBUG, "SupervisorImpl.resume()"); 381 Iterator it = bladesItf.values().iterator(); 382 while (it.hasNext()) 383 { 384 ((BladeControl) it.next()).resume(); 385 } 386 } 387 388 389 392 public void join() 393 { 394 log.log(BasicLevel.DEBUG, "SupervisorImpl.join()"); 395 Iterator it = bladesItf.values().iterator(); 396 while (it.hasNext()) 397 { 398 ((BladeControl) it.next()).join(); 399 } 400 } 401 402 403 407 408 411 public void alarm(AlarmEvent alarm) 412 { 413 setChanged(); 414 notifyObservers(alarm); 415 } 416 417 418 423 public void setBladeState(String id, BladeState state) 424 { 425 setChanged(); 426 notifyObservers(new BladeObservation(id, state)); 427 } 428 } 429 | Popular Tags |