tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/engine/event/EventEngine.java
changeset 56 aa2539c91954
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 * Implements TraceCompilerEvents to forward events to the event router plug-in
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.engine.event;
       
    20 
       
    21 import com.nokia.tracecompiler.TraceCompilerGlobals;
       
    22 import com.nokia.tracecompiler.TraceCompilerLogger;
       
    23 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorMessages;
       
    24 import com.nokia.tracecompiler.engine.TraceCompilerEngineEvents;
       
    25 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
       
    26 import com.nokia.tracecompiler.model.TraceCompilerException;
       
    27 import com.nokia.tracecompiler.model.TraceModel;
       
    28 import com.nokia.tracecompiler.source.SourceConstants;
       
    29 import com.nokia.tracecompiler.source.SourceLocationBase;
       
    30 
       
    31 /**
       
    32  * Implements TraceCompilerEvents to forward events to the event router plug-in
       
    33  * 
       
    34  */
       
    35 public final class EventEngine implements TraceCompilerEngineEvents {
       
    36 
       
    37 	/**
       
    38 	 * Info event type
       
    39 	 */
       
    40 	public final static int INFO = 1; // CodForChk_Dis_Magic
       
    41 
       
    42 	/**
       
    43 	 * Warning event type
       
    44 	 */
       
    45 	public final static int WARNING = 2; // CodForChk_Dis_Magic
       
    46 
       
    47 	/**
       
    48 	 * Error event type
       
    49 	 */
       
    50 	public final static int ERROR = 3; // CodForChk_Dis_Magic
       
    51 
       
    52 	/**
       
    53 	 * Critical assertion failure event type
       
    54 	 */
       
    55 	public final static int ASSERT_CRITICAL = 4; // CodForChk_Dis_Magic
       
    56 
       
    57 	/**
       
    58 	 * Non-critical assertion failure event type
       
    59 	 */
       
    60 	public final static int ASSERT_NORMAL = 5; // CodForChk_Dis_Magic
       
    61 	
       
    62 
       
    63 	/**
       
    64 	 * indicates if an error happened for all files
       
    65 	 */
       
    66 	private boolean globalProcessingError = false;
       
    67 	
       
    68 
       
    69 
       
    70 	/**
       
    71 	 * Constructor
       
    72 	 * 
       
    73 	 * @param model
       
    74 	 *            the trace model
       
    75 	 */
       
    76 	public EventEngine(TraceModel model) {
       
    77 	}
       
    78 
       
    79 	/**
       
    80 	 * check if an error happened
       
    81 	 * 
       
    82 	 * @return the error
       
    83 	 */
       
    84 	public boolean hasErrorHappened() {
       
    85 		return globalProcessingError;
       
    86 	}
       
    87 
       
    88 	/*
       
    89 	 * (non-Javadoc)
       
    90 	 * 
       
    91 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEvents#
       
    92 	 *      postError(com.nokia.tracecompiler.engine.TraceCompilerException)
       
    93 	 */
       
    94 	public void postError(TraceCompilerException exception) {
       
    95 		String message = TraceCompilerEngineErrorMessages.getErrorMessage(exception);
       
    96 		if (message != null && message.length() > 0) {
       
    97 			postErrorMessage(message, exception.getErrorSource(), exception.isEventWantedToPost());
       
    98 		}
       
    99 	}
       
   100 
       
   101 	/*
       
   102 	 * (non-Javadoc)
       
   103 	 * 
       
   104 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEvents#postErrorMessage(java.lang.String, java.lang.Object, boolean)
       
   105 	 */
       
   106 	public void postErrorMessage(String message, Object source, boolean postEvent) {
       
   107 		
       
   108 		if (postEvent) {
       
   109 			postEvent(ERROR, message, source);
       
   110 		}
       
   111 	}
       
   112 
       
   113 	/*
       
   114 	 * (non-Javadoc)
       
   115 	 * 
       
   116 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEvents#
       
   117 	 *      postWarningMessage(java.lang.String, java.lang.Object)
       
   118 	 */
       
   119 	public void postWarningMessage(String message, Object source) {
       
   120 		postEvent(WARNING, message, source);
       
   121 	}
       
   122 
       
   123 	/*
       
   124 	 * (non-Javadoc)
       
   125 	 * 
       
   126 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEvents#
       
   127 	 *      postInfoMessage(java.lang.String, java.lang.Object)
       
   128 	 */
       
   129 	public void postInfoMessage(String message, Object source) {
       
   130 		postEvent(INFO, message, source);
       
   131 	}
       
   132 
       
   133 	/*
       
   134 	 * (non-Javadoc)
       
   135 	 * 
       
   136 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEvents#
       
   137 	 *      postAssertionFailed(java.lang.String, java.lang.Object)
       
   138 	 */
       
   139 	public void postAssertionFailed(String message, Object source) {
       
   140 		message = "Assertion failed. Reason: " //$NON-NLS-1$
       
   141 				+ message;
       
   142 		postEvent(ASSERT_NORMAL, message, source);
       
   143 	}
       
   144 
       
   145 	/*
       
   146 	 * (non-Javadoc)
       
   147 	 * 
       
   148 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEvents#
       
   149 	 *      postCriticalAssertionFailed(java.lang.String, java.lang.Object)
       
   150 	 */
       
   151 	public void postCriticalAssertionFailed(String message, Object source) throws TraceCompilerException {
       
   152 		message = "Critical assertion failure, the project has been closed. Reason: " //$NON-NLS-1$
       
   153 				+ message;
       
   154 		// Closes the trace project and shows an error message
       
   155 		TraceCompilerEngineGlobals.getTraceCompiler().closeProject();
       
   156 		postEvent(ASSERT_CRITICAL, message, source);
       
   157 	}
       
   158 
       
   159 
       
   160 	/**
       
   161 	 * Print an event to console
       
   162 	 * @param type
       
   163 	 *            event type
       
   164 	 * @param message
       
   165 	 *            event message
       
   166 	 * @param source
       
   167 	 *            event source
       
   168 	 */
       
   169 	private void postEvent(int type, String message, Object source) {
       
   170 		switch (type) {
       
   171 		case INFO:
       
   172 				if (TraceCompilerGlobals.isVebose()) {
       
   173 					printEvent(type, message, source);
       
   174 
       
   175 					// This means that the component has been processed. Let's
       
   176 					// print one space to ensure that parse_mmp.pl will print
       
   177 					// out something.
       
   178 				} else if (message.contains(
       
   179 						"Dictionary File written")) { //$NON-NLS-1$
       
   180 					System.out.print(" "); //$NON-NLS-1$
       
   181 				} else if (!message
       
   182 						.contains("No files were written")) { //$NON-NLS-1$
       
   183 					// If we get "no files written" message, let's dismiss it ->
       
   184 					// parse_mmp.pl won't print out anything
       
   185 					printEvent(type, message, source);
       
   186 				}
       
   187 			break;
       
   188 		case WARNING:
       
   189 		case ERROR:
       
   190 		case ASSERT_NORMAL:
       
   191 		case ASSERT_CRITICAL:
       
   192 			printEvent(type, message, source);
       
   193 			break;
       
   194 		}		
       
   195 	}
       
   196 
       
   197 	/**
       
   198 	 * Prints event
       
   199 	 * @param type error type
       
   200 	 * @param message
       
   201 	 *            event message
       
   202 	 * @param source
       
   203 	 *            event source
       
   204 	 */
       
   205 	private void printEvent(int type, String message, Object source) {
       
   206 		if (message == null) {
       
   207 			message = Messages.getString("EventEngine.unknowError"); //$NON-NLS-1$
       
   208 		}
       
   209 		String fullMessage = ""; //$NON-NLS-1$
       
   210 
       
   211 		
       
   212 		if (source instanceof SourceLocationBase) {
       
   213 			SourceLocationBase location = (SourceLocationBase) source;
       
   214 			fullMessage = location.getFileName() + Messages.getString("EventEngine.lineNumberPrefix") + location.getLineNumber() + SourceConstants.COLON + SourceConstants.SPACE; //$NON-NLS-1$
       
   215 		} else if (source instanceof String) {
       
   216 			message = (String) source;
       
   217 		}
       
   218 		fullMessage = fullMessage + message;
       
   219 		
       
   220 		switch (type) {
       
   221 		case INFO:
       
   222 			TraceCompilerLogger.printInfo(fullMessage);
       
   223 			break;
       
   224 		case WARNING:
       
   225 		case ASSERT_NORMAL:
       
   226 			TraceCompilerLogger.printWarning(fullMessage);
       
   227 			break;
       
   228 		case ERROR:
       
   229 		case ASSERT_CRITICAL:
       
   230 			globalProcessingError = true;
       
   231 			TraceCompilerLogger.printError(fullMessage);
       
   232 			break;
       
   233 		default:
       
   234 			break;
       
   235 		}
       
   236 	}
       
   237 	
       
   238 
       
   239 }