use of org.apache.felix.utils.properties.Properties in project fabric8 by fabric8io.
the class FabricProfileFileSystemProvider method buildFileSystem.
private FabricProfileFileSystem buildFileSystem(final Path path) throws IOException {
final Map<String, Object> contents = new HashMap<>();
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
addChild(dir, new ArrayList<String>());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
byte[] content = Files.readAllBytes(file);
if (file.getFileName().toString().equals("io.fabric8.agent.properties")) {
file = file.resolveSibling("profile.cfg");
} else if (file.getFileName().toString().contains(".properties")) {
file = file.resolveSibling(file.getFileName().toString().replace(".properties", ".cfg"));
}
if (file.getFileName().toString().contains(".cfg")) {
Properties props = new Properties(false);
props.load(new ByteArrayInputStream(content));
for (Map.Entry<String, String> entry : props.entrySet()) {
String val = entry.getValue();
val = val.replace("${profile:io.fabric8.agent/", "${profile:profile/");
val = val.replace("${version:", "${profile:io.fabric8.version/");
val = val.replace("${runtime.", "${karaf.");
Matcher matcher = Pattern.compile(".*\\$\\{(.*?):.*?\\}.*").matcher(val);
if (matcher.matches()) {
String scheme = matcher.group(1);
if (!"profile".equals(scheme)) {
System.out.println("Unsupported scheme: " + entry.getKey() + " = " + val + " in " + path.relativize(file));
}
}
entry.setValue(val);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
props.save(baos);
baos.flush();
content = baos.toByteArray();
}
addChild(file, content);
return FileVisitResult.CONTINUE;
}
private void addChild(Path child, Object content) {
String par = "/" + path.relativize(child.getParent()).toString();
String str = "/" + path.relativize(child).toString();
if (!"/".equals(str)) {
if (str.endsWith("/")) {
str = str.substring(0, str.length() - 1);
}
((List<String>) contents.get(par)).add(str);
}
contents.put(str, content);
}
});
return new FabricProfileFileSystem(this, contents);
}
use of org.apache.felix.utils.properties.Properties in project ddf by codice.
the class CfgStrategy method read.
@Override
public Dictionary<String, Object> read(InputStream in) throws IOException {
notNull(in, "Input stream cannot be null");
final Properties props = new Properties();
props.load(in);
return new Hashtable<>(props);
}
use of org.apache.felix.utils.properties.Properties in project ddf by codice.
the class CfgStrategy method write.
@Override
public void write(OutputStream out, Dictionary<String, Object> inputDictionary) throws IOException {
notNull(out, "Output stream cannot be null");
notNull(inputDictionary, "Properties cannot be null");
final Properties props = new Properties();
final Enumeration<String> keys = inputDictionary.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
props.put(key, inputDictionary.get(key).toString());
}
props.save(out);
}
use of org.apache.felix.utils.properties.Properties in project ddf by codice.
the class SystemPropertiesAdmin method writeOutUsersDotAttributesFile.
/*
* Writes security and claims data for the system-level user and default admin.
*/
private void writeOutUsersDotAttributesFile(File userAttributesFile) {
Map<String, Object> json = null;
try (BufferedReader br = Files.newBufferedReader(Paths.get(userAttributesFile.toURI()))) {
json = GSON.fromJson(br, MAP_STRING_TO_OBJECT_TYPE);
} catch (IOException e) {
LOGGER.warn("Unable to read system user attribute file for hostname update.", e);
return;
}
addGuestClaimsProfileAttributes(json);
if (json.containsKey(oldHostName)) {
Properties systemDotProperties = null;
try {
systemDotProperties = new Properties(systemPropertiesFile);
json.put(systemDotProperties.get(SystemBaseUrl.INTERNAL_HOST), json.remove(oldHostName));
} catch (IOException e) {
LOGGER.warn("Exception while reading the system.properties file.", e);
}
}
try {
for (Map.Entry<String, Object> entry : json.entrySet()) {
json.put(entry.getKey(), replaceLocalhost(entry.getValue()));
}
FileUtils.writeStringToFile(userAttributesFile, GSON.toJson(json), Charset.defaultCharset());
} catch (IOException e) {
LOGGER.warn("Unable to write user attribute file for system update.", e);
}
}
use of org.apache.felix.utils.properties.Properties in project ddf by codice.
the class SystemPropertiesAdmin method writeSystemProperties.
@Override
public void writeSystemProperties(Map<String, String> updatedSystemProperties) {
if (updatedSystemProperties == null) {
return;
}
Properties systemDotProperties;
try {
systemDotProperties = new Properties(systemPropertiesFile);
} catch (IOException e) {
LOGGER.warn("Exception reading system.properties file.", e);
return;
}
// save off the current/old hostname before we make any changes
oldHostName = systemDotProperties.getProperty(SystemBaseUrl.INTERNAL_HOST);
updatedSystemProperties.forEach((key, value) -> {
// Clears out the property value before setting it
//
// We have to do this because when we read in the properties, the values are
// expanded which can lead to a state where the value is erroneously not updated
// after being checked.
systemDotProperties.put(key, "");
systemDotProperties.put(key, value);
});
try {
systemDotProperties.save();
} catch (IOException e) {
LOGGER.warn("Exception writing to system.properties file.", e);
}
writeOutUsersDotPropertiesFile(userPropertiesFile);
writeOutUsersDotAttributesFile(userAttributesFile);
}
Aggregations