00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 package cast.core;
00027
00028 import java.util.Map;
00029 import java.util.Vector;
00030 import java.util.concurrent.Semaphore;
00031
00032 import org.apache.log4j.Level;
00033
00034 import Ice.Communicator;
00035 import Ice.Current;
00036 import Ice.Identity;
00037 import Ice.ObjectAdapter;
00038 import cast.CASTException;
00039 import cast.cdl.CASTTime;
00040 import cast.cdl.COMPONENTNUMBERKEY;
00041 import cast.cdl.ComponentDescription;
00042 import cast.cdl.DEBUGKEY;
00043 import cast.cdl.LOGKEY;
00044 import cast.core.logging.ComponentLogger;
00045 import cast.core.logging.LogAdditions;
00046 import cast.interfaces.ComponentManagerPrx;
00047 import cast.interfaces.TimeServerPrx;
00048 import cast.interfaces._CASTComponentOperations;
00049
00056 public abstract class CASTComponent implements _CASTComponentOperations,
00057 Runnable {
00058
00062 private TimeServerPrx m_timeServer;
00063
00064
00065 protected Semaphore m_semaphore;
00066
00067 protected boolean m_asleep;
00068
00069 protected Object m_unlockNotification;
00070
00071 private String m_startColourEscape;
00072
00073 private boolean m_configureCalled;
00074
00075 private boolean m_startCalled;
00076
00077 private String m_componentID;
00078
00079 private ComponentManagerPrx m_manager;
00080
00081 private Thread m_runThread;
00082
00083 private Vector<Identity> m_serverIdentities;;
00084
00085
00086 private ObjectAdapter m_adapter;
00087 private Identity m_iceIdentity;
00088
00089 protected ComponentLogger m_logger;
00090
00091 private LogAdditions m_additions;
00092
00093 private Level m_logLevel = null;
00094
00095 @Deprecated
00096 protected boolean m_bDebugOutput;
00097 @Deprecated
00098 protected boolean m_bLogOutput;
00099
00106 public CASTComponent() {
00107
00108 m_semaphore = new Semaphore(1, true);
00109 m_unlockNotification = new Object();
00110 m_startCalled = false;
00111 m_configureCalled = false;
00112 m_startColourEscape = END_COLOUR_ESCAPE;
00113 m_componentID = null;
00114 m_manager = null;
00115
00116 m_logger = ComponentLogger.getLogger("cast.init");
00117 m_additions = new LogAdditions("", END_COLOUR_ESCAPE);
00118 m_bDebugOutput = false;
00119 m_bLogOutput = false;
00120 }
00121
00122 protected CASTTime getCASTTime() {
00123 assert (m_timeServer != null);
00124 CASTTime ct = m_timeServer.getCASTTime();
00125 assert (ct != null);
00126 return ct;
00127 }
00128
00129 public TimeServerPrx getTimeServer() {
00130 assert (m_timeServer != null);
00131 return m_timeServer;
00132 }
00133
00134 public ComponentManagerPrx getComponentManager() {
00135 assert (m_manager != null);
00136 return m_manager;
00137 }
00138
00144 protected Communicator getCommunicator() {
00145 assert (m_adapter != null);
00146 return m_adapter.getCommunicator();
00147 }
00148
00154 public void debug(Object _o) {
00155 m_logger.trace(_o, m_additions);
00156 }
00157
00163 public void debug(Object _o, Throwable _t) {
00164 m_logger.trace(_o, _t, m_additions);
00165 }
00166
00172 @Deprecated
00173 public String getProcessIdentifier() {
00174 return getComponentID();
00175 }
00176
00182 public String getComponentID() {
00183 return m_componentID;
00184 }
00185
00186 public String getID(Current __current) {
00187 return getComponentID();
00188 }
00189
00193 protected void waitForUnlock() {
00194
00195 synchronized (m_unlockNotification) {
00196 try {
00197 m_unlockNotification.wait();
00198 } catch (InterruptedException e) {
00199 logException(e);
00200 System.exit(1);
00201 }
00202 }
00203 }
00204
00212 public void logException(Throwable _t) {
00213 m_logger.error(_t.getLocalizedMessage(), _t, getLogAdditions());
00214 }
00215
00222 public void logException(Object _message, Throwable _t) {
00223 m_logger.error(_message, _t, getLogAdditions());
00224 }
00225
00231 public boolean isLocked() {
00232 return m_semaphore.availablePermits() == 0;
00233 }
00234
00238 public void lockComponent() {
00239 try {
00240 m_semaphore.acquire();
00241
00242
00243
00244
00245 } catch (InterruptedException e) {
00246 logException(e);
00247 }
00248 }
00249
00255 public void log(Object _o) {
00256 m_logger.debug(_o, m_additions);
00257 }
00258
00264 public void log(Object _o, Throwable _t) {
00265 m_logger.debug(_o, _t, m_additions);
00266 }
00267
00268 protected final String getStartColourEscape() {
00269 return m_startColourEscape;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00292 public void println(Object _o) {
00293 m_logger.info(_o, m_additions);
00294 }
00295
00302 public void println(Object _o, Throwable _t) {
00303 m_logger.info(_o, _t, m_additions);
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00319 protected void runComponent() {
00320 }
00321
00328 protected void sleepComponent(long _millis) {
00329 try {
00330 m_asleep = true;
00331 Thread.sleep(_millis);
00332 m_asleep = false;
00333 } catch (InterruptedException e) {
00334 logException(e);
00335 }
00336 }
00337
00341 public void unlockComponent() {
00342 m_semaphore.release();
00343 synchronized (m_unlockNotification) {
00344 m_unlockNotification.notifyAll();
00345 }
00346 }
00347
00348 private final static String END_COLOUR_ESCAPE = "\033[0m";
00349
00350 protected void startInternal() {
00351 debug("CASTComponent.startInternal()");
00352 m_startCalled = true;
00353 }
00354
00355 public final boolean isRunning() {
00356 return m_startCalled;
00357 }
00358
00359 protected void start() {
00360
00361 }
00362
00363 protected void configure(Map<String, String> _config) {
00364 }
00365
00366
00367
00368
00369
00370
00371
00372 protected void configureInternal(Map<String, String> __config) {
00373
00374 m_configureCalled = true;
00375
00376 String logString = __config.get(LOGKEY.value);
00377 if (logString != null) {
00378 if (Boolean.parseBoolean(logString)) {
00379 m_logLevel = Level.DEBUG;
00380 m_bLogOutput = true;
00381 }
00382 }
00383
00384 String debugString = __config.get(DEBUGKEY.value);
00385 if (debugString != null) {
00386 if (Boolean.parseBoolean(debugString)) {
00387 m_logLevel = Level.TRACE;
00388 m_bLogOutput = true;
00389 m_bDebugOutput = true;
00390 }
00391 }
00392
00393 String numberString = __config.get(COMPONENTNUMBERKEY.value);
00394 assert (numberString != null);
00395 int myNumber = Integer.parseInt(numberString);
00396 int printNumber = (myNumber % 7);
00397 int bold = (myNumber / 7) % 2;
00398 if (printNumber == 0) {
00399
00400 m_startColourEscape = "\033[0m";
00401 } else {
00402
00403 m_startColourEscape = "\033[3" + printNumber;
00404 if (bold != 0) {
00405 m_startColourEscape += ";1m";
00406 } else {
00407 m_startColourEscape += "m";
00408 }
00409 }
00410
00411 }
00412
00413
00414
00415
00416
00417
00418 protected void stopInternal() {
00419 try {
00420 m_runThread.join();
00421 } catch (InterruptedException e) {
00422 logException(e);
00423 }
00424
00425 synchronized (m_unlockNotification) {
00426 m_unlockNotification.notifyAll();
00427 }
00428 }
00429
00430 protected void stop() {
00431 }
00432
00433 public void beat(Current __current) {
00434 }
00435
00436 public void configure(Map<String, String> _config, Current __current) {
00437 configureInternal(_config);
00438 configureLogging();
00439 configure(_config);
00440 }
00441
00445 private void configureLogging() {
00446
00447 m_logger = null;
00448
00449
00450 m_logger = getLogger();
00451 m_additions = getLogAdditions();
00452 }
00453
00459 public LogAdditions getLogAdditions() {
00460 if (m_additions == null) {
00461 m_additions = new LogAdditions(getComponentID(),
00462 m_startColourEscape);
00463 }
00464 return m_additions;
00465 }
00466
00467 public void run(Current __current) {
00468 m_runThread = new Thread(this, getComponentID() + "-runComponent");
00469 m_runThread.start();
00470 }
00471
00472 public void setComponentManager(ComponentManagerPrx _man, Current __current) {
00473 CASTUtils.setComponentManager(_man);
00474 m_manager = _man;
00475 }
00476
00477 public void setID(String _id, Current __current) {
00478 assert (m_componentID == null);
00479 m_componentID = _id;
00480 }
00481
00482 public void start(Current __current) {
00483 startInternal();
00484 start();
00485 }
00486
00487 public void stop(Current __current) {
00488 lockComponent();
00489 m_startCalled = false;
00490 stop();
00491 stopInternal();
00492
00493
00494 unlockComponent();
00495
00496 }
00497
00501 public void run() {
00502 assert (m_startCalled);
00503 assert (m_configureCalled);
00504 try {
00505 runComponent();
00506 } catch (RuntimeException e) {
00507 m_logger.error("Caught exception from runComponent(): "
00508 + e.getMessage());
00509 logException(e);
00510 }
00511 }
00512
00517 public void setObjectAdapter(ObjectAdapter adapter) {
00518 m_adapter = adapter;
00519 }
00520
00524 public ObjectAdapter getObjectAdapter() {
00525 assert (m_adapter != null);
00526 return m_adapter;
00527 }
00528
00533 public void setIceIdentity(Identity iceIdentity) {
00534 m_iceIdentity = iceIdentity;
00535 }
00536
00540 public Identity getIceIdentity() {
00541 assert (m_iceIdentity != null);
00542 return m_iceIdentity;
00543 }
00544
00545 public void destroy(Current __current) {
00546 destroy();
00547 destroyInternal(__current);
00548 }
00549
00550 public void destroyInternal(Current __current) {
00551 if (m_serverIdentities != null) {
00552 for (Identity id : m_serverIdentities) {
00553 getObjectAdapter().remove(id);
00554 }
00555 }
00556 __current.adapter.remove(__current.id);
00557 }
00558
00562 protected void destroy() {
00563
00564 }
00565
00566 public void setTimeServer(TimeServerPrx _ts, Current __current) {
00567 m_timeServer = _ts;
00568 }
00569
00579 public <InterfaceType extends Ice.Object> void registerIceServer(
00580 Class<InterfaceType> _interface, InterfaceType _servant) {
00581 registerIceInterface(getComponentID(), CASTUtils
00582 .toServantCategory(_interface), _servant);
00583 }
00584
00594 public <InterfaceType extends Ice.Object> void registerIceInterface(
00595 String name, String category, InterfaceType _servant) {
00596 Identity id = new Identity(name, category);
00597 getObjectAdapter().add(_servant, id);
00598
00599 if (m_serverIdentities == null) {
00600 m_serverIdentities = new Vector<Identity>(1);
00601 }
00602 m_serverIdentities.add(id);
00603 }
00604
00614 public <Type extends Ice.Object, TypePrx extends Ice.ObjectPrx> TypePrx getIceServer(
00615 String _componentID, Class<Type> _cls, Class<TypePrx> _prxCls)
00616 throws CASTException {
00617
00618 ComponentDescription desc = getComponentManager()
00619 .getComponentDescription(_componentID);
00620 assert (desc != null);
00621
00622 String host = desc.hostName;
00623 return CASTUtils.getCASTIceServer(_componentID, host, desc.language,
00624 _cls, _prxCls, getCommunicator());
00625 }
00626
00633 public ComponentLogger getLogger() {
00634 if (m_logger == null) {
00635 m_logger = ComponentLogger.getLogger(getLoggerName());
00636 if (m_logLevel != null) {
00637 m_logger.setLevel(m_logLevel);
00638
00639 }
00640 }
00641 return m_logger;
00642 }
00643
00651 public ComponentLogger getLogger(String _postfix) {
00652 return ComponentLogger.getLogger(getLoggerName() + _postfix);
00653 }
00654
00661 public String getLoggerName() {
00662 return getComponentID();
00663 }
00664
00665 }