Return-Path: X-Original-To: apmail-incubator-cloudstack-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-cloudstack-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 27308EC25 for ; Mon, 14 Jan 2013 18:00:48 +0000 (UTC) Received: (qmail 32187 invoked by uid 500); 14 Jan 2013 18:00:36 -0000 Delivered-To: apmail-incubator-cloudstack-commits-archive@incubator.apache.org Received: (qmail 32122 invoked by uid 500); 14 Jan 2013 18:00:36 -0000 Mailing-List: contact cloudstack-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cloudstack-dev@incubator.apache.org Delivered-To: mailing list cloudstack-commits@incubator.apache.org Received: (qmail 30846 invoked by uid 99); 14 Jan 2013 18:00:35 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Jan 2013 18:00:34 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id CCE731A415; Mon, 14 Jan 2013 18:00:34 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: bfederle@apache.org To: cloudstack-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [9/51] git commit: PropertiesUtil: Refactor process config file method in utils, return map of key=value Message-Id: <20130114180034.CCE731A415@tyr.zones.apache.org> Date: Mon, 14 Jan 2013 18:00:34 +0000 (UTC) PropertiesUtil: Refactor process config file method in utils, return map of key=value Signed-off-by: Rohit Yadav Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/f2ae0ae5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/f2ae0ae5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/f2ae0ae5 Branch: refs/heads/ui-quick-view-v2 Commit: f2ae0ae5ae70fbb5f33c004e05e4a17327094651 Parents: 1ac48bc Author: Rohit Yadav Authored: Thu Jan 10 15:29:01 2013 -0800 Committer: Rohit Yadav Committed: Thu Jan 10 15:55:01 2013 -0800 ---------------------------------------------------------------------- utils/src/com/cloud/utils/PropertiesUtil.java | 40 ++++++++++++++++++++ 1 files changed, 40 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/f2ae0ae5/utils/src/com/cloud/utils/PropertiesUtil.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/PropertiesUtil.java b/utils/src/com/cloud/utils/PropertiesUtil.java index 3909ca8..90f8af8 100755 --- a/utils/src/com/cloud/utils/PropertiesUtil.java +++ b/utils/src/com/cloud/utils/PropertiesUtil.java @@ -17,6 +17,8 @@ package com.cloud.utils; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -28,6 +30,7 @@ import java.util.Set; import org.apache.log4j.Logger; public class PropertiesUtil { + private static final Logger s_logger = Logger.getLogger(PropertiesUtil.class); /** * Searches the class path and local paths to find the config file. * @param path path to find. if it starts with / then it's absolute path. @@ -116,4 +119,41 @@ public class PropertiesUtil { } return null; } + + // Returns key=value pairs by parsing a commands.properties/config file + // with syntax; key=cmd;value (with this syntax cmd is stripped) and key=value + public static Map processConfigFile(String[] configFiles) { + Map configMap = new HashMap(); + Properties preProcessedCommands = new Properties(); + for (String configFile : configFiles) { + File commandsFile = findConfigFile(configFile); + if (commandsFile != null) { + try { + preProcessedCommands.load(new FileInputStream(commandsFile)); + } catch (FileNotFoundException fnfex) { + // in case of a file within a jar in classpath, try to open stream using url + InputStream stream = PropertiesUtil.openStreamFromURL(configFile); + if (stream != null) { + try { + preProcessedCommands.load(stream); + } catch (IOException e) { + s_logger.error("IO Exception, unable to find properties file:", fnfex); + } + } else { + s_logger.error("Unable to find properites file", fnfex); + } + } catch (IOException ioe) { + s_logger.error("IO Exception loading properties file", ioe); + } + } + } + + for (Object key : preProcessedCommands.keySet()) { + String preProcessedCommand = preProcessedCommands.getProperty((String) key); + int splitIndex = preProcessedCommand.lastIndexOf(";"); + String value = preProcessedCommand.substring(splitIndex+1); + configMap.put((String)key, value); + } + return configMap; + } }