1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Philippe Proulx
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.module
;
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
;
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
;
28 * Factory which builds {@link LamiAnalysis} objects out of configuration
31 * @author Philippe Proulx
33 public final class ConfigFileLamiAnalysisFactory
{
36 * Class-specific exception, for when things go wrong with the
37 * {@link ConfigFileLamiAnalysisFactory}.
39 public static class ConfigFileLamiAnalysisFactoryException
extends Exception
{
41 private static final long serialVersionUID
= 1349804105078874111L;
46 public ConfigFileLamiAnalysisFactoryException() {
51 * Constructor specifying a message
54 * The exception message
56 public ConfigFileLamiAnalysisFactoryException(String message
) {
61 * Constructor specifying both a cause and a message
64 * The exception message
66 * The exception that caused this one
68 public ConfigFileLamiAnalysisFactoryException(String message
, Throwable cause
) {
69 super(message
, cause
);
73 * Constructor specifying a cause
76 * The exception that caused this one
78 public ConfigFileLamiAnalysisFactoryException(Throwable cause
) {
84 private ConfigFileLamiAnalysisFactory() {
87 private static String
getProperty(Properties props
, String propName
) throws ConfigFileLamiAnalysisFactoryException
{
88 String prop
= props
.getProperty(propName
);
91 throw new ConfigFileLamiAnalysisFactoryException(String
.format("Cannot find \"%s\" property", propName
)); //$NON-NLS-1$
97 throw new ConfigFileLamiAnalysisFactoryException(String
.format("\"%s\" property cannot be empty", propName
)); //$NON-NLS-1$
104 * Builds a {@link LamiAnalysis} object from an input stream providing the
105 * content of a configuration file.
107 * The caller is responsible for opening and closing {@code 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
115 * Predicate to use to check whether or not this analysis applies
117 * @return Built {@link LamiAnalysis} object
118 * @throws ConfigFileLamiAnalysisFactoryException
119 * If something go wrong
121 public static LamiAnalysis
buildFromInputStream(InputStream inputStream
, boolean isUserDefined
,
122 Predicate
<ITmfTrace
> appliesTo
) throws ConfigFileLamiAnalysisFactoryException
{
123 Properties props
= new Properties();
127 props
.load(inputStream
);
128 } catch (IOException e
) {
129 throw new ConfigFileLamiAnalysisFactoryException(e
);
132 // Get analysis' name and command
133 String name
= getProperty(props
, LamiConfigFileStrings
.PROP_NAME
);
134 String command
= getProperty(props
, LamiConfigFileStrings
.PROP_COMMAND
);
136 // Get individual arguments from command string
137 List
<String
> args
= ShellUtils
.commandStringToArgs(command
);
139 return new LamiAnalysis(name
, isUserDefined
, appliesTo
, args
);
143 * Builds a {@link LamiAnalysis} object from a configuration file.
145 * @param configFilePath
146 * Configuration file path
147 * @param isUserDefined
148 * {@code true} if the analysis to build is user-defined
150 * Predicate to use to check whether or not this analysis applies
152 * @return Built {@link LamiAnalysis} object
153 * @throws ConfigFileLamiAnalysisFactoryException
154 * If something go wrong
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
);
166 * Builds a list of {@link LamiAnalysis} objects from a directory containing
167 * configuration files.
170 * Configuration directory containing the configuration files to
172 * @param isUserDefined
173 * {@code true} if the analyses to build are user-defined
175 * Predicate to use to check whether or not those analyses apply
177 * @return List of built {@link LamiAnalysis} objects
178 * @throws ConfigFileLamiAnalysisFactoryException
179 * If something go wrong
181 public static List
<LamiAnalysis
> buildFromConfigDir(Path configDir
, boolean isUserDefined
,
182 Predicate
<ITmfTrace
> appliesTo
) throws ConfigFileLamiAnalysisFactoryException
{
183 List
<LamiAnalysis
> analyses
= new ArrayList
<>();
185 try (DirectoryStream
<Path
> directoryStream
= Files
.newDirectoryStream(configDir
)) {
186 for (Path path
: directoryStream
) {
187 analyses
.add(buildFromConfigFile(path
, isUserDefined
, appliesTo
));
189 } catch (IOException e
) {
190 throw new ConfigFileLamiAnalysisFactoryException(e
);