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.command.Command
23 import java.io.BufferedReader
24 import java.io.IOException
25 import java.io.InputStream
26 import java.io.InputStreamReader
27 import java.io.InterruptedIOException
28 import java.io.OutputStream
30 class EchoShellFactory : Factory<Command> {
32 override fun create(): Command {
38 val INSTANCE = EchoShellFactory()
42 class EchoShell : Command, Runnable {
44 var `in`: InputStream? = null
46 var out: OutputStream? = null
48 var err: OutputStream? = null
50 private var callback: ExitCallback? = null
51 var environment: Environment? = null
53 private var thread: Thread? = null
55 override fun setInputStream(`in`: InputStream) {
59 override fun setOutputStream(out: OutputStream) {
63 override fun setErrorStream(err: OutputStream) {
67 override fun setExitCallback(callback: ExitCallback) {
68 this.callback = callback
71 @Throws(IOException::class)
72 override fun start(env: Environment) {
74 thread = Thread(this, "EchoShell")
75 thread!!.isDaemon = true
79 override fun destroy() {
84 val r = BufferedReader(InputStreamReader(`in`))
87 val s = r.readLine() ?: return
88 out!!.write((s + "\n").toByteArray())
89 out!!.write("#".toByteArray())
95 } catch (e: InterruptedIOException) {
97 } catch (e: Exception) {