Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 5ECB8200CF5 for ; Sat, 12 Aug 2017 09:12:26 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 5D85B16C960; Sat, 12 Aug 2017 07:12:26 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 34C4216C984 for ; Sat, 12 Aug 2017 09:12:25 +0200 (CEST) Received: (qmail 81467 invoked by uid 500); 12 Aug 2017 07:12:24 -0000 Mailing-List: contact commits-help@karaf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@karaf.apache.org Delivered-To: mailing list commits@karaf.apache.org Received: (qmail 81374 invoked by uid 99); 12 Aug 2017 07:12:24 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 12 Aug 2017 07:12:24 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9216FF3308; Sat, 12 Aug 2017 07:12:23 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: cschneider@apache.org To: commits@karaf.apache.org Date: Sat, 12 Aug 2017 07:12:30 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [08/14] karaf git commit: Refactor FeatureConfigInstaller archived-at: Sat, 12 Aug 2017 07:12:26 -0000 Refactor FeatureConfigInstaller Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/244fafdc Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/244fafdc Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/244fafdc Branch: refs/heads/model_features Commit: 244fafdc40a6b8210c8f5b2c65a3f8f5d7f7f07e Parents: 5cc9c81 Author: Christian Schneider Authored: Fri Aug 11 12:01:28 2017 +0200 Committer: Christian Schneider Committed: Fri Aug 11 12:01:28 2017 +0200 ---------------------------------------------------------------------- .../service/FeatureConfigInstaller.java | 182 +++++++++---------- 1 file changed, 89 insertions(+), 93 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/244fafdc/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java ---------------------------------------------------------------------- diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java index 5a86e53..694427e 100644 --- a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java +++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureConfigInstaller.java @@ -62,15 +62,17 @@ public class FeatureConfigInstaller { this.configCfgStore = configCfgStore; } - private String[] parsePid(String pid) { + private ConfigId parsePid(String pid) { int n = pid.indexOf('-'); + ConfigId cid = new ConfigId(); + cid.fullPid = pid; if (n > 0) { - String factoryPid = pid.substring(n + 1); - pid = pid.substring(0, n); - return new String[]{pid, factoryPid}; + cid.factoryPid = pid.substring(n + 1); + cid.pid = pid.substring(0, n); } else { - return new String[]{pid, null}; + cid.pid = pid; } + return cid; } private Configuration createConfiguration(ConfigurationAdmin configurationAdmin, String pid, @@ -83,21 +85,16 @@ public class FeatureConfigInstaller { } } - private Configuration findExistingConfiguration(ConfigurationAdmin configurationAdmin, String pid, - String factoryPid) + private Configuration findExistingConfiguration(ConfigurationAdmin configurationAdmin, ConfigId cid) throws IOException, InvalidSyntaxException { String filter; - if (factoryPid == null) { - filter = "(" + Constants.SERVICE_PID + "=" + pid + ")"; + if (cid.factoryPid == null) { + filter = "(" + Constants.SERVICE_PID + "=" + cid.pid + ")"; } else { - String key = createConfigurationKey(pid, factoryPid); - filter = "(" + CONFIG_KEY + "=" + key + ")"; + filter = "(" + CONFIG_KEY + "=" + cid.fullPid + ")"; } Configuration[] configurations = configurationAdmin.listConfigurations(filter); - if (configurations != null && configurations.length > 0) { - return configurations[0]; - } - return null; + return (configurations != null && configurations.length > 0) ? configurations[0] : null; } public void installFeatureConfigs(Feature feature) throws IOException, InvalidSyntaxException { @@ -110,26 +107,20 @@ public class FeatureConfigInstaller { } else { props.load(new StringReader(val)); } - String[] pid = parsePid(config.getName()); - Configuration cfg = findExistingConfiguration(configAdmin, pid[0], pid[1]); + ConfigId cid = parsePid(config.getName()); + Configuration cfg = findExistingConfiguration(configAdmin, cid); if (cfg == null) { Dictionary cfgProps = convertToDict(props); - cfg = createConfiguration(configAdmin, pid[0], pid[1]); - String key = createConfigurationKey(pid[0], pid[1]); - cfgProps.put(CONFIG_KEY, key); - props.put(CONFIG_KEY, key); + cfg = createConfiguration(configAdmin, cid.pid, cid.factoryPid); + cfgProps.put(CONFIG_KEY, cid.fullPid); + props.put(CONFIG_KEY, cid.fullPid); if (storage != null && configCfgStore) { - File cfgFile; - if (pid[1] != null) { - cfgFile = new File(storage, pid[0] + "-" + pid[1] + ".cfg"); - } else { - cfgFile = new File(storage, pid[0] + ".cfg"); - } + File cfgFile = new File(storage, cid.fullPid + ".cfg"); cfgProps.put(FILEINSTALL_FILE_NAME, cfgFile.getAbsoluteFile().toURI().toString()); } cfg.update(cfgProps); try { - updateStorage(pid[0], pid[1], props, false); + updateStorage(cid, props, false); } catch (Exception e) { LOGGER.warn("Can't update cfg file", e); } @@ -145,7 +136,7 @@ public class FeatureConfigInstaller { if (update) { cfg.update(properties); try { - updateStorage(pid[0], pid[1], props, true); + updateStorage(cid, props, true); } catch (Exception e) { LOGGER.warn("Can't update cfg file", e); } @@ -166,10 +157,6 @@ public class FeatureConfigInstaller { return cfgProps; } - private String createConfigurationKey(String pid, String factoryPid) { - return factoryPid == null ? pid : pid + "-" + factoryPid; - } - /** * Substitute variables in the final name and append prefix if necessary. * @@ -260,77 +247,86 @@ public class FeatureConfigInstaller { } } - protected void updateStorage(String pid, String factoryPid, TypedProperties props, boolean append) + protected void updateStorage(ConfigId cid, 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"); + File cfgFile = getConfigFile(cid); + if (!cfgFile.exists()) { + props.save(cfgFile); } else { - cfgFile = new File(storage, pid + ".cfg"); + updateExistingConfig(props, append, cfgFile); } - Configuration cfg = findExistingConfiguration(configAdmin, pid, factoryPid); - // 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); + } + } + + private File getConfigFile(ConfigId cid) throws IOException, InvalidSyntaxException { + Configuration cfg = findExistingConfiguration(configAdmin, cid); + // update the cfg file depending of the configuration + File cfgFile = new File(storage, cid.fullPid + ".cfg"); + 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 comments = props.getComments(key); - List value = props.getRaw(key); - Object writeValue = (value.size() == 1) ? value.get(0) : value; - if (!properties.containsKey(key)) { - properties.put(key, comments, writeValue); - } else if (!append) { - if (comments.isEmpty()) { - comments = properties.getComments(key); - } - properties.put(key, comments, writeValue); - } + } + LOGGER.trace("Update {}", cfgFile.getName()); + return cfgFile; + } + + private void updateExistingConfig(TypedProperties props, boolean append, File cfgFile) + throws IOException { + TypedProperties properties = new TypedProperties(); + properties.load(cfgFile); + for (String key : props.keySet()) { + if (!isInternalKey(key)) { + List comments = props.getComments(key); + List value = props.getRaw(key); + Object writeValue = (value.size() == 1) ? value.get(0) : value; + if (!properties.containsKey(key)) { + properties.put(key, comments, writeValue); + } else if (!append) { + if (comments.isEmpty()) { + comments = properties.getComments(key); } + properties.put(key, comments, writeValue); } - if (!append) { - // remove "removed" properties from the cfg file - ArrayList 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); - } + } + } + if (!append) { + // remove "removed" properties from the cfg file + ArrayList propertiesToRemove = new ArrayList<>(); + for (String key : properties.keySet()) { + if (!props.containsKey(key) && !isInternalKey(key)) { + propertiesToRemove.add(key); } - // save the cfg file - storage.mkdirs(); - properties.save(cfgFile); + } + for (String key : propertiesToRemove) { + properties.remove(key); } } + storage.mkdirs(); + properties.save(cfgFile); + } + + private boolean isInternalKey(String key) { + return Constants.SERVICE_PID.equals(key) + || ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) + || FILEINSTALL_FILE_NAME.equals(key); } + class ConfigId { + String fullPid; + String pid; + String factoryPid; + } }