1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.sftp; |
8 | |
|
9 | |
import org.apache.commons.pool.PoolableObjectFactory; |
10 | |
import org.apache.log4j.Logger; |
11 | |
import org.mule.api.endpoint.EndpointURI; |
12 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
13 | |
|
14 | |
import java.io.IOException; |
15 | |
|
16 | |
public class SftpConnectionFactory implements PoolableObjectFactory |
17 | |
{ |
18 | 0 | private final static Logger logger = Logger.getLogger(SftpConnectionFactory.class); |
19 | |
|
20 | |
private final ImmutableEndpoint endpoint; |
21 | |
|
22 | |
public SftpConnectionFactory(ImmutableEndpoint endpoint) |
23 | 0 | { |
24 | 0 | this.endpoint = endpoint; |
25 | 0 | } |
26 | |
|
27 | |
public void activateObject(Object o) throws Exception |
28 | |
{ |
29 | |
|
30 | 0 | } |
31 | |
|
32 | |
public void destroyObject(Object o) throws Exception |
33 | |
{ |
34 | 0 | SftpClient client = (SftpClient) o; |
35 | 0 | client.disconnect(); |
36 | 0 | } |
37 | |
|
38 | |
public Object makeObject() throws Exception |
39 | |
{ |
40 | 0 | return createClient(endpoint); |
41 | |
} |
42 | |
|
43 | |
public static SftpClient createClient(ImmutableEndpoint endpoint) throws Exception |
44 | |
{ |
45 | 0 | EndpointURI endpointURI = endpoint.getEndpointURI(); |
46 | |
|
47 | 0 | String host = endpointURI.getHost(); |
48 | 0 | if (logger.isDebugEnabled()) |
49 | |
{ |
50 | 0 | logger.debug("Using host: " + host); |
51 | |
} |
52 | |
|
53 | 0 | SftpClient client = new SftpClient(host); |
54 | |
|
55 | |
try |
56 | |
{ |
57 | 0 | int uriPort = endpointURI.getPort(); |
58 | 0 | if (uriPort != -1) |
59 | |
{ |
60 | 0 | if (logger.isDebugEnabled()) |
61 | |
{ |
62 | 0 | logger.debug("Using port: " + uriPort); |
63 | |
} |
64 | 0 | client.setPort(uriPort); |
65 | |
} |
66 | |
|
67 | 0 | SftpUtil sftpUtil = new SftpUtil(endpoint); |
68 | 0 | String identityFile = sftpUtil.getIdentityFile(); |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | 0 | if (identityFile != null) |
87 | |
{ |
88 | 0 | String passphrase = sftpUtil.getPassphrase(); |
89 | |
|
90 | 0 | client.login(endpointURI.getUser(), identityFile, passphrase); |
91 | 0 | } |
92 | |
else |
93 | |
{ |
94 | 0 | client.login(endpointURI.getUser(), endpointURI.getPassword()); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | 0 | if (logger.isDebugEnabled()) |
105 | |
{ |
106 | 0 | logger.debug("Successfully connected to: " + endpointURI); |
107 | |
} |
108 | |
|
109 | 0 | return client; |
110 | |
} |
111 | 0 | catch (IOException e) |
112 | |
{ |
113 | 0 | client.disconnect(); |
114 | 0 | throw e; |
115 | |
} |
116 | |
} |
117 | |
|
118 | |
public void passivateObject(Object o) throws Exception |
119 | |
{ |
120 | |
|
121 | 0 | } |
122 | |
|
123 | |
public boolean validateObject(Object o) |
124 | |
{ |
125 | 0 | SftpClient client = (SftpClient) o; |
126 | 0 | if (logger.isDebugEnabled()) |
127 | |
{ |
128 | 0 | logger.debug("Inside validateObject - will return " + client.isConnected()); |
129 | |
} |
130 | 0 | return client.isConnected(); |
131 | |
} |
132 | |
} |