|
| 1 | +package org.eclipse.tracecompass.tmf.core.config; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.eclipse.jdt.annotation.NonNull; |
| 6 | +import org.eclipse.tracecompass.tmf.core.dataprovider.IDataProviderDescriptor; |
| 7 | +import org.eclipse.tracecompass.tmf.core.exceptions.TmfConfigurationException; |
| 8 | +import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; |
| 9 | +import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal; |
| 10 | +import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; |
| 11 | +import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 12 | +import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 13 | +import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment; |
| 14 | + |
| 15 | +import com.google.common.collect.HashBasedTable; |
| 16 | +import com.google.common.collect.Table; |
| 17 | + |
| 18 | +/** |
| 19 | + * |
| 20 | + */ |
| 21 | +public abstract class AbstractTmfDataProviderConfigurator implements ITmfDataProviderConfigurator{ |
| 22 | + |
| 23 | + private static final Table<String, ITmfTrace, ITmfConfiguration> fTmfConfigurationTable = HashBasedTable.create(); |
| 24 | + |
| 25 | + @Override |
| 26 | + public @NonNull IDataProviderDescriptor createDataProviderDescriptors(ITmfTrace trace, ITmfConfiguration configuration) throws TmfConfigurationException { |
| 27 | + |
| 28 | + if (configuration.getName().equals(TmfConfiguration.UNKNOWN)) { |
| 29 | + throw new TmfConfigurationException("Missing configuration name of InAndOut analysis"); //$NON-NLS-1$ |
| 30 | + } |
| 31 | + |
| 32 | + if (configuration.getSourceTypeId().equals(TmfConfiguration.UNKNOWN)) { |
| 33 | + throw new TmfConfigurationException("Missing configuration type for InAndOut analysis"); //$NON-NLS-1$ |
| 34 | + } |
| 35 | + |
| 36 | + String description = configuration.getDescription(); |
| 37 | + if (configuration.getDescription().equals(TmfConfiguration.UNKNOWN)) { |
| 38 | + description = "InAndOut Analysis defined by configuration " + configuration.getName(); //$NON-NLS-1$ |
| 39 | + } |
| 40 | + |
| 41 | + TmfConfiguration.Builder builder = new TmfConfiguration.Builder(); |
| 42 | + builder.setId(configuration.getId()) |
| 43 | + .setSourceTypeId(configuration.getSourceTypeId()) |
| 44 | + .setName(configuration.getName()) |
| 45 | + .setDescription(description) |
| 46 | + .setParameters(configuration.getParameters()) |
| 47 | + .build(); |
| 48 | + |
| 49 | + ITmfConfiguration config = builder.build(); |
| 50 | + |
| 51 | + applyConfiguration(trace, config, true); |
| 52 | + if (fTmfConfigurationTable.contains(config.getId(), trace)) { |
| 53 | + throw new TmfConfigurationException("Configuration already existis with label: " + config.getName()); //$NON-NLS-1$ |
| 54 | + } |
| 55 | + fTmfConfigurationTable.put(config.getId(), trace, config); |
| 56 | + return getDescriptorFromConfig(config); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param config |
| 61 | + * @return A data provider descriptor based on the configuration parameter |
| 62 | + */ |
| 63 | + protected abstract IDataProviderDescriptor getDescriptorFromConfig(ITmfConfiguration config); |
| 64 | + |
| 65 | + /** |
| 66 | + * This is the method that handles what happens when a configuration is applied |
| 67 | + * @param trace |
| 68 | + * @param config |
| 69 | + * @param writeConfig |
| 70 | + */ |
| 71 | + protected abstract void applyConfiguration(ITmfTrace trace, ITmfConfiguration config, boolean writeConfig); |
| 72 | + |
| 73 | + // efrooo: the below move to open source |
| 74 | + @Override |
| 75 | + public void removeDataProviderDescriptor(ITmfTrace trace, IDataProviderDescriptor descriptor) throws TmfConfigurationException { |
| 76 | + ITmfConfiguration creationConfiguration = descriptor.getConfiguration(); |
| 77 | + if (creationConfiguration == null) { |
| 78 | + throw new TmfConfigurationException("Data provider was not created by a configuration"); //$NON-NLS-1$ |
| 79 | + } |
| 80 | + |
| 81 | + String configId = creationConfiguration.getId(); |
| 82 | + ITmfConfiguration config = fTmfConfigurationTable.get(configId, trace); |
| 83 | + if (config == null) { |
| 84 | + return; |
| 85 | + } |
| 86 | + config = fTmfConfigurationTable.remove(configId, trace); |
| 87 | + removeConfiguration(trace, config); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * This is the method that handles what happens when a configuration is removed (e.g. remove analysis, dp etc) |
| 92 | + * @param trace |
| 93 | + * @param config |
| 94 | + */ |
| 95 | + protected abstract void removeConfiguration(@NonNull ITmfTrace trace, @NonNull ITmfConfiguration config); |
| 96 | + |
| 97 | + // efroroo: to open source |
| 98 | + /** |
| 99 | + * Signal handler for opened trace signal. Will populate trace |
| 100 | + * configurations |
| 101 | + * |
| 102 | + * @param signal |
| 103 | + * the signal to handle |
| 104 | + */ |
| 105 | + @TmfSignalHandler |
| 106 | + public void traceOpened(TmfTraceOpenedSignal signal, String subfolder) { |
| 107 | + ITmfTrace trace = signal.getTrace(); |
| 108 | + if (trace == null) { |
| 109 | + return; |
| 110 | + } |
| 111 | + try { |
| 112 | + if (trace instanceof TmfExperiment) { |
| 113 | + for (ITmfTrace tr : TmfTraceManager.getTraceSet(trace)) { |
| 114 | + // Read configurations from sub-trace |
| 115 | + List<ITmfConfiguration> configs = TmfConfiguration.readConfigurations(tr, subfolder); |
| 116 | + readAndApplyConfiguration(trace, configs); |
| 117 | + } |
| 118 | + } else { |
| 119 | + // Read configurations trace |
| 120 | + List<ITmfConfiguration> configs = TmfConfiguration.readConfigurations(trace, subfolder); |
| 121 | + readAndApplyConfiguration(trace, configs); |
| 122 | + } |
| 123 | + } catch (TmfConfigurationException e) { |
| 124 | + // FIXME: use proper logging |
| 125 | + // Activator.logError("Error applying configurations for trace " + trace.getName(), e); //$NON-NLS-1$ |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Handles trace closed signal |
| 131 | + * |
| 132 | + * @param signal |
| 133 | + * the close signal to handle |
| 134 | + */ |
| 135 | + @TmfSignalHandler |
| 136 | + public void traceClosed(TmfTraceClosedSignal signal) { |
| 137 | + ITmfTrace trace = signal.getTrace(); |
| 138 | + fTmfConfigurationTable.column(trace).clear(); |
| 139 | + } |
| 140 | + |
| 141 | + private void readAndApplyConfiguration(ITmfTrace trace, List<ITmfConfiguration> configs) throws TmfConfigurationException { |
| 142 | + for (ITmfConfiguration config : configs) { |
| 143 | + if (!fTmfConfigurationTable.contains(config.getId(), trace)) { |
| 144 | + fTmfConfigurationTable.put(config.getId(), trace, config); |
| 145 | + applyConfiguration(trace, config, false); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments