tmf: Add a listener element to the analysis extension point
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Mon, 25 Jan 2016 03:16:04 +0000 (22:16 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Wed, 27 Jan 2016 01:12:56 +0000 (20:12 -0500)
This element allows to add listener classes that will be registered to
the
analysis manager.

Change-Id: I65fc1949350156ec945db6c2a48020f8aec3e9f7
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/65114
Reviewed-by: Hudson CI
Reviewed-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tested-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
tmf/org.eclipse.tracecompass.tmf.core.tests/plugin.xml
tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/analysis/AnalysisListenerTest.java [new file with mode: 0644]
tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/analysis/NewModuleListenerStub.java [new file with mode: 0644]
tmf/org.eclipse.tracecompass.tmf.core/schema/org.eclipse.linuxtools.tmf.core.analysis.exsd
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/internal/tmf/core/analysis/TmfAnalysisModuleSources.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/TmfAnalysisModuleOutputs.java

index ece4ff3c5a91fb5abd5237868b7bf82b5633f625..7953e4acc835bc3377e6f9362ee98ddfa0722c70 100644 (file)
@@ -89,6 +89,9 @@
                id="org.eclipse.linuxtools.tmf.core.tests.analysis.testParamProvider">
          </analysisId>
       </parameterProvider>
+      <listener
+            class="org.eclipse.tracecompass.tmf.tests.stubs.analysis.NewModuleListenerStub">
+      </listener>
    </extension>
    <extension
          point="org.eclipse.linuxtools.tmf.core.tracetype">
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 (file)
index 0000000..ff4a554
--- /dev/null
@@ -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 (file)
index 0000000..65297e6
--- /dev/null
@@ -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;
+    }
+
+}
index 9695d50abaa9cea64d85bd762bf23a50f7c9ca59..9ea0db22668b83c0cee4fdcc77121b5cef8d01f0 100644 (file)
@@ -22,6 +22,7 @@
             <element ref="source"/>
             <element ref="output"/>
             <element ref="parameterProvider"/>
+            <element ref="listener"/>
          </sequence>
          <attribute name="point" type="string" use="required">
             <annotation>
          <attribute name="applies_experiment" type="boolean">
             <annotation>
                <documentation>
-                  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&apos;s analysis (default false).
                </documentation>
             </annotation>
          </attribute>
       </complexType>
    </element>
 
+   <element name="listener">
+      <complexType>
+         <attribute name="class" type="string" use="required">
+            <annotation>
+               <documentation>
+                  Specify a class that will listen to new modules created. This listener will listen to the creation of any module.
+               </documentation>
+               <appinfo>
+                  <meta.attribute kind="java" basedOn=":org.eclipse.tracecompass.tmf.core.analysis.ITmfNewAnalysisModuleListener"/>
+               </appinfo>
+            </annotation>
+         </attribute>
+      </complexType>
+   </element>
+
    <annotation>
       <appinfo>
          <meta.section type="since"/>
index c1d1f57a65b4e26c971aeeb0ef918e88063d2359..9a628894fd90d9fb7fd090c0e10eb36e91445e1e 100644 (file)
@@ -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;
     }
+
 }
index bd78f5897f742eb5123a940851125e37dbfb4785..de09eedf63ece0144a3591e5214171e6e9d868cc 100644 (file)
@@ -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;
+    }
+
 }
This page took 0.031204 seconds and 5 git commands to generate.