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.command.Command
22 import org.apache.sshd.server.ExitCallback
23 import java.io.InputStream
24 import java.io.OutputStream
25 import java.io.IOException
26 import java.io.BufferedReader
27 import java.io.InputStreamReader
28 import java.io.InterruptedIOException
30 class EchoShellFactory : Factory<Command> {
32 override fun create(): Command {
37 val INSTANCE = EchoShellFactory()
41 class EchoShell : Command, Runnable {
43 var `in`: InputStream? = null
45 var out: OutputStream? = null
47 var err: OutputStream? = null
49 private var callback: ExitCallback? = null
50 var environment: Environment? = null
52 private var thread: Thread? = null
54 override fun setInputStream(`in`: InputStream) {
58 override fun setOutputStream(out: OutputStream) {
62 override fun setErrorStream(err: OutputStream) {
66 override fun setExitCallback(callback: ExitCallback) {
67 this.callback = callback
70 @Throws(IOException::class)
71 override fun start(env: Environment) {
73 thread = Thread(this, "EchoShell")
74 thread!!.isDaemon = true
78 override fun destroy() {
83 val r = BufferedReader(InputStreamReader(`in`))
86 val s = r.readLine() ?: return
87 out!!.write((s + "\n").toByteArray())
88 out!!.write("#".toByteArray())
94 } catch (e: InterruptedIOException) {
96 } catch (e: Exception) {