Search in sources :

Example 1 with TypedProperties

use of org.apache.felix.utils.properties.TypedProperties in project karaf by apache.

the class InstanceServiceImpl method getKarafHost.

private String getKarafHost(State state, String name, String path, final String key) {
    InstanceState instance = state.instances.get(name);
    if (instance == null) {
        throw new IllegalArgumentException("Instance " + name + " not found");
    }
    File f = new File(instance.loc, path);
    try {
        return FileLockUtils.execute(f, (TypedProperties properties) -> properties.get(key).toString(), false);
    } catch (IOException e) {
        return "0.0.0.0";
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) TypedProperties(org.apache.felix.utils.properties.TypedProperties) File(java.io.File)

Example 2 with TypedProperties

use of org.apache.felix.utils.properties.TypedProperties in project karaf by apache.

the class FileLockUtils method load.

private static TypedProperties load(RandomAccessFile raf) throws IOException {
    byte[] buffer = new byte[(int) raf.length()];
    raf.readFully(buffer);
    TypedProperties props = new TypedProperties();
    props.load(new ByteArrayInputStream(buffer));
    return props;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TypedProperties(org.apache.felix.utils.properties.TypedProperties)

Example 3 with TypedProperties

use of org.apache.felix.utils.properties.TypedProperties in project karaf by apache.

the class FeatureConfigInstaller method installFeatureConfigs.

public void installFeatureConfigs(Feature feature) throws IOException, InvalidSyntaxException {
    for (ConfigInfo config : feature.getConfigurations()) {
        TypedProperties props = new TypedProperties();
        // trim lines
        String val = config.getValue();
        if (config.isExternal()) {
            props.load(new URL(val));
        } else {
            props.load(new StringReader(val));
        }
        String[] pid = parsePid(config.getName());
        Configuration cfg = findExistingConfiguration(configAdmin, pid[0], pid[1]);
        if (cfg == null) {
            Dictionary<String, Object> cfgProps = convertToDict(props);
            cfg = createConfiguration(configAdmin, pid[0], pid[1]);
            String key = createConfigurationKey(pid[0], pid[1]);
            cfgProps.put(CONFIG_KEY, key);
            cfg.update(cfgProps);
            try {
                updateStorage(pid[0], pid[1], props, false);
            } catch (Exception e) {
                LOGGER.warn("Can't update cfg file", e);
            }
        } else if (config.isAppend()) {
            boolean update = false;
            Dictionary<String, Object> properties = cfg.getProperties();
            for (String key : props.keySet()) {
                if (properties.get(key) == null) {
                    properties.put(key, props.get(key));
                    update = true;
                }
            }
            if (update) {
                cfg.update(properties);
                try {
                    updateStorage(pid[0], pid[1], props, true);
                } catch (Exception e) {
                    LOGGER.warn("Can't update cfg file", e);
                }
            }
        }
    }
    for (ConfigFileInfo configFile : feature.getConfigurationFiles()) {
        installConfigurationFile(configFile.getLocation(), configFile.getFinalname(), configFile.isOverride());
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) StringReader(java.io.StringReader) ConfigFileInfo(org.apache.karaf.features.ConfigFileInfo) ConfigInfo(org.apache.karaf.features.ConfigInfo) TypedProperties(org.apache.felix.utils.properties.TypedProperties) URL(java.net.URL) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 4 with TypedProperties

use of org.apache.felix.utils.properties.TypedProperties in project karaf by apache.

the class InstanceHelper method updateInstancePid.

static void updateInstancePid(final File karafHome, final File karafBase, final boolean isStartingInstance) {
    try {
        final String instanceName = System.getProperty("karaf.name");
        final String pid = isStartingInstance ? getPid() : "0";
        if (instanceName != null) {
            String storage = System.getProperty("karaf.instances");
            if (storage == null) {
                throw new Exception("System property 'karaf.instances' is not set. \n" + "This property needs to be set to the full path of the instance.properties file.");
            }
            File storageFile = new File(storage);
            final File propertiesFile = new File(storageFile, "instance.properties");
            if (!propertiesFile.getParentFile().exists()) {
                try {
                    if (!propertiesFile.getParentFile().mkdirs()) {
                        throw new Exception("Unable to create directory " + propertiesFile.getParentFile());
                    }
                } catch (SecurityException se) {
                    throw new Exception(se.getMessage());
                }
            }
            // don't instance.properties if we're stopping and can't acquire lock
            if (!isStartingInstance) {
                boolean proceed = true;
                try (RandomAccessFile raf = new RandomAccessFile(propertiesFile, "rw")) {
                    FileLock lock = raf.getChannel().tryLock();
                    if (lock == null) {
                        proceed = false;
                    } else {
                        lock.release();
                    }
                }
                if (!proceed) {
                    // stopping the child
                    return;
                }
            }
            FileLockUtils.execute(propertiesFile, (TypedProperties props) -> {
                if (props.isEmpty()) {
                    // it's the first instance running, so we consider as root
                    props.put("count", "1");
                    props.put("item.0.name", instanceName);
                    props.put("item.0.loc", karafBase.getAbsolutePath());
                    props.put("item.0.pid", pid);
                    props.put("item.0.root", "true");
                } else {
                    int count = Integer.parseInt(props.get("count").toString());
                    for (int i = 0; i < count; i++) {
                        String name = props.get("item." + i + ".name").toString();
                        if (name.equals(instanceName)) {
                            props.put("item." + i + ".pid", pid);
                            return;
                        }
                    }
                    // it's not found, let assume it's the root instance, so 0
                    props.put("item.0.name", instanceName);
                    props.put("item.0.pid", pid);
                }
            }, true);
        }
    } catch (Exception e) {
        System.err.println("Unable to update instance pid: " + e.getMessage());
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) TypedProperties(org.apache.felix.utils.properties.TypedProperties) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 5 with TypedProperties

use of org.apache.felix.utils.properties.TypedProperties in project karaf by apache.

the class FeatureConfigInstaller method updateStorage.

protected void updateStorage(String pid, String factoryPid, TypedProperties props, boolean append) throws Exception {
    if (storage != null && configCfgStore) {
        // get the cfg file
        File cfgFile;
        if (factoryPid != null) {
            cfgFile = new File(storage, pid + "-" + factoryPid + ".cfg");
        } else {
            cfgFile = new File(storage, pid + ".cfg");
        }
        Configuration cfg = findExistingConfiguration(configAdmin, factoryPid, pid);
        // update the cfg file depending of the configuration
        if (cfg != null && cfg.getProperties() != null) {
            Object val = cfg.getProperties().get(FILEINSTALL_FILE_NAME);
            try {
                if (val instanceof URL) {
                    cfgFile = new File(((URL) val).toURI());
                }
                if (val instanceof URI) {
                    cfgFile = new File((URI) val);
                }
                if (val instanceof String) {
                    cfgFile = new File(new URL((String) val).toURI());
                }
            } catch (Exception e) {
                throw new IOException(e.getMessage(), e);
            }
        }
        LOGGER.trace("Update {}", cfgFile.getName());
        // update the cfg file
        if (!cfgFile.exists()) {
            props.save(cfgFile);
        } else {
            TypedProperties properties = new TypedProperties();
            properties.load(cfgFile);
            for (String key : props.keySet()) {
                if (!Constants.SERVICE_PID.equals(key) && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) && !FILEINSTALL_FILE_NAME.equals(key)) {
                    List<String> comments = props.getComments(key);
                    List<String> value = props.getRaw(key);
                    if (!properties.containsKey(key)) {
                        properties.put(key, comments, value);
                    } else if (!append) {
                        if (comments.isEmpty()) {
                            comments = properties.getComments(key);
                        }
                        properties.put(key, comments, value);
                    }
                }
            }
            if (!append) {
                // remove "removed" properties from the cfg file
                ArrayList<String> propertiesToRemove = new ArrayList<>();
                for (String key : properties.keySet()) {
                    if (!props.containsKey(key) && !Constants.SERVICE_PID.equals(key) && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) && !FILEINSTALL_FILE_NAME.equals(key)) {
                        propertiesToRemove.add(key);
                    }
                }
                for (String key : propertiesToRemove) {
                    properties.remove(key);
                }
            }
            // save the cfg file
            storage.mkdirs();
            properties.save(cfgFile);
        }
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException) TypedProperties(org.apache.felix.utils.properties.TypedProperties) File(java.io.File) URI(java.net.URI) URL(java.net.URL) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

TypedProperties (org.apache.felix.utils.properties.TypedProperties)9 IOException (java.io.IOException)4 File (java.io.File)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 Configuration (org.osgi.service.cm.Configuration)2 InterruptedIOException (java.io.InterruptedIOException)1 RandomAccessFile (java.io.RandomAccessFile)1 StringReader (java.io.StringReader)1 URI (java.net.URI)1 FileLock (java.nio.channels.FileLock)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConfigFileInfo (org.apache.karaf.features.ConfigFileInfo)1 ConfigInfo (org.apache.karaf.features.ConfigInfo)1 PlaceholderResolver (org.apache.karaf.profile.PlaceholderResolver)1 ProfileBuilder (org.apache.karaf.profile.ProfileBuilder)1