2 * Copyright © 2019 IBM. Bell Canada
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.ssh.service.echoShell
19 import org.apache.sshd.common.Factory
20 import org.apache.sshd.server.Environment
21 import org.apache.sshd.server.ExitCallback
22 import org.apache.sshd.server.channel.ChannelSession
23 import org.apache.sshd.server.command.Command
24 import org.apache.sshd.server.shell.ShellFactory
25 import java.io.BufferedReader
26 import java.io.IOException
27 import java.io.InputStream
28 import java.io.InputStreamReader
29 import java.io.InterruptedIOException
30 import java.io.OutputStream
32 class EchoShellFactory : Factory<Command>, ShellFactory {
34 override fun create(): Command {
38 override fun createShell(channel: ChannelSession?): Command {
44 val INSTANCE = EchoShellFactory()
48 class EchoShell : Command, Runnable {
50 var `in`: InputStream? = null
52 var out: OutputStream? = null
54 var err: OutputStream? = null
56 private var callback: ExitCallback? = null
57 var environment: Environment? = null
59 private var thread: Thread? = null
61 override fun setInputStream(`in`: InputStream) {
65 override fun setOutputStream(out: OutputStream) {
69 override fun setErrorStream(err: OutputStream) {
73 override fun setExitCallback(callback: ExitCallback) {
74 this.callback = callback
77 @Throws(IOException::class)
78 override fun start(channel: ChannelSession?, env: Environment?) {
80 thread = Thread(this, "EchoShell")
81 thread!!.isDaemon = true
85 override fun destroy(channel: ChannelSession?) {
90 val r = BufferedReader(InputStreamReader(`in`))
93 val s = r.readLine() ?: return
94 out!!.write((s + "\n").toByteArray())
95 out!!.write("#".toByteArray())
101 } catch (e: InterruptedIOException) {
103 } catch (e: Exception) {