1 12 package org.eclipse.jsch.internal.core; 13 14 import java.util.Hashtable ; 16 import java.util.Map ; 17 18 import org.eclipse.core.net.proxy.IProxyService; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.core.runtime.preferences.*; 21 import org.eclipse.jsch.core.IJSchService; 22 import org.eclipse.osgi.util.NLS; 23 import org.osgi.framework.BundleContext; 24 import org.osgi.framework.ServiceRegistration; 25 import org.osgi.util.tracker.ServiceTracker; 26 27 import com.jcraft.jsch.JSch; 28 import com.jcraft.jsch.JSchException; 29 30 public class JSchCorePlugin extends Plugin{ 31 32 public static String ID="org.eclipse.jsch.core"; 34 private static final String PROP_REGISTER_SERVICE="org.eclipse.jsch.core.enableService"; 36 public static final int DEFAULT_TIMEOUT=60; 38 private int communicationsTimeout=DEFAULT_TIMEOUT; 39 private boolean needToLoadKnownHosts=true; 40 private boolean needToLoadKeys=true; 41 42 private JSch jsch; 43 44 private String current_pkeys=""; 46 private static final String INFO_PROXY_USER="org.eclipse.team.cvs.core.proxy.user"; private static final String INFO_PROXY_PASS="org.eclipse.team.cvs.core.proxy.pass"; 49 public static final String PT_AUTHENTICATOR="authenticator"; 51 private static JSchCorePlugin plugin; 52 private ServiceTracker tracker; 53 54 private ServiceRegistration proxyService; 55 56 public JSchCorePlugin(){ 57 plugin=this; 58 } 59 60 public static JSchCorePlugin getPlugin(){ 61 return plugin; 62 } 63 64 70 public static void log(CoreException e){ 71 log(e.getStatus().getSeverity(), e.getMessage(), e); 72 } 73 74 82 public static void log(IStatus status){ 83 getPlugin().getLog().log(status); 84 } 85 86 public static void log(int severity, String message, Throwable e){ 87 log(new Status(severity, ID, 0, message, e)); 88 } 89 90 boolean isUseProxy(){ 92 IPreferencesService ps=Platform.getPreferencesService(); 93 String value=ps.get(IConstants.PREF_USE_PROXY, 94 "false", getProxyPreferenceStore()); return value==null ? false : !value.equals("false"); } 97 98 String getProxyType(){ 99 IPreferencesService ps=Platform.getPreferencesService(); 100 return ps.get(IConstants.PREF_PROXY_TYPE, "", getProxyPreferenceStore()); } 102 103 String getProxyHost(){ 104 IPreferencesService ps=Platform.getPreferencesService(); 105 return ps.get(IConstants.PREF_PROXY_HOST, "", getProxyPreferenceStore()); } 107 108 String getProxyPort(){ 109 IPreferencesService ps=Platform.getPreferencesService(); 110 return ps.get(IConstants.PREF_PROXY_PORT, "", getProxyPreferenceStore()); } 112 113 boolean isUseProxyAuth(){ 114 IPreferencesService ps=Platform.getPreferencesService(); 115 String value=ps.get(IConstants.PREF_PROXY_AUTH, 116 "false", getProxyPreferenceStore()); return value==null ? false : !value.equals("false"); } 119 120 String getProxyUser(){ 121 Object user=getAuthInfo().get(INFO_PROXY_USER); 122 return user==null ? "" : (String )user; } 124 125 private Map getAuthInfo(){ 126 return null; 128 } 129 130 String getProxyPassword(){ 131 Object pass=getAuthInfo().get(INFO_PROXY_PASS); 132 return pass==null ? "" : (String )pass; } 134 135 140 public int getTimeout(){ 141 return communicationsTimeout; 142 } 143 144 151 public void setTimeout(int timeout){ 152 this.communicationsTimeout=Math.max(0, timeout); 153 } 154 155 158 IEclipsePreferences getInstancePreferences(){ 159 return new InstanceScope().getNode(ID); 160 } 161 162 private static IEclipsePreferences[] proxyPreferences; 163 164 static IEclipsePreferences[] getProxyPreferenceStore(){ 165 if(proxyPreferences==null){ 166 proxyPreferences=new IEclipsePreferences[] { 167 new InstanceScope().getNode("org.eclipse.team.cvs.ui"), new DefaultScope().getNode("org.eclipse.team.cvs.ui") }; 170 } 171 return proxyPreferences; 172 } 173 174 public synchronized JSch getJSch(){ 175 if(jsch==null) 176 jsch=new JSch(); 177 return jsch; 178 } 179 180 public void loadKnownHosts(){ 181 Preferences preferences=JSchCorePlugin.getPlugin().getPluginPreferences(); 182 String ssh_home=preferences.getString(IConstants.KEY_SSH2HOME); 183 184 if(ssh_home.length()==0) 185 ssh_home=PreferenceInitializer.SSH_HOME_DEFAULT; 186 187 java.io.File file=new java.io.File (ssh_home, "known_hosts"); try{ 189 getJSch().setKnownHosts(file.getPath()); 190 } 191 catch(JSchException e){ 192 JSchCorePlugin.log(IStatus.ERROR, NLS.bind( 193 "An error occurred while loading the know hosts file {0}", file .getAbsolutePath()), e); 195 } 196 needToLoadKnownHosts=false; 197 } 198 199 public boolean isNeedToLoadKnownHosts(){ 200 return needToLoadKnownHosts; 201 } 202 203 public void setNeedToLoadKnownHosts(boolean needToLoadKnownHosts){ 204 this.needToLoadKnownHosts=needToLoadKnownHosts; 205 } 206 207 public boolean isNeedToLoadKeys(){ 208 return needToLoadKeys; 209 } 210 211 public void setNeedToLoadKeys(boolean needToLoadKeys){ 212 this.needToLoadKeys=needToLoadKeys; 213 } 214 215 public void loadPrivateKeys(){ 216 current_pkeys=Utils.loadPrivateKeys(getJSch(), current_pkeys); 217 setNeedToLoadKeys(false); 218 } 219 220 226 public IProxyService getProxyService(){ 227 return (IProxyService)tracker.getService(); 228 } 229 230 public void start(BundleContext context) throws Exception { 231 super.start(context); 232 tracker=new ServiceTracker(getBundle().getBundleContext(), 233 IProxyService.class.getName(), null); 234 tracker.open(); 235 if(Boolean 236 .valueOf(System.getProperty(PROP_REGISTER_SERVICE, "true")).booleanValue()){ proxyService=getBundle().getBundleContext().registerService( 238 IJSchService.class.getName(), JSchProvider.getInstance(), new Hashtable ()); 239 } 240 } 241 242 public void stop(BundleContext context) throws Exception { 243 super.stop(context); 244 tracker.close(); 245 if(proxyService!=null){ 246 proxyService.unregister(); 247 proxyService=null; 248 } 249 } 250 } 251 | Popular Tags |