Subversion Repositories Programming Utils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 rm5248 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
package org.apache.sshd.util;
20
 
21
import java.io.BufferedReader;
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.io.InputStreamReader;
25
import java.io.InterruptedIOException;
26
import java.io.OutputStream;
27
 
28
import org.apache.sshd.common.Factory;
29
import org.apache.sshd.server.Command;
30
import org.apache.sshd.server.Environment;
31
import org.apache.sshd.server.ExitCallback;
32
 
33
/**
34
 * TODO Add javadoc
35
 *
36
 * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
37
 */
38
public class EchoShellFactory implements Factory<Command> {
39
 
40
    public Command create() {
41
        return new EchoShell();
42
    }
43
 
44
    public static class EchoShell implements Command, Runnable {
45
 
46
        private InputStream in;
47
        private OutputStream out;
48
        private OutputStream err;
49
        private ExitCallback callback;
50
        private Environment environment;
51
        private Thread thread;
52
 
53
        public InputStream getIn() {
54
            return in;
55
        }
56
 
57
        public OutputStream getOut() {
58
            return out;
59
        }
60
 
61
        public OutputStream getErr() {
62
            return err;
63
        }
64
 
65
        public Environment getEnvironment() {
66
            return environment;
67
        }
68
 
69
        public void setInputStream(InputStream in) {
70
            this.in = in;
71
        }
72
 
73
        public void setOutputStream(OutputStream out) {
74
            this.out = out;
75
        }
76
 
77
        public void setErrorStream(OutputStream err) {
78
            this.err = err;
79
        }
80
 
81
        public void setExitCallback(ExitCallback callback) {
82
            this.callback = callback;
83
        }
84
 
85
        public void start(Environment env) throws IOException {
86
            environment = env;
87
            thread = new Thread(this, "EchoShell");
88
            thread.start();
89
        }
90
 
91
        public void destroy() {
92
            thread.interrupt();
93
        }
94
 
95
        public void run() {
96
            BufferedReader r = new BufferedReader(new InputStreamReader(in));
97
            try {
98
                for (;;) {
99
                    String s = r.readLine();
100
                    if (s == null) {
101
                        return;
102
                    }
103
                    out.write((s + "\n").getBytes());
104
                    out.flush();
105
                    if ("exit".equals(s)) {
106
                        return;
107
                    }
108
                }
109
            } catch (InterruptedIOException e) {
110
                // Ignore
111
            } catch (Exception e) {
112
                e.printStackTrace();
113
            } finally {
114
                callback.onExit(0);
115
            }
116
        }
117
    }
118
}