karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject [karaf] 01/02: Clean a bit the console branding loading code
Date Thu, 18 Jan 2018 16:12:19 GMT
This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git

commit 3dec614120d7eda283e03afd45cb89074e0608d7
Author: Guillaume Nodet <gnodet@gmail.com>
AuthorDate: Thu Jan 18 17:11:23 2018 +0100

    Clean a bit the console branding loading code
---
 .../apache/karaf/shell/impl/console/Branding.java  | 63 +++++++---------------
 .../shell/impl/console/ConsoleSessionImpl.java     |  2 +-
 2 files changed, 19 insertions(+), 46 deletions(-)

diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/Branding.java b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/Branding.java
index 6af4691..775c5ea 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/Branding.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/Branding.java
@@ -18,7 +18,6 @@
  */
 package org.apache.karaf.shell.impl.console;
 
-import org.apache.karaf.shell.api.console.Terminal;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -31,60 +30,34 @@ public final class Branding {
 
     private Branding() { }
 
-    public static Properties loadBrandingProperties() {
+    public static Properties loadBrandingProperties(boolean ssh) {
         Properties props = new Properties();
-        loadProps(props, "org/apache/karaf/shell/console/branding.properties");
-        loadProps(props, "org/apache/karaf/branding/branding.properties");
+        String name = ssh ? "branding-ssh.properties" : "branding.properties";
+        loadPropsFromResource(props, "org/apache/karaf/shell/console/" + name);
+        loadPropsFromResource(props, "org/apache/karaf/branding/" + name);
+        loadPropsFromFile(props, System.getProperty("karaf.etc") + "/" + name);
         return props;
     }
 
-    public static Properties loadBrandingProperties(Terminal terminal) {
-        Properties props = new Properties();
-        if (terminal != null && terminal.getClass().getName().endsWith("SshTerminal"))
{
-            loadProps(props, "org/apache/karaf/shell/console/branding-ssh.properties");
-            loadProps(props, "org/apache/karaf/branding/branding.properties");
-            return loadEtcBrandingFile("branding-ssh.properties",props);
-        } else {
-            loadProps(props, "org/apache/karaf/shell/console/branding.properties");
-            loadProps(props, "org/apache/karaf/branding/branding.properties");
-            return loadEtcBrandingFile("branding.properties",props);
+    private static void loadPropsFromFile(Properties props, String fileName) {
+        try (FileInputStream is = new FileInputStream(fileName)) {
+            loadProps(props, is);
+        } catch (IOException e) {
+            LOGGER.trace("Could not load branding.", e);
         }
     }
 
-    private static Properties loadEtcBrandingFile(String fileName, Properties props){
-        File etcBranding = new File(System.getProperty("karaf.etc"), fileName);
-        if (etcBranding.exists()) {
-            FileInputStream etcBrandingIs = null;
-            try {
-                etcBrandingIs = new FileInputStream(etcBranding);
-            } catch (FileNotFoundException e) {
-                LOGGER.trace("Could not load branding.", e);
-            }
-            loadProps(props, etcBrandingIs);
+    private static void loadPropsFromResource(Properties props, String resource) {
+        try (InputStream is = Branding.class.getClassLoader().getResourceAsStream(resource))
{
+            loadProps(props, is);
+        } catch (IOException e) {
+            LOGGER.trace("Could not load branding.", e);
         }
-        return props;
     }
 
-    protected static void loadProps(Properties props, String resource) {
-        InputStream is = Branding.class.getClassLoader().getResourceAsStream(resource);
-        loadProps(props, is);
-    }
-
-    protected static void loadProps(Properties props, InputStream is) {
-        try {
-            if (is != null) {
-                props.load(is);
-            }
-        } catch (IOException e) {
-            // ignore
-        } finally {
-            if (is != null) {
-                try {
-                    is.close();
-                } catch (IOException e) {
-                    // Ignore
-                }
-            }
+    private static void loadProps(Properties props, InputStream is) throws IOException {
+        if (is != null) {
+            props.load(is);
         }
     }
 
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java
b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java
index b0e2765..1563ad0 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java
@@ -153,7 +153,7 @@ public class ConsoleSessionImpl implements Session {
             }
         }
 
-        brandingProps = Branding.loadBrandingProperties(terminal);
+        brandingProps = Branding.loadBrandingProperties(terminal.getClass().getName().endsWith("SshTerminal"));
 
         // Create session
         session = processor.createSession(jlineTerminal.input(),

-- 
To stop receiving notification emails like this one, please contact
"commits@karaf.apache.org" <commits@karaf.apache.org>.

Mime
View raw message