Commit | Line | Data |
---|---|---|
83d1b60e 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.module; | |
11 | ||
12 | import java.io.FileInputStream; | |
13 | import java.io.IOException; | |
14 | import java.io.InputStream; | |
15 | import java.nio.file.DirectoryStream; | |
16 | import java.nio.file.Files; | |
17 | import java.nio.file.Path; | |
18 | import java.util.ArrayList; | |
19 | import java.util.List; | |
20 | import java.util.Properties; | |
21 | import java.util.function.Predicate; | |
22 | ||
23 | import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.LamiConfigFileStrings; | |
24 | import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.ShellUtils; | |
25 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
26 | ||
27 | /** | |
28 | * Factory which builds {@link LamiAnalysis} objects out of configuration | |
29 | * files. | |
30 | * | |
31 | * @author Philippe Proulx | |
32 | */ | |
33 | public final class ConfigFileLamiAnalysisFactory { | |
34 | ||
35 | /** | |
36 | * Class-specific exception, for when things go wrong with the | |
37 | * {@link ConfigFileLamiAnalysisFactory}. | |
38 | */ | |
39 | public static class ConfigFileLamiAnalysisFactoryException extends Exception { | |
40 | ||
41 | private static final long serialVersionUID = 1349804105078874111L; | |
42 | ||
43 | /** | |
44 | * Default constructor | |
45 | */ | |
46 | public ConfigFileLamiAnalysisFactoryException() { | |
47 | super(); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Constructor specifying a message | |
52 | * | |
53 | * @param message | |
54 | * The exception message | |
55 | */ | |
56 | public ConfigFileLamiAnalysisFactoryException(String message) { | |
57 | super(message); | |
58 | } | |
59 | ||
60 | /** | |
61 | * Constructor specifying both a cause and a message | |
62 | * | |
63 | * @param message | |
64 | * The exception message | |
65 | * @param cause | |
66 | * The exception that caused this one | |
67 | */ | |
68 | public ConfigFileLamiAnalysisFactoryException(String message, Throwable cause) { | |
69 | super(message, cause); | |
70 | } | |
71 | ||
72 | /** | |
73 | * Constructor specifying a cause | |
74 | * | |
75 | * @param cause | |
76 | * The exception that caused this one | |
77 | */ | |
78 | public ConfigFileLamiAnalysisFactoryException(Throwable cause) { | |
79 | super(cause); | |
80 | } | |
81 | ||
82 | } | |
83 | ||
84 | private ConfigFileLamiAnalysisFactory() { | |
85 | } | |
86 | ||
87 | private static String getProperty(Properties props, String propName) throws ConfigFileLamiAnalysisFactoryException { | |
88 | String prop = props.getProperty(propName); | |
89 | ||
90 | if (prop == null) { | |
91 | throw new ConfigFileLamiAnalysisFactoryException(String.format("Cannot find \"%s\" property", propName)); //$NON-NLS-1$ | |
92 | } | |
93 | ||
94 | prop = prop.trim(); | |
95 | ||
96 | if (prop.isEmpty()) { | |
97 | throw new ConfigFileLamiAnalysisFactoryException(String.format("\"%s\" property cannot be empty", propName)); //$NON-NLS-1$ | |
98 | } | |
99 | ||
100 | return prop; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Builds a {@link LamiAnalysis} object from an input stream providing the | |
105 | * content of a configuration file. | |
106 | * <p> | |
107 | * The caller is responsible for opening and closing {@code inputStream}. | |
108 | * | |
109 | * @param inputStream | |
110 | * Input stream for reading the configuration file; the stream is | |
111 | * not closed by this method | |
112 | * @param isUserDefined | |
113 | * {@code true} if the analysis to build is user-defined | |
114 | * @param appliesTo | |
115 | * Predicate to use to check whether or not this analysis applies | |
116 | * to a given trace | |
117 | * @return Built {@link LamiAnalysis} object | |
118 | * @throws ConfigFileLamiAnalysisFactoryException | |
119 | * If something go wrong | |
120 | */ | |
121 | public static LamiAnalysis buildFromInputStream(InputStream inputStream, boolean isUserDefined, | |
122 | Predicate<ITmfTrace> appliesTo) throws ConfigFileLamiAnalysisFactoryException { | |
123 | Properties props = new Properties(); | |
124 | ||
125 | // Load properties | |
126 | try { | |
127 | props.load(inputStream); | |
128 | } catch (IOException e) { | |
129 | throw new ConfigFileLamiAnalysisFactoryException(e); | |
130 | } | |
131 | ||
132 | // Get analysis' name and command | |
133 | String name = getProperty(props, LamiConfigFileStrings.PROP_NAME); | |
134 | String command = getProperty(props, LamiConfigFileStrings.PROP_COMMAND); | |
135 | ||
136 | // Get individual arguments from command string | |
137 | List<String> args = ShellUtils.commandStringToArgs(command); | |
138 | ||
139 | return new LamiAnalysis(name, isUserDefined, appliesTo, args); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Builds a {@link LamiAnalysis} object from a configuration file. | |
144 | * | |
145 | * @param configFilePath | |
146 | * Configuration file path | |
147 | * @param isUserDefined | |
148 | * {@code true} if the analysis to build is user-defined | |
149 | * @param appliesTo | |
150 | * Predicate to use to check whether or not this analysis applies | |
151 | * to a given trace | |
152 | * @return Built {@link LamiAnalysis} object | |
153 | * @throws ConfigFileLamiAnalysisFactoryException | |
154 | * If something go wrong | |
155 | */ | |
156 | public static LamiAnalysis buildFromConfigFile(Path configFilePath, boolean isUserDefined, | |
157 | Predicate<ITmfTrace> appliesTo) throws ConfigFileLamiAnalysisFactoryException { | |
158 | try (FileInputStream propsStream = new FileInputStream(configFilePath.toFile())) { | |
159 | return buildFromInputStream(propsStream, isUserDefined, appliesTo); | |
160 | } catch (IOException e) { | |
161 | throw new ConfigFileLamiAnalysisFactoryException(e); | |
162 | } | |
163 | } | |
164 | ||
165 | /** | |
166 | * Builds a list of {@link LamiAnalysis} objects from a directory containing | |
167 | * configuration files. | |
168 | * | |
169 | * @param configDir | |
170 | * Configuration directory containing the configuration files to | |
171 | * load | |
172 | * @param isUserDefined | |
173 | * {@code true} if the analyses to build are user-defined | |
174 | * @param appliesTo | |
175 | * Predicate to use to check whether or not those analyses apply | |
176 | * to a given trace | |
177 | * @return List of built {@link LamiAnalysis} objects | |
178 | * @throws ConfigFileLamiAnalysisFactoryException | |
179 | * If something go wrong | |
180 | */ | |
181 | public static List<LamiAnalysis> buildFromConfigDir(Path configDir, boolean isUserDefined, | |
182 | Predicate<ITmfTrace> appliesTo) throws ConfigFileLamiAnalysisFactoryException { | |
183 | List<LamiAnalysis> analyses = new ArrayList<>(); | |
184 | ||
185 | try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(configDir)) { | |
186 | for (Path path : directoryStream) { | |
187 | analyses.add(buildFromConfigFile(path, isUserDefined, appliesTo)); | |
188 | } | |
189 | } catch (IOException e) { | |
190 | throw new ConfigFileLamiAnalysisFactoryException(e); | |
191 | } | |
192 | ||
193 | return analyses; | |
194 | } | |
195 | ||
196 | } |