Search in sources :

Example 91 with Properties

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);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(org.apache.felix.utils.properties.Properties) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 92 with Properties

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);
}
Also used : Hashtable(java.util.Hashtable) Properties(org.apache.felix.utils.properties.Properties)

Example 93 with Properties

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);
}
Also used : Properties(org.apache.felix.utils.properties.Properties)

Example 94 with Properties

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);
    }
}
Also used : BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Properties(org.apache.felix.utils.properties.Properties) Map(java.util.Map)

Example 95 with Properties

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);
}
Also used : IOException(java.io.IOException) Properties(org.apache.felix.utils.properties.Properties)

Aggregations

Properties (org.apache.felix.utils.properties.Properties)95 IOException (java.io.IOException)35 File (java.io.File)33 Test (org.junit.Test)27 Subject (javax.security.auth.Subject)25 NamePasswordCallbackHandler (org.apache.karaf.jaas.modules.NamePasswordCallbackHandler)21 Path (java.nio.file.Path)13 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)9 FileInputStream (java.io.FileInputStream)8 URL (java.net.URL)8 MalformedURLException (java.net.MalformedURLException)7 HashSet (java.util.HashSet)6 Hashtable (java.util.Hashtable)6 LinkedHashMap (java.util.LinkedHashMap)6 TreeMap (java.util.TreeMap)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 LoginException (javax.security.auth.login.LoginException)5