Search in sources :

Example 1 with Properties

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

the class ConfigRepositoryImpl method updateStorage.

protected void updateStorage(String pid, Dictionary<String, Object> props) throws IOException {
    if (storage != null) {
        Configuration cfg = configAdmin.getConfiguration(pid, null);
        // Initialize cfgFile with default location. Value gets overwritten when the existing configuration references a correct location.
        File cfgFile = new File(storage, pid + ".cfg");
        if (cfg != null) {
            Dictionary<String, Object> oldProps = cfg.getProperties();
            if (oldProps != null && oldProps.get(FILEINSTALL_FILE_NAME) != null) {
                try {
                    cfgFile = getCfgFileFromProperties(oldProps);
                    if (cfgFile == null) {
                        throw new IOException("The configuration value '" + oldProps.get(FILEINSTALL_FILE_NAME) + "' for '" + FILEINSTALL_FILE_NAME + "' does not represent a valid file location.");
                    }
                } catch (URISyntaxException | MalformedURLException e) {
                    throw new IOException(e);
                }
            }
        }
        LOGGER.trace("Update {}", cfgFile.getName());
        // update the cfg file
        Properties properties = new Properties(cfgFile);
        for (Enumeration<String> keys = props.keys(); keys.hasMoreElements(); ) {
            String key = keys.nextElement();
            if (!Constants.SERVICE_PID.equals(key) && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) && !FILEINSTALL_FILE_NAME.equals(key)) {
                if (props.get(key) != null) {
                    properties.put(key, props.get(key).toString());
                }
            }
        }
        // remove "removed" properties from the cfg file
        ArrayList<String> propertiesToRemove = new ArrayList<>();
        for (String key : properties.keySet()) {
            if (props.get(key) == null && !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();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Configuration(org.osgi.service.cm.Configuration) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URISyntaxException(java.net.URISyntaxException) Properties(org.apache.felix.utils.properties.Properties) File(java.io.File)

Example 2 with Properties

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

the class PropertiesLoginModuleTest method testCannotLoginAsGroupDirectly.

private void testCannotLoginAsGroupDirectly(final String name) throws IOException, LoginException {
    File f = File.createTempFile(getClass().getName(), ".tmp");
    try {
        Properties p = new Properties(f);
        PropertiesBackingEngine pbe = new PropertiesBackingEngine(p);
        pbe.addUser("abc", "xyz");
        pbe.addRole("abc", "myrole");
        pbe.addUser("pqr", "abc");
        pbe.addGroup("pqr", "group1");
        pbe.addGroupRole("group1", "r1");
        PropertiesLoginModule module = new PropertiesLoginModule();
        Map<String, String> options = new HashMap<>();
        options.put(PropertiesLoginModule.USER_FILE, f.getAbsolutePath());
        module.initialize(new Subject(), new NamePasswordCallbackHandler(name, "group"), null, options);
        try {
            module.login();
            Assert.fail("The login should have failed as you cannot log in under a group name directly");
        } catch (FailedLoginException fle) {
        // good
        }
    } finally {
        if (!f.delete()) {
            Assert.fail("Could not delete temporary file: " + f);
        }
    }
}
Also used : FailedLoginException(javax.security.auth.login.FailedLoginException) HashMap(java.util.HashMap) NamePasswordCallbackHandler(org.apache.karaf.jaas.modules.NamePasswordCallbackHandler) Properties(org.apache.felix.utils.properties.Properties) File(java.io.File) Subject(javax.security.auth.Subject)

Example 3 with Properties

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

the class ConfigProperties method performInit.

public void performInit() throws Exception {
    File cleanAllIndicatorFile = new File(karafData, "clean_all");
    File cleanCacheIndicatorFile = new File(karafData, "clean_cache");
    if (Boolean.getBoolean("karaf.clean.all") || cleanAllIndicatorFile.exists()) {
        if (cleanAllIndicatorFile.exists()) {
            cleanAllIndicatorFile.delete();
        }
        Utils.deleteDirectory(this.karafData);
        this.karafData = Utils.getKarafDirectory(PROP_KARAF_DATA, ENV_KARAF_DATA, new File(karafBase, "data"), true, true);
    } else {
        if (Boolean.getBoolean("karaf.clean.cache") || cleanCacheIndicatorFile.exists()) {
            if (cleanCacheIndicatorFile.exists()) {
                cleanCacheIndicatorFile.delete();
            }
            File karafCache = Utils.validateDirectoryExists(new File(karafData, "cache").getPath(), "Invalid cache directory", true, true);
            Utils.deleteDirectory(karafCache);
        }
    }
    String frameworkStoragePath = props.getProperty(Constants.FRAMEWORK_STORAGE);
    if (frameworkStoragePath == null) {
        File storage = new File(karafData.getPath(), "cache");
        try {
            storage.mkdirs();
        } catch (SecurityException se) {
            throw new Exception(se.getMessage());
        }
        props.setProperty(Constants.FRAMEWORK_STORAGE, storage.getAbsolutePath());
    }
    if (shutdownCommand == null || shutdownCommand.isEmpty()) {
        try {
            shutdownCommand = UUID.randomUUID().toString();
            Properties temp = new Properties(new File(karafEtc, CONFIG_PROPERTIES_FILE_NAME));
            temp.put(KARAF_SHUTDOWN_COMMAND, Arrays.asList("", "#", "# Generated command shutdown", "#"), shutdownCommand);
            temp.save();
        } catch (IOException ioException) {
            System.err.println("WARN: can't update etc/config.properties with the generated command shutdown. We advise to manually add the karaf.shutdown.command property.");
        }
    }
    if (threadMonitoring) {
        ThreadMXBean threadsBean = ManagementFactory.getThreadMXBean();
        if (threadsBean.isThreadCpuTimeSupported()) {
            threadsBean.setThreadCpuTimeEnabled(true);
        }
        if (threadsBean.isThreadContentionMonitoringSupported()) {
            threadsBean.setThreadContentionMonitoringEnabled(true);
        }
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) IOException(java.io.IOException) Properties(org.apache.felix.utils.properties.Properties) File(java.io.File) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with Properties

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

the class BootstrapLogManager method getLogFilePath.

String getLogFilePath() {
    String filename = configProps == null ? null : configProps.getProperty(KARAF_BOOTSTRAP_LOG);
    if (filename != null) {
        return filename;
    }
    Properties props = loadPaxLoggingConfig();
    // Make a best effort to log to the default file appender configured for log4j
    return props.getProperty(LOG4J_APPENDER_FILE, "${karaf.data}/log/karaf.log");
}
Also used : Properties(org.apache.felix.utils.properties.Properties)

Example 5 with Properties

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

the class BootstrapLogManager method loadPaxLoggingConfig.

private Properties loadPaxLoggingConfig() {
    Properties props = new Properties();
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(log4jConfigPath);
        props.load(fis);
    } catch (Exception e) {
    // Ignore
    } finally {
        close(fis);
    }
    return props;
}
Also used : Properties(org.apache.felix.utils.properties.Properties) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

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