/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.karaf.shell.impl.console;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.karaf.shell.api.console.Terminal;


public final class Branding {
   
    static final Logger LOGGER = LoggerFactory.getLogger(Branding.class);
     
    private Branding() { }

    public static Properties loadBrandingProperties() {
        Properties props = new Properties();
        loadProps(props, "org/apache/karaf/shell/console/branding.properties");
        loadProps(props, "org/apache/karaf/branding/branding.properties");
        return props;
    }

    public static Properties loadBrandingProperties(Terminal terminal) {
        Properties props = new Properties();
        if (terminal != null && terminal.getClass().getName().endsWith("SshTerminal")) {
            //it's a ssh client, so load branding seperately
            loadProps(props, "org/apache/karaf/shell/console/branding-ssh.properties");
        } else {
            loadProps(props, "org/apache/karaf/shell/console/branding.properties");
        }

        loadProps(props, "org/apache/karaf/branding/branding.properties");
        // load branding from etc/branding.properties
        File etcBranding = new File(System.getProperty("karaf.etc"), "branding.properties");
        if (etcBranding.exists()) {
            FileInputStream etcBrandingIs = null;
            try {
                etcBrandingIs = new FileInputStream(etcBranding);
            } catch (FileNotFoundException e) {
                LOGGER.trace("Could not load branding.", e);
            }
            loadProps(props, etcBrandingIs);
        }

        return props;
    }
    
    protected static void loadProps(Properties props, String resource) {
        InputStream is = null;
        try {
            is = Branding.class.getClassLoader().getResourceAsStream(resource);
            if (is != null) {
                props.load(is);
            }
        } catch (IOException e) {
            // ignore
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }
    }

    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
                }
            }
        }
    }

}

@@ -857,177 +857,52 @@
 ort 
-java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.Properties;%0A
+org.apache.karaf.shell.api.console.Terminal;
 %0Aimp
@@ -967,51 +967,46 @@
 ort 
-org.apache.karaf.shell.api.console.Terminal
+java.io.*;%0Aimport java.util.Properties
 ;%0A%0A%0A
@@ -1035,20 +1035,16 @@
 nding %7B%0A
-   %0A
     stat
@@ -1109,21 +1109,16 @@
 class);%0A
-     
 %0A    pri
@@ -1634,69 +1634,8 @@
 ) %7B%0A
-            //it's a ssh client, so load branding seperately%0A
     
@@ -1710,32 +1710,105 @@
 h.properties%22);%0A
+            return loadEtcBrandingFile(%22branding-ssh.properties%22,props);%0A
         %7D else %7B
@@ -1904,63 +1904,40 @@
     
-%7D%0A%0A
     
-    loadProps(props, %22org/apache/karaf/b
+return loadEtcB
 randing
-/
+File(%22
 bran
@@ -1944,32 +1944,38 @@
 ding.properties%22
+,props
 );%0A        // lo
@@ -1973,44 +1973,85 @@
     
-//
+%7D%0A    %7D%0A%0A    private static Properties
  load
- b
+EtcB
 randing
- from etc/branding.p
+File(String fileName, P
 rope
@@ -2055,16 +2055,24 @@
 operties
+ props)%7B
 %0A       
@@ -2133,37 +2133,24 @@
 .etc%22), 
-%22branding.properties%22
+fileName
 );%0A     
@@ -2488,25 +2488,24 @@
 ;%0A        %7D%0A
-%0A
         retu
@@ -2520,20 +2520,16 @@
 ;%0A    %7D%0A
-    
 %0A    pro
@@ -2622,45 +2622,8 @@
 is =
- null;%0A        try %7B%0A            is =
  Bra
@@ -2694,349 +2694,29 @@
     
-    if (is != null) %7B%0A                props.load(is);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            // ignore%0A        %7D finally %7B%0A            if (is != null) %7B%0A                try %7B%0A                    is.close();%0A                %7D catch (IOException e) %7B%0A                    // Ignore%0A                %7D%0A            %7D%0A        %7D
+loadProps(props, is);
 %0A   

Add support of etc/branding-ssh.properties file to brand the ssh terminal