1212
1313import java .io .File ;
1414import java .io .FileReader ;
15- import java .io .FileWriter ;
1615import java .io .IOException ;
1716import java .io .Reader ;
18- import java .io .Writer ;
1917import java .nio .charset .Charset ;
20- import java .util .ArrayList ;
2118import java .util .HashMap ;
22- import java .util .List ;
2319import java .util .Map ;
2420import java .util .Objects ;
2521import 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 ;
3023import org .eclipse .jdt .annotation .Nullable ;
3124import 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 ;
3425import 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-
3926import com .google .gson .Gson ;
4027import com .google .gson .JsonParseException ;
4128import 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