Skip to content

Commit 7b47e78

Browse files
committed
tmf: extend api for configuration 3
1 parent 384fa92 commit 7b47e78

2 files changed

Lines changed: 88 additions & 152 deletions

File tree

tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/AbstractTmfDataProviderConfigurator.java

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package org.eclipse.tracecompass.tmf.core.config;
22

3+
import java.io.File;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.io.Writer;
7+
import java.util.ArrayList;
38
import java.util.List;
49

10+
import org.eclipse.core.runtime.IPath;
11+
import org.eclipse.core.runtime.Path;
512
import org.eclipse.jdt.annotation.NonNull;
13+
import org.eclipse.tracecompass.internal.tmf.core.Activator;
614
import org.eclipse.tracecompass.tmf.core.dataprovider.IDataProviderDescriptor;
715
import org.eclipse.tracecompass.tmf.core.exceptions.TmfConfigurationException;
816
import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
@@ -14,14 +22,28 @@
1422

1523
import com.google.common.collect.HashBasedTable;
1624
import com.google.common.collect.Table;
25+
import com.google.gson.Gson;
26+
import com.google.gson.JsonParseException;
1727

1828
/**
1929
*
2030
*/
2131
public abstract class AbstractTmfDataProviderConfigurator implements ITmfDataProviderConfigurator{
32+
/**
33+
* The json file extension
34+
* @since 9.5
35+
*/
36+
public static final String JSON_EXTENSION = "json"; //$NON-NLS-1$
2237

2338
private static final Table<String, ITmfTrace, ITmfConfiguration> fTmfConfigurationTable = HashBasedTable.create();
2439

40+
/**
41+
* @return a table mapping configuration id and trace (exp) to its configuration
42+
*/
43+
protected Table<String, ITmfTrace, ITmfConfiguration> getConfigurationTable(){
44+
return fTmfConfigurationTable;
45+
}
46+
2547
@Override
2648
public @NonNull IDataProviderDescriptor createDataProviderDescriptors(ITmfTrace trace, ITmfConfiguration configuration) throws TmfConfigurationException {
2749

@@ -112,12 +134,12 @@ public void traceOpened(TmfTraceOpenedSignal signal, String subfolder) {
112134
if (trace instanceof TmfExperiment) {
113135
for (ITmfTrace tr : TmfTraceManager.getTraceSet(trace)) {
114136
// Read configurations from sub-trace
115-
List<ITmfConfiguration> configs = TmfConfiguration.readConfigurations(tr, subfolder);
137+
List<ITmfConfiguration> configs = readConfigurations(tr, subfolder);
116138
readAndApplyConfiguration(trace, configs);
117139
}
118140
} else {
119141
// Read configurations trace
120-
List<ITmfConfiguration> configs = TmfConfiguration.readConfigurations(trace, subfolder);
142+
List<ITmfConfiguration> configs = readConfigurations(trace, subfolder);
121143
readAndApplyConfiguration(trace, configs);
122144
}
123145
} catch (TmfConfigurationException e) {
@@ -147,4 +169,68 @@ private void readAndApplyConfiguration(ITmfTrace trace, List<ITmfConfiguration>
147169
}
148170
}
149171

172+
/**
173+
* Reads the configurations for a given trace
174+
*
175+
* @param trace
176+
* the trace to read configurations from
177+
* @return list of configurations if any
178+
* @throws TmfConfigurationException
179+
* if an error occurs
180+
*/
181+
private @NonNull List<ITmfConfiguration> readConfigurations(@NonNull ITmfTrace trace, String subfolder) throws TmfConfigurationException {
182+
IPath rootPath = getConfigurationRootFolder(trace, subfolder);
183+
File folder = rootPath.toFile();
184+
List<ITmfConfiguration> list = new ArrayList<>();
185+
if (folder.exists()) {
186+
File[] listOfFiles = folder.listFiles();
187+
for (File file : listOfFiles) {
188+
IPath path = new Path(file.getName());
189+
if (path.getFileExtension().equals(JSON_EXTENSION)) {
190+
ITmfConfiguration config = TmfConfiguration.fromJsonFile(file);
191+
list.add(config);
192+
}
193+
}
194+
}
195+
return list;
196+
}
197+
198+
/**
199+
* Serialize {@link ITmfConfiguration} to JSON file with name configId.json
200+
*
201+
* @param configuration
202+
* the configuration to serialize
203+
* @param rootPath
204+
* the root path to store the configuration
205+
* @throws TmfConfigurationException
206+
* if an error occurs
207+
* @since 9.5
208+
*/
209+
protected static void writeConfiguration(ITmfConfiguration configuration, IPath rootPath) throws TmfConfigurationException {
210+
IPath supplPath = rootPath;
211+
File folder = supplPath.toFile();
212+
if (!folder.exists()) {
213+
folder.mkdir();
214+
}
215+
supplPath = supplPath.addTrailingSeparator().append(configuration.getId()).addFileExtension(JSON_EXTENSION);
216+
File file = supplPath.toFile();
217+
try (Writer writer = new FileWriter(file)) {
218+
writer.append(new Gson().toJson(configuration));
219+
} catch (IOException | JsonParseException e) {
220+
Activator.logError(e.getMessage(), e);
221+
throw new TmfConfigurationException("Error writing configuration.", e); //$NON-NLS-1$
222+
}
223+
}
224+
225+
// @SuppressWarnings("null")
226+
// protected static @NonNull IPath getConfigurationRootFolder(@NonNull ITmfTrace trace, String subFolder) {
227+
// String supplFolder = TmfTraceManager.getSupplementaryFileDir(trace);
228+
// IPath supplPath = new Path(supplFolder);
229+
// supplPath = supplPath.addTrailingSeparator().append(subFolder);
230+
// return supplPath;
231+
// }
232+
233+
@SuppressWarnings("null")
234+
protected @NonNull abstract IPath getConfigurationRootFolder(@NonNull ITmfTrace trace, String subFolder);
235+
150236
}

tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/config/TmfConfiguration.java

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,17 @@
1212

1313
import java.io.File;
1414
import java.io.FileReader;
15-
import java.io.FileWriter;
1615
import java.io.IOException;
1716
import java.io.Reader;
18-
import java.io.Writer;
1917
import java.nio.charset.Charset;
20-
import java.util.ArrayList;
2118
import java.util.HashMap;
22-
import java.util.List;
2319
import java.util.Map;
2420
import java.util.Objects;
2521
import java.util.UUID;
2622

27-
import org.eclipse.core.runtime.IPath;
28-
import org.eclipse.core.runtime.Path;
29-
import org.eclipse.jdt.annotation.NonNull;
3023
import org.eclipse.jdt.annotation.Nullable;
3124
import org.eclipse.tracecompass.internal.tmf.core.Activator;
32-
import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
33-
import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
3425
import org.eclipse.tracecompass.tmf.core.exceptions.TmfConfigurationException;
35-
import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
36-
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
37-
import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
38-
3926
import com.google.gson.Gson;
4027
import com.google.gson.JsonParseException;
4128
import com.google.gson.annotations.Expose;
@@ -56,12 +43,6 @@ public class TmfConfiguration implements ITmfConfiguration {
5643
*/
5744
public static final String UNKNOWN = "---unknown---"; //$NON-NLS-1$
5845

59-
/**
60-
* The json file extension
61-
* @since 9.5
62-
*/
63-
public static final String JSON_EXTENSION = "json"; //$NON-NLS-1$
64-
6546
@Expose
6647
@SerializedName(value = "id")
6748
@Nullable
@@ -326,135 +307,4 @@ public static ITmfConfiguration fromJsonFile(File jsonFile) throws TmfConfigurat
326307
}
327308
}
328309

329-
/**
330-
* Serialize {@link ITmfConfiguration} to JSON file with name configId.json
331-
*
332-
* @param configuration
333-
* the configuration to serialize
334-
* @param rootPath
335-
* the root path to store the configuration
336-
* @throws TmfConfigurationException
337-
* if an error occurs
338-
* @since 9.5
339-
*/
340-
public static void writeConfiguration(ITmfConfiguration configuration, IPath rootPath) throws TmfConfigurationException {
341-
IPath supplPath = rootPath;
342-
File folder = supplPath.toFile();
343-
if (!folder.exists()) {
344-
folder.mkdir();
345-
}
346-
supplPath = supplPath.addTrailingSeparator().append(configuration.getId()).addFileExtension(JSON_EXTENSION);
347-
File file = supplPath.toFile();
348-
try (Writer writer = new FileWriter(file)) {
349-
writer.append(new Gson().toJson(configuration));
350-
} catch (IOException | JsonParseException e) {
351-
Activator.logError(e.getMessage(), e);
352-
throw new TmfConfigurationException("Error writing configuration.", e); //$NON-NLS-1$
353-
}
354-
}
355-
356-
@SuppressWarnings("null")
357-
private static @NonNull IPath getConfigurationRootFolder(@NonNull ITmfTrace trace, String subFolder) {
358-
String supplFolder = TmfTraceManager.getSupplementaryFileDir(trace);
359-
IPath supplPath = new Path(supplFolder);
360-
supplPath = supplPath.addTrailingSeparator().append(subFolder);
361-
return supplPath;
362-
}
363-
364-
/**
365-
* Reads the configurations for a given trace
366-
*
367-
* @param trace
368-
* the trace to read configurations from
369-
* @return list of configurations if any
370-
* @throws TmfConfigurationException
371-
* if an error occurs
372-
*/
373-
public static @NonNull List<ITmfConfiguration> readConfigurations(@NonNull ITmfTrace trace, String subfolder) throws TmfConfigurationException {
374-
IPath rootPath = getConfigurationRootFolder(trace, subfolder);
375-
File folder = rootPath.toFile();
376-
List<ITmfConfiguration> list = new ArrayList<>();
377-
if (folder.exists()) {
378-
File[] listOfFiles = folder.listFiles();
379-
for (File file : listOfFiles) {
380-
IPath path = new Path(file.getName());
381-
if (path.getFileExtension().equals(TmfConfiguration.JSON_EXTENSION)) {
382-
ITmfConfiguration config = TmfConfiguration.fromJsonFile(file);
383-
list.add(config);
384-
}
385-
}
386-
}
387-
return list;
388-
}
389-
390-
/**
391-
* Removes configuration from trace:
392-
* - delete configuration file
393-
* - remove analysis module from trace object
394-
*
395-
* @param config
396-
* the configuration to remove
397-
* @param trace
398-
* the
399-
* @throws TmfConfigurationException if an error occurs
400-
*/
401-
public static void remove(ITmfConfiguration config, @NonNull ITmfTrace trace, String baseAnalysisId) throws TmfConfigurationException {
402-
IPath traceConfig = getConfigurationRootFolder(trace, config.getSourceTypeId());
403-
traceConfig = traceConfig.append(File.separator).append(config.getId()).addFileExtension(TmfConfiguration.JSON_EXTENSION);
404-
File configFile = traceConfig.toFile();
405-
if ((!configFile.exists()) || !configFile.delete()) {
406-
throw new TmfConfigurationException("Configuration file can't be deleted from trace: configId=" + config.getId()); //$NON-NLS-1$
407-
}
408-
409-
// Remove and clear persistent data
410-
try {
411-
IAnalysisModule module = trace.removeAnalysisModule(baseAnalysisId + config.getId());
412-
if (module != null) {
413-
module.dispose();
414-
module.clearPersistentData();
415-
}
416-
} catch (TmfTraceException e) {
417-
throw new TmfConfigurationException("Error removing analysis module from trace: analysis ID=" + baseAnalysisId + config.getId(), e); //$NON-NLS-1$
418-
}
419-
}
420-
421-
/**
422-
* Create the InAndOutAnalysisModule for a given configuration and trace
423-
*
424-
* @param config
425-
* the input {@link ITmfConfiguration}
426-
* @param trace
427-
* the trace to apply it to
428-
* @param writeConfig
429-
* write the config (do only once)
430-
* @return InAndOutAnalysisModule
431-
* @throws TmfConfigurationException
432-
* if an error occurs
433-
*/
434-
public static void create(@NonNull ITmfConfiguration config, @NonNull ITmfTrace trace, boolean writeConfig, IAnalysisModule module) throws TmfConfigurationException {
435-
/*
436-
* Apply configuration to each trace (no need to check trace type here)
437-
*/
438-
module.setConfiguration(config);
439-
if (writeConfig) {
440-
IPath traceConfigPath = TmfConfiguration.getConfigurationRootFolder(trace, config.getSourceTypeId());
441-
TmfConfiguration.writeConfiguration(config, traceConfigPath);
442-
}
443-
try {
444-
if (module.setTrace(trace)) {
445-
IAnalysisModule oldModule = trace.addAnalysisModule(module);
446-
if (oldModule != null) {
447-
oldModule.dispose();
448-
oldModule.clearPersistentData();
449-
}
450-
} else {
451-
module.dispose();
452-
throw new TmfConfigurationException("InAndOut analysis module can't be created"); //$NON-NLS-1$
453-
}
454-
} catch (TmfAnalysisException | TmfTraceException e) {
455-
module.dispose();
456-
throw new TmfConfigurationException("Exception when setting trace", e); //$NON-NLS-1$
457-
}
458-
}
459-
460310
}

0 commit comments

Comments
 (0)