From: Geneviève Bastien Date: Mon, 25 Jan 2016 03:16:04 +0000 (-0500) Subject: tmf: Add a listener element to the analysis extension point X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=d91f8ef38753ad60c30fc38e87aac52fc7dc24df;p=deliverable%2Ftracecompass.git tmf: Add a listener element to the analysis extension point This element allows to add listener classes that will be registered to the analysis manager. Change-Id: I65fc1949350156ec945db6c2a48020f8aec3e9f7 Signed-off-by: Geneviève Bastien Reviewed-on: https://git.eclipse.org/r/65114 Reviewed-by: Hudson CI Reviewed-by: Marc-Andre Laperle Tested-by: Marc-Andre Laperle --- diff --git a/tmf/org.eclipse.tracecompass.tmf.core.tests/plugin.xml b/tmf/org.eclipse.tracecompass.tmf.core.tests/plugin.xml index ece4ff3c5a..7953e4acc8 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core.tests/plugin.xml +++ b/tmf/org.eclipse.tracecompass.tmf.core.tests/plugin.xml @@ -89,6 +89,9 @@ id="org.eclipse.linuxtools.tmf.core.tests.analysis.testParamProvider"> + + diff --git a/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/analysis/AnalysisListenerTest.java b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/analysis/AnalysisListenerTest.java new file mode 100644 index 0000000000..ff4a554db9 --- /dev/null +++ b/tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/analysis/AnalysisListenerTest.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2016 École Polytechnique de Montréal + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +package org.eclipse.tracecompass.tmf.core.tests.analysis; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisManager; +import org.eclipse.tracecompass.tmf.tests.stubs.analysis.NewModuleListenerStub; +import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis; +import org.junit.Test; + +/** + * Test the analysis listener extension point + * + * @author Geneviève Bastien + */ +public class AnalysisListenerTest { + + private static final @NonNull String MODULE_GENERIC_ID = "test.id"; + private static final @NonNull String MODULE_GENERIC_NAME = "Test analysis"; + + /** + * Test if the listener was created by using a manually created module + */ + @Test + public void testNewModuleListener() { + TestAnalysis module = new TestAnalysis(); + + module.setName(MODULE_GENERIC_NAME); + module.setId(MODULE_GENERIC_ID); + + int countBefore = NewModuleListenerStub.getModuleCount(); + TmfAnalysisManager.analysisModuleCreated(module); + /* + * The listener should have run on this module and the count increment + * by 1 + */ + assertEquals(countBefore + 1, NewModuleListenerStub.getModuleCount()); + } + +} diff --git a/tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/analysis/NewModuleListenerStub.java b/tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/analysis/NewModuleListenerStub.java new file mode 100644 index 0000000000..65297e65cc --- /dev/null +++ b/tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/analysis/NewModuleListenerStub.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2016 École Polytechnique de Montréal + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +package org.eclipse.tracecompass.tmf.tests.stubs.analysis; + +import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; +import org.eclipse.tracecompass.tmf.core.analysis.ITmfNewAnalysisModuleListener; + +/** + * A test listener for newly created modules + * + * @author Geneviève Bastien + */ +public class NewModuleListenerStub implements ITmfNewAnalysisModuleListener { + + private static int fNewModuleCount = 0; + + @Override + public void moduleCreated(IAnalysisModule module) { + fNewModuleCount++; + } + + /** + * Get the count of newly created modules + * + * @return the number of modules that were created + */ + public static int getModuleCount() { + return fNewModuleCount; + } + +} diff --git a/tmf/org.eclipse.tracecompass.tmf.core/schema/org.eclipse.linuxtools.tmf.core.analysis.exsd b/tmf/org.eclipse.tracecompass.tmf.core/schema/org.eclipse.linuxtools.tmf.core.analysis.exsd index 9695d50aba..9ea0db2266 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/schema/org.eclipse.linuxtools.tmf.core.analysis.exsd +++ b/tmf/org.eclipse.tracecompass.tmf.core/schema/org.eclipse.linuxtools.tmf.core.analysis.exsd @@ -22,6 +22,7 @@ + @@ -100,7 +101,7 @@ - If true, indicate that an instance of this analysis will also be added to an experiment containing one or more trace(s) it applies to. In this case, the analysis will be run on the full experiment and the result is more than just the aggregation of each trace's analysis (default false). + If true, indicate that an instance of this analysis will also be added to an experiment containing one or more trace(s) it applies to. In this case, the analysis will be run on the full experiment and the result is more than just the aggregation of each trace's analysis (default false). @@ -272,6 +273,21 @@ + + + + + + Specify a class that will listen to new modules created. This listener will listen to the creation of any module. + + + + + + + + + diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/analysis/TmfAnalysisModuleSources.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/analysis/TmfAnalysisModuleSources.java index c1d1f57a65..9a628894fd 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/analysis/TmfAnalysisModuleSources.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/analysis/TmfAnalysisModuleSources.java @@ -44,8 +44,8 @@ public final class TmfAnalysisModuleSources { } /** - * Return the analysis module sources advertised in the extension - * point, in iterable format. + * Return the analysis module sources advertised in the extension point, in + * iterable format. * * @return List of {@link IAnalysisModuleSource} */ @@ -71,4 +71,5 @@ public final class TmfAnalysisModuleSources { } return sources; } + } diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/TmfAnalysisModuleOutputs.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/TmfAnalysisModuleOutputs.java index bd78f5897f..de09eedf63 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/TmfAnalysisModuleOutputs.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/TmfAnalysisModuleOutputs.java @@ -55,6 +55,9 @@ public class TmfAnalysisModuleOutputs { */ public static final String MODULE_CLASS_ELEM = "analysisModuleClass"; //$NON-NLS-1$ + /** Extension point element 'listener' */ + private static final String LISTENER_ELEM = "listener"; //$NON-NLS-1$ + private TmfAnalysisModuleOutputs() { } @@ -71,29 +74,48 @@ public class TmfAnalysisModuleOutputs { IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_ANALYSIS_TYPE_ID); for (IConfigurationElement ce : config) { String elementName = ce.getName(); + ITmfNewAnalysisModuleListener listener = null; if (elementName.equals(OUTPUT_ELEM)) { - try { - IAnalysisOutput output = (IAnalysisOutput) ce.createExecutableExtension(CLASS_ATTR); - if (output == null) { - Activator.logWarning("An output could not be created"); //$NON-NLS-1$ - continue; - } - ITmfNewAnalysisModuleListener listener = null; - for (IConfigurationElement childCe : ce.getChildren()) { - if (childCe.getName().equals(ANALYSIS_ID_ELEM)) { - listener = new TmfNewAnalysisOutputListener(output, childCe.getAttribute(ID_ATTR), null); - } else if (childCe.getName().equals(MODULE_CLASS_ELEM)) { - listener = new TmfNewAnalysisOutputListener(output, null, childCe.createExecutableExtension(CLASS_ATTR).getClass().asSubclass(IAnalysisModule.class)); - } - } - if (listener != null) { - newModuleListeners.add(listener); - } - } catch (InvalidRegistryObjectException | CoreException e) { - Activator.logError("Error creating module output listener", e); //$NON-NLS-1$ - } + listener = getListenerFromOutputElement(ce); + } else if (elementName.equals(LISTENER_ELEM)) { + listener = getListenerFromListenerElement(ce); + } + if (listener != null) { + newModuleListeners.add(listener); } } return newModuleListeners; } + + private static ITmfNewAnalysisModuleListener getListenerFromOutputElement(IConfigurationElement ce) { + ITmfNewAnalysisModuleListener listener = null; + try { + IAnalysisOutput output = (IAnalysisOutput) ce.createExecutableExtension(CLASS_ATTR); + if (output == null) { + Activator.logWarning("An output could not be created"); //$NON-NLS-1$ + return listener; + } + for (IConfigurationElement childCe : ce.getChildren()) { + if (childCe.getName().equals(ANALYSIS_ID_ELEM)) { + listener = new TmfNewAnalysisOutputListener(output, childCe.getAttribute(ID_ATTR), null); + } else if (childCe.getName().equals(MODULE_CLASS_ELEM)) { + listener = new TmfNewAnalysisOutputListener(output, null, childCe.createExecutableExtension(CLASS_ATTR).getClass().asSubclass(IAnalysisModule.class)); + } + } + } catch (InvalidRegistryObjectException | CoreException e) { + Activator.logError("Error creating module output listener", e); //$NON-NLS-1$ + } + return listener; + } + + private static ITmfNewAnalysisModuleListener getListenerFromListenerElement(IConfigurationElement ce) { + ITmfNewAnalysisModuleListener listener = null; + try { + listener = (ITmfNewAnalysisModuleListener) ce.createExecutableExtension(CLASS_ATTR); + } catch (CoreException e) { + Activator.logError("Error creating new module listener", e); //$NON-NLS-1$ + } + return listener; + } + }