KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jsch > internal > core > Utils


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.internal.core;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.core.net.proxy.IProxyData;
16 import org.eclipse.core.net.proxy.IProxyService;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Preferences;
19 import org.eclipse.core.runtime.preferences.InstanceScope;
20
21 import com.jcraft.jsch.*;
22
23 public class Utils{
24
25   public static String JavaDoc loadPrivateKeys(JSch jsch, String JavaDoc current_pkeys){
26     Preferences preferences=JSchCorePlugin.getPlugin().getPluginPreferences();
27     String JavaDoc ssh_home=preferences.getString(IConstants.KEY_SSH2HOME);
28     String JavaDoc pkeys=preferences.getString(IConstants.KEY_PRIVATEKEY);
29
30     if(ssh_home.length()==0)
31       ssh_home=PreferenceInitializer.SSH_HOME_DEFAULT;
32
33     java.io.File JavaDoc file;
34     String JavaDoc[] pkey=pkeys.split(","); //$NON-NLS-1$
35
String JavaDoc[] _pkey=current_pkeys.split(","); //$NON-NLS-1$
36
String JavaDoc result=""; //$NON-NLS-1$
37
for(int i=0; i<pkey.length; i++){
38       file=new java.io.File JavaDoc(pkey[i]);
39       if(!file.isAbsolute()){
40         file=new java.io.File JavaDoc(ssh_home, pkey[i]);
41       }
42       if(file.exists()){
43         boolean notyet=true;
44         for(int j=0; j<_pkey.length; j++){
45           if(pkey[i].equals(_pkey[j])){
46             notyet=false;
47             break;
48           }
49         }
50         try{
51           if(notyet)
52             jsch.addIdentity(file.getPath());
53           if(result.length()==0){
54             result=pkey[i];
55           }
56           else{
57             result+=(","+pkey[i]); //$NON-NLS-1$
58
}
59         }
60         catch(JSchException e){
61           JSchCorePlugin.log(IStatus.ERROR,
62               "An error occurred loading the SSH2 private keys", e); //$NON-NLS-1$
63
}
64       }
65     }
66     return result;
67   }
68
69   public static Session createSession(JSch jsch, String JavaDoc username,
70       String JavaDoc hostname, int port) throws JSchException{
71     Session session=jsch.getSession(username, hostname, port);
72     setProxy(session);
73     Hashtable JavaDoc config=new Hashtable JavaDoc();
74     config.put("PreferredAuthentications", //$NON-NLS-1$
75
"gssapi-with-mic,publickey,password,keyboard-interactive"); //$NON-NLS-1$
76
session.setConfig(config);
77     return session;
78   }
79
80   public static void setProxy(Session session){
81     Proxy proxy=getProxyForHost(session.getHost(), IProxyData.SOCKS_PROXY_TYPE);
82     if(proxy==null)
83       proxy=getProxyForHost(session.getHost(), IProxyData. HTTPS_PROXY_TYPE);
84     if(proxy!=null)
85       session.setProxy(proxy);
86   }
87
88   private static int getPort(IProxyData data){
89     int port=data.getPort();
90     if(port==-1){
91       if(data.getType().equals(IProxyData.HTTP_PROXY_TYPE))
92         port=80;
93       else if(data.getType().equals(IProxyData.HTTPS_PROXY_TYPE))
94         port=443;
95       else if(data.getType().equals(IProxyData.SOCKS_PROXY_TYPE))
96         port=1080;
97     }
98     return port;
99   }
100
101   private static IProxyData getProxyData(String JavaDoc host, String JavaDoc type){
102     IProxyService proxyService=JSchCorePlugin.getPlugin().getProxyService();
103     if(proxyService==null)
104       return null;
105     IProxyData data=proxyService.getProxyDataForHost(host, type);
106     if(data==null||data.getHost()==null||getJSchProxyType(data)==null)
107       return null;
108     return data;
109   }
110
111   private static String JavaDoc getJSchProxyType(IProxyData data){
112     if(data.getType().equals(IProxyData.HTTPS_PROXY_TYPE))
113       return IConstants.PROXY_TYPE_HTTP;
114     if(data.getType().equals(IProxyData.SOCKS_PROXY_TYPE))
115       return IConstants.PROXY_TYPE_SOCKS5;
116     return null;
117   }
118
119   public static Proxy getProxyForHost(String JavaDoc host, String JavaDoc proxyType){
120     IProxyService proxyService=JSchCorePlugin.getPlugin().getProxyService();
121     if(proxyService==null)
122       return null;
123     boolean useProxy=proxyService.isProxiesEnabled();
124     if(!useProxy)
125       return null;
126     Proxy proxy=null;
127     IProxyData data=getProxyData(host, proxyType);
128     if(data==null)
129       return null;
130     String JavaDoc _type=getJSchProxyType(data);
131     if(_type==null)
132       return null;
133     String JavaDoc _host=data.getHost();
134     int _port=getPort(data);
135
136     boolean useAuth=data.isRequiresAuthentication();
137     String JavaDoc _user=""; //$NON-NLS-1$
138
String JavaDoc _pass=""; //$NON-NLS-1$
139

140     // Retrieve username and password from keyring.
141
if(useAuth){
142       _user=data.getUserId();
143       _pass=data.getPassword();
144     }
145
146     String JavaDoc proxyhost=_host+":"+_port; //$NON-NLS-1$
147
if(_type.equals(IConstants.PROXY_TYPE_HTTP)){
148       proxy=new ProxyHTTP(proxyhost);
149       if(useAuth){
150         ((ProxyHTTP)proxy).setUserPasswd(_user, _pass);
151       }
152     }
153     else if(_type.equals(IConstants.PROXY_TYPE_SOCKS5)){
154       proxy=new ProxySOCKS5(proxyhost);
155       if(useAuth){
156         ((ProxySOCKS5)proxy).setUserPasswd(_user, _pass);
157       }
158     }
159     return proxy;
160   }
161   
162   public static void migrateSSH2Preferences() {
163     Preferences preferences = JSchCorePlugin.getPlugin().getPluginPreferences();
164     if(!preferences.getBoolean(IConstants.PREF_HAS_MIGRATED_SSH2_PREFS)){
165       preferences.setValue(IConstants.PREF_HAS_MIGRATED_SSH2_PREFS, true);
166       migrateSSH2Preferences(new InstanceScope().getNode("")); //$NON-NLS-1$
167
}
168   }
169   
170   public static void migrateSSH2Preferences(org.osgi.service.prefs.Preferences node) {
171     org.osgi.service.prefs.Preferences jschPrefs=node.node(JSchCorePlugin.ID);
172     org.osgi.service.prefs.Preferences ssh2Prefs=node.node("org.eclipse.team.cvs.ssh2"); //$NON-NLS-1$
173
String JavaDoc oldHome = ssh2Prefs.get(IConstants.KEY_OLD_SSH2HOME, null);
174     String JavaDoc oldKey = ssh2Prefs.get(IConstants.KEY_OLD_PRIVATEKEY, null);
175     if (oldHome != null) {
176       jschPrefs.put(IConstants.KEY_SSH2HOME, oldHome);
177       ssh2Prefs.remove(IConstants.KEY_OLD_SSH2HOME);
178     }
179     if (oldKey != null) {
180       jschPrefs.put(IConstants.KEY_PRIVATEKEY, oldKey);
181       ssh2Prefs.remove(IConstants.KEY_OLD_PRIVATEKEY);
182     }
183   }
184
185 }
186
Popular Tags