KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Atsuhiko Yamanaka, JCraft,Inc. - initial API and implementation.
10  * IBM Corporation - ongoing maintenance
11  *******************************************************************************/

12 package org.eclipse.jsch.internal.core;
13
14 // import org.eclipse.core.runtime.Plugin;
15
import java.util.Hashtable JavaDoc;
16 import java.util.Map JavaDoc;
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 JavaDoc ID="org.eclipse.jsch.core"; //$NON-NLS-1$
33

34   private static final String JavaDoc PROP_REGISTER_SERVICE="org.eclipse.jsch.core.enableService"; //$NON-NLS-1$
35

36   // communication timeout with the server
37
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 JavaDoc current_pkeys=""; //$NON-NLS-1$
45

46   private static final String JavaDoc INFO_PROXY_USER="org.eclipse.team.cvs.core.proxy.user"; //$NON-NLS-1$
47
private static final String JavaDoc INFO_PROXY_PASS="org.eclipse.team.cvs.core.proxy.pass"; //$NON-NLS-1$
48

49   public static final String JavaDoc PT_AUTHENTICATOR="authenticator"; //$NON-NLS-1$
50

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   /**
65    * Convenience method for logging CoreExceptions to the plugin log
66    *
67    * @param e
68    * the exception
69    */

70   public static void log(CoreException e){
71     log(e.getStatus().getSeverity(), e.getMessage(), e);
72   }
73
74   /**
75    * Log the given status. Do not use this method for the IStatus from a
76    * CoreException. Use<code>log(CoreException)</code> instead so the stack
77    * trace is not lost.
78    *
79    * @param status
80    * the status
81    */

82   public static void log(IStatus status){
83     getPlugin().getLog().log(status);
84   }
85
86   public static void log(int severity, String JavaDoc message, Throwable JavaDoc e){
87     log(new Status(severity, ID, 0, message, e));
88   }
89
90   // proxy configuration
91
boolean isUseProxy(){
92     IPreferencesService ps=Platform.getPreferencesService();
93     String JavaDoc value=ps.get(IConstants.PREF_USE_PROXY,
94         "false", getProxyPreferenceStore()); //$NON-NLS-1$
95
return value==null ? false : !value.equals("false"); //$NON-NLS-1$
96
}
97
98   String JavaDoc getProxyType(){
99     IPreferencesService ps=Platform.getPreferencesService();
100     return ps.get(IConstants.PREF_PROXY_TYPE, "", getProxyPreferenceStore()); //$NON-NLS-1$
101
}
102
103   String JavaDoc getProxyHost(){
104     IPreferencesService ps=Platform.getPreferencesService();
105     return ps.get(IConstants.PREF_PROXY_HOST, "", getProxyPreferenceStore()); //$NON-NLS-1$
106
}
107
108   String JavaDoc getProxyPort(){
109     IPreferencesService ps=Platform.getPreferencesService();
110     return ps.get(IConstants.PREF_PROXY_PORT, "", getProxyPreferenceStore()); //$NON-NLS-1$
111
}
112
113   boolean isUseProxyAuth(){
114     IPreferencesService ps=Platform.getPreferencesService();
115     String JavaDoc value=ps.get(IConstants.PREF_PROXY_AUTH,
116         "false", getProxyPreferenceStore()); //$NON-NLS-1$
117
return value==null ? false : !value.equals("false"); //$NON-NLS-1$
118
}
119
120   String JavaDoc getProxyUser(){
121     Object JavaDoc user=getAuthInfo().get(INFO_PROXY_USER);
122     return user==null ? "" : (String JavaDoc)user; //$NON-NLS-1$
123
}
124
125   private Map JavaDoc getAuthInfo(){
126     // XXX Auto-generated method stub
127
return null;
128   }
129
130   String JavaDoc getProxyPassword(){
131     Object JavaDoc pass=getAuthInfo().get(INFO_PROXY_PASS);
132     return pass==null ? "" : (String JavaDoc)pass; //$NON-NLS-1$
133
}
134
135   /**
136    * Get the communications timeout value in seconds
137    *
138    * @return the timeout value in seconds
139    */

140   public int getTimeout(){
141     return communicationsTimeout;
142   }
143
144   /**
145    * Set the timeout value for communications to a value in seconds. The value
146    * must be greater than or equal 0. If is it 0, there is no timeout.
147    *
148    * @param timeout
149    * the timeout value in seconds
150    */

151   public void setTimeout(int timeout){
152     this.communicationsTimeout=Math.max(0, timeout);
153   }
154
155   /**
156    * Return the preferences node in the instance scope
157    */

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"), //$NON-NLS-1$
168
new DefaultScope().getNode("org.eclipse.team.cvs.ui") //$NON-NLS-1$
169
};
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 JavaDoc 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 JavaDoc file=new java.io.File JavaDoc(ssh_home, "known_hosts"); //$NON-NLS-1$
188
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 //$NON-NLS-1$
194
.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   /**
221    * Return the {@link IProxyService} or <code>null</code> if the service is
222    * not available.
223    *
224    * @return the {@link IProxyService} or <code>null</code>
225    */

226   public IProxyService getProxyService(){
227     return (IProxyService)tracker.getService();
228   }
229
230   public void start(BundleContext context) throws Exception JavaDoc{
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()){ //$NON-NLS-1$
237
proxyService=getBundle().getBundleContext().registerService(
238           IJSchService.class.getName(), JSchProvider.getInstance(), new Hashtable JavaDoc());
239     }
240   }
241
242   public void stop(BundleContext context) throws Exception JavaDoc{
243     super.stop(context);
244     tracker.close();
245     if(proxyService!=null){
246       proxyService.unregister();
247       proxyService=null;
248     }
249   }
250 }
251
Popular Tags