/*
 * 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.ssh;

import jline.TerminalSupport;
import org.apache.karaf.shell.api.console.Signal;
import org.apache.karaf.shell.api.console.SignalListener;
import org.apache.karaf.shell.api.console.Terminal;
import org.apache.sshd.server.Environment;

import java.util.EnumSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

public class SshTerminal extends TerminalSupport implements Terminal {

    private Environment environment;
    private final Map<Signal, Set<SignalListener>> listeners;

    public SshTerminal(Environment environment) {
        super(true);
        setAnsiSupported(true);
        listeners = new ConcurrentHashMap<>(3);
        this.environment = environment;
        this.environment.addSignalListener(new org.apache.sshd.server.SignalListener() {
            @Override
            public void signal(org.apache.sshd.server.Signal signal) {
                SshTerminal.this.signal(Signal.WINCH);
            }
        }, org.apache.sshd.server.Signal.WINCH);
    }

    public void init() throws Exception {
    }

    public void restore() throws Exception {
    }

    public void addSignalListener(SignalListener listener, Signal... signals) {
        if (signals == null) {
            throw new IllegalArgumentException("signals may not be null");
        }
        if (listener == null) {
            throw new IllegalArgumentException("listener may not be null");
        }
        for (Signal s : signals) {
            getSignalListeners(s, true).add(listener);
        }
    }

    public void addSignalListener(SignalListener listener) {
        addSignalListener(listener, EnumSet.allOf(Signal.class));
    }

    public void addSignalListener(SignalListener listener, EnumSet<Signal> signals) {
        if (signals == null) {
            throw new IllegalArgumentException("signals may not be null");
        }
        addSignalListener(listener, signals.toArray(new Signal[signals.size()]));
    }

    public void removeSignalListener(SignalListener listener) {
        if (listener == null) {
            throw new IllegalArgumentException("listener may not be null");
        }
        for (Signal s : EnumSet.allOf(Signal.class)) {
            final Set<SignalListener> ls = getSignalListeners(s, false);
            if (ls != null) {
                ls.remove(listener);
            }
        }
    }

    public void signal(Signal signal) {
        final Set<SignalListener> ls = getSignalListeners(signal, false);
        if (ls != null) {
            for (SignalListener l : ls) {
                l.signal(signal);
            }
        }
    }

    protected Set<SignalListener> getSignalListeners(Signal signal, boolean create) {
        Set<SignalListener> ls = listeners.get(signal);
        if (ls == null && create) {
            synchronized (listeners) {
                ls = listeners.get(signal);
                if (ls == null) {
                    ls = new CopyOnWriteArraySet<>();
                    listeners.put(signal, ls);
                }
            }
        }
        // may be null in case create=false
        return ls;
    }

    @Override
    public int getWidth() {
        int width = 0;
        try {
            width = Integer.valueOf(this.environment.getEnv().get(Environment.ENV_COLUMNS));
        } catch (Throwable t) {
            // Ignore
        }
        return width > 0 ? width : super.getWidth();
    }

    @Override
    public int getHeight() {
        int height = 0;
        try {
            height = Integer.valueOf(this.environment.getEnv().get(Environment.ENV_LINES));
        } catch (Throwable t) {
            // Ignore
        }
        return height > 0 ? height : super.getHeight();
    }

}
@@ -841,38 +841,8 @@
 h;%0A%0A
-import jline.TerminalSupport;%0A
 impo
@@ -929,30 +929,24 @@
 console.
-SignalListener
+Terminal
 ;%0Aimport
@@ -969,36 +969,46 @@
 f.shell.
-api.console.Terminal
+support.terminal.SignalSupport
 ;%0Aimport
@@ -1049,175 +1049,8 @@
 t;%0A%0A
-import java.util.EnumSet;%0Aimport java.util.Map;%0Aimport java.util.Set;%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.CopyOnWriteArraySet;%0A%0A
 publ
@@ -1078,21 +1078,19 @@
 extends 
-Termi
+Sig
 nalSuppo
@@ -1156,221 +1156,58 @@
 nt;%0A
-    private final Map%3CSignal, Set%3CSignalListener%3E%3E listeners;%0A%0A    public SshTerminal(Environment environment) %7B%0A        super(true);%0A        setAnsiSupported(true);%0A        listeners = new ConcurrentHashMap%3C%3E(3);
+%0A    public SshTerminal(Environment environment) %7B
 %0A   
@@ -1554,2118 +1554,8 @@
  %7D%0A%0A
-    public void init() throws Exception %7B%0A    %7D%0A%0A    public void restore() throws Exception %7B%0A    %7D%0A%0A    public void addSignalListener(SignalListener listener, Signal... signals) %7B%0A        if (signals == null) %7B%0A            throw new IllegalArgumentException(%22signals may not be null%22);%0A        %7D%0A        if (listener == null) %7B%0A            throw new IllegalArgumentException(%22listener may not be null%22);%0A        %7D%0A        for (Signal s : signals) %7B%0A            getSignalListeners(s, true).add(listener);%0A        %7D%0A    %7D%0A%0A    public void addSignalListener(SignalListener listener) %7B%0A        addSignalListener(listener, EnumSet.allOf(Signal.class));%0A    %7D%0A%0A    public void addSignalListener(SignalListener listener, EnumSet%3CSignal%3E signals) %7B%0A        if (signals == null) %7B%0A            throw new IllegalArgumentException(%22signals may not be null%22);%0A        %7D%0A        addSignalListener(listener, signals.toArray(new Signal%5Bsignals.size()%5D));%0A    %7D%0A%0A    public void removeSignalListener(SignalListener listener) %7B%0A        if (listener == null) %7B%0A            throw new IllegalArgumentException(%22listener may not be null%22);%0A        %7D%0A        for (Signal s : EnumSet.allOf(Signal.class)) %7B%0A            final Set%3CSignalListener%3E ls = getSignalListeners(s, false);%0A            if (ls != null) %7B%0A                ls.remove(listener);%0A            %7D%0A        %7D%0A    %7D%0A%0A    public void signal(Signal signal) %7B%0A        final Set%3CSignalListener%3E ls = getSignalListeners(signal, false);%0A        if (ls != null) %7B%0A            for (SignalListener l : ls) %7B%0A                l.signal(signal);%0A            %7D%0A        %7D%0A    %7D%0A%0A    protected Set%3CSignalListener%3E getSignalListeners(Signal signal, boolean create) %7B%0A        Set%3CSignalListener%3E ls = listeners.get(signal);%0A        if (ls == null && create) %7B%0A            synchronized (listeners) %7B%0A                ls = listeners.get(signal);%0A                if (ls == null) %7B%0A                    ls = new CopyOnWriteArraySet%3C%3E();%0A                    listeners.put(signal, ls);%0A                %7D%0A            %7D%0A        %7D%0A        // may be null in case create=false%0A        return ls;%0A    %7D%0A%0A
     
@@ -1825,24 +1825,10 @@
 h : 
-super.getWidth()
+80
 ;%0A  
@@ -2110,26 +2110,290 @@
 t : 
-super.getHeight();
+24;%0A    %7D%0A%0A    @Override%0A    public boolean isAnsiSupported() %7B%0A        return true;%0A    %7D%0A%0A    @Override%0A    public boolean isEchoEnabled() %7B%0A        return true;%0A    %7D%0A%0A    @Override%0A    public void setEchoEnabled(boolean enabled) %7B%0A        // TODO: how to disable echo over ssh ?
 %0A   
@@ -2397,8 +2397,9 @@
     %7D%0A%0A%7D
+%0A

Revert "[KARAF-3911] Workaround on SshTerminal resulting to a change in jline"