~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/ScpClient.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.eclipse.linuxtools.systemtap.ui.consolelog;
 
2
 
 
3
import com.jcraft.jsch.*;
 
4
import java.awt.*;
 
5
import javax.swing.*;
 
6
 
 
7
import org.eclipse.linuxtools.systemtap.ui.consolelog.dialogs.ErrorMessage;
 
8
import org.eclipse.linuxtools.systemtap.ui.consolelog.internal.ConsoleLogPlugin;
 
9
import org.eclipse.linuxtools.systemtap.ui.consolelog.preferences.ConsoleLogPreferenceConstants;
 
10
 
 
11
 
 
12
import java.io.*;
 
13
 
 
14
@SuppressWarnings("deprecation")
 
15
public class ScpClient {
 
16
        
 
17
        private Session session;
 
18
  
 
19
        public ScpClient() throws Exception {
 
20
        //public static void main(String[] arg){
 
21
     
 
22
  
 
23
      String user=ConsoleLogPlugin.getDefault().getPluginPreferences().getString(ConsoleLogPreferenceConstants.SCP_USER);
 
24
      String host=ConsoleLogPlugin.getDefault().getPluginPreferences().getString(ConsoleLogPreferenceConstants.HOST_NAME);
 
25
      
 
26
      
 
27
      //System.out.println(lfile + " " + rfile);
 
28
 
 
29
      try{
 
30
      JSch jsch=new JSch();
 
31
      
 
32
      session=jsch.getSession(user, host, 22);
 
33
 
 
34
      // username and password will be given via UserInfo interface.
 
35
      //UserInfo ui=new MyUserInfo();
 
36
      //session.setUserInfo(ui);
 
37
      session.setPassword(ConsoleLogPlugin.getDefault().getPluginPreferences().getString(ConsoleLogPreferenceConstants.SCP_PASSWORD));
 
38
      java.util.Properties config = new java.util.Properties();
 
39
                      config.put("StrictHostKeyChecking", "no");
 
40
                      session.setConfig(config); 
 
41
      session.connect();
 
42
      }catch(Exception e)
 
43
      {
 
44
          e.printStackTrace(System.err);
 
45
          new ErrorMessage("Error in connection", "File Transfer failed.\n See stderr for more details").open();
 
46
          throw e;
 
47
      }
 
48
    }
 
49
 
 
50
    public void transfer(String fromFile, String toFile) throws Exception {
 
51
      // exec 'scp -t rfile' remotely
 
52
        FileInputStream fis=null;       
 
53
      String rfile=toFile;
 
54
      String lfile=fromFile;
 
55
      String command="scp -t "+rfile;
 
56
      try {
 
57
      Channel channel=session.openChannel("exec");
 
58
      ((ChannelExec)channel).setCommand(command);
 
59
 
 
60
      // get I/O streams for remote scp
 
61
      OutputStream out=channel.getOutputStream();
 
62
      InputStream in=channel.getInputStream();
 
63
 
 
64
      channel.connect();
 
65
      
 
66
 
 
67
      if(checkAck(in)!=0){
 
68
          System.out.println("err");
 
69
      }
 
70
 
 
71
      // send "C0644 filesize filename", where filename should not include '/'
 
72
      long filesize=(new File(lfile)).length();
 
73
      command="C0644 "+filesize+" ";
 
74
      if(lfile.lastIndexOf('/')>0){
 
75
        command+=lfile.substring(lfile.lastIndexOf('/')+1);
 
76
      }
 
77
      else{
 
78
        command+=lfile;
 
79
      }
 
80
      command+="\n";
 
81
      
 
82
      out.write(command.getBytes()); out.flush();
 
83
      if(checkAck(in)!=0){
 
84
          System.out.println("err");
 
85
      }
 
86
      
 
87
 
 
88
 
 
89
      // send a content of lfile
 
90
      fis=new FileInputStream(lfile);
 
91
      byte[] buf=new byte[1024];
 
92
      while(true){
 
93
        int len=fis.read(buf, 0, buf.length);
 
94
        if(len<=0) break;
 
95
        out.write(buf, 0, len); //out.flush();
 
96
 
 
97
      }
 
98
      fis.close();
 
99
      fis=null;
 
100
      // send '\0'
 
101
      buf[0]=0; out.write(buf, 0, 1); out.flush();
 
102
      if(checkAck(in)!=0){
 
103
        System.out.println("err");
 
104
      }
 
105
      out.close();
 
106
 
 
107
      channel.disconnect();
 
108
      session.disconnect();
 
109
      
 
110
    }
 
111
    catch(Exception e){
 
112
      try{if(fis!=null)fis.close();}catch(Exception ee){}
 
113
      throw e;
 
114
    }
 
115
  }
 
116
 
 
117
  static int checkAck(InputStream in) throws IOException{
 
118
    int b=in.read();
 
119
    // b may be 0 for success,
 
120
    //          1 for error,
 
121
    //          2 for fatal error,
 
122
    //          -1
 
123
    if(b==0) return b;
 
124
    if(b==-1) return b;
 
125
 
 
126
    if(b==1 || b==2){
 
127
      StringBuffer sb=new StringBuffer();
 
128
      int c;
 
129
      do {
 
130
        c=in.read();
 
131
        sb.append((char)c);
 
132
      }
 
133
      while(c!='\n');
 
134
      if(b==1){ // error
 
135
        //System.out.print(sb.toString());
 
136
      }
 
137
      if(b==2){ // fatal error
 
138
        //System.out.print(sb.toString());
 
139
      }
 
140
    }
 
141
    return b;
 
142
  }
 
143
 
 
144
  public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
 
145
    public String getPassword(){ return passwd; }
 
146
    public boolean promptYesNo(String str){
 
147
      Object[] options={ "yes", "no" };
 
148
      int foo=JOptionPane.showOptionDialog(null, 
 
149
             str,
 
150
             "Warning", 
 
151
             JOptionPane.DEFAULT_OPTION, 
 
152
             JOptionPane.WARNING_MESSAGE,
 
153
             null, options, options[0]);
 
154
       return foo==0;
 
155
    }
 
156
  
 
157
    String passwd;
 
158
    JTextField passwordField=(JTextField)new JPasswordField(20);
 
159
 
 
160
    public String getPassphrase(){ return null; }
 
161
    public boolean promptPassphrase(String message){ return true; }
 
162
    public boolean promptPassword(String message){
 
163
      Object[] ob={passwordField}; 
 
164
      int result=
 
165
          JOptionPane.showConfirmDialog(null, ob, message,
 
166
                                        JOptionPane.OK_CANCEL_OPTION);
 
167
      if(result==JOptionPane.OK_OPTION){
 
168
        passwd=passwordField.getText();
 
169
        return true;
 
170
      }
 
171
      else{ return false; }
 
172
    }
 
173
    public void showMessage(String message){
 
174
      JOptionPane.showMessageDialog(null, message);
 
175
    }
 
176
    final GridBagConstraints gbc = 
 
177
      new GridBagConstraints(0,0,1,1,1,1,
 
178
                             GridBagConstraints.NORTHWEST,
 
179
                             GridBagConstraints.NONE,
 
180
                             new Insets(0,0,0,0),0,0);
 
181
    private Container panel;
 
182
    public String[] promptKeyboardInteractive(String destination,
 
183
                                              String name,
 
184
                                              String instruction,
 
185
                                              String[] prompt,
 
186
                                              boolean[] echo){
 
187
      panel = new JPanel();
 
188
      panel.setLayout(new GridBagLayout());
 
189
 
 
190
      gbc.weightx = 1.0;
 
191
      gbc.gridwidth = GridBagConstraints.REMAINDER;
 
192
      gbc.gridx = 0;
 
193
      panel.add(new JLabel(instruction), gbc);
 
194
      gbc.gridy++;
 
195
 
 
196
      gbc.gridwidth = GridBagConstraints.RELATIVE;
 
197
 
 
198
      JTextField[] texts=new JTextField[prompt.length];
 
199
      for(int i=0; i<prompt.length; i++){
 
200
        gbc.fill = GridBagConstraints.NONE;
 
201
        gbc.gridx = 0;
 
202
        gbc.weightx = 1;
 
203
        panel.add(new JLabel(prompt[i]),gbc);
 
204
 
 
205
        gbc.gridx = 1;
 
206
        gbc.fill = GridBagConstraints.HORIZONTAL;
 
207
        gbc.weighty = 1;
 
208
        if(echo[i]){
 
209
          texts[i]=new JTextField(20);
 
210
        }
 
211
        else{
 
212
          texts[i]=new JPasswordField(20);
 
213
        }
 
214
        panel.add(texts[i], gbc);
 
215
        gbc.gridy++;
 
216
      }
 
217
 
 
218
      if(JOptionPane.showConfirmDialog(null, panel, 
 
219
                                       destination+": "+name,
 
220
                                       JOptionPane.OK_CANCEL_OPTION,
 
221
                                       JOptionPane.QUESTION_MESSAGE)
 
222
         ==JOptionPane.OK_OPTION){
 
223
        String[] response=new String[prompt.length];
 
224
        for(int i=0; i<prompt.length; i++){
 
225
          response[i]=texts[i].getText();
 
226
        }
 
227
        return response;
 
228
      }
 
229
      else{
 
230
        return null;  // cancel
 
231
      }
 
232
    }
 
233
  }
 
234
}
 
 
b'\\ No newline at end of file'