Skip to content

Commit 7d51657

Browse files
authored
Merge pull request #44 from embulk/implement-original-sftp-provider
Implement original SFTP provider for Apache Commons VFS2
2 parents 3d6d2e2 + af1819a commit 7d51657

File tree

6 files changed

+236
-5
lines changed

6 files changed

+236
-5
lines changed

src/main/java/org/embulk/output/sftp/SftpUtils.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.apache.commons.vfs2.FileObject;
66
import org.apache.commons.vfs2.FileSystemException;
77
import org.apache.commons.vfs2.FileSystemOptions;
8-
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
8+
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
99
import org.apache.commons.vfs2.provider.sftp.IdentityInfo;
1010
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
1111
import org.embulk.config.ConfigException;
@@ -32,22 +32,29 @@
3232
public class SftpUtils
3333
{
3434
private final Logger logger = Exec.getLogger(SftpUtils.class);
35-
private final StandardFileSystemManager manager;
35+
private final DefaultFileSystemManager manager;
3636
private final FileSystemOptions fsOptions;
3737
private final String userInfo;
3838
private final String host;
3939
private final int port;
4040
private final int maxConnectionRetry;
4141

42-
private StandardFileSystemManager initializeStandardFileSystemManager()
42+
private DefaultFileSystemManager initializeStandardFileSystemManager()
4343
{
4444
if (!logger.isDebugEnabled()) {
4545
// TODO: change logging format: org.apache.commons.logging.Log
4646
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
4747
}
48-
StandardFileSystemManager manager = new StandardFileSystemManager();
49-
manager.setClassLoader(SftpUtils.class.getClassLoader());
48+
/*
49+
* We can use StandardFileSystemManager instead of DefaultFileSystemManager
50+
* when Apache Commons VFS2 removes permission check logic when remote file renaming.
51+
* https://github.com/embulk/embulk-output-sftp/issues/40
52+
* https://github.com/embulk/embulk-output-sftp/pull/44
53+
* https://issues.apache.org/jira/browse/VFS-590
54+
*/
55+
DefaultFileSystemManager manager = new DefaultFileSystemManager();
5056
try {
57+
manager.addProvider("sftp", new org.embulk.output.sftp.provider.sftp.SftpFileProvider());
5158
manager.init();
5259
}
5360
catch (FileSystemException e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.embulk.output.sftp.provider.sftp;
18+
19+
import org.apache.commons.vfs2.FileSystemException;
20+
import org.apache.commons.vfs2.provider.AbstractFileName;
21+
import org.apache.commons.vfs2.provider.sftp.SftpFileSystem;
22+
23+
/*
24+
* We can remove this file when Apache Commons VFS2 removes permission check logic when remote file renaming.
25+
* https://github.com/embulk/embulk-output-sftp/issues/40
26+
* https://github.com/embulk/embulk-output-sftp/pull/44
27+
* https://issues.apache.org/jira/browse/VFS-590
28+
*/
29+
/**
30+
* An SFTP file.
31+
*/
32+
public class SftpFileObject extends org.apache.commons.vfs2.provider.sftp.SftpFileObject
33+
{
34+
protected SftpFileObject(final AbstractFileName name, final SftpFileSystem fileSystem) throws FileSystemException
35+
{
36+
super(name, fileSystem);
37+
}
38+
39+
@Override
40+
protected boolean doIsWriteable() throws Exception
41+
{
42+
return true;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.embulk.output.sftp.provider.sftp;
18+
19+
import com.jcraft.jsch.Session;
20+
import org.apache.commons.vfs2.FileName;
21+
import org.apache.commons.vfs2.FileSystem;
22+
import org.apache.commons.vfs2.FileSystemException;
23+
import org.apache.commons.vfs2.FileSystemOptions;
24+
import org.apache.commons.vfs2.UserAuthenticationData;
25+
import org.apache.commons.vfs2.provider.GenericFileName;
26+
import org.apache.commons.vfs2.provider.sftp.SftpClientFactory;
27+
import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
28+
29+
/*
30+
* We can remove this file when Apache Commons VFS2 removes permission check logic when remote file renaming.
31+
* https://github.com/embulk/embulk-output-sftp/issues/40
32+
* https://github.com/embulk/embulk-output-sftp/pull/44
33+
* https://issues.apache.org/jira/browse/VFS-590
34+
*/
35+
/**
36+
* A provider for accessing files over SFTP.
37+
*/
38+
public class SftpFileProvider extends org.apache.commons.vfs2.provider.sftp.SftpFileProvider
39+
{
40+
/**
41+
* Constructs a new provider.
42+
*/
43+
public SftpFileProvider()
44+
{
45+
super();
46+
}
47+
48+
/**
49+
* Creates a {@link FileSystem}.
50+
*/
51+
@Override
52+
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
53+
throws FileSystemException
54+
{
55+
// JSch jsch = createJSch(fileSystemOptions);
56+
57+
// Create the file system
58+
final GenericFileName rootName = (GenericFileName) name;
59+
60+
Session session;
61+
UserAuthenticationData authData = null;
62+
try {
63+
authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
64+
65+
session = SftpClientFactory.createConnection(rootName.getHostName(), rootName.getPort(),
66+
UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
67+
UserAuthenticatorUtils.toChar(rootName.getUserName())),
68+
UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD,
69+
UserAuthenticatorUtils.toChar(rootName.getPassword())),
70+
fileSystemOptions);
71+
}
72+
catch (final Exception e) {
73+
throw new FileSystemException("vfs.provider.sftp/connect.error", name, e);
74+
}
75+
finally {
76+
UserAuthenticatorUtils.cleanup(authData);
77+
}
78+
79+
return new SftpFileSystem(rootName, session, fileSystemOptions);
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.embulk.output.sftp.provider.sftp;
18+
19+
import com.jcraft.jsch.Session;
20+
import org.apache.commons.vfs2.FileObject;
21+
import org.apache.commons.vfs2.FileSystemException;
22+
import org.apache.commons.vfs2.FileSystemOptions;
23+
import org.apache.commons.vfs2.provider.AbstractFileName;
24+
import org.apache.commons.vfs2.provider.GenericFileName;
25+
26+
/*
27+
* We can remove this file when Apache Commons VFS2 removes permission check logic when remote file renaming.
28+
* https://github.com/embulk/embulk-output-sftp/issues/40
29+
* https://github.com/embulk/embulk-output-sftp/pull/44
30+
* https://issues.apache.org/jira/browse/VFS-590
31+
*/
32+
/**
33+
* Represents the files on an SFTP server.
34+
*/
35+
public class SftpFileSystem extends org.apache.commons.vfs2.provider.sftp.SftpFileSystem
36+
{
37+
protected SftpFileSystem(final GenericFileName rootName, final Session session,
38+
final FileSystemOptions fileSystemOptions)
39+
{
40+
super(rootName, null, fileSystemOptions);
41+
}
42+
43+
/**
44+
* Creates a file object. This method is called only if the requested file is not cached.
45+
*/
46+
@Override
47+
protected FileObject createFile(final AbstractFileName name) throws FileSystemException
48+
{
49+
return new SftpFileObject(name, this);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<body>
18+
<p>The SFTP Provider.</p>
19+
</body>

src/main/resources/providers.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<!--
18+
We can remove this file when Apache Commons VFS2 removes permission check logic when remote file renaming.
19+
https://github.com/embulk/embulk-output-sftp/issues/40
20+
https://github.com/embulk/embulk-output-sftp/pull/44
21+
https://issues.apache.org/jira/browse/VFS-590
22+
-->
23+
<providers>
24+
<provider class-name="org.embulk.output.sftp.provider.sftp.SftpFileProvider">
25+
<scheme name="sftp"/>
26+
<if-available class-name="javax.crypto.Cipher"/>
27+
<if-available class-name="com.jcraft.jsch.JSch"/>
28+
</provider>
29+
</providers>

0 commit comments

Comments
 (0)