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.IOException;
22
import java.io.InputStream;
23
import java.io.OutputStream;
24
 
25
import org.apache.sshd.common.Factory;
26
import org.apache.sshd.common.channel.BufferedIoOutputStream;
27
import org.apache.sshd.common.future.CloseFuture;
28
import org.apache.sshd.common.future.SshFutureListener;
29
import org.apache.sshd.common.io.IoInputStream;
30
import org.apache.sshd.common.io.IoOutputStream;
31
import org.apache.sshd.common.io.IoWriteFuture;
32
import org.apache.sshd.common.util.Buffer;
33
import org.apache.sshd.server.AsyncCommand;
34
import org.apache.sshd.server.ChannelSessionAware;
35
import org.apache.sshd.server.Command;
36
import org.apache.sshd.server.Environment;
37
import org.apache.sshd.server.ExitCallback;
38
import org.apache.sshd.server.channel.ChannelDataReceiver;
39
import org.apache.sshd.server.channel.ChannelSession;
40
 
41
/**
42
 * TODO Add javadoc
43
 *
44
 * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
45
 */
46
public class AsyncEchoShellFactory implements Factory<Command> {
47
 
48
    public Command create() {
49
        return new EchoShell();
50
    }
51
 
52
    public static class EchoShell implements AsyncCommand, ChannelDataReceiver, ChannelSessionAware {
53
 
54
        private IoOutputStream out;
55
        private IoOutputStream err;
56
        private ExitCallback callback;
57
        private Environment environment;
58
        private ChannelSession session;
59
        private StringBuilder buffer = new StringBuilder();
60
 
61
        public IoOutputStream getOut() {
62
            return out;
63
        }
64
 
65
        public IoOutputStream getErr() {
66
            return err;
67
        }
68
 
69
        public Environment getEnvironment() {
70
            return environment;
71
        }
72
 
73
        public void setInputStream(InputStream in) {
74
        }
75
 
76
        public void setOutputStream(OutputStream out) {
77
        }
78
 
79
        public void setErrorStream(OutputStream err) {
80
        }
81
 
82
        public void setIoInputStream(IoInputStream in) {
83
        }
84
 
85
        public void setIoOutputStream(IoOutputStream out) {
86
            this.out = new BufferedIoOutputStream(out);
87
        }
88
 
89
        public void setIoErrorStream(IoOutputStream err) {
90
            this.err = new BufferedIoOutputStream(err);
91
        }
92
 
93
        public void setExitCallback(ExitCallback callback) {
94
            this.callback = callback;
95
        }
96
 
97
        public void setChannelSession(ChannelSession session) {
98
            this.session = session;
99
        }
100
 
101
        public void start(Environment env) throws IOException {
102
            environment = env;
103
            session.setDataReceiver(this);
104
        }
105
 
106
        public void close() throws IOException {
107
            out.close(false).addListener(new SshFutureListener<CloseFuture>() {
108
                public void operationComplete(CloseFuture future) {
109
                    callback.onExit(0);
110
                }
111
            });
112
        }
113
 
114
        public void destroy() {
115
        }
116
 
117
        public int data(final ChannelSession channel, byte[] buf, int start, int len) throws IOException {
118
            buffer.append(new String(buf, start, len));
119
            for (int i = 0; i < buffer.length(); i++) {
120
                if (buffer.charAt(i) == '\n') {
121
                    final String s = buffer.substring(0, i + 1);
122
                    final byte[] bytes = s.getBytes();
123
                    out.write(new Buffer(bytes)).addListener(new SshFutureListener<IoWriteFuture>() {
124
                        public void operationComplete(IoWriteFuture future) {
125
                            try {
126
                                channel.getLocalWindow().consumeAndCheck(bytes.length);
127
                            } catch (IOException e) {
128
                                channel.getSession().exceptionCaught(e);
129
                            }
130
                        }
131
                    });
132
                    buffer = new StringBuilder(buffer.substring(i + 1));
133
                    i = 0;
134
                }
135
            }
136
            return 0;
137
        }
138
    }
139
 
140
}