|
17 | 17 | import java.io.Reader; |
18 | 18 | import java.io.Writer; |
19 | 19 | import java.nio.charset.Charset; |
| 20 | +import java.util.ArrayList; |
20 | 21 | import java.util.HashMap; |
| 22 | +import java.util.List; |
21 | 23 | import java.util.Map; |
22 | 24 | import java.util.Objects; |
23 | 25 | import java.util.UUID; |
24 | 26 |
|
25 | 27 | import org.eclipse.core.runtime.IPath; |
| 28 | +import org.eclipse.core.runtime.Path; |
| 29 | +import org.eclipse.jdt.annotation.NonNull; |
26 | 30 | import org.eclipse.jdt.annotation.Nullable; |
27 | 31 | 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; |
28 | 34 | 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; |
29 | 38 |
|
30 | 39 | import com.google.gson.Gson; |
31 | 40 | import com.google.gson.JsonParseException; |
@@ -343,4 +352,109 @@ public static void writeConfiguration(ITmfConfiguration configuration, IPath roo |
343 | 352 | throw new TmfConfigurationException("Error writing configuration.", e); //$NON-NLS-1$ |
344 | 353 | } |
345 | 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 | + |
346 | 460 | } |
0 commit comments