diff --git a/analyses/org.eclipse.tracecompass.incubator.inandout.core/src/org/eclipse/tracecompass/incubator/internal/inandout/core/analysis/InAndOutAnalysisModule.java b/analyses/org.eclipse.tracecompass.incubator.inandout.core/src/org/eclipse/tracecompass/incubator/internal/inandout/core/analysis/InAndOutAnalysisModule.java index 399bd5109..a11c59b75 100644 --- a/analyses/org.eclipse.tracecompass.incubator.inandout.core/src/org/eclipse/tracecompass/incubator/internal/inandout/core/analysis/InAndOutAnalysisModule.java +++ b/analyses/org.eclipse.tracecompass.incubator.inandout.core/src/org/eclipse/tracecompass/incubator/internal/inandout/core/analysis/InAndOutAnalysisModule.java @@ -23,18 +23,12 @@ import java.util.List; import java.util.Objects; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.tracecompass.analysis.profiling.core.instrumented.InstrumentedCallStackAnalysis; import org.eclipse.tracecompass.incubator.internal.inandout.core.Activator; -import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; import org.eclipse.tracecompass.tmf.core.config.ITmfConfiguration; -import org.eclipse.tracecompass.tmf.core.config.TmfConfiguration; -import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; import org.eclipse.tracecompass.tmf.core.exceptions.TmfConfigurationException; -import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider; import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; @@ -183,110 +177,4 @@ public static void write(File file, List<@NonNull SegmentSpecifier> specifiers) } } - /** - * Create the InAndOutAnalysisModule for a given configuration and trace - * - * @param config - * the input {@link ITmfConfiguration} - * @param trace - * the trace to apply it to - * @param writeConfig - * write the config (do only once) - * @return InAndOutAnalysisModule - * @throws TmfConfigurationException - * if an error occurs - */ - public static InAndOutAnalysisModule create(@NonNull ITmfConfiguration config, @NonNull ITmfTrace trace, boolean writeConfig) throws TmfConfigurationException { - /* - * Apply configuration to each trace (no need to check trace type here) - */ - InAndOutAnalysisModule module = new InAndOutAnalysisModule(); - module.setConfiguration(config); - if (writeConfig) { - IPath traceConfigPath = getTraceRootFolder(trace, config.getSourceTypeId()); - TmfConfiguration.writeConfiguration(config, traceConfigPath); - } - try { - if (module.setTrace(trace)) { - IAnalysisModule oldModule = trace.addAnalysisModule(module); - if (oldModule != null) { - oldModule.dispose(); - oldModule.clearPersistentData(); - } - } else { - module.dispose(); - throw new TmfConfigurationException("InAndOut analysis module can't be created"); //$NON-NLS-1$ - } - } catch (TmfAnalysisException | TmfTraceException e) { - module.dispose(); - throw new TmfConfigurationException("Exception when setting trace", e); //$NON-NLS-1$ - } - return module; - } - - /** - * Removes configuration from trace: - * - delete configuration file - * - remove analysis module from trace object - * - * @param config - * the configuration to remove - * @param trace - * the - * @throws TmfConfigurationException if an error occurs - */ - public static void remove(ITmfConfiguration config, @NonNull ITmfTrace trace) throws TmfConfigurationException { - IPath traceConfig = getTraceRootFolder(trace, config.getSourceTypeId()); - traceConfig = traceConfig.append(File.separator).append(config.getId()).addFileExtension(TmfConfiguration.JSON_EXTENSION); - File configFile = traceConfig.toFile(); - if ((!configFile.exists()) || !configFile.delete()) { - throw new TmfConfigurationException("InAndOut configuration file can't be deleted from trace: configId=" + config.getId()); //$NON-NLS-1$ - } - - // Remove and clear persistent data - try { - IAnalysisModule module = trace.removeAnalysisModule(ID + config.getId()); - if (module != null) { - module.dispose(); - module.clearPersistentData(); - } - } catch (TmfTraceException e) { - throw new TmfConfigurationException("Error removing analysis module from trace: analysis ID=" + ID + config.getId(), e); //$NON-NLS-1$ - } - } - - @SuppressWarnings("null") - private static @NonNull IPath getTraceRootFolder(@NonNull ITmfTrace trace, String subFolder) { - String supplFolder = TmfTraceManager.getSupplementaryFileDir(trace); - IPath supplPath = new Path(supplFolder); - supplPath = supplPath.addTrailingSeparator().append(subFolder); - return supplPath; - } - - /** - * Reads the configurations for a given trace - * - * @param trace - * the trace to read configurations from - * @return list of configurations if any - * @throws TmfConfigurationException - * if an error occurs - */ - public static @NonNull List readConfigurations(@NonNull ITmfTrace trace) throws TmfConfigurationException { - IPath rootPath = getTraceRootFolder(trace, SegmentSpecifierConfiguration.IN_AND_OUT_CONFIG_SOURCE_TYPE_ID); - File folder = rootPath.toFile(); - List list = new ArrayList<>(); - if (folder.exists()) { - File[] listOfFiles = folder.listFiles(); - for (File file : listOfFiles) { - IPath path = new Path(file.getName()); - if (path.getFileExtension().equals(TmfConfiguration.JSON_EXTENSION)) { - ITmfConfiguration config = TmfConfiguration.fromJsonFile(file); - list.add(config); - } - } - } - return list; - } - } diff --git a/analyses/org.eclipse.tracecompass.incubator.inandout.core/src/org/eclipse/tracecompass/incubator/internal/inandout/core/analysis/InAndOutDataProviderFactory.java b/analyses/org.eclipse.tracecompass.incubator.inandout.core/src/org/eclipse/tracecompass/incubator/internal/inandout/core/analysis/InAndOutDataProviderFactory.java index fa2f00fd4..649f4bb71 100644 --- a/analyses/org.eclipse.tracecompass.incubator.inandout.core/src/org/eclipse/tracecompass/incubator/internal/inandout/core/analysis/InAndOutDataProviderFactory.java +++ b/analyses/org.eclipse.tracecompass.incubator.inandout.core/src/org/eclipse/tracecompass/incubator/internal/inandout/core/analysis/InAndOutDataProviderFactory.java @@ -28,37 +28,32 @@ import org.eclipse.jdt.annotation.Nullable; import org.eclipse.osgi.util.NLS; import org.eclipse.tracecompass.incubator.internal.inandout.core.Activator; +import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; +import org.eclipse.tracecompass.tmf.core.config.AbstractTmfDataProviderConfigurator; import org.eclipse.tracecompass.tmf.core.config.ITmfConfiguration; import org.eclipse.tracecompass.tmf.core.config.ITmfConfigurationSourceType; -import org.eclipse.tracecompass.tmf.core.config.ITmfDataProviderConfigurator; -import org.eclipse.tracecompass.tmf.core.config.TmfConfiguration; import org.eclipse.tracecompass.tmf.core.config.TmfConfigurationSourceType; import org.eclipse.tracecompass.tmf.core.dataprovider.IDataProviderDescriptor; import org.eclipse.tracecompass.tmf.core.dataprovider.IDataProviderDescriptor.ProviderType; import org.eclipse.tracecompass.tmf.core.dataprovider.IDataProviderFactory; +import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; import org.eclipse.tracecompass.tmf.core.exceptions.TmfConfigurationException; +import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; import org.eclipse.tracecompass.tmf.core.model.DataProviderCapabilities; import org.eclipse.tracecompass.tmf.core.model.DataProviderDescriptor; import org.eclipse.tracecompass.tmf.core.model.tree.ITmfTreeDataModel; import org.eclipse.tracecompass.tmf.core.model.tree.ITmfTreeDataProvider; -import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; -import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; -import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal; -import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; import org.osgi.framework.Bundle; -import com.google.common.collect.HashBasedTable; -import com.google.common.collect.Table; - /** * Data provider factory for InAndOut analysis. It doesn't have any output but is able * to create custom InAndOut analyses. */ @NonNullByDefault -public class InAndOutDataProviderFactory implements IDataProviderFactory, ITmfDataProviderConfigurator { +public class InAndOutDataProviderFactory extends AbstractTmfDataProviderConfigurator implements IDataProviderFactory { /** Data Provider factory ID */ public static final String ID = "org.eclipse.tracecompass.incubator.inandout.core.analysis.inAndOutdataProviderFactory"; //$NON-NLS-1$ @@ -73,8 +68,6 @@ public class InAndOutDataProviderFactory implements IDataProviderFactory, ITmfDa private static final String CUSTOM_IN_AND_OUT_ANALYSIS_NAME = "InAndOut Analysis ({0})"; //$NON-NLS-1$ private static final String CUSTOM_IN_AND_OUT_ANALYSIS_DESCRIPTION = "Custom InAndOut analysis configured by \" {0}\""; //$NON-NLS-1$ - private Table fTmfConfigurationTable = HashBasedTable.create(); - static { Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID); IPath defaultPath = new Path(SegmentSpecifierConfiguration.IN_AND_OUT_CONFIG_SOURCE_SCHEMA); @@ -102,13 +95,6 @@ public class InAndOutDataProviderFactory implements IDataProviderFactory, ITmfDa .setCapabilities(new DataProviderCapabilities.Builder().setCanCreate(true).build()) .build(); - /** - * Default constructor - */ - public InAndOutDataProviderFactory() { - TmfSignalManager.register(this); - } - @Override public @Nullable ITmfTreeDataProvider createProvider(ITmfTrace trace) { return null; @@ -118,7 +104,7 @@ public InAndOutDataProviderFactory() { public Collection getDescriptors(ITmfTrace trace) { List list = new ArrayList<>(); list.add(DESCRIPTOR); - for (ITmfConfiguration config : fTmfConfigurationTable.column(trace).values()) { + for (ITmfConfiguration config : getConfigurationTable().column(trace).values()) { list.add(getDescriptorFromConfig(config)); } return list; @@ -131,142 +117,132 @@ public Collection getDescriptors(ITmfTrace trace) { } @Override - public @NonNull IDataProviderDescriptor createDataProviderDescriptors(ITmfTrace trace, ITmfConfiguration configuration) throws TmfConfigurationException { - - if (configuration.getName().equals(TmfConfiguration.UNKNOWN)) { - throw new TmfConfigurationException("Missing configuration name of InAndOut analysis"); //$NON-NLS-1$ - } - - if (configuration.getSourceTypeId().equals(TmfConfiguration.UNKNOWN)) { - throw new TmfConfigurationException("Missing configuration type for InAndOut analysis"); //$NON-NLS-1$ - } - - String description = configuration.getDescription(); - if (configuration.getDescription().equals(TmfConfiguration.UNKNOWN)) { - description = "InAndOut Analysis defined by configuration " + configuration.getName(); //$NON-NLS-1$ - } - - TmfConfiguration.Builder builder = new TmfConfiguration.Builder(); - builder.setId(configuration.getId()) - .setSourceTypeId(configuration.getSourceTypeId()) - .setName(configuration.getName()) - .setDescription(description) - .setParameters(configuration.getParameters()) - .build(); - - ITmfConfiguration config = builder.build(); - - applyConfiguration(trace, config, true); - if (fTmfConfigurationTable.contains(config.getId(), trace)) { - throw new TmfConfigurationException("Configuration already existis with label: " + config.getName()); //$NON-NLS-1$ - } - fTmfConfigurationTable.put(config.getId(), trace, config); - return getDescriptorFromConfig(config); + @SuppressWarnings("null") + protected IDataProviderDescriptor getDescriptorFromConfig(ITmfConfiguration config) { + return new DataProviderDescriptor.Builder() + .setParentId(ID) + .setId(InAndOutAnalysisModule.ID + config.getId()) + .setName(NLS.bind(CUSTOM_IN_AND_OUT_ANALYSIS_NAME, config.getName())) + .setDescription(NLS.bind(CUSTOM_IN_AND_OUT_ANALYSIS_DESCRIPTION, config.getName())) + .setProviderType(ProviderType.NONE) + .setConfiguration(config) + .setCapabilities(new DataProviderCapabilities.Builder().setCanDelete(true).build()) + .build(); } @Override - public void removeDataProviderDescriptor(ITmfTrace trace, IDataProviderDescriptor descriptor) throws TmfConfigurationException { - - ITmfConfiguration creationConfiguration = descriptor.getConfiguration(); - if (creationConfiguration == null) { - throw new TmfConfigurationException("Data provider was not created by a configuration"); //$NON-NLS-1$ - } - - String configId = creationConfiguration.getId(); - ITmfConfiguration config = fTmfConfigurationTable.get(configId, trace); - if (config == null) { + protected void applyConfiguration(ITmfTrace trace, ITmfConfiguration config, boolean writeConfig) { + if (trace instanceof TmfExperiment) { + for (ITmfTrace tr : TmfTraceManager.getTraceSet(trace)) { + applyConfiguration(tr, config, writeConfig); + } + // Only apply for traces in experiment return; } - config = fTmfConfigurationTable.remove(configId, trace); - removeConfiguration(trace, config); + // Apply configuration to any trace (no need to check trace type here) + try { + create(config, trace, writeConfig, new InAndOutAnalysisModule()); + } catch (TmfConfigurationException e) { + Activator.getInstance().logError(e.getMessage(), e); + } } - /** - * Signal handler for opened trace signal. Will populate trace - * configurations - * - * @param signal - * the signal to handle - */ - @TmfSignalHandler - public void traceOpened(TmfTraceOpenedSignal signal) { - ITmfTrace trace = signal.getTrace(); - if (trace == null) { + @Override + protected void removeConfiguration(ITmfTrace trace, ITmfConfiguration config) { + if (trace instanceof TmfExperiment) { + for (ITmfTrace tr : TmfTraceManager.getTraceSet(trace)) { + removeConfiguration(tr, config); + } + // only remove for traces in experiment return; } try { - if (trace instanceof TmfExperiment) { - for (ITmfTrace tr : TmfTraceManager.getTraceSet(trace)) { - @SuppressWarnings("null") - // Read configurations from sub-trace - List configs = InAndOutAnalysisModule.readConfigurations(tr); - readAndApplyConfiguration(trace, configs); - } - } else { - @SuppressWarnings("null") - // Read configurations trace - List configs = InAndOutAnalysisModule.readConfigurations(trace); - readAndApplyConfiguration(trace, configs); - } + remove(config, trace, InAndOutAnalysisModule.ID); } catch (TmfConfigurationException e) { - Activator.getInstance().logError("Error applying configurations for trace " + trace.getName(), e); //$NON-NLS-1$ + Activator.getInstance().logError(e.getMessage(), e); } } /** - * Handles trace closed signal + * Removes configuration from trace: + * - delete configuration file + * - remove analysis module from trace object * - * @param signal - * the close signal to handle + * @param config + * the configuration to remove + * @param trace + * the + * @throws TmfConfigurationException if an error occurs */ - @TmfSignalHandler - public void traceClosed(TmfTraceClosedSignal signal) { - ITmfTrace trace = signal.getTrace(); - fTmfConfigurationTable.column(trace).clear(); - } + private void remove(ITmfConfiguration config, ITmfTrace trace, String baseAnalysisId) throws TmfConfigurationException { + IPath traceConfig = getConfigurationRootFolder(trace); + traceConfig = traceConfig.append(File.separator).append(config.getId()).addFileExtension(JSON_EXTENSION); + File configFile = traceConfig.toFile(); + if ((!configFile.exists()) || !configFile.delete()) { + throw new TmfConfigurationException("Configuration file can't be deleted from trace: configId=" + config.getId()); //$NON-NLS-1$ + } - private void readAndApplyConfiguration(ITmfTrace trace, List configs) throws TmfConfigurationException { - for (ITmfConfiguration config : configs) { - if (!fTmfConfigurationTable.contains(config.getId(), trace)) { - fTmfConfigurationTable.put(config.getId(), trace, config); - applyConfiguration(trace, config, false); + // Remove and clear persistent data + try { + IAnalysisModule module = trace.removeAnalysisModule(baseAnalysisId + config.getId()); + if (module != null) { + module.dispose(); + module.clearPersistentData(); } + } catch (TmfTraceException e) { + throw new TmfConfigurationException("Error removing analysis module from trace: analysis ID=" + baseAnalysisId + config.getId(), e); //$NON-NLS-1$ } } - private void applyConfiguration(ITmfTrace trace, ITmfConfiguration config, boolean writeConfig) throws TmfConfigurationException { - if (trace instanceof TmfExperiment) { - for (ITmfTrace tr : TmfTraceManager.getTraceSet(trace)) { - applyConfiguration(tr, config, writeConfig); + /** + * Create the InAndOutAnalysisModule for a given configuration and trace + * + * @param config + * the input {@link ITmfConfiguration} + * @param trace + * the trace to apply it to + * @param writeConfig + * write the config (do only once) + * @throws TmfConfigurationException + * if an error occurs + */ + private void create(ITmfConfiguration config, ITmfTrace trace, boolean writeConfig, IAnalysisModule module) throws TmfConfigurationException { + /* + * Apply configuration to each trace (no need to check trace type here) + */ + module.setConfiguration(config); + if (writeConfig) { + IPath traceConfigPath = getConfigurationRootFolder(trace); + writeConfiguration(config, traceConfigPath); + } + try { + if (module.setTrace(trace)) { + IAnalysisModule oldModule = trace.addAnalysisModule(module); + if (oldModule != null) { + oldModule.dispose(); + oldModule.clearPersistentData(); + } + } else { + module.dispose(); + throw new TmfConfigurationException("InAndOut analysis module can't be created"); //$NON-NLS-1$ } - // Only apply for traces in experiment - return; + } catch (TmfAnalysisException | TmfTraceException e) { + module.dispose(); + throw new TmfConfigurationException("Exception when setting trace", e); //$NON-NLS-1$ } - // Apply configuration to any trace (no need to check trace type here) - InAndOutAnalysisModule.create(config, trace, writeConfig); } - private void removeConfiguration(ITmfTrace trace, ITmfConfiguration config) throws TmfConfigurationException { - if (trace instanceof TmfExperiment) { - for (ITmfTrace tr : TmfTraceManager.getTraceSet(trace)) { - removeConfiguration(tr, config); - } - // only remove for traces in experiment - return; - } - InAndOutAnalysisModule.remove(config, trace); + @Override + protected IPath getConfigurationRootFolder(ITmfTrace trace) { + String supplFolder = TmfTraceManager.getSupplementaryFileDir(trace); + IPath supplPath = new Path(supplFolder); + supplPath = supplPath.addTrailingSeparator().append(ID); + return supplPath; } - @SuppressWarnings("null") - private static IDataProviderDescriptor getDescriptorFromConfig(ITmfConfiguration config) { - return new DataProviderDescriptor.Builder() - .setParentId(ID) - .setId(InAndOutAnalysisModule.ID + config.getId()) - .setName(NLS.bind(CUSTOM_IN_AND_OUT_ANALYSIS_NAME, config.getName())) - .setDescription(NLS.bind(CUSTOM_IN_AND_OUT_ANALYSIS_DESCRIPTION, config.getName())) - .setProviderType(ProviderType.NONE) - .setConfiguration(config) - .setCapabilities(new DataProviderCapabilities.Builder().setCanDelete(true).build()) - .build(); + @Override + public void dispose() { + super.dispose(); } + }