]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SshShell.java
add some private constructors with no arg, some getters & setters and some ids to...
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / SshShell.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.jsch;
18
19 import java.io.BufferedReader;
20 import java.io.BufferedWriter;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.OutputStreamWriter;
24
25 import org.apache.commons.io.IOUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.slc.SlcException;
29 import org.springframework.core.io.Resource;
30 import org.springframework.util.StringUtils;
31
32 import com.jcraft.jsch.Channel;
33 import com.jcraft.jsch.Session;
34
35 public class SshShell extends AbstractJschTask {
36 private final static Log log = LogFactory.getLog(SshShell.class);
37 private Resource input;
38
39 @Override
40 void run(Session session) {
41 try {
42 final Channel channel = session.openChannel("shell");
43
44 // Enable agent-forwarding.
45 // ((ChannelShell)channel).setAgentForwarding(true);
46
47 // channel.setInputStream(System.in);
48 // channel.setInputStream(input.getInputStream());
49 /*
50 * // a hack for MS-DOS prompt on Windows.
51 * channel.setInputStream(new FilterInputStream(System.in){ public
52 * int read(byte[] b, int off, int len)throws IOException{ return
53 * in.read(b, off, (len>1024?1024:len)); } });
54 */
55
56 // channel.setOutputStream(System.out);
57
58 /*
59 * // Choose the pty-type "vt102".
60 * ((ChannelShell)channel).setPtyType("vt102");
61 */
62
63 /*
64 * // Set environment variable "LANG" as "ja_JP.eucJP".
65 * ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
66 */
67
68 // Writer thread
69 final BufferedWriter writer = new BufferedWriter(
70 new OutputStreamWriter(channel.getOutputStream()));
71
72 // channel.connect();
73 channel.connect(3 * 1000);
74
75 // while (!channel.isConnected())
76 // try {
77 // Thread.sleep(500);
78 // } catch (InterruptedException e1) {
79 // // silent
80 // }
81
82 Thread writerThread = new Thread("Shell writer " + getSshTarget()) {
83
84 @Override
85 public void run() {
86
87 if (log.isDebugEnabled())
88 log.debug("Start writing to shell");
89
90 BufferedReader reader = null;
91 try {
92 reader = new BufferedReader(new InputStreamReader(input
93 .getInputStream()));
94 String line = null;
95 while ((line = reader.readLine()) != null) {
96 if (!StringUtils.hasText(line))
97 continue;
98 writer.write(line);
99 writer.newLine();
100 }
101 writer.append("exit");
102 writer.newLine();
103 writer.flush();
104 // channel.disconnect();
105 } catch (IOException e) {
106 throw new SlcException("Cannot write to shell on "
107 + getSshTarget(), e);
108 } finally {
109 IOUtils.closeQuietly(reader);
110 }
111 }
112 };
113 writerThread.start();
114
115 BufferedReader execIn = null;
116 try {
117 execIn = new BufferedReader(new InputStreamReader(channel
118 .getInputStream()));
119 String line = null;
120 while ((line = execIn.readLine()) != null) {
121 if (!line.trim().equals(""))
122 log.info(line);
123 }
124 } catch (Exception e) {
125 throw new SlcException("Cannot read from shell on "
126 + getSshTarget(), e);
127 } finally {
128 IOUtils.closeQuietly(execIn);
129 }
130
131 } catch (Exception e) {
132 throw new SlcException("Cannot use SSH shell on " + getSshTarget(),
133 e);
134 }
135 }
136
137 public void setInput(Resource input) {
138 this.input = input;
139 }
140
141 }