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