tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/engine/plugin/PluginEngine.java
changeset 56 aa2539c91954
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2 * Copyright (c) 2007 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 * Export plugin manager, which delegates calls to plug-ins
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.engine.plugin;
       
    20 
       
    21 import java.util.ArrayList;
       
    22 import java.util.Iterator;
       
    23 
       
    24 import com.nokia.tracecompiler.engine.TraceCompilerEngineBase;
       
    25 import com.nokia.tracecompiler.engine.TraceCompilerEngineConfiguration;
       
    26 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorMessages;
       
    27 import com.nokia.tracecompiler.engine.TraceCompilerEngineGlobals;
       
    28 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode;
       
    29 import com.nokia.tracecompiler.model.TraceCompilerException;
       
    30 import com.nokia.tracecompiler.model.TraceModel;
       
    31 import com.nokia.tracecompiler.model.TraceObjectPropertyVerifier;
       
    32 import com.nokia.tracecompiler.plugin.TraceAPIPlugin;
       
    33 import com.nokia.tracecompiler.plugin.TraceCompilerExport;
       
    34 import com.nokia.tracecompiler.plugin.TraceCompilerPlugin;
       
    35 
       
    36 /**
       
    37  * Plugin engine, which delegates calls to plug-ins
       
    38  * 
       
    39  */
       
    40 public final class PluginEngine extends TraceCompilerEngineBase {
       
    41 
       
    42 	/**
       
    43 	 * List of plug-ins
       
    44 	 */
       
    45 	private ArrayList<TraceCompilerPlugin> plugins = new ArrayList<TraceCompilerPlugin>();
       
    46 
       
    47 	/**
       
    48 	 * Property verifier
       
    49 	 */
       
    50 	private PluginTracePropertyVerifier verifier = new PluginTracePropertyVerifier(
       
    51 			this);
       
    52 
       
    53 	/**
       
    54 	 * Trace model
       
    55 	 */
       
    56 	private TraceModel model;
       
    57 
       
    58 	/**
       
    59 	 * Project open flag
       
    60 	 */
       
    61 	private boolean projectOpen;
       
    62 
       
    63 	/**
       
    64 	 * Sets the trace model. This is not set in constructor, since plug-in
       
    65 	 * engine is created before the model
       
    66 	 * 
       
    67 	 * @param model
       
    68 	 *            the trace model
       
    69 	 */
       
    70 	public void setModel(TraceModel model) {
       
    71 		this.model = model;
       
    72 	}
       
    73 
       
    74 	/**
       
    75 	 * Gets the started flag
       
    76 	 * 
       
    77 	 * @return true if started, false if not
       
    78 	 */
       
    79 	public boolean isProjectOpen() {
       
    80 		return projectOpen;
       
    81 	}
       
    82 
       
    83 	/**
       
    84 	 * Adds a plugin
       
    85 	 * 
       
    86 	 * @param plugin
       
    87 	 *            the plugin to be added
       
    88 	 */
       
    89 	public void add(TraceCompilerPlugin plugin) {
       
    90 		plugins.add(plugin);
       
    91 		if (plugin instanceof TraceAPIPlugin) {
       
    92 			TraceAPIPlugin api = (TraceAPIPlugin) plugin;
       
    93 			TraceAPIPluginManager manager = model
       
    94 					.getExtension(TraceAPIPluginManager.class);
       
    95 			manager.addFormatters(api.getFormatters());
       
    96 			manager.addParsers(api.getParsers());
       
    97 		}
       
    98 	}
       
    99 
       
   100 	/**
       
   101 	 * Removes a plugin
       
   102 	 * 
       
   103 	 * @param plugin
       
   104 	 *            the plugin to be removed
       
   105 	 */
       
   106 	public void remove(TraceCompilerPlugin plugin) {
       
   107 		// Formatters / parsers are not removed. Currently this is not a
       
   108 		// problem since plug-in's are removed only on shutdown
       
   109 		plugins.remove(plugin);
       
   110 	}
       
   111 
       
   112 	/**
       
   113 	 * Gets the property verifier interface
       
   114 	 * 
       
   115 	 * @return the verifier
       
   116 	 */
       
   117 	public TraceObjectPropertyVerifier getVerifier() {
       
   118 		return verifier;
       
   119 	}
       
   120 
       
   121 	/**
       
   122 	 * Checks if there are plug-ins
       
   123 	 * 
       
   124 	 * @return true if plug-ins exist
       
   125 	 */
       
   126 	public boolean hasPlugins() {
       
   127 		return !plugins.isEmpty();
       
   128 	}
       
   129 
       
   130 	/**
       
   131 	 * Gets the plug-ins
       
   132 	 * 
       
   133 	 * @return the plug-ins
       
   134 	 */
       
   135 	Iterator<TraceCompilerPlugin> getPlugins() {
       
   136 		return plugins.iterator();
       
   137 	}
       
   138 
       
   139 	/* (non-Javadoc)
       
   140 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEngineBase#exportProject()
       
   141 	 */
       
   142 	@Override
       
   143 	public void exportProject() throws TraceCompilerException {
       
   144 		if (model.hasTraces()) {
       
   145 			for (TraceCompilerPlugin plugin : plugins) {
       
   146 				if (plugin instanceof TraceCompilerExport) {
       
   147 					try {
       
   148 						((TraceCompilerExport) plugin).exportTraceProject();
       
   149 					} catch (TraceCompilerException e) {
       
   150 						TraceCompilerEngineGlobals.getEvents().postError(e);
       
   151 							throw e;
       
   152 					}
       
   153 				}
       
   154 			}
       
   155 		} else {
       
   156 			TraceCompilerEngineGlobals.getEvents().postInfoMessage(
       
   157 					TraceCompilerEngineErrorMessages.getErrorMessage(
       
   158 							TraceCompilerErrorCode.NO_TRACES_TO_EXPORT, null),
       
   159 					null);
       
   160 		}
       
   161 	}
       
   162 
       
   163 	/*
       
   164 	 * (non-Javadoc)
       
   165 	 * 
       
   166 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEngine#projectOpened()
       
   167 	 */
       
   168 	@Override
       
   169 	public void projectOpened() {
       
   170 		if (!projectOpen) {
       
   171 			for (TraceCompilerPlugin plugin : plugins) {
       
   172 				plugin.traceProjectOpened(model);
       
   173 			}
       
   174 			projectOpen = true;
       
   175 		} else {
       
   176 			if (TraceCompilerEngineConfiguration.ASSERTIONS_ENABLED) {
       
   177 				TraceCompilerEngineGlobals.getEvents().postAssertionFailed(
       
   178 						"PluginEngine.traceProjectOpened", null); //$NON-NLS-1$
       
   179 			}
       
   180 		}
       
   181 	}
       
   182 
       
   183 	/*
       
   184 	 * (non-Javadoc)
       
   185 	 * 
       
   186 	 * @see com.nokia.tracecompiler.engine.TraceCompilerEngine#projectClosing()
       
   187 	 */
       
   188 	@Override
       
   189 	public void projectClosed() {
       
   190 		if (projectOpen) {
       
   191 			for (TraceCompilerPlugin plugin : plugins) {
       
   192 				plugin.traceProjectClosed();
       
   193 			}
       
   194 			projectOpen = false;
       
   195 		}
       
   196 	}
       
   197 
       
   198 }