KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > StreamForwarding


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 import com.jcraft.jsch.*;
3 import java.awt.*;
4 import javax.swing.*;
5
6 public class StreamForwarding{
7   public static void main(String JavaDoc[] arg){
8     int port;
9
10     try{
11       JSch jsch=new JSch();
12
13       String JavaDoc host=JOptionPane.showInputDialog("Enter username@hostname",
14                           System.getProperty("user.name")+
15                           "@localhost");
16       String JavaDoc user=host.substring(0, host.indexOf('@'));
17       host=host.substring(host.indexOf('@')+1);
18
19       Session session=jsch.getSession(user, host, 22);
20
21       // username and password will be given via UserInfo interface.
22
UserInfo ui=new MyUserInfo();
23       session.setUserInfo(ui);
24       session.connect();
25
26       String JavaDoc foo=JOptionPane.showInputDialog("Enter host and port",
27                          "host:port");
28       host=foo.substring(0, foo.indexOf(':'));
29       port=Integer.parseInt(foo.substring(foo.indexOf(':')+1));
30
31       System.out.println("System.{in,out} will be forwarded to "+
32              host+":"+port+".");
33       Channel channel=session.openChannel("direct-tcpip");
34       ((ChannelDirectTCPIP)channel).setInputStream(System.in);
35       ((ChannelDirectTCPIP)channel).setOutputStream(System.out);
36       ((ChannelDirectTCPIP)channel).setHost(host);
37       ((ChannelDirectTCPIP)channel).setPort(port);
38       channel.connect();
39     }
40     catch(Exception JavaDoc e){
41       System.out.println(e);
42     }
43   }
44
45   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
46     public String JavaDoc getPassword(){ return passwd; }
47     public boolean promptYesNo(String JavaDoc str){
48       Object JavaDoc[] options={ "yes", "no" };
49       int foo=JOptionPane.showOptionDialog(null,
50              str,
51              "Warning",
52              JOptionPane.DEFAULT_OPTION,
53              JOptionPane.WARNING_MESSAGE,
54              null, options, options[0]);
55        return foo==0;
56     }
57   
58     String JavaDoc passwd;
59     JTextField passwordField=(JTextField)new JPasswordField(20);
60
61     public String JavaDoc getPassphrase(){ return null; }
62     public boolean promptPassphrase(String JavaDoc message){ return true; }
63     public boolean promptPassword(String JavaDoc message){
64       Object JavaDoc[] ob={passwordField};
65       int result=
66       JOptionPane.showConfirmDialog(null, ob, message,
67                     JOptionPane.OK_CANCEL_OPTION);
68       if(result==JOptionPane.OK_OPTION){
69     passwd=passwordField.getText();
70     return true;
71       }
72       else{ return false; }
73     }
74     public void showMessage(String JavaDoc message){
75       JOptionPane.showMessageDialog(null, message);
76     }
77     final GridBagConstraints gbc =
78       new GridBagConstraints(0,0,1,1,1,1,
79                              GridBagConstraints.NORTHWEST,
80                              GridBagConstraints.NONE,
81                              new Insets(0,0,0,0),0,0);
82     private Container panel;
83     public String JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
84                                               String JavaDoc name,
85                                               String JavaDoc instruction,
86                                               String JavaDoc[] prompt,
87                                               boolean[] echo){
88       panel = new JPanel();
89       panel.setLayout(new GridBagLayout());
90
91       gbc.weightx = 1.0;
92       gbc.gridwidth = GridBagConstraints.REMAINDER;
93       gbc.gridx = 0;
94       panel.add(new JLabel(instruction), gbc);
95       gbc.gridy++;
96
97       gbc.gridwidth = GridBagConstraints.RELATIVE;
98
99       JTextField[] texts=new JTextField[prompt.length];
100       for(int i=0; i<prompt.length; i++){
101         gbc.fill = GridBagConstraints.NONE;
102         gbc.gridx = 0;
103         gbc.weightx = 1;
104         panel.add(new JLabel(prompt[i]),gbc);
105
106         gbc.gridx = 1;
107         gbc.fill = GridBagConstraints.HORIZONTAL;
108         gbc.weighty = 1;
109         if(echo[i]){
110           texts[i]=new JTextField(20);
111         }
112         else{
113           texts[i]=new JPasswordField(20);
114         }
115         panel.add(texts[i], gbc);
116         gbc.gridy++;
117       }
118
119       if(JOptionPane.showConfirmDialog(null, panel,
120                                        destination+": "+name,
121                                        JOptionPane.OK_CANCEL_OPTION,
122                                        JOptionPane.QUESTION_MESSAGE)
123          ==JOptionPane.OK_OPTION){
124         String JavaDoc[] response=new String JavaDoc[prompt.length];
125         for(int i=0; i<prompt.length; i++){
126           response[i]=texts[i].getText();
127         }
128     return response;
129       }
130       else{
131         return null; // cancel
132
}
133     }
134   }
135 }
136
137
138
Popular Tags