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