Search in sources :

Example 81 with Properties

use of org.apache.felix.utils.properties.Properties in project fabric8 by jboss-fuse.

the class DeploymentAgent method createDownloadExecutor.

protected ScheduledExecutorService createDownloadExecutor() {
    // TODO: this should not be loaded from a static file
    // TODO: or at least from the bundle context, but preferably from the config
    String size = DEFAULT_DOWNLOAD_THREADS;
    try {
        Properties customProps = new Properties(new File(KARAF_BASE + File.separator + "etc" + File.separator + "custom.properties"));
        size = customProps.getProperty(DOWNLOAD_THREADS, size);
    } catch (Exception e) {
    // ignore
    }
    int num = Integer.parseInt(size);
    LOGGER.info("Creating fabric-agent-download thread pool with size: {}", num);
    return Executors.newScheduledThreadPool(num, new NamedThreadFactory("fabric-agent-download"));
}
Also used : NamedThreadFactory(io.fabric8.utils.NamedThreadFactory) Properties(org.apache.felix.utils.properties.Properties) File(java.io.File) ConfigurationException(org.osgi.service.cm.ConfigurationException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException)

Example 82 with Properties

use of org.apache.felix.utils.properties.Properties in project fabric8 by jboss-fuse.

the class DeploymentAgent method doUpdate.

public boolean doUpdate(Dictionary<String, ?> props) throws Exception {
    if (props == null || Boolean.parseBoolean((String) props.get("disabled"))) {
        return false;
    }
    final Hashtable<String, String> properties = new Hashtable<>();
    for (Enumeration e = props.keys(); e.hasMoreElements(); ) {
        Object key = e.nextElement();
        Object val = props.get(key);
        if (!"service.pid".equals(key) && !FeatureConfigInstaller.FABRIC_ZOOKEEPER_PID.equals(key)) {
            properties.put(key.toString(), val.toString());
        }
    }
    updateStatus("analyzing", null);
    // Building configuration
    curatorCompleteService.waitForService(TimeUnit.SECONDS.toMillis(30));
    String httpUrl;
    List<URI> mavenRepoURIs;
    // force reading of updated informations from ZK
    if (!fabricService.isEmpty()) {
        updateMavenRepositoryConfiguration(fabricService.getService());
    }
    try {
        fabricServiceOperations.lock();
        // no one will change the members now
        httpUrl = this.httpUrl;
        mavenRepoURIs = this.mavenRepoURIs;
    } finally {
        fabricServiceOperations.unlock();
    }
    addMavenProxies(properties, httpUrl, mavenRepoURIs);
    final MavenResolver resolver = MavenResolvers.createMavenResolver(properties, "org.ops4j.pax.url.mvn");
    final DownloadManager manager = DownloadManagers.createDownloadManager(resolver, getDownloadExecutor());
    manager.addListener(new DownloadCallback() {

        @Override
        public void downloaded(StreamProvider provider) throws Exception {
            int pending = manager.pending();
            updateStatus(pending > 0 ? "downloading (" + pending + " pending)" : "downloading", null);
        }
    });
    // Update framework, libs, system and config props
    final Object lock = new Object();
    final AtomicBoolean restart = new AtomicBoolean();
    final Set<String> libsToRemove = new HashSet<>(managedLibs.keySet());
    final Set<String> endorsedLibsToRemove = new HashSet<>(managedEndorsedLibs.keySet());
    final Set<String> extensionLibsToRemove = new HashSet<>(managedExtensionLibs.keySet());
    final Set<String> sysPropsToRemove = new HashSet<>(managedSysProps.keySet());
    final Set<String> configPropsToRemove = new HashSet<>(managedConfigProps.keySet());
    final Set<String> etcsToRemove = new HashSet<>(managedEtcs.keySet());
    final Properties configProps = new Properties(new File(KARAF_BASE + File.separator + "etc" + File.separator + "config.properties"));
    final Properties systemProps = new Properties(new File(KARAF_BASE + File.separator + "etc" + File.separator + "system.properties"));
    Downloader downloader = manager.createDownloader();
    for (String key : properties.keySet()) {
        if (key.equals("framework")) {
            String url = properties.get(key);
            if (!url.startsWith("mvn:")) {
                throw new IllegalArgumentException("Framework url must use the mvn: protocol");
            }
            downloader.download(url, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File file = provider.getFile();
                    String path = file.getPath();
                    if (path.startsWith(KARAF_HOME)) {
                        path = path.substring(KARAF_HOME.length() + 1);
                    }
                    synchronized (lock) {
                        if (!path.equals(configProps.get("karaf.framework.felix"))) {
                            configProps.put("karaf.framework", "felix");
                            configProps.put("karaf.framework.felix", path);
                            restart.set(true);
                        }
                    }
                }
            });
        } else if (key.startsWith("config.")) {
            String k = key.substring("config.".length());
            String v = properties.get(key);
            synchronized (lock) {
                managedConfigProps.put(k, v);
                configPropsToRemove.remove(k);
                if (!v.equals(configProps.get(k))) {
                    configProps.put(k, v);
                    restart.set(true);
                }
            }
        } else if (key.startsWith("system.")) {
            String k = key.substring("system.".length());
            synchronized (lock) {
                String v = properties.get(key);
                managedSysProps.put(k, v);
                sysPropsToRemove.remove(k);
                if (!v.equals(systemProps.get(k))) {
                    systemProps.put(k, v);
                    restart.set(true);
                }
            }
        } else if (key.startsWith("lib.")) {
            String value = properties.get(key);
            downloader.download(value, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File libFile = provider.getFile();
                    String libName = libFile.getName();
                    Long checksum = ChecksumUtils.checksum(libFile);
                    boolean update;
                    synchronized (lock) {
                        managedLibs.put(libName, "true");
                        libsToRemove.remove(libName);
                        update = !Long.toString(checksum).equals(libChecksums.getProperty(libName));
                    }
                    if (update) {
                        Files.copy(libFile, new File(LIB_PATH, libName));
                        restart.set(true);
                    }
                }
            });
        } else if (key.startsWith("endorsed.")) {
            String value = properties.get(key);
            downloader.download(value, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File libFile = provider.getFile();
                    String libName = libFile.getName();
                    Long checksum = ChecksumUtils.checksum(new FileInputStream(libFile));
                    boolean update;
                    synchronized (lock) {
                        managedEndorsedLibs.put(libName, "true");
                        endorsedLibsToRemove.remove(libName);
                        update = !Long.toString(checksum).equals(endorsedChecksums.getProperty(libName));
                    }
                    if (update) {
                        Files.copy(libFile, new File(LIB_ENDORSED_PATH, libName));
                        restart.set(true);
                    }
                }
            });
        } else if (key.startsWith("extension.")) {
            String value = properties.get(key);
            downloader.download(value, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File libFile = provider.getFile();
                    String libName = libFile.getName();
                    Long checksum = ChecksumUtils.checksum(libFile);
                    boolean update;
                    synchronized (lock) {
                        managedExtensionLibs.put(libName, "true");
                        extensionLibsToRemove.remove(libName);
                        update = !Long.toString(checksum).equals(extensionChecksums.getProperty(libName));
                    }
                    if (update) {
                        Files.copy(libFile, new File(LIB_EXT_PATH, libName));
                        restart.set(true);
                    }
                }
            });
        } else if (key.startsWith("etc.")) {
            String value = properties.get(key);
            downloader.download(value, new DownloadCallback() {

                @Override
                public void downloaded(StreamProvider provider) throws Exception {
                    File etcFile = provider.getFile();
                    String etcName = etcFile.getName();
                    Long checksum = ChecksumUtils.checksum(new FileInputStream(etcFile));
                    boolean update;
                    synchronized (lock) {
                        managedEtcs.put(etcName, "true");
                        etcsToRemove.remove(etcName);
                        update = !Long.toString(checksum).equals(etcChecksums.getProperty(etcName));
                    }
                    if (update) {
                        Files.copy(etcFile, new File(KARAF_ETC, etcName));
                    }
                }
            });
        }
    }
    downloader.await();
    // Remove unused libs, system & config properties
    for (String sysProp : sysPropsToRemove) {
        systemProps.remove(sysProp);
        managedSysProps.remove(sysProp);
        System.clearProperty(sysProp);
        restart.set(true);
    }
    for (String configProp : configPropsToRemove) {
        configProps.remove(configProp);
        managedConfigProps.remove(configProp);
        restart.set(true);
    }
    for (String lib : libsToRemove) {
        File libFile = new File(LIB_PATH, lib);
        libFile.delete();
        libChecksums.remove(lib);
        managedLibs.remove(lib);
        restart.set(true);
    }
    for (String lib : endorsedLibsToRemove) {
        File libFile = new File(LIB_ENDORSED_PATH, lib);
        libFile.delete();
        endorsedChecksums.remove(lib);
        managedEndorsedLibs.remove(lib);
        restart.set(true);
    }
    for (String lib : extensionLibsToRemove) {
        File libFile = new File(LIB_EXT_PATH, lib);
        libFile.delete();
        extensionChecksums.remove(lib);
        managedExtensionLibs.remove(lib);
        restart.set(true);
    }
    for (String etc : etcsToRemove) {
        File etcFile = new File(KARAF_ETC, etc);
        etcFile.delete();
        etcChecksums.remove(etc);
        managedEtcs.remove(etc);
    }
    libChecksums.save();
    endorsedChecksums.save();
    extensionChecksums.save();
    etcChecksums.save();
    managedLibs.save();
    managedEndorsedLibs.save();
    managedExtensionLibs.save();
    managedConfigProps.save();
    managedSysProps.save();
    managedEtcs.save();
    if (restart.get()) {
        updateStatus("restarting", null);
        configProps.save();
        systemProps.save();
        System.setProperty("karaf.restart", "true");
        bundleContext.getBundle(0).stop();
        return false;
    }
    FeatureConfigInstaller configInstaller = null;
    ServiceReference configAdminServiceReference = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
    if (configAdminServiceReference != null) {
        ConfigurationAdmin configAdmin = (ConfigurationAdmin) bundleContext.getService(configAdminServiceReference);
        configInstaller = new FeatureConfigInstaller(bundleContext, configAdmin, manager);
    }
    int bundleStartTimeout = Constants.BUNDLE_START_TIMEOUT;
    String overriddenTimeout = properties.get(Constants.BUNDLE_START_TIMEOUT_PID_KEY);
    try {
        if (overriddenTimeout != null)
            bundleStartTimeout = Integer.parseInt(overriddenTimeout);
    } catch (Exception e) {
        LOGGER.warn("Failed to set {} value: [{}], applying default value: {}", Constants.BUNDLE_START_TIMEOUT_PID_KEY, overriddenTimeout, Constants.BUNDLE_START_TIMEOUT);
    }
    Agent agent = new Agent(bundleContext.getBundle(), systemBundleContext, manager, configInstaller, null, DEFAULT_FEATURE_RESOLUTION_RANGE, DEFAULT_BUNDLE_UPDATE_RANGE, DEFAULT_UPDATE_SNAPSHOTS, bundleContext.getDataFile(STATE_FILE), bundleStartTimeout) {

        @Override
        public void updateStatus(String status) {
            DeploymentAgent.this.updateStatus(status, null, false);
        }

        @Override
        public void updateStatus(String status, boolean force) {
            DeploymentAgent.this.updateStatus(status, null, force);
        }

        @Override
        protected void saveState(State newState) throws IOException {
            super.saveState(newState);
            DeploymentAgent.this.state.replace(newState);
        }

        @Override
        protected void provisionList(Set<Resource> resources) {
            DeploymentAgent.this.provisionList = resources;
        }

        @Override
        protected boolean done(boolean agentStarted, List<String> urls) {
            if (agentStarted) {
                // let's do patch-management "last touch" only if new agent wasn't started.
                return true;
            }
            // agent finished provisioning, we can call back to low level patch management
            ServiceReference<PatchManagement> srPm = systemBundleContext.getServiceReference(PatchManagement.class);
            ServiceReference<FabricService> srFs = systemBundleContext.getServiceReference(FabricService.class);
            if (srPm != null && srFs != null) {
                PatchManagement pm = systemBundleContext.getService(srPm);
                FabricService fs = systemBundleContext.getService(srFs);
                if (pm != null && fs != null) {
                    LOGGER.info("Validating baseline information");
                    this.updateStatus("validating baseline information", true);
                    Profile profile = fs.getCurrentContainer().getOverlayProfile();
                    Map<String, String> versions = profile.getConfiguration("io.fabric8.version");
                    File localRepository = resolver.getLocalRepository();
                    if (pm.alignTo(versions, urls, localRepository, new PatchSynchronization())) {
                        this.updateStatus("requires full restart", true);
                        // let's reuse the same flag
                        restart.set(true);
                        return false;
                    }
                    if (handleRestartJvmFlag(profile, restart)) {
                        return false;
                    }
                }
            }
            return true;
        }
    };
    agent.setDeploymentAgentId(deploymentAgentId);
    agent.provision(getPrefixedProperties(properties, "repository."), getPrefixedProperties(properties, "feature."), getPrefixedProperties(properties, "bundle."), getPrefixedProperties(properties, "req."), getPrefixedProperties(properties, "override."), getPrefixedProperties(properties, "optional."), getMetadata(properties, "metadata#"));
    if (restart.get()) {
        // prevent updating status to "success"
        return false;
    }
    return true;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) DownloadCallback(io.fabric8.agent.download.DownloadCallback) Downloader(io.fabric8.agent.download.Downloader) Properties(org.apache.felix.utils.properties.Properties) URI(java.net.URI) DownloadManager(io.fabric8.agent.download.DownloadManager) Profile(io.fabric8.api.Profile) PatchManagement(io.fabric8.patch.management.PatchManagement) MavenResolver(io.fabric8.maven.MavenResolver) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Agent(io.fabric8.agent.service.Agent) StreamProvider(io.fabric8.agent.download.StreamProvider) Enumeration(java.util.Enumeration) Hashtable(java.util.Hashtable) ConfigurationException(org.osgi.service.cm.ConfigurationException) BundleException(org.osgi.framework.BundleException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ServiceReference(org.osgi.framework.ServiceReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FeatureConfigInstaller(io.fabric8.agent.service.FeatureConfigInstaller) State(io.fabric8.agent.service.State) FabricService(io.fabric8.api.FabricService) File(java.io.File) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 83 with Properties

use of org.apache.felix.utils.properties.Properties in project fabric8 by jboss-fuse.

the class PropertiesLoader method loadSystemProperties.

/**
 * <p>
 * Loads the properties in the system property file associated with the
 * framework installation into <tt>System.setProperty()</tt>. These properties
 * are not directly used by the framework in anyway. By default, the system
 * property file is located in the <tt>conf/</tt> directory of the Felix
 * installation directory and is called "<tt>system.properties</tt>". The
 * installation directory of Felix is assumed to be the parent directory of
 * the <tt>felix.jar</tt> file as found on the system class path property.
 * The precise file from which to load system properties can be set by
 * initializing the "<tt>felix.system.properties</tt>" system property to an
 * arbitrary URL.
 * </p>
 *
 * @param karafBase the karaf base folder
 * @throws IOException
 */
static void loadSystemProperties(File file) throws IOException {
    Properties props = new Properties(false);
    try {
        InputStream is = new FileInputStream(file);
        props.load(is);
        is.close();
    } catch (Exception e1) {
    // Ignore
    }
    for (Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); ) {
        String name = (String) e.nextElement();
        if (name.startsWith(OVERRIDE_PREFIX)) {
            String overrideName = name.substring(OVERRIDE_PREFIX.length());
            String value = props.getProperty(name);
            System.setProperty(overrideName, substVars(value, name, null, props));
        } else {
            String value = System.getProperty(name, props.getProperty(name));
            System.setProperty(name, substVars(value, name, null, props));
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Properties(org.apache.felix.utils.properties.Properties) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 84 with Properties

use of org.apache.felix.utils.properties.Properties in project fabric8 by jboss-fuse.

the class PropertiesLoader method loadConfigProperties.

/**
 * <p>
 * Loads the configuration properties in the configuration property file
 * associated with the framework installation; these properties
 * are accessible to the framework and to bundles and are intended
 * for configuration purposes. By default, the configuration property
 * file is located in the <tt>conf/</tt> directory of the Felix
 * installation directory and is called "<tt>config.properties</tt>".
 * The installation directory of Felix is assumed to be the parent
 * directory of the <tt>felix.jar</tt> file as found on the system class
 * path property. The precise file from which to load configuration
 * properties can be set by initializing the "<tt>felix.config.properties</tt>"
 * system property to an arbitrary URL.
 * </p>
 *
 * @return A <tt>Properties</tt> instance or <tt>null</tt> if there was an error.
 * @throws Exception if something wrong occurs
 */
static Properties loadConfigProperties(File file) throws Exception {
    // See if the property URL was specified as a property.
    URL configPropURL;
    try {
        configPropURL = file.toURI().toURL();
    } catch (MalformedURLException ex) {
        System.err.print("Main: " + ex);
        return null;
    }
    Properties configProps = loadPropertiesFile(configPropURL, false);
    copySystemProperties(configProps);
    configProps.substitute();
    return configProps;
}
Also used : MalformedURLException(java.net.MalformedURLException) Properties(org.apache.felix.utils.properties.Properties) URL(java.net.URL)

Example 85 with Properties

use of org.apache.felix.utils.properties.Properties in project fabric8 by jboss-fuse.

the class PropertiesFileResolver method resolve.

@Override
public String resolve(File firstChange, File base, File secondChange, boolean useFirstChangeAsBase, boolean rollback) /*=false*/
{
    try {
        Properties baseProperties = new Properties(false);
        if (base != null) {
            baseProperties.load(base);
        }
        Properties firstProperties = new Properties(false);
        firstProperties.load(firstChange);
        Properties secondProperties = new Properties(secondChange, false);
        secondProperties.load(secondChange);
        Properties result = useFirstChangeAsBase ? firstProperties : secondProperties;
        Properties otherSource = useFirstChangeAsBase ? secondProperties : firstProperties;
        // first let's iterate over what we have in selected base - we may already find new properties in
        // incoming change vs. base version
        Set<String> keys = new HashSet<>();
        for (Enumeration<?> e = result.propertyNames(); e.hasMoreElements(); ) {
            keys.add((String) e.nextElement());
        }
        for (String key : keys) {
            // treat more important properties (result) as "ours"
            Change state = kindOfChange(key, baseProperties, result, otherSource);
            switch(state) {
                case NONE:
                case ADDED_BY_US:
                case MODIFIED_BY_US:
                    // already reflected in "result" properties
                    break;
                case DELETED_BY_US:
                case BOTH_DELETED:
                case ADDED_BY_THEM:
                    // can't happen in this loop
                    break;
                case BOTH_ADDED:
                    result.put(key, specialPropertyMerge(key, firstProperties, secondProperties, rollback));
                    break;
                case BOTH_MODIFIED:
                    // may mean also that we have change vs. removal
                    if (secondProperties.getProperty(key) == null) {
                        result.remove(key);
                    } else {
                        result.put(key, specialPropertyMerge(key, firstProperties, secondProperties, rollback));
                    }
                    break;
                case DELETED_BY_THEM:
                    result.remove(key);
                    break;
                case MODIFIED_BY_THEM:
                    result.put(key, otherSource.getProperty(key));
                    break;
            }
        }
        // then we can have additions in less important change, for example if patch adds new properties
        // but we want to preserve layout of properties file from user (it may have user comments for example)
        // we will handle only properties added in "otherSource"
        keys.clear();
        for (Enumeration<?> e = otherSource.propertyNames(); e.hasMoreElements(); ) {
            keys.add((String) e.nextElement());
        }
        for (Enumeration<?> e = result.propertyNames(); e.hasMoreElements(); ) {
            String key = (String) e.nextElement();
            keys.remove(key);
        }
        for (String key : keys) {
            // treat more important properties (result) as "ours"
            Change state = kindOfChange(key, baseProperties, result, otherSource);
            switch(state) {
                case NONE:
                case BOTH_DELETED:
                case BOTH_ADDED:
                case BOTH_MODIFIED:
                case ADDED_BY_US:
                case MODIFIED_BY_US:
                case DELETED_BY_THEM:
                case DELETED_BY_US:
                    break;
                case ADDED_BY_THEM:
                case MODIFIED_BY_THEM:
                    result.put(key, otherSource.getProperty(key));
                    break;
            }
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        result.store(baos, null);
        return new String(baos.toByteArray(), "UTF-8");
    } catch (Exception e) {
        Activator.log(LogService.LOG_ERROR, null, "Problem resolving conflict: " + e.getMessage(), e, true);
    }
    return null;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(org.apache.felix.utils.properties.Properties) HashSet(java.util.HashSet)

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