tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/engine/TraceCompilerEngine.java
changeset 56 aa2539c91954
parent 41 838cdffd57ce
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2  * Copyright (c) 2009 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  * A singleton access point to functionality of Trace Compiler
       
    17  *
       
    18  */
       
    19 package com.nokia.tracecompiler.engine;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 
       
    23 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode;
       
    24 import com.nokia.tracecompiler.engine.event.EventEngine;
       
    25 import com.nokia.tracecompiler.engine.header.HeaderEngine;
       
    26 import com.nokia.tracecompiler.engine.plugin.PluginEngine;
       
    27 import com.nokia.tracecompiler.engine.project.ProjectEngine;
       
    28 import com.nokia.tracecompiler.engine.propertyfile.PropertyFileEngine;
       
    29 import com.nokia.tracecompiler.engine.rules.RulesEngine;
       
    30 import com.nokia.tracecompiler.engine.source.SourceEngine;
       
    31 import com.nokia.tracecompiler.engine.source.SourceListener;
       
    32 import com.nokia.tracecompiler.engine.source.SourceProperties;
       
    33 import com.nokia.tracecompiler.model.TraceCompilerException;
       
    34 import com.nokia.tracecompiler.model.TraceModel;
       
    35 import com.nokia.tracecompiler.plugin.TraceCompilerPlugin;
       
    36 
       
    37 /**
       
    38  * A singleton access point to functionality of Trace Compiler
       
    39  * 
       
    40  */
       
    41 final class TraceCompilerEngine implements SourceListener, TraceCompilerEngineInterface {
       
    42 
       
    43 	/**
       
    44 	 * TraceCompiler plug-in engine
       
    45 	 */
       
    46 	private PluginEngine pluginEngine;
       
    47 
       
    48 	/**
       
    49 	 * Event handler interface implementation
       
    50 	 */
       
    51 	private TraceCompilerEngineEvents eventEngine;
       
    52 
       
    53 	/**
       
    54 	 * Source editor engine
       
    55 	 */
       
    56 	private SourceEngine sourceEngine;
       
    57 
       
    58 	/**
       
    59 	 * Trace header engine
       
    60 	 */
       
    61 	private HeaderEngine headerEngine;
       
    62 
       
    63 	/**
       
    64 	 * Trace project engine
       
    65 	 */
       
    66 	private ProjectEngine projectEngine;
       
    67 
       
    68 	/**
       
    69 	 * Rule engine
       
    70 	 */
       
    71 	private RulesEngine rulesEngine;
       
    72 
       
    73 	/**
       
    74 	 * Trace property file engine
       
    75 	 */
       
    76 	private PropertyFileEngine propertyFileEngine;
       
    77 
       
    78 	/**
       
    79 	 * Source context manager
       
    80 	 */
       
    81 	private SourceContextManager contextManager;
       
    82 
       
    83 	/**
       
    84 	 * Location map
       
    85 	 */
       
    86 	private TraceLocationMap locationMap;
       
    87 
       
    88 	/**
       
    89 	 * Location converter
       
    90 	 */
       
    91 	private TraceLocationConverter locationConverter;
       
    92 
       
    93 	/**
       
    94 	 * Trace model
       
    95 	 */
       
    96 	private TraceModel model;
       
    97 
       
    98 	/**
       
    99 	 * Interface to the view
       
   100 	 */
       
   101 	private TraceCompilerEngineView view = new ViewAdapter();
       
   102 
       
   103 	/**
       
   104 	 * Configuration
       
   105 	 */
       
   106 	private ConfigurationDelegate configurationDelegate;
       
   107 
       
   108 	/**
       
   109 	 * List of engines that need project open / export / close notifications
       
   110 	 */
       
   111 	private ArrayList<TraceCompilerEngineBase> engines = new ArrayList<TraceCompilerEngineBase>();
       
   112 
       
   113 	/**
       
   114 	 * View has been registered flag
       
   115 	 */
       
   116 	private boolean isViewRegistered = false;
       
   117 
       
   118 	/**
       
   119 	 * Project path is stored in case the view unregisters and re-registers
       
   120 	 */
       
   121 	private String currentProjectPath;
       
   122 
       
   123 	/**
       
   124 	 * Gets the configuration of TraceCompiler
       
   125 	 * 
       
   126 	 * @return the configuration
       
   127 	 */
       
   128 	TraceCompilerEngineConfiguration getConfiguration() {
       
   129 		return configurationDelegate;
       
   130 	}
       
   131 
       
   132 	/**
       
   133 	 * Gets the trace compiler interface
       
   134 	 * 
       
   135 	 * @return this object
       
   136 	 */
       
   137 	TraceCompilerEngineInterface getTraceCompiler() {
       
   138 		return this;
       
   139 	}
       
   140 
       
   141 	/**
       
   142 	 * Gets the trace model
       
   143 	 * 
       
   144 	 * @return the trace model
       
   145 	 */
       
   146 	TraceModel getModel() {
       
   147 		return model;
       
   148 	}
       
   149 
       
   150 	/**
       
   151 	 * Gets the source engine
       
   152 	 * 
       
   153 	 * @return the source engine
       
   154 	 */
       
   155 	SourceEngine getSourceEngine() {
       
   156 		return sourceEngine;
       
   157 	}
       
   158 
       
   159 	/**
       
   160 	 * Gets the events interface
       
   161 	 * 
       
   162 	 * @return the events interface
       
   163 	 */
       
   164 	TraceCompilerEngineEvents getEvents() {
       
   165 		return eventEngine;
       
   166 	}
       
   167 
       
   168 	/**
       
   169 	 * Adds a plug-in
       
   170 	 * 
       
   171 	 * @param plugin
       
   172 	 *            the plugin
       
   173 	 */
       
   174 	void registerPlugin(TraceCompilerPlugin plugin) {
       
   175 		pluginEngine.add(plugin);
       
   176 	}
       
   177 
       
   178 	/**
       
   179 	 * Removes an existing plug-in
       
   180 	 * 
       
   181 	 * @param plugin
       
   182 	 *            the plugin
       
   183 	 */
       
   184 	void unregisterPlugin(TraceCompilerPlugin plugin) {
       
   185 		pluginEngine.remove(plugin);
       
   186 	}
       
   187 
       
   188 	/**
       
   189 	 * Gets the source context manager
       
   190 	 * 
       
   191 	 * @return the context manager
       
   192 	 */
       
   193 	public SourceContextManager getSourceContextManager() {
       
   194 		return contextManager;
       
   195 	}
       
   196 
       
   197 	/**
       
   198 	 * Starts TraceCompiler.
       
   199 	 * @throws TraceCompilerException 
       
   200 	 */
       
   201 	void start() throws TraceCompilerException {
       
   202 		configurationDelegate = new ConfigurationDelegate();
       
   203 		rulesEngine = new RulesEngine();
       
   204 		pluginEngine = new PluginEngine();
       
   205 		model = new TraceModel(rulesEngine, pluginEngine.getVerifier());
       
   206 		pluginEngine.setModel(model);
       
   207 		projectEngine = new ProjectEngine(model);
       
   208 		propertyFileEngine = new PropertyFileEngine(model);
       
   209 		eventEngine = new EventEngine(model);
       
   210 		sourceEngine = new SourceEngine(model);
       
   211 		locationMap = new TraceLocationMap(model);
       
   212 		locationConverter = new TraceLocationConverter(model);
       
   213 		sourceEngine.addSourceListener(this);
       
   214 		contextManager = new SourceContextManagerImpl();
       
   215 		headerEngine = new HeaderEngine(model);
       
   216 		// Keep this order -> Exports need to be in correct order
       
   217 		// Property file engine manages the ID cache so it needs to be run
       
   218 		// before plug-in's and header
       
   219 		engines.add(projectEngine);
       
   220 		engines.add(propertyFileEngine);
       
   221 		engines.add(headerEngine);
       
   222 		engines.add(pluginEngine);
       
   223 	}
       
   224 
       
   225 	/**
       
   226 	 * Shuts down TraceCompiler
       
   227 	 * @throws TraceCompilerException 
       
   228 	 */
       
   229 	void shutdown() throws TraceCompilerException {
       
   230 		closeProject();
       
   231 	}
       
   232 
       
   233 	/**
       
   234 	 * Registers the view
       
   235 	 * 
       
   236 	 * @param view
       
   237 	 *            the view
       
   238 	 * @throws TraceCompilerException 
       
   239 	 */
       
   240 	void setView(TraceCompilerEngineView view) throws TraceCompilerException {
       
   241 		if (view == null) {
       
   242 			// Closes the project, but leaves the currentProjectName and
       
   243 			// currentProjectPath variables. When view re-registers the project
       
   244 			// is opened back
       
   245 			internalCloseProject();
       
   246 			this.view = new ViewAdapter();
       
   247 			isViewRegistered = false;
       
   248 		} else {
       
   249 			this.view = view;
       
   250 			isViewRegistered = true;
       
   251 		}
       
   252 		configurationDelegate.setConfiguration(this.view.getConfiguration());
       
   253 	}
       
   254 
       
   255 	/**
       
   256 	 * Checks that model is valid
       
   257 	 * 
       
   258 	 * @throws TraceCompilerException
       
   259 	 *             if model is not valid
       
   260 	 */
       
   261 	private void checkIsModelValid() throws TraceCompilerException {
       
   262 		if (!model.isValid()) {
       
   263 			throw new TraceCompilerException(
       
   264 					TraceCompilerErrorCode.MODEL_NOT_READY);
       
   265 		}
       
   266 	}
       
   267 
       
   268 	/*
       
   269 	 * (non-Javadoc)
       
   270 	 * 
       
   271 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEngineInterface#openProject(java.lang.String,
       
   272 	 *      java.lang.String)
       
   273 	 */
       
   274 	public void openProject(String modelName) throws Exception {
       
   275 		try {
       
   276 			if (view != null && isViewRegistered && !model.isValid()) {
       
   277 				projectEngine.openTraceProject(currentProjectPath, modelName);
       
   278 				for (TraceCompilerEngineBase engine : engines) {
       
   279 					engine.projectOpened();
       
   280 				}
       
   281 				model.setValid(true);
       
   282 				sourceEngine.start();
       
   283 			}
       
   284 		} catch (TraceCompilerException e) {
       
   285 			closeModel();
       
   286 			throw e;
       
   287 		}
       
   288 	}
       
   289 
       
   290 	/*
       
   291 	 * (non-Javadoc)
       
   292 	 * 
       
   293 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEngineInterface#exportProject()
       
   294 	 */
       
   295 	public void exportProject() throws TraceCompilerException {
       
   296 		checkIsModelValid();
       
   297 		if (model.getID() != 0) {
       
   298 			for (TraceCompilerEngineBase engine : engines) {
       
   299 					engine.exportProject();
       
   300 			}
       
   301 		} else {
       
   302 			throw new TraceCompilerException(
       
   303 					TraceCompilerErrorCode.INVALID_MODEL_PROPERTIES_FOR_EXPORT);
       
   304 		}
       
   305 	}
       
   306 
       
   307 	/*
       
   308 	 * (non-Javadoc)
       
   309 	 * 
       
   310 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEngineInterface#closeProject()
       
   311 	 */
       
   312 	public void closeProject() throws TraceCompilerException {
       
   313 		internalCloseProject();
       
   314 		currentProjectPath = null;
       
   315 	}
       
   316 
       
   317 	/**
       
   318 	 * Closes the project
       
   319 	 * @throws TraceCompilerException 
       
   320 	 */
       
   321 	private void internalCloseProject() throws TraceCompilerException {
       
   322 		if (model.isValid()) {
       
   323 			if (!TraceCompilerEngineGlobals.getConfiguration().getFlag(
       
   324 					TraceCompilerEngineConfiguration.CONSOLE_UI_MODE)) {
       
   325 				try {
       
   326 					// Plug-in's are not exported when project is closed
       
   327 					// They can do cleanup in traceProjectClosed call
       
   328 					for (TraceCompilerEngineBase engine : engines) {
       
   329 						if (engine != pluginEngine) {
       
   330 							engine.exportProject();
       
   331 						}
       
   332 					}
       
   333 				} catch (TraceCompilerException e) {
       
   334 					getEvents().postError(e);
       
   335 						throw e;
       
   336 				}
       
   337 			}
       
   338 			// Engines are closed in reverse order
       
   339 			for (int i = engines.size() - 1; i >= 0; i--) {
       
   340 				engines.get(i).projectClosed();
       
   341 			}
       
   342 			sourceEngine.shutdown();
       
   343 			closeModel();
       
   344 		}
       
   345 	}
       
   346 
       
   347 	/**
       
   348 	 * Closes the trace model
       
   349 	 */
       
   350 	private void closeModel() {
       
   351 		try {
       
   352 			if (model != null) {
       
   353 				model.reset();
       
   354 				model.setValid(false);
       
   355 			}
       
   356 		} catch (Exception e) {
       
   357 			if (TraceCompilerEngineConfiguration.ASSERTIONS_ENABLED) {
       
   358 				getEvents().postAssertionFailed("Failed to close model", e); //$NON-NLS-1$
       
   359 			}
       
   360 		}
       
   361 	}
       
   362 
       
   363 	/*
       
   364 	 * (non-Javadoc)
       
   365 	 * 
       
   366 	 * @see com.nokia.tracecompiler.engine.SourceListener#
       
   367 	 *      sourceOpened(com.nokia.tracecompiler.engine.SourceProperties)
       
   368 	 */
       
   369 	public void sourceOpened(SourceProperties properties) throws TraceCompilerException {
       
   370 		locationMap.addSource(properties);
       
   371 		locationConverter.sourceOpened(properties);
       
   372 	}
       
   373 
       
   374 	/**
       
   375 	 * Get project path
       
   376 	 * 
       
   377 	 * @return project path
       
   378 	 */
       
   379 	public String getProjectPath() {
       
   380 		return currentProjectPath;
       
   381 	}
       
   382 
       
   383 	/**
       
   384 	 * Set project path
       
   385 	 * 
       
   386 	 * @param path
       
   387 	 *            the path
       
   388 	 */
       
   389 	public void setProjectPath(String path) {
       
   390 		currentProjectPath = path;
       
   391 	}
       
   392 
       
   393 }