Commit | Line | Data |
---|---|---|
664dac59 PP |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 EfficiOS Inc., Philippe Proulx | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | *******************************************************************************/ | |
9 | ||
10 | package org.eclipse.tracecompass.internal.provisional.analysis.lami.core; | |
11 | ||
12 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
13 | ||
14 | import java.io.File; | |
15 | import java.io.FileOutputStream; | |
16 | import java.io.IOException; | |
17 | import java.nio.file.Files; | |
18 | import java.nio.file.Path; | |
19 | import java.nio.file.Paths; | |
20 | import java.util.Date; | |
21 | import java.util.Properties; | |
22 | ||
23 | import org.eclipse.core.runtime.IPath; | |
24 | import org.eclipse.tracecompass.internal.analysis.lami.core.Activator; | |
25 | ||
26 | /** | |
27 | * Utilities related to user-defined LAMI analysis configuration | |
28 | * files. | |
29 | * | |
30 | * @author Philippe Proulx | |
31 | */ | |
32 | public class LamiConfigUtils { | |
33 | ||
34 | private static final String CONFIG_DIR = "user-defined-configs"; //$NON-NLS-1$ | |
35 | ||
36 | private LamiConfigUtils() { | |
37 | } | |
38 | ||
39 | /** | |
40 | * Returns the path of the directory, in the workspace, where | |
41 | * configuration files are stored. | |
42 | * | |
43 | * @return Path to configuration directory | |
44 | */ | |
45 | public static Path getConfigDirPath() { | |
46 | IPath path = Activator.instance().getStateLocation(); | |
47 | path = path.addTrailingSeparator().append(CONFIG_DIR); | |
48 | ||
49 | /* Check if directory exists, otherwise create it */ | |
50 | final File dir = path.toFile(); | |
51 | ||
52 | if (!dir.exists() || !dir.isDirectory()) { | |
53 | dir.mkdirs(); | |
54 | } | |
55 | ||
56 | return checkNotNull(dir.toPath()); | |
57 | } | |
58 | ||
59 | private static Path getConfigFilePath(String name) { | |
60 | final Path configDirPath = getConfigDirPath(); | |
61 | String normName = name.replaceAll("\\s+", "-"); //$NON-NLS-1$ //$NON-NLS-2$ | |
62 | normName = normName.replaceAll("[^a-zA-Z0-9_]", ""); //$NON-NLS-1$ //$NON-NLS-2$ | |
63 | ||
64 | return checkNotNull(Paths.get(configDirPath.toString(), normName + ".properties")); //$NON-NLS-1$ | |
65 | } | |
66 | ||
67 | /** | |
68 | * Creates a new configuration file in the configuration directory. | |
69 | * | |
70 | * @param name | |
71 | * Name of the external analysis | |
72 | * @param command | |
73 | * Command of the external analysis | |
74 | * @return Path of created configuration file | |
75 | * @throws IOException | |
76 | * If the file cannot be found or read | |
77 | */ | |
78 | public static Path createConfigFile(String name, String command) throws IOException { | |
79 | final Properties props = new Properties(); | |
80 | ||
81 | props.setProperty(LamiConfigFileStrings.PROP_NAME, name); | |
82 | props.setProperty(LamiConfigFileStrings.PROP_COMMAND, command); | |
83 | final Path configFilePath = getConfigFilePath(name); | |
84 | ||
85 | if (Files.exists(configFilePath)) { | |
86 | throw new IOException(String.format("Configuration file \"%s\" exists", configFilePath.toString())); //$NON-NLS-1$ | |
87 | } | |
88 | ||
89 | try (final FileOutputStream out = new FileOutputStream(configFilePath.toFile())) { | |
90 | String userName = System.getProperty("user.name"); //$NON-NLS-1$ | |
91 | ||
92 | if (userName == null) { | |
93 | userName = "unknown user"; //$NON-NLS-1$ | |
94 | } | |
95 | ||
96 | final Date curDate = new Date(); | |
97 | final String comment = String.format("Trace Compass external analysis descriptor created by user %s on %s", userName, curDate); //$NON-NLS-1$ | |
98 | props.store(out, comment); | |
99 | } | |
100 | ||
101 | return configFilePath; | |
102 | } | |
103 | ||
104 | /** | |
105 | * Removes the configuration file which corresponds to the analysis named | |
106 | * {@code name}. | |
107 | * | |
108 | * @param name | |
109 | * Analysis name | |
110 | * @throws IOException | |
111 | * If there was an error attempting to delete the file | |
112 | */ | |
113 | public static void removeConfigFile(String name) throws IOException { | |
114 | Path configFilePath = getConfigFilePath(name); | |
115 | Files.delete(configFilePath); | |
116 | } | |
117 | ||
118 | } |