KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jsch > ui > UserInfoPrompter


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jsch.ui;
12
13 import org.eclipse.core.runtime.OperationCanceledException;
14 import org.eclipse.jface.dialogs.IDialogConstants;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jsch.core.IJSchService;
17 import org.eclipse.jsch.internal.ui.*;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
20
21 import com.jcraft.jsch.*;
22
23 /**
24  * A {@link UserInfo} prompter implementation that can be used when connecting a
25  * {@link Session}.
26  * <p>
27  * Clients may instantiate or subclass this class.
28  *
29  * @since 1.0
30  * @see IJSchService#createSession(String, int, String)
31  * @see IJSchService#connect(Session, int,
32  * org.eclipse.core.runtime.IProgressMonitor)
33  */

34 public class UserInfoPrompter implements UserInfo, UIKeyboardInteractive{
35
36   private String JavaDoc passphrase;
37   private String JavaDoc password;
38   private final Session session;
39   private int attemptCount;
40
41   /**
42    * Create a prompter for the given session. This constructor will associate
43    * this prompter with the session using {@link Session#setUserInfo(UserInfo)}.
44    *
45    * @param session
46    * the session
47    */

48   public UserInfoPrompter(Session session){
49     super();
50     this.session=session;
51     session.setUserInfo(this);
52   }
53
54   /**
55    * Return the session to which this prompter is assigned.
56    *
57    * @return the session to which this prompter is assigned
58    */

59   public Session getSession(){
60     return session;
61   }
62
63   /*
64    * (non-Javadoc)
65    *
66    * @see com.jcraft.jsch.UserInfo#getPassphrase()
67    */

68   public String JavaDoc getPassphrase(){
69     return passphrase;
70   }
71
72   /*
73    * (non-Javadoc)
74    *
75    * @see com.jcraft.jsch.UserInfo#getPassword()
76    */

77   public String JavaDoc getPassword(){
78     return password;
79   }
80
81   /**
82    * Set the pass phrase to be used when connecting the session. Return
83    * <code>null</code> if the pass phrase is not known.
84    *
85    * @param passphrase
86    * the pass phrase to be used when connecting the session or
87    * <code>null</code>
88    */

89   public void setPassphrase(String JavaDoc passphrase){
90     this.passphrase=passphrase;
91   }
92
93   /**
94    * Set the password to be used when connecting the session. Return
95    * <code>null</code> if the password is not known.
96    *
97    * @param password
98    * the password to be used when connecting the session or
99    * <code>null</code>
100    */

101   public void setPassword(String JavaDoc password){
102     this.password=password;
103   }
104
105   /*
106    * (non-Javadoc)
107    *
108    * @see com.jcraft.jsch.UserInfo#promptPassphrase(java.lang.String)
109    */

110   public boolean promptPassphrase(String JavaDoc message){
111     String JavaDoc _passphrase=promptSecret(message);
112     if(_passphrase!=null){
113       setPassphrase(_passphrase);
114     }
115     return _passphrase!=null;
116   }
117
118   /*
119    * (non-Javadoc)
120    *
121    * @see com.jcraft.jsch.UserInfo#promptPassword(java.lang.String)
122    */

123   public boolean promptPassword(String JavaDoc message){
124     String JavaDoc _password=promptSecret(message);
125     if(_password!=null){
126       setPassword(_password);
127     }
128     return _password!=null;
129   }
130
131   private String JavaDoc promptSecret(final String JavaDoc message){
132     // ask the user for a password
133
final String JavaDoc[] result=new String JavaDoc[1];
134     Display display=Display.getCurrent();
135     if(display!=null){
136       result[0]=promptForPassword(message);
137     }
138     else{
139       // sync exec in default thread
140
Display.getDefault().syncExec(new Runnable JavaDoc(){
141         public void run(){
142           result[0]=promptForPassword(message);
143         }
144       });
145     }
146
147     if(result[0]==null){
148       throw new OperationCanceledException();
149     }
150     return result[0];
151   }
152
153   /* package */String JavaDoc promptForPassword(final String JavaDoc message){
154     String JavaDoc username=getSession().getUserName();
155     UserValidationDialog dialog=new UserValidationDialog(null, null,
156         (username==null) ? "" : username, message); //$NON-NLS-1$
157
dialog.setUsernameMutable(false);
158     dialog.open();
159     return dialog.getPassword();
160   }
161
162   /*
163    * (non-Javadoc)
164    *
165    * @see com.jcraft.jsch.UIKeyboardInteractive#promptKeyboardInteractive(java.lang.String,
166    * java.lang.String, java.lang.String, java.lang.String[], boolean[])
167    */

168   public String JavaDoc[] promptKeyboardInteractive(String JavaDoc destination, String JavaDoc name,
169       String JavaDoc instruction, String JavaDoc[] prompt, boolean[] echo){
170     if(prompt.length==0){
171       // No need to prompt, just return an empty String array
172
return new String JavaDoc[0];
173     }
174     try{
175       if(attemptCount==0&&password!=null&&prompt.length==1
176           &&prompt[0].trim().equalsIgnoreCase("password:")){ //$NON-NLS-1$
177
// Return the provided password the first time but always prompt on
178
// subsequent tries
179
attemptCount++;
180         return new String JavaDoc[] {password};
181       }
182       String JavaDoc[] result=iromptForKeyboradInteractiveInUI(destination, name,
183           instruction, prompt, echo);
184       if(result==null)
185         return null; // canceled
186
if(result.length==1&&prompt.length==1
187           &&prompt[0].trim().equalsIgnoreCase("password:")){ //$NON-NLS-1$
188
password=result[0];
189       }
190       attemptCount++;
191       return result;
192     }
193     catch(OperationCanceledException e){
194       return null;
195     }
196   }
197
198   private String JavaDoc[] iromptForKeyboradInteractiveInUI(final String JavaDoc destination,
199       final String JavaDoc name, final String JavaDoc instruction, final String JavaDoc[] prompt,
200       final boolean[] echo){
201     final String JavaDoc[][] result=new String JavaDoc[1][];
202     Display display=Display.getCurrent();
203     if(display!=null){
204       result[0]=internalPromptForUserInteractive(destination, name,
205           instruction, prompt, echo);
206     }
207     else{
208       // sync exec in default thread
209
Display.getDefault().syncExec(new Runnable JavaDoc(){
210         public void run(){
211           result[0]=internalPromptForUserInteractive(destination, name,
212               instruction, prompt, echo);
213         }
214       });
215     }
216     return result[0];
217   }
218
219   /* package */ String JavaDoc[] internalPromptForUserInteractive(String JavaDoc destination,
220       String JavaDoc name, String JavaDoc instruction, String JavaDoc[] prompt, boolean[] echo){
221     String JavaDoc domain=null;
222     String JavaDoc userName=getSession().getUserName();
223     KeyboardInteractiveDialog dialog=new KeyboardInteractiveDialog(null,
224         domain, destination, name, userName, instruction, prompt, echo);
225     dialog.open();
226     String JavaDoc[] _result=dialog.getResult();
227     return _result;
228   }
229
230   /*
231    * (non-Javadoc)
232    *
233    * @see com.jcraft.jsch.UserInfo#promptYesNo(java.lang.String)
234    */

235   public boolean promptYesNo(String JavaDoc question){
236     int prompt=prompt(MessageDialog.QUESTION, Messages.UserInfoPrompter_0,
237         question, new int[] {IDialogConstants.YES_ID, IDialogConstants.NO_ID},
238         0 // yes
239
// the
240
// default
241
);
242     return prompt==0;
243   }
244
245   /*
246    * (non-Javadoc)
247    *
248    * @see com.jcraft.jsch.UserInfo#showMessage(java.lang.String)
249    */

250   public void showMessage(String JavaDoc message){
251     prompt(MessageDialog.INFORMATION, Messages.UserInfoPrompter_1, message,
252         new int[] {IDialogConstants.OK_ID}, 0);
253   }
254
255   private int prompt(final int promptType, final String JavaDoc title,
256       final String JavaDoc message, final int[] promptResponses,
257       final int defaultResponse){
258     final Display display=getStandardDisplay();
259     final int[] retval=new int[1];
260     final String JavaDoc[] buttons=new String JavaDoc[promptResponses.length];
261     for(int i=0; i<promptResponses.length; i++){
262       int prompt=promptResponses[i];
263       switch(prompt){
264         case IDialogConstants.OK_ID:
265           buttons[i]=IDialogConstants.OK_LABEL;
266           break;
267         case IDialogConstants.CANCEL_ID:
268           buttons[i]=IDialogConstants.CANCEL_LABEL;
269           break;
270         case IDialogConstants.NO_ID:
271           buttons[i]=IDialogConstants.NO_LABEL;
272           break;
273         case IDialogConstants.YES_ID:
274           buttons[i]=IDialogConstants.YES_LABEL;
275           break;
276       }
277     }
278
279     display.syncExec(new Runnable JavaDoc(){
280       public void run(){
281         final MessageDialog dialog=new MessageDialog(new Shell(display), title,
282             null /* title image */, message, promptType, buttons,
283             defaultResponse);
284         retval[0]=dialog.open();
285       }
286     });
287     return retval[0];
288   }
289
290   private Display getStandardDisplay(){
291     Display display=Display.getCurrent();
292     if(display==null){
293       display=Display.getDefault();
294     }
295     return display;
296   }
297
298 }
299
Popular Tags