|
Removed
Link Here
|
| 1 |
/********************************************************************** |
| 2 |
* Copyright (c) 2005, 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* $Id: Controller.java,v 1.9 2007/03/22 05:29:24 dnsmith Exp $ |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* IBM - Initial API and implementation |
| 11 |
* |
| 12 |
* Change History: |
| 13 |
* Bugzilla Description |
| 14 |
* 79014 Improved error handling and message logging |
| 15 |
* |
| 16 |
**********************************************************************/ |
| 17 |
package org.eclipse.hyades.logging.adapter.internal.util; |
| 18 |
|
| 19 |
import java.io.FileInputStream; |
| 20 |
import java.io.FileNotFoundException; |
| 21 |
import java.io.IOException; |
| 22 |
import java.io.InputStream; |
| 23 |
import java.util.Iterator; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 27 |
import javax.xml.parsers.ParserConfigurationException; |
| 28 |
|
| 29 |
import org.eclipse.hyades.logging.adapter.AdapterException; |
| 30 |
import org.eclipse.hyades.logging.adapter.AdapterInvalidConfig; |
| 31 |
import org.eclipse.hyades.logging.adapter.AdapterPlugin; |
| 32 |
import org.eclipse.hyades.logging.adapter.IComponent; |
| 33 |
import org.eclipse.hyades.logging.adapter.IContext; |
| 34 |
import org.eclipse.hyades.logging.adapter.IContextListener; |
| 35 |
import org.eclipse.hyades.logging.adapter.IOutputter; |
| 36 |
import org.eclipse.hyades.logging.adapter.IProcessUnit; |
| 37 |
import org.eclipse.hyades.logging.adapter.impl.AdapterContext; |
| 38 |
import org.eclipse.hyades.logging.adapter.impl.AdapterXMLConstants; |
| 39 |
import org.eclipse.hyades.logging.adapter.impl.Component; |
| 40 |
import org.eclipse.hyades.logging.adapter.impl.Context; |
| 41 |
import org.eclipse.hyades.logging.adapter.impl.Status; |
| 42 |
import org.eclipse.hyades.logging.adapter.parsers.PreparationException; |
| 43 |
import org.eclipse.hyades.logging.adapter.util.AdapterConstants; |
| 44 |
import org.eclipse.hyades.logging.adapter.util.AdapterUtilities; |
| 45 |
import org.eclipse.hyades.logging.adapter.util.Messages; |
| 46 |
import org.w3c.dom.Document; |
| 47 |
import org.w3c.dom.Element; |
| 48 |
import org.w3c.dom.Node; |
| 49 |
import org.w3c.dom.NodeList; |
| 50 |
import org.xml.sax.SAXException; |
| 51 |
/** |
| 52 |
* Controller manages a set of Context objects by setting the configurations and then starting each context. |
| 53 |
* |
| 54 |
*/ |
| 55 |
public class Controller implements Runnable { |
| 56 |
|
| 57 |
private boolean singleFileInputMode = false; |
| 58 |
|
| 59 |
/* Keep our contexts and thier associated threads within a list. It is imperative that the index |
| 60 |
* of each list be kept consistant. That is, the thread running context at index n must be in the |
| 61 |
* thread list at index n |
| 62 |
*/ |
| 63 |
private Context[] contexts; |
| 64 |
private Thread[] contextThreads; |
| 65 |
|
| 66 |
// Adapter log outputter component |
| 67 |
private IOutputter logOutputter = null; |
| 68 |
|
| 69 |
private String contextConfigurationFile = AdapterConstants.HyadesGADefaultContextConfigurationFile; |
| 70 |
private String componentConfigurationFile = AdapterConstants.HyadesGADefaultComponentConfigurationsFile; |
| 71 |
private DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
| 72 |
private InputStream inContextParams = null; |
| 73 |
private InputStream inAppParams = null; |
| 74 |
private boolean running = false; |
| 75 |
private long sleepTime = 500; |
| 76 |
|
| 77 |
// Overall logging level for the Adapter |
| 78 |
private short loggingLevel = -1; |
| 79 |
/** |
| 80 |
* Constructor for Controller. |
| 81 |
*/ |
| 82 |
public Controller() { |
| 83 |
super(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns whether the Controller is running. |
| 88 |
* @return true if Controller is running, false otherwise |
| 89 |
*/ |
| 90 |
public boolean isRunning() { |
| 91 |
return running; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Start running the controller which means starting up each of the contexts. |
| 96 |
*/ |
| 97 |
public void start() { |
| 98 |
/* RKD: We will run all of the contexts except the first one |
| 99 |
* (our internal logging context), each in its own thread. |
| 100 |
* The internal logging context was already started |
| 101 |
* when we went through the prepare stage. |
| 102 |
*/ |
| 103 |
synchronized(contexts) { |
| 104 |
for (int i = 1; i < contextThreads.length; ++i) { |
| 105 |
if (contexts[i] != null && !contexts[i].isDisabled()) { |
| 106 |
contextThreads[i]=new Thread(contexts[i]); |
| 107 |
contextThreads[i].setName(contexts[i].getName() + AdapterConstants.HyadesGA + contexts[i].getUniqueID()); |
| 108 |
contextThreads[i].setDaemon(true); |
| 109 |
contextThreads[i].start(); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
running = true; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Stop the controller which means stopping all of the contexts if they |
| 118 |
* are still running. |
| 119 |
*/ |
| 120 |
public void stop() { |
| 121 |
|
| 122 |
if (contexts != null && contextThreads != null) { |
| 123 |
synchronized(contexts) { |
| 124 |
int contextStopCount = 0; |
| 125 |
/* stop them in reverse order so that the logging context gets stopped last */ |
| 126 |
for (int i=contexts.length-1; i>0; i--) { |
| 127 |
/* bugzilla 74713 - set stopping flag instead of calling Context.stop() because |
| 128 |
* we're running in a different thread than the context. Also, only stop the |
| 129 |
* context if the context thread is still alive |
| 130 |
*/ |
| 131 |
if (contexts[i] != null && contextThreads.length > i && contextThreads[i] != null && contextThreads[i].isAlive()) { |
| 132 |
contexts[i].setStopping(true); |
| 133 |
contextStopCount++; |
| 134 |
} |
| 135 |
} |
| 136 |
/* If other contexts are stopping Then sleep before stopping |
| 137 |
* the logging context to allow the other contexts |
| 138 |
* to log messages and end. |
| 139 |
* bugzilla 91218 - only sleep if there are contexts in the |
| 140 |
* process of stopping. |
| 141 |
*/ |
| 142 |
if (contextStopCount > 0) { |
| 143 |
try { |
| 144 |
Thread.sleep(1500); |
| 145 |
} |
| 146 |
catch (InterruptedException e) { |
| 147 |
|
| 148 |
} |
| 149 |
} |
| 150 |
// Stop the logging context. |
| 151 |
if (contexts[0] != null && contextThreads.length > 0 && contextThreads[0] != null && contextThreads[0].isAlive()) { |
| 152 |
contexts[0].setStopping(true); |
| 153 |
// Wait until logging context is finished |
| 154 |
while (contextThreads[0] != null && contextThreads[0].isAlive()) { |
| 155 |
try { |
| 156 |
Thread.sleep(200); |
| 157 |
// System.out.println("Waited a fifth of a second for logging context to end"); |
| 158 |
} |
| 159 |
catch (InterruptedException e) { |
| 160 |
|
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
running = false; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* This stop method will still cause the contexts to be stopped but will |
| 171 |
* not call the context listener methods when flushing the components. |
| 172 |
*/ |
| 173 |
public void hardStop() { |
| 174 |
synchronized(contexts) { |
| 175 |
if (contexts != null && contextThreads != null) |
| 176 |
/* stop them in reverse order so that the logging context gets stopped last */ |
| 177 |
for (int i=contexts.length-1; i>=0; i--) { |
| 178 |
/* bugzilla 74713 - set hard stop flag instead of calling Context.stop() because |
| 179 |
* we're running in a different thread than the context. Also, only stop the |
| 180 |
* context if the context thread is still alive |
| 181 |
*/ |
| 182 |
if (contexts[i] != null && contextThreads.length > i && contextThreads[i] != null && contextThreads[i].isAlive()) { |
| 183 |
// Set hardStop flag to stop the context |
| 184 |
contexts[i].setHardStop(); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
running = false; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get the set of Context objects |
| 193 |
* @return the set of Context objects |
| 194 |
*/ |
| 195 |
public IContext[] getContexts() { |
| 196 |
synchronized(contexts) { |
| 197 |
// Ensure the contexts have been created |
| 198 |
if (contexts == null || contexts.length <= 1) { |
| 199 |
return null; |
| 200 |
} |
| 201 |
// Exclude the internal logging context |
| 202 |
IContext newContextSet[] = new IContext[contexts.length-1]; |
| 203 |
for (int i=1; i < contexts.length; i++) { |
| 204 |
newContextSet[i-1] = contexts[i]; |
| 205 |
} |
| 206 |
return newContextSet; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Set the name of the file containing the configurations for the |
| 212 |
* contexts. |
| 213 |
* @param config - name of the context configuation file |
| 214 |
*/ |
| 215 |
public void setContextConfigPath(String config) { |
| 216 |
contextConfigurationFile = config; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Get the name of the file containing the configurations for the contexts |
| 221 |
* @return the contextConfigurationFile |
| 222 |
*/ |
| 223 |
public String getContextConfigPath() { |
| 224 |
return contextConfigurationFile; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Set the name of the file containing the configurations for the |
| 229 |
* components of the contexts. |
| 230 |
* @param config - name of the component configuation file |
| 231 |
*/ |
| 232 |
public void setComponentConfigPath(String config) { |
| 233 |
componentConfigurationFile = config; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Get the name of the file containing the configurations for the components of the contexts. |
| 238 |
* @return the componentConfigurationFile |
| 239 |
*/ |
| 240 |
public String getComponentConfigPath() { |
| 241 |
return componentConfigurationFile; |
| 242 |
} |
| 243 |
|
| 244 |
/* |
| 245 |
* Validates an adapter file against the schema files. |
| 246 |
*/ |
| 247 |
private void validateAdapterConfigurations(InputStream inAppStream, IProcessUnit logger) throws AdapterInvalidConfig { |
| 248 |
AdapterConfigValidator validator = new AdapterConfigValidator(logger); |
| 249 |
try{ |
| 250 |
validator.validate(inAppStream); |
| 251 |
}catch(AdapterException e){ |
| 252 |
log(e.toString()); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Prepare the adapter for execution. This involves reading and validating the adapter |
| 258 |
* configuration file(s), getting the context listeners from eclipse, starting the internal |
| 259 |
* logging context, and initializing the contexts. |
| 260 |
* @param validating boolean flag to indicate this method is called as part of adapter validation |
| 261 |
*/ |
| 262 |
public void prepareAdapter(boolean validating) throws AdapterException { |
| 263 |
/* Open the context and configuration files */ |
| 264 |
try { |
| 265 |
inContextParams = new FileInputStream(contextConfigurationFile); |
| 266 |
if (singleFileInputMode) { |
| 267 |
inAppParams = new FileInputStream(contextConfigurationFile); |
| 268 |
} |
| 269 |
else { |
| 270 |
inAppParams = new FileInputStream(componentConfigurationFile); |
| 271 |
} |
| 272 |
} |
| 273 |
catch(FileNotFoundException e) { |
| 274 |
String filename=null; |
| 275 |
if(inContextParams==null) { |
| 276 |
filename=contextConfigurationFile; |
| 277 |
} |
| 278 |
else { |
| 279 |
filename=componentConfigurationFile; |
| 280 |
try { |
| 281 |
inContextParams.close(); |
| 282 |
} |
| 283 |
catch(IOException eprime) { |
| 284 |
/* We will ignore if this fails. It should be successful as we just opened the file */ |
| 285 |
} |
| 286 |
} |
| 287 |
/* We cannot open the adapter file */ |
| 288 |
throw new PreparationException(Messages.getString("HyadesGA_CBE_Adapter_Config_File_Open_ERROR_", filename)); |
| 289 |
} |
| 290 |
catch (Throwable e) { |
| 291 |
String filename=null; |
| 292 |
if(inContextParams==null) { |
| 293 |
filename=contextConfigurationFile; |
| 294 |
} |
| 295 |
else { |
| 296 |
filename=componentConfigurationFile; |
| 297 |
try { |
| 298 |
inContextParams.close(); |
| 299 |
} |
| 300 |
catch(IOException eprime) { |
| 301 |
/* We will ignore if this fails. It should be successful as we just opened the file */ |
| 302 |
} |
| 303 |
} |
| 304 |
/* We cannot open the adapter file */ |
| 305 |
throw new PreparationException(Messages.getString("HyadesGA_CBE_Adapter_Config_File_Open_ERROR_", filename),e); |
| 306 |
} |
| 307 |
|
| 308 |
// build the contexts based on configurations |
| 309 |
try { |
| 310 |
contexts = createContextsAndComponents(inContextParams); |
| 311 |
synchronized(contexts) { |
| 312 |
setComponentConfigurations(inAppParams); |
| 313 |
} |
| 314 |
|
| 315 |
/* We are done with the files. Lets close them */ |
| 316 |
try { |
| 317 |
inContextParams.close(); |
| 318 |
if(!singleFileInputMode) { |
| 319 |
inAppParams.close(); |
| 320 |
} |
| 321 |
} |
| 322 |
catch(IOException e) { |
| 323 |
/* We just had the file open. We are now closing it */ |
| 324 |
} |
| 325 |
|
| 326 |
/* Before we start the contexts locate any IContextListeners that |
| 327 |
* are registered with the plugin. This only works when we are running |
| 328 |
* as a plugin inside of Eclipse. Outside of Eclipse there is no notion |
| 329 |
* of IContextListeners. |
| 330 |
*/ |
| 331 |
if (AdapterUtilities.isWorkbench()) { |
| 332 |
try { |
| 333 |
List contextListeners = AdapterPlugin.getContextListeners(); |
| 334 |
|
| 335 |
/* If this context has a listener then set it */ |
| 336 |
if (contextListeners != null) { |
| 337 |
Iterator listenerIterator = contextListeners.iterator(); |
| 338 |
while (listenerIterator.hasNext()) { |
| 339 |
IContextListener listener = (IContextListener) listenerIterator.next(); |
| 340 |
String[] targetContexts = listener.getTargetContextUniqueIds(); |
| 341 |
for (int j = 0; j < targetContexts.length; j++) { |
| 342 |
for (int k = 0; k < contexts.length; k++) { |
| 343 |
if (contexts[k].getUniqueID().equals(targetContexts[j])) { |
| 344 |
contexts[k].setContextListener(listener); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
catch (AdapterException e) { |
| 352 |
throw e; |
| 353 |
} |
| 354 |
catch (Throwable e) { |
| 355 |
throw new AdapterException(Messages.getString("HyadesGAInitialization_GetContextListeners_Failure_ERROR_"), e); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
/* Start the logging context in its entirty before we continue with anything else */ |
| 360 |
IProcessUnit logger=startInternalLoggingContext(); |
| 361 |
|
| 362 |
/* Validate the adapter configuration file against the schema files before starting the rest of the contexts. |
| 363 |
* It would have been good to have done this above, however, we want our internal logging context running so that we |
| 364 |
* can report any errors during validation |
| 365 |
*/ |
| 366 |
inAppParams = new FileInputStream(contextConfigurationFile); |
| 367 |
validateAdapterConfigurations(inAppParams, logger); |
| 368 |
|
| 369 |
if (!singleFileInputMode) { |
| 370 |
inAppParams = new FileInputStream(componentConfigurationFile); |
| 371 |
validateAdapterConfigurations(inAppParams, logger); |
| 372 |
} |
| 373 |
|
| 374 |
// Count the number of contexts that are initialized and ready to execute |
| 375 |
int contextsReadyCount = 0; |
| 376 |
AdapterException initException = null; |
| 377 |
|
| 378 |
synchronized(contexts) { |
| 379 |
for (int i = 1; i < contexts.length; ++i) { |
| 380 |
if (contexts[i] != null) { |
| 381 |
|
| 382 |
/* Set the logger for this context to be the internal logging sensor */ |
| 383 |
contexts[i].setLogger(logger); |
| 384 |
|
| 385 |
/* Set the validating status for the context */ |
| 386 |
contexts[i].setValidating(validating); |
| 387 |
|
| 388 |
/* bugzilla 96433 |
| 389 |
* Set logging level of context and components if it needs to be globally set |
| 390 |
* and it is not being validated. |
| 391 |
*/ |
| 392 |
if (!validating && loggingLevel > -1) { |
| 393 |
contexts[i].setLoggingLevel(loggingLevel); |
| 394 |
IComponent comp[] = contexts[i].getComponents(); |
| 395 |
for (int j=0; j<comp.length; j++) { |
| 396 |
comp[j].setLoggingLevel(loggingLevel); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
// set all the context config info |
| 401 |
if (!(contexts[i].init())) { |
| 402 |
contexts[i]=null; |
| 403 |
/* Make sure we log that this context will not be started */ |
| 404 |
log(Messages.getString("HyadesGAContextFatal_ERROR_")); |
| 405 |
continue; |
| 406 |
} |
| 407 |
// set all the component config info |
| 408 |
|
| 409 |
/* Setup the configuration information for this context. At this point in time the context |
| 410 |
* may fail to initialize itself properly. If that is the case then this context should be |
| 411 |
* disabled. |
| 412 |
*/ |
| 413 |
try { |
| 414 |
contexts[i].update(); |
| 415 |
// Only increment context ready count if context is not disabled |
| 416 |
if (!contexts[i].isDisabled()) { |
| 417 |
contextsReadyCount++; |
| 418 |
} |
| 419 |
} |
| 420 |
catch(AdapterException e) { |
| 421 |
if (!validating) { |
| 422 |
contexts[i]=null; |
| 423 |
/* Make sure we log that this context will not be started */ |
| 424 |
log(Messages.getString("HyadesGAContextFatal_ERROR_")); |
| 425 |
} |
| 426 |
initException = e; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
// If there are no contexts ready to execute then quit! |
| 432 |
if (contextsReadyCount == 0) { |
| 433 |
if (initException != null) { |
| 434 |
throw initException; |
| 435 |
} |
| 436 |
else { |
| 437 |
throw new AdapterException(Messages.getString("HyadesGAContextFatal_ERROR_")); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
catch(Throwable e) { |
| 442 |
/* RKD: Our caller will need to use our exception information for now to handle the error.*/ |
| 443 |
/* Don't log anything here. Assume the caller handles the exception and |
| 444 |
* logs a message if necessary. |
| 445 |
* log(Messages.getString("HyadesGAAdapterFatal_ERROR_")); |
| 446 |
* log(e.toString()); |
| 447 |
*/ |
| 448 |
if (e instanceof AdapterException) { |
| 449 |
throw (AdapterException)e; |
| 450 |
} |
| 451 |
|
| 452 |
// Wrap non-AdapterException in an AdapterException |
| 453 |
String errorMsg = e.getMessage(); |
| 454 |
if (errorMsg == null) { |
| 455 |
errorMsg = e.toString(); |
| 456 |
} |
| 457 |
throw new AdapterException(errorMsg); |
| 458 |
} |
| 459 |
finally { |
| 460 |
|
| 461 |
/* Close our configuration file */ |
| 462 |
try { |
| 463 |
if (inAppParams != null) { |
| 464 |
inAppParams.close(); |
| 465 |
} |
| 466 |
if (inContextParams != null) { |
| 467 |
inContextParams.close(); |
| 468 |
} |
| 469 |
} |
| 470 |
catch (IOException e) { |
| 471 |
/* We couldn't close the configuration files. This should only occur if we failed to |
| 472 |
* open the file in the first place. |
| 473 |
*/ |
| 474 |
log(e.toString()); |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Prepare the adapter configuration for execution. This involves |
| 481 |
* starting the internal logging context, and initializing the contexts. |
| 482 |
* @param validating boolean flag to indicate this method is called as part of adapter validation |
| 483 |
*/ |
| 484 |
public void prepareConfiguration(boolean validating) throws AdapterException { |
| 485 |
|
| 486 |
try { |
| 487 |
/* Start the logging context in its entirty before we continue with anything else */ |
| 488 |
IProcessUnit logger=startInternalLoggingContext(); |
| 489 |
|
| 490 |
// Count the number of contexts that are initialized and ready to execute |
| 491 |
int contextsReadyCount = 0; |
| 492 |
AdapterException initException = null; |
| 493 |
|
| 494 |
synchronized(contexts) { |
| 495 |
for (int i = 1; i < contexts.length; ++i) { |
| 496 |
if (contexts[i] != null) { |
| 497 |
|
| 498 |
/* Set the logger for this context to be the internal logging sensor */ |
| 499 |
contexts[i].setLogger(logger); |
| 500 |
|
| 501 |
/* Set the validating status for the context */ |
| 502 |
contexts[i].setValidating(validating); |
| 503 |
|
| 504 |
/* bugzilla 96433 |
| 505 |
* Set logging level of context and components if it needs to be globally set |
| 506 |
* and it is not being validated. |
| 507 |
*/ |
| 508 |
if (!validating && loggingLevel > -1) { |
| 509 |
contexts[i].setLoggingLevel(loggingLevel); |
| 510 |
IComponent comp[] = contexts[i].getComponents(); |
| 511 |
for (int j=0; j<comp.length; j++) { |
| 512 |
comp[j].setLoggingLevel(loggingLevel); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
// set all the context config info |
| 517 |
if (!(contexts[i].init())) { |
| 518 |
contexts[i]=null; |
| 519 |
/* Make sure we log that this context will not be started */ |
| 520 |
log(Messages.getString("HyadesGAContextFatal_ERROR_")); |
| 521 |
continue; |
| 522 |
} |
| 523 |
// set all the component config info |
| 524 |
|
| 525 |
/* Setup the configuration information for this context. At this point in time the context |
| 526 |
* may fail to initialize itself properly. If that is the case then this context should be |
| 527 |
* disabled. |
| 528 |
*/ |
| 529 |
try { |
| 530 |
contexts[i].update(); |
| 531 |
// Only increment context ready count if context is not disabled |
| 532 |
if (!contexts[i].isDisabled()) { |
| 533 |
contextsReadyCount++; |
| 534 |
} |
| 535 |
} |
| 536 |
catch(AdapterException e) { |
| 537 |
if (!validating) { |
| 538 |
contexts[i]=null; |
| 539 |
/* Make sure we log that this context will not be started */ |
| 540 |
log(Messages.getString("HyadesGAContextFatal_ERROR_")); |
| 541 |
} |
| 542 |
initException = e; |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
// If there are no contexts ready to execute then quit! |
| 548 |
if (contextsReadyCount == 0) { |
| 549 |
if (initException != null) { |
| 550 |
throw initException; |
| 551 |
} |
| 552 |
else { |
| 553 |
throw new AdapterException(Messages.getString("HyadesGAContextFatal_ERROR_")); |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
catch(Throwable e) { |
| 558 |
/* RKD: Our caller will need to use our exception information for now to handle the error.*/ |
| 559 |
/* Don't log anything here. Assume the caller handles the exception and |
| 560 |
* logs a message if necessary. |
| 561 |
* log(Messages.getString("HyadesGAAdapterFatal_ERROR_")); |
| 562 |
* log(e.toString()); |
| 563 |
*/ |
| 564 |
if (e instanceof AdapterException) { |
| 565 |
throw (AdapterException)e; |
| 566 |
} |
| 567 |
|
| 568 |
// Wrap non-AdapterException in an AdapterException |
| 569 |
String errorMsg = e.getMessage(); |
| 570 |
if (errorMsg == null) { |
| 571 |
errorMsg = e.toString(); |
| 572 |
} |
| 573 |
throw new AdapterException(errorMsg); |
| 574 |
} |
| 575 |
|
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
* parses Context configurations and build the Context objects |
| 580 |
* as well as set teh context config for each component |
| 581 |
*/ |
| 582 |
private Context[] createContextsAndComponents(InputStream inContextStream) throws PreparationException { |
| 583 |
|
| 584 |
Document doc = null; |
| 585 |
Context[] contexts = null; |
| 586 |
try { |
| 587 |
doc = docFactory.newDocumentBuilder().parse(inContextStream); |
| 588 |
|
| 589 |
//Add the adapter logging context as default: |
| 590 |
Element loggingContext = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_CONTEXT); |
| 591 |
loggingContext.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "Context instance for the current component"); |
| 592 |
loggingContext.setAttribute(AdapterXMLConstants.HyadesGAExecutableClassAttributeName, "org.eclipse.hyades.logging.adapter.impl.AdapterContext"); |
| 593 |
loggingContext.setAttribute(AdapterXMLConstants.HyadesGAImplementationCreationDateAttributeName, "Fri Jan 09 15:27:17 EST 2004"); |
| 594 |
loggingContext.setAttribute(AdapterXMLConstants.HyadesGALoggingLevelAttributeName, "60"); |
| 595 |
loggingContext.setAttribute(AdapterXMLConstants.HyadesGANameAttributeName, AdapterXMLConstants.HyadesGAAdapterContextName); |
| 596 |
loggingContext.setAttribute(AdapterXMLConstants.HyadesGARoleAttributeName, "context"); |
| 597 |
loggingContext.setAttribute(AdapterXMLConstants.HyadesGARoleCreationDateAttributeName, "Fri Jan 09 15:27:17 EST 2004"); |
| 598 |
loggingContext.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "NB1F4ED002DA11D8A519FBE7C98C2F53"); |
| 599 |
|
| 600 |
Element loggingSensor = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_COMPONENT); |
| 601 |
loggingSensor.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "Adapter logging sensor"); |
| 602 |
loggingSensor.setAttribute(AdapterXMLConstants.HyadesGAExecutableClassAttributeName, "org.eclipse.hyades.logging.adapter.internal.util.AdapterSensor"); |
| 603 |
loggingSensor.setAttribute(AdapterXMLConstants.HyadesGAImplementationCreationDateAttributeName, "Fri Jan 09 15:27:17 EST 2004"); |
| 604 |
// bugzilla 84698 - If there is an outputter Then let WARNING or higher messages be logged. |
| 605 |
if (logOutputter != null) { |
| 606 |
loggingSensor.setAttribute(AdapterXMLConstants.HyadesGALoggingLevelAttributeName, "30"); |
| 607 |
} |
| 608 |
// Otherwise let all messages be logged |
| 609 |
else { |
| 610 |
loggingSensor.setAttribute(AdapterXMLConstants.HyadesGALoggingLevelAttributeName, "0"); |
| 611 |
} |
| 612 |
loggingSensor.setAttribute(AdapterXMLConstants.HyadesGANameAttributeName, "AdapterLogSensor"); |
| 613 |
loggingSensor.setAttribute(AdapterXMLConstants.HyadesGARoleAttributeName, "sensor"); |
| 614 |
loggingSensor.setAttribute(AdapterXMLConstants.HyadesGARoleCreationDateAttributeName, "Fri Jan 09 15:27:17 EST 2004"); |
| 615 |
loggingSensor.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "NF991E0004FF11D8930381B6A308BEB5"); |
| 616 |
|
| 617 |
loggingContext.appendChild(loggingSensor); |
| 618 |
|
| 619 |
Element loggingOutputter = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_COMPONENT); |
| 620 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "Adapter logging outputter"); |
| 621 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAImplementationCreationDateAttributeName, "Fri Jan 09 15:27:17 EST 2004"); |
| 622 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAImplementationVersionAttributeName, ""); |
| 623 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAImplementationVersionDescriptionAttributeName, ""); |
| 624 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGARoleAttributeName, "outputter"); |
| 625 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGARoleCreationDateAttributeName, "Fri Jan 09 15:27:17 EST 2004"); |
| 626 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "N5F286A002DA11D8BC799C6AF4352915"); |
| 627 |
|
| 628 |
// Add the outputter specified by the user (eg. set by StaticParserWrapper in the log import case) |
| 629 |
if (logOutputter != null) { |
| 630 |
//Set the logging level so we get Warning, Critical and Fatal error messages |
| 631 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGALoggingLevelAttributeName, "30"); |
| 632 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAExecutableClassAttributeName, logOutputter.getClass().getName()); |
| 633 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGANameAttributeName, "AdapterLogOutputter"); |
| 634 |
|
| 635 |
/************************************************************************************************* |
| 636 |
* bugzilla 79014 |
| 637 |
* Comment this out because we should not need to log to a file in the log import |
| 638 |
* case. GLA messages will be handled by the outputter set by the user. |
| 639 |
* However, we'll keep the code here in case we find it necessar to log to a file as well. |
| 640 |
* |
| 641 |
* loggingContext.appendChild(loggingOutputter); |
| 642 |
* |
| 643 |
* // Create a file outputter too |
| 644 |
* loggingOutputter = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_COMPONENT); |
| 645 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "Adapter File logging outputter"); |
| 646 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAImplementationCreationDateAttributeName, "Fri Jan 09 15:27:17 EST 2004"); |
| 647 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAImplementationVersionAttributeName, ""); |
| 648 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAImplementationVersionDescriptionAttributeName, ""); |
| 649 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGARoleAttributeName, "outputter"); |
| 650 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGARoleCreationDateAttributeName, "Fri Jan 09 15:27:17 EST 2004"); |
| 651 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "AdapterFileLoggerId"); |
| 652 |
* //Set the logging level so we get Warning, Critical and Fatal error messages |
| 653 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGALoggingLevelAttributeName, "30"); |
| 654 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAExecutableClassAttributeName, "org.eclipse.hyades.logging.adapter.internal.util.AdapterLogFileOutputter"); |
| 655 |
* loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGANameAttributeName, "AdapterLogFileOutputter"); |
| 656 |
* |
| 657 |
**************************************************************************************************/ |
| 658 |
} |
| 659 |
//Add the Eclipse Problems View outputter if in Eclipse mode. This is used when the context runs in the GLA editor. |
| 660 |
else if (AdapterUtilities.isWorkbench()) { |
| 661 |
//Set the logging level so we get any messages that are at least informational |
| 662 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGALoggingLevelAttributeName, "10"); |
| 663 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAExecutableClassAttributeName, "org.eclipse.hyades.logging.adapter.internal.util.GlaTaskOutputter"); |
| 664 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGANameAttributeName, "CBEEclipseProblemsViewOutputter"); |
| 665 |
} |
| 666 |
//Add the log file outputter if in standalone mode: |
| 667 |
else { |
| 668 |
//Set the logging level so we get Warning, Critical and Fatal error messages |
| 669 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGALoggingLevelAttributeName, "30"); |
| 670 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGAExecutableClassAttributeName, "org.eclipse.hyades.logging.adapter.internal.util.AdapterLogFileOutputter"); |
| 671 |
loggingOutputter.setAttribute(AdapterXMLConstants.HyadesGANameAttributeName, "AdapterLogFileOutputter"); |
| 672 |
} |
| 673 |
|
| 674 |
loggingContext.appendChild(loggingOutputter); |
| 675 |
|
| 676 |
NodeList contextsNodeList = doc.getElementsByTagName(AdapterXMLConstants.ELEMENT_TAG_NAME_CONTEXTS); |
| 677 |
|
| 678 |
if (contextsNodeList.getLength() == 0) { |
| 679 |
|
| 680 |
//Create a contexts element: |
| 681 |
Element contextsElement = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_CONTEXTS); |
| 682 |
|
| 683 |
/* Insert the logging context at the first of the child list of elements */ |
| 684 |
NodeList children=contextsElement.getChildNodes(); |
| 685 |
for(int i=0; i<children.getLength(); i++) { |
| 686 |
if(children.item(i).getNodeType()==Node.ELEMENT_NODE) { |
| 687 |
contextsElement.insertBefore(loggingContext, children.item(i)); |
| 688 |
break; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
doc.getDocumentElement().appendChild(contextsElement); |
| 693 |
} |
| 694 |
else { |
| 695 |
|
| 696 |
//ASSUMPTION: Only one contexts element permitted. |
| 697 |
|
| 698 |
//Retrieve the contexts element: |
| 699 |
Node contextsElement = contextsNodeList.item(0); |
| 700 |
/* Insert the logging context at the first of the child list of elements */ |
| 701 |
NodeList children=contextsElement.getChildNodes(); |
| 702 |
for(int i=0; i<children.getLength(); i++) { |
| 703 |
if(children.item(i).getNodeType()==Node.ELEMENT_NODE) { |
| 704 |
contextsElement.insertBefore(loggingContext, children.item(i)); |
| 705 |
break; |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
doc.getDocumentElement().appendChild(contextsElement); |
| 710 |
} |
| 711 |
|
| 712 |
try { |
| 713 |
NodeList contextList = doc.getElementsByTagName(AdapterXMLConstants.ELEMENT_TAG_NAME_CONTEXT); |
| 714 |
// check to see if this file contains everything or just the context config |
| 715 |
NodeList tempInstanceList = doc.getElementsByTagName(AdapterXMLConstants.ELEMENT_TAG_NAME_CONTEXTINSTANCE); |
| 716 |
if (tempInstanceList.getLength() > 0) |
| 717 |
singleFileInputMode = true; |
| 718 |
else |
| 719 |
singleFileInputMode = false; |
| 720 |
int count = contextList.getLength(); |
| 721 |
contexts = new Context[count]; |
| 722 |
int j = 0; |
| 723 |
Element element = null; |
| 724 |
Context context = null; |
| 725 |
Component component = null; |
| 726 |
|
| 727 |
for (int i = 0; i < count; ++i) { |
| 728 |
//TODO: HS need to support imbedded/nested contexts/components |
| 729 |
// Extract each root context and copy the expected attributes |
| 730 |
element = (Element) contextList.item(i); |
| 731 |
/* try and build the context. A number of things can go wrong here as we use relection to build the instance. */ |
| 732 |
context = ContextFactory.buildContext(element.getAttribute(AdapterXMLConstants.HyadesGAExecutableClassAttributeName), element.getAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName), element.getAttribute(AdapterXMLConstants.HyadesGANameAttributeName)); |
| 733 |
context.setContextConfiguration(element); |
| 734 |
|
| 735 |
// |
| 736 |
// get a list of all sub elements and assume they are components |
| 737 |
NodeList componentList = element.getElementsByTagName(AdapterXMLConstants.ELEMENT_TAG_NAME_COMPONENT); |
| 738 |
IComponent[] compArray = null; |
| 739 |
int componentCount = componentList.getLength(); |
| 740 |
compArray = new IComponent[componentCount]; |
| 741 |
for (int k = 0, l = 0; k < componentCount; k++) { |
| 742 |
// Extract each component config |
| 743 |
element = (Element) componentList.item(k); |
| 744 |
|
| 745 |
// Check if we have a log outputter object already and if so then use that |
| 746 |
if (i == 0 && k ==1 && logOutputter != null) { |
| 747 |
component = (Component)logOutputter; |
| 748 |
component.setName(element.getAttribute(AdapterXMLConstants.HyadesGANameAttributeName)); |
| 749 |
component.setUniqueID(element.getAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName)); |
| 750 |
component.setExecutableClassName(element.getAttribute(AdapterXMLConstants.HyadesGAExecutableClassAttributeName)); |
| 751 |
} |
| 752 |
else { |
| 753 |
component = ComponentFactory.buildComponent(element.getAttribute(AdapterXMLConstants.HyadesGAExecutableClassAttributeName), element.getAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName), element.getAttribute(AdapterXMLConstants.HyadesGANameAttributeName)); |
| 754 |
} |
| 755 |
component.setContextConfiguration(element); |
| 756 |
compArray[l++] = component; |
| 757 |
} |
| 758 |
// put the components in the context |
| 759 |
context.setComponents(compArray); |
| 760 |
// put this context in the controller's array |
| 761 |
contexts[j++] = context; |
| 762 |
} |
| 763 |
} |
| 764 |
catch (PreparationException e) { |
| 765 |
throw e; |
| 766 |
} |
| 767 |
catch (Exception e) { |
| 768 |
throw new PreparationException(Messages.getString("HyadesGAInitialization_ContextConfiguration_Failure_ERROR_", contextConfigurationFile), e); |
| 769 |
} |
| 770 |
} |
| 771 |
catch (SAXException e) { |
| 772 |
/* We have a problem parsing the configuration file ABORT */ |
| 773 |
throw new PreparationException(Messages.getString("HyadesGA_CBE_Adapter_Config_File_Parse_ERROR_", contextConfigurationFile), e); |
| 774 |
} |
| 775 |
catch (ParserConfigurationException e) { |
| 776 |
throw new PreparationException(Messages.getString("HyadesGA_CBE_Adapter_Config_File_Parse_ERROR_", contextConfigurationFile), e); |
| 777 |
} |
| 778 |
catch (IOException e) { |
| 779 |
throw new PreparationException(Messages.getString("HyadesGA_CBE_Adapter_Config_File_Parse_ERROR_", contextConfigurationFile), e); |
| 780 |
} |
| 781 |
return contexts; |
| 782 |
} |
| 783 |
|
| 784 |
/** |
| 785 |
* CreateContextsConfig takes in an entire component configurations file and |
| 786 |
* separates out the context instances and gives each context the sub tree for itself and |
| 787 |
* it's components. |
| 788 |
*/ |
| 789 |
private void setComponentConfigurations(InputStream inAppStream) throws AdapterInvalidConfig, PreparationException { |
| 790 |
|
| 791 |
Document doc = null; |
| 792 |
|
| 793 |
try { |
| 794 |
|
| 795 |
doc = docFactory.newDocumentBuilder().parse(inAppStream); |
| 796 |
|
| 797 |
//Add the adapter internal logging context instance as default: |
| 798 |
Element loggingSensorInstance = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_SENSOR); |
| 799 |
loggingSensorInstance.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "An adapter CBE sensor"); |
| 800 |
loggingSensorInstance.setAttribute(AdapterXMLConstants.HyadesGAmaximumBlockingAttributeName, "5"); |
| 801 |
loggingSensorInstance.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "NF991E0004FF11D8930381B6A308BEB5"); |
| 802 |
|
| 803 |
Element loggerContextInstance = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_CONTEXTINSTANCE); |
| 804 |
loggerContextInstance.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "Context instance for the current component"); |
| 805 |
/* Configure the internal logging context to run forever or until the adatper is explicitly stopped */ |
| 806 |
loggerContextInstance.setAttribute(AdapterXMLConstants.CONTEXTINSTANCE_ATTR_NAME_CONTINUOUS_OPERATION, "true"); |
| 807 |
loggerContextInstance.setAttribute(AdapterXMLConstants.CONTEXTINSTANCE_ATTR_NAME_MAXIMUM_IDLE_TIME, "0"); |
| 808 |
loggerContextInstance.setAttribute(AdapterXMLConstants.CONTEXTINSTANCE_ATTR_NAME_PAUSE_INTERVAL, "10"); |
| 809 |
loggerContextInstance.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "NB1F4ED002DA11D8A519FBE7C98C2F53"); |
| 810 |
|
| 811 |
loggerContextInstance.appendChild(loggingSensorInstance); |
| 812 |
|
| 813 |
// Configure the log outputter for the log import case |
| 814 |
if (logOutputter != null) { |
| 815 |
Element loggingOutputterInstance = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_OUTPUTTER); |
| 816 |
loggingOutputterInstance.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "Adapter log outputter"); |
| 817 |
loggingOutputterInstance.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "N5F286A002DA11D8BC799C6AF4352915"); |
| 818 |
|
| 819 |
/* This outputter is created and configured by the user so there will not be any configuration properties for it. */ |
| 820 |
|
| 821 |
loggerContextInstance.appendChild(loggingOutputterInstance); |
| 822 |
|
| 823 |
/************************************************************************************************* |
| 824 |
* bugzilla 79014 |
| 825 |
* Comment this out because we should not need to log to a file in the log import |
| 826 |
* case. GLA messages will be handled by the outputter set by the user. |
| 827 |
* However, we'll keep the code here in case we find it necessary to log to a file as well. |
| 828 |
* |
| 829 |
* // Get the directory for the GLA log file |
| 830 |
* String glaLogDirectory = "."; |
| 831 |
* try { |
| 832 |
* |
| 833 |
* // Try to get the plugin's workspace directory (e.g. <workspace>/.metadata/plugins/org.eclipse.hyades.logging.adapter) |
| 834 |
* |
| 835 |
* Class platformClass = Class.forName("org.eclipse.core.runtime.Platform"); |
| 836 |
* |
| 837 |
* Method getPluginStateLocationMethod = platformClass.getMethod("getPluginStateLocation",new Class[]{Class.forName("org.eclipse.core.runtime.Plugin")}); |
| 838 |
* |
| 839 |
* Object iPathObject = getPluginStateLocationMethod.invoke(null,new Object[]{AdapterPlugin.getPlugin()}); |
| 840 |
* |
| 841 |
* Class iPathClass = iPathObject.getClass(); |
| 842 |
* |
| 843 |
* Method toOSStringMethod = iPathClass.getMethod("toOSString",null); |
| 844 |
* |
| 845 |
* glaLogDirectory = (String)(toOSStringMethod.invoke(iPathObject,null)); |
| 846 |
* } |
| 847 |
* catch (Exception e) { |
| 848 |
* // Ignore this and use the current directory for the GLA log file |
| 849 |
* } |
| 850 |
* |
| 851 |
* // Add the file logger component configuration |
| 852 |
* loggingOutputterInstance = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_OUTPUTTER); |
| 853 |
* loggingOutputterInstance.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "Single file outputter"); |
| 854 |
* loggingOutputterInstance.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "AdapterFileLoggerId"); |
| 855 |
* |
| 856 |
* Element outputterProperty = doc.createElement(AdapterXMLConstants.HyadesGAPropertyElementTagName); |
| 857 |
* outputterProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyValueAttributeName, glaLogDirectory); |
| 858 |
* outputterProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyNameAttributeName,AdapterXMLConstants.HyadesGAdirectoryAttributeName); |
| 859 |
* loggingOutputterInstance.appendChild(outputterProperty); |
| 860 |
* |
| 861 |
* outputterProperty = doc.createElement(AdapterXMLConstants.HyadesGAPropertyElementTagName); |
| 862 |
* outputterProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyValueAttributeName, "hgla.log"); |
| 863 |
* outputterProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyNameAttributeName,AdapterXMLConstants.HyadesGAfileNameAttributeName); |
| 864 |
* loggingOutputterInstance.appendChild(outputterProperty); |
| 865 |
* |
| 866 |
* loggerContextInstance.appendChild(loggingOutputterInstance); |
| 867 |
* |
| 868 |
************************************************************************************/ |
| 869 |
} |
| 870 |
// Configure the Problems View outputter for the GLA editor case |
| 871 |
else if (AdapterUtilities.isWorkbench()) { |
| 872 |
|
| 873 |
Element loggingOutputterInstance = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_OUTPUTTER); |
| 874 |
loggingOutputterInstance.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "Eclipse error dialog outputter"); |
| 875 |
loggingOutputterInstance.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "N5F286A002DA11D8BC799C6AF4352915"); |
| 876 |
|
| 877 |
/* RKD: We need to inform the outputter of the configuration path so it can create markers for the resource */ |
| 878 |
Element resourcePathProperty=doc.createElement(AdapterXMLConstants.HyadesGAPropertyElementTagName); |
| 879 |
resourcePathProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyNameAttributeName, "resourceName"); |
| 880 |
resourcePathProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyValueAttributeName, contextConfigurationFile); |
| 881 |
|
| 882 |
loggingOutputterInstance.appendChild(resourcePathProperty); |
| 883 |
loggerContextInstance.appendChild(loggingOutputterInstance); |
| 884 |
} |
| 885 |
else{ |
| 886 |
|
| 887 |
Element loggingOutputterInstance = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_OUTPUTTER); |
| 888 |
loggingOutputterInstance.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "Single file outputter"); |
| 889 |
loggingOutputterInstance.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "N5F286A002DA11D8BC799C6AF4352915"); |
| 890 |
|
| 891 |
Element outputterProperty = doc.createElement(AdapterXMLConstants.HyadesGAPropertyElementTagName); |
| 892 |
outputterProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyValueAttributeName, "."); |
| 893 |
outputterProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyNameAttributeName,AdapterXMLConstants.HyadesGAdirectoryAttributeName); |
| 894 |
loggingOutputterInstance.appendChild(outputterProperty); |
| 895 |
|
| 896 |
outputterProperty = doc.createElement(AdapterXMLConstants.HyadesGAPropertyElementTagName); |
| 897 |
outputterProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyValueAttributeName, "hgla.log"); |
| 898 |
outputterProperty.setAttribute(AdapterXMLConstants.HyadesGAPropertyNameAttributeName,AdapterXMLConstants.HyadesGAfileNameAttributeName); |
| 899 |
loggingOutputterInstance.appendChild(outputterProperty); |
| 900 |
|
| 901 |
loggerContextInstance.appendChild(loggingOutputterInstance); |
| 902 |
} |
| 903 |
|
| 904 |
NodeList configurationNodeList = doc.getElementsByTagName(AdapterXMLConstants.ELEMENT_TAG_NAME_CONFIGURATION); |
| 905 |
|
| 906 |
if (configurationNodeList.getLength() == 0) { |
| 907 |
|
| 908 |
//Create a configuration element: |
| 909 |
Element loggerConfigurationInstance = doc.createElement(AdapterXMLConstants.ELEMENT_TAG_NAME_CONFIGURATION); |
| 910 |
loggerConfigurationInstance.setAttribute(AdapterXMLConstants.HyadesGADescriptionAttributeName, "The component level configurations for this adapter"); |
| 911 |
loggerConfigurationInstance.setAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName, "N06FBD3004FF11D8BCF4CFA9EA8F31E7"); |
| 912 |
|
| 913 |
loggerConfigurationInstance.appendChild(loggerContextInstance); |
| 914 |
|
| 915 |
doc.getDocumentElement().appendChild(loggerConfigurationInstance); |
| 916 |
} |
| 917 |
else { |
| 918 |
|
| 919 |
//ASSUMPTION: Only one configuration element permitted. |
| 920 |
|
| 921 |
//Retrieve the configuration element: |
| 922 |
Node loggerConfigurationInstance = configurationNodeList.item(0); |
| 923 |
loggerConfigurationInstance.appendChild(loggerContextInstance); |
| 924 |
|
| 925 |
doc.getDocumentElement().appendChild(loggerConfigurationInstance); |
| 926 |
} |
| 927 |
|
| 928 |
try { |
| 929 |
// get a list of all the context instances and associates each with a context |
| 930 |
NodeList contextInstanceList = doc.getElementsByTagName(AdapterXMLConstants.ELEMENT_TAG_NAME_CONTEXTINSTANCE); |
| 931 |
|
| 932 |
int contextInstanceCount = contextInstanceList.getLength(); |
| 933 |
for (int i = 0; i < contextInstanceCount; ++i) { |
| 934 |
try { |
| 935 |
//find the matching Context and Context Instance |
| 936 |
Element contextElement = (Element) contextInstanceList.item(i); |
| 937 |
String contextInstanceID = contextElement.getAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName); |
| 938 |
|
| 939 |
int contextCount = contexts.length; |
| 940 |
/* Throw an exception if we have a mismatch in the number of Context's and |
| 941 |
* the number of ContextInstance's. |
| 942 |
*/ |
| 943 |
if (contextInstanceCount != contextCount) { |
| 944 |
throw new AdapterInvalidConfig(Messages.getString("HyadesGAContextConfigurationErrorContextCountMismatchFatal_ERROR_")); |
| 945 |
} |
| 946 |
|
| 947 |
boolean contextFound = false; |
| 948 |
for (int j = 0; j < contextCount; j++) { |
| 949 |
if (contexts[j].getUniqueID().equals(contextInstanceID)) { |
| 950 |
contexts[j].setConfiguration(contextElement); |
| 951 |
contextFound = true; |
| 952 |
// so now we have a good context which may or may not have components |
| 953 |
//assume each child of the contextInstance is a component |
| 954 |
NodeList componentInstanceList = contextElement.getChildNodes(); |
| 955 |
int numberOfComponentInstances = componentInstanceList.getLength(); |
| 956 |
// get the components from the context |
| 957 |
IComponent components[] = contexts[j].getComponents(); |
| 958 |
int componentCount = components.length; |
| 959 |
|
| 960 |
int componentInstanceCount = 0; |
| 961 |
for (int k = 0; k < numberOfComponentInstances; k++) { |
| 962 |
if (componentInstanceList.item(k).getNodeType() == Node.ELEMENT_NODE) { |
| 963 |
Element componentElement = (Element) componentInstanceList.item(k); |
| 964 |
String componentInstanceID = componentElement.getAttribute(AdapterXMLConstants.HyadesGAUniqueIDAttributeName); |
| 965 |
boolean componentFound = false; |
| 966 |
componentInstanceCount++; |
| 967 |
// walk through the components of the context and set the config |
| 968 |
for (int l = 0; l < componentCount; l++) { |
| 969 |
if (components[l].getUniqueID().equals(componentInstanceID)) { |
| 970 |
components[l].setConfiguration(componentElement); |
| 971 |
componentFound = true; |
| 972 |
/* bugzilla 82193 |
| 973 |
* If this is a formatter and we are running in the GLA editor |
| 974 |
*/ |
| 975 |
if (componentElement.getNodeName().equals(AdapterXMLConstants.ELEMENT_TAG_NAME_FORMATTER) && |
| 976 |
logOutputter == null && AdapterUtilities.isWorkbench()) { |
| 977 |
/* Add the test attribute to the formatter element to indicate we are running |
| 978 |
* in the GLA editor environment. |
| 979 |
*/ |
| 980 |
componentElement.setAttribute(AdapterConstants.AttrubuteName_Test,AdapterConstants.AttrubuteValue_Test_True); |
| 981 |
} |
| 982 |
break; |
| 983 |
} |
| 984 |
} |
| 985 |
if (!componentFound) { |
| 986 |
throw new AdapterInvalidConfig(Messages.getString("HyadesGAContextInstanceConfigurationErrorComponentIdNotFoundFatal_ERROR_",componentInstanceID , contexts[j].getUniqueID())); |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
/* Throw an exception if we have a mismatch in number of components between |
| 991 |
* the context and the contextInstance. |
| 992 |
*/ |
| 993 |
if (componentInstanceCount != componentCount) { |
| 994 |
throw new AdapterInvalidConfig(Messages.getString("HyadesGAContextInstanceConfigurationErrorComponentMismatchFatal_ERROR_",contexts[j].getUniqueID())); |
| 995 |
} |
| 996 |
break; |
| 997 |
} |
| 998 |
} |
| 999 |
if (!contextFound) { |
| 1000 |
throw new AdapterInvalidConfig(Messages.getString("HyadesGAContextInstanceConfigurationErrorContextIdNotFoundFatal_ERROR_", contextInstanceID)); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
catch (AdapterInvalidConfig e) { |
| 1004 |
throw e; |
| 1005 |
} |
| 1006 |
catch (Exception e) { |
| 1007 |
throw new PreparationException(Messages.getString("HyadesGAInitialization_ContextInstanceConfiguration_Failure_ERROR_", componentConfigurationFile), e); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
} |
| 1011 |
catch (AdapterInvalidConfig e) { |
| 1012 |
throw e; |
| 1013 |
} |
| 1014 |
catch (Exception e) { |
| 1015 |
throw new PreparationException(Messages.getString("HyadesGAInitialization_ContextInstanceConfiguration_Failure_ERROR_", componentConfigurationFile), e); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
catch (SAXException e) { |
| 1019 |
/* We have a problem parsing the configuration file ABORT */ |
| 1020 |
throw new PreparationException(Messages.getString("HyadesGA_CBE_Adapter_Config_File_Parse_ERROR_", componentConfigurationFile), e); |
| 1021 |
} |
| 1022 |
catch (ParserConfigurationException e) { |
| 1023 |
throw new PreparationException(Messages.getString("HyadesGA_CBE_Adapter_Config_File_Parse_ERROR_", componentConfigurationFile), e); |
| 1024 |
} |
| 1025 |
catch (IOException e) { |
| 1026 |
throw new PreparationException(Messages.getString("HyadesGA_CBE_Adapter_Config_File_Parse_ERROR_", componentConfigurationFile), e); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
/** |
| 1030 |
* run provides the basic loop for a Controller thread. It starts the Controller thread and |
| 1031 |
* then monitors it. |
| 1032 |
*/ |
| 1033 |
public void run() { |
| 1034 |
start(); |
| 1035 |
while (isRunning()) { |
| 1036 |
synchronized (this) { |
| 1037 |
try { |
| 1038 |
wait(sleepTime); |
| 1039 |
} |
| 1040 |
catch (Exception e) { |
| 1041 |
log(e.toString()); |
| 1042 |
} |
| 1043 |
if (areContextsDone()) { |
| 1044 |
// Stop the Adapter logging context |
| 1045 |
stop(); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
} |
| 1049 |
} |
| 1050 |
/** |
| 1051 |
* return false if any context is still alive, otherwise return true |
| 1052 |
*/ |
| 1053 |
private boolean areContextsDone() { |
| 1054 |
if (contextThreads != null) { |
| 1055 |
/* Ignore the internal logging context. hense we start at index 1 */ |
| 1056 |
for (int i = 1; i < contextThreads.length; i++) { |
| 1057 |
if (contextThreads[i] != null) { |
| 1058 |
if (contextThreads[i].isAlive() && !(contexts[i] instanceof AdapterContext)) |
| 1059 |
return false; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
} |
| 1063 |
return true; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/** |
| 1067 |
* clean a controller by nulling out everything |
| 1068 |
*/ |
| 1069 |
private void clean() { |
| 1070 |
if (contexts != null) { |
| 1071 |
for (int i = 0; i < contexts.length; i++) { |
| 1072 |
contexts[i] = null; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
contexts = null; |
| 1076 |
// hashContextConfig.clear(); |
| 1077 |
} |
| 1078 |
|
| 1079 |
/** |
| 1080 |
* Starts the internal logging context. |
| 1081 |
* |
| 1082 |
*/ |
| 1083 |
private IProcessUnit startInternalLoggingContext() { |
| 1084 |
contexts[0].init(); |
| 1085 |
contextThreads=new Thread[contexts.length]; |
| 1086 |
try { |
| 1087 |
contexts[0].update(); |
| 1088 |
// bugzilla 96433 - Reset the logging context status |
| 1089 |
contexts[0].setStopping(false); |
| 1090 |
contextThreads[0]=new Thread(contexts[0]); |
| 1091 |
contextThreads[0].setName(contexts[0].getName() + AdapterConstants.HyadesGA + contexts[0].getUniqueID()); |
| 1092 |
contextThreads[0].setDaemon(true); |
| 1093 |
contextThreads[0].start(); |
| 1094 |
return (IProcessUnit)contexts[0].getComponents()[0]; |
| 1095 |
|
| 1096 |
} |
| 1097 |
catch(AdapterException e) { |
| 1098 |
/* Since we are generating the context content we should not have configuration problems */ |
| 1099 |
/* However, we may get an exception here if the GLA logging cannot be initialized. */ |
| 1100 |
log(Messages.getString("HyadesGAInitialization_Internal_Logging_Not_Started_WARN_", e.getLocalizedMessage())); |
| 1101 |
} |
| 1102 |
catch(Exception e) { |
| 1103 |
/* Catch any other kind of exception and log a message */ |
| 1104 |
log(Messages.getString("HyadesGAInitialization_Internal_Logging_Not_Started_WARN_", e.toString())); |
| 1105 |
} |
| 1106 |
return null; |
| 1107 |
} |
| 1108 |
|
| 1109 |
/** |
| 1110 |
* Log the string message to standard error since the logging outputter(s) |
| 1111 |
* associated with the <code>AdapterSensor</code> logger will never be |
| 1112 |
* started. |
| 1113 |
* |
| 1114 |
* @param logRecord The string message to be logged to standard error. |
| 1115 |
*/ |
| 1116 |
public void log(String logRecord) { |
| 1117 |
if(!AdapterUtilities.isWorkbench()) { |
| 1118 |
System.err.println(logRecord); |
| 1119 |
} |
| 1120 |
|
| 1121 |
} |
| 1122 |
|
| 1123 |
/** |
| 1124 |
* Get the status of the contexts |
| 1125 |
* @return the array of Status objects representing the status of the contexts |
| 1126 |
*/ |
| 1127 |
public Status [] getStatus() { |
| 1128 |
Status [] statuses = null; |
| 1129 |
|
| 1130 |
/* If there are context then get their status */ |
| 1131 |
if (contexts != null) { |
| 1132 |
synchronized(contexts) { |
| 1133 |
statuses = new Status[contexts.length]; |
| 1134 |
/* get the status for each context */ |
| 1135 |
for (int i=0; i < contexts.length; i++) { |
| 1136 |
if (contexts[i] != null) { |
| 1137 |
statuses[i] = contexts[i].getContextStatus(); |
| 1138 |
} |
| 1139 |
else { |
| 1140 |
statuses[i] = null; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
return statuses; |
| 1147 |
} |
| 1148 |
|
| 1149 |
/** |
| 1150 |
* Get the custom Outputter that the internal logging context is using. |
| 1151 |
* @return Returns the custom Outputter that the internal logging context is using. |
| 1152 |
*/ |
| 1153 |
public IOutputter getLogOutputter() { |
| 1154 |
return logOutputter; |
| 1155 |
} |
| 1156 |
/** |
| 1157 |
* Sets the custom Outputter for the internal logging context to use. |
| 1158 |
* @param logOutputter The custom Outputter for the internal logging context. |
| 1159 |
*/ |
| 1160 |
public void setLogOutputter(IOutputter logOutputter) { |
| 1161 |
this.logOutputter = logOutputter; |
| 1162 |
} |
| 1163 |
|
| 1164 |
/** |
| 1165 |
* Sets the loggingLevel of the Adapter object. This value will override |
| 1166 |
* all values in the adapter configuration. The logging level value specified will |
| 1167 |
* take effect the next time the Adapter is started. If this method is |
| 1168 |
* not called or is called with a value of -1, the logging levels specified in the |
| 1169 |
* adapter configuration will be used. |
| 1170 |
* @param newLevel The level of logging that the Adapter will use during execution |
| 1171 |
*/ |
| 1172 |
public void setLoggingLevel(short newLevel) |
| 1173 |
{ |
| 1174 |
loggingLevel = newLevel; |
| 1175 |
} |
| 1176 |
|
| 1177 |
/** |
| 1178 |
* Get the logging level of the Adapter object. |
| 1179 |
* @return the level of logging the Adapter is currently using |
| 1180 |
*/ |
| 1181 |
public short getLoggingLevel() { |
| 1182 |
return loggingLevel; |
| 1183 |
} |
| 1184 |
} |