KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.jsch.core.IJSchService;
15
16 import com.jcraft.jsch.*;
17
18 /**
19  * A static class whose purpose is to ensure that all the JSch preferences are properly pushed into JSch
20  * before a {@link Session} is created.
21  * @since 1.0
22  */

23 public class JSchProvider implements IJSchService {
24   
25   private static JSchProvider instance;
26
27   /* (non-Javadoc)
28    * @see org.eclipse.jsch.core.IJSchService#createSession(java.lang.String, int, java.lang.String)
29    */

30   public Session createSession(String JavaDoc host, int port, String JavaDoc username) throws JSchException {
31     if(port == -1)
32       port = IConstants.SSH_DEFAULT_PORT;
33
34     if(JSchCorePlugin.getPlugin().isNeedToLoadKnownHosts()){
35       JSchCorePlugin.getPlugin().loadKnownHosts();
36     }
37
38     if(JSchCorePlugin.getPlugin().isNeedToLoadKeys()){
39       JSchCorePlugin.getPlugin().loadPrivateKeys();
40     }
41     
42     return Utils.createSession(JSchCorePlugin.getPlugin().getJSch(), username, host, port);
43   }
44   
45   /* (non-Javadoc)
46    * @see org.eclipse.jsch.core.IJSchService#connect(com.jcraft.jsch.Session, int, org.eclipse.core.runtime.IProgressMonitor)
47    */

48   public void connect(Session session, int timeout,
49       IProgressMonitor monitor) throws JSchException{
50     session.setSocketFactory(new ResponsiveSocketFactory(monitor, timeout));
51     try{
52       session.connect();
53     }
54     catch(JSchException e){
55       if(session.isConnected())
56         session.disconnect();
57       throw e;
58     }
59   }
60   
61   /* (non-Javadoc)
62    * @see org.eclipse.jsch.core.IJSchService#getProxyForHost(java.lang.String, java.lang.String)
63    */

64   public Proxy getProxyForHost(String JavaDoc host, String JavaDoc proxyType) {
65     return Utils.getProxyForHost(host, proxyType);
66   }
67
68   public static IJSchService getInstance(){
69     if (instance == null)
70       instance = new JSchProvider();
71     return instance;
72   }
73
74   /* (non-Javadoc)
75    * @see org.eclipse.jsch.core.IJSchService#connect(com.jcraft.jsch.Proxy, java.lang.String, java.lang.String, int, org.eclipse.core.runtime.IProgressMonitor)
76    */

77   public void connect(Proxy proxy, String JavaDoc host, int port, int timeout,
78       IProgressMonitor monitor) throws JSchException {
79     try{
80       proxy.connect(new ResponsiveSocketFactory(monitor, timeout), host, port, timeout);
81     }
82     catch(JSchException e){
83       throw e;
84     }
85     catch(Exception JavaDoc e){
86       new JSchException(e.getMessage());
87     }
88   }
89
90 }
91
Popular Tags