KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Compression


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