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