KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Subsystem


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 Subsystem{
7   public static void main(String JavaDoc[] arg){
8     try{
9       JSch jsch=new JSch();
10
11       String JavaDoc host=JOptionPane.showInputDialog("Enter username@hostname",
12                                               System.getProperty("user.name")+
13                                               "@localhost");
14       String JavaDoc user=host.substring(0, host.indexOf('@'));
15       host=host.substring(host.indexOf('@')+1);
16
17       Session session=jsch.getSession(user, host, 22);
18       
19       UserInfo ui=new MyUserInfo();
20       session.setUserInfo(ui);
21       session.connect();
22
23       String JavaDoc subsystem=JOptionPane.showInputDialog("Enter subsystem name", "");
24
25       Channel channel=session.openChannel("subsystem");
26       ((ChannelSubsystem)channel).setSubsystem(subsystem);
27       ((ChannelSubsystem)channel).setPty(true);
28
29       channel.setInputStream(System.in);
30       ((ChannelSubsystem)channel).setErrStream(System.err);
31       channel.setOutputStream(System.out);
32       channel.connect();
33
34       /*
35       channel.setInputStream(System.in);
36       ((ChannelSubsystem)channel).setErrStream(System.err);
37       InputStream in = channel.getInputStream();
38       channel.connect();
39
40       byte[] tmp=new byte[1024];
41       while(true){
42         while(in.available()>0){
43           int i=in.read(tmp, 0, 1024);
44           if(i<0)break;
45           System.out.print(new String(tmp, 0, i));
46         }
47         if(channel.isClosed()){
48           System.out.println("exit-status: "+channel.getExitStatus());
49           break;
50         }
51         try{Thread.sleep(1000);}catch(Exception ee){}
52       }
53       channel.disconnect();
54       session.disconnect();
55       */

56     }
57     catch(Exception JavaDoc e){
58       System.out.println(e);
59     }
60   }
61
62   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
63     public String JavaDoc getPassword(){ return passwd; }
64     public boolean promptYesNo(String JavaDoc str){
65       Object JavaDoc[] options={ "yes", "no" };
66       int foo=JOptionPane.showOptionDialog(null,
67              str,
68              "Warning",
69              JOptionPane.DEFAULT_OPTION,
70              JOptionPane.WARNING_MESSAGE,
71              null, options, options[0]);
72        return foo==0;
73     }
74   
75     String JavaDoc passwd;
76     JTextField passwordField=(JTextField)new JPasswordField(20);
77
78     public String JavaDoc getPassphrase(){ return null; }
79     public boolean promptPassphrase(String JavaDoc message){ return true; }
80     public boolean promptPassword(String JavaDoc message){
81       Object JavaDoc[] ob={passwordField};
82       int result=
83         JOptionPane.showConfirmDialog(null, ob, message,
84                                       JOptionPane.OK_CANCEL_OPTION);
85       if(result==JOptionPane.OK_OPTION){
86         passwd=passwordField.getText();
87         return true;
88       }
89       else{
90         return false;
91       }
92     }
93     public void showMessage(String JavaDoc message){
94       JOptionPane.showMessageDialog(null, message);
95     }
96     final GridBagConstraints gbc =
97       new GridBagConstraints(0,0,1,1,1,1,
98                              GridBagConstraints.NORTHWEST,
99                              GridBagConstraints.NONE,
100                              new Insets(0,0,0,0),0,0);
101     private Container panel;
102     public String JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
103                                               String JavaDoc name,
104                                               String JavaDoc instruction,
105                                               String JavaDoc[] prompt,
106                                               boolean[] echo){
107       panel = new JPanel();
108       panel.setLayout(new GridBagLayout());
109
110       gbc.weightx = 1.0;
111       gbc.gridwidth = GridBagConstraints.REMAINDER;
112       gbc.gridx = 0;
113       panel.add(new JLabel(instruction), gbc);
114       gbc.gridy++;
115
116       gbc.gridwidth = GridBagConstraints.RELATIVE;
117
118       JTextField[] texts=new JTextField[prompt.length];
119       for(int i=0; i<prompt.length; i++){
120         gbc.fill = GridBagConstraints.NONE;
121         gbc.gridx = 0;
122         gbc.weightx = 1;
123         panel.add(new JLabel(prompt[i]),gbc);
124
125         gbc.gridx = 1;
126         gbc.fill = GridBagConstraints.HORIZONTAL;
127         gbc.weighty = 1;
128         if(echo[i]){
129           texts[i]=new JTextField(20);
130         }
131         else{
132           texts[i]=new JPasswordField(20);
133         }
134         panel.add(texts[i], gbc);
135         gbc.gridy++;
136       }
137
138       if(JOptionPane.showConfirmDialog(null, panel,
139                                        destination+": "+name,
140                                        JOptionPane.OK_CANCEL_OPTION,
141                                        JOptionPane.QUESTION_MESSAGE)
142          ==JOptionPane.OK_OPTION){
143         String JavaDoc[] response=new String JavaDoc[prompt.length];
144         for(int i=0; i<prompt.length; i++){
145           response[i]=texts[i].getText();
146         }
147     return response;
148       }
149       else{
150         return null; // cancel
151
}
152     }
153   }
154 }
155
Popular Tags