
net.sourceforge.tink.model.impl.TinkPublisherVFSImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tink-model Show documentation
Show all versions of tink-model Show documentation
Core tink library. Can integrate easily to any programming environment.
The newest version!
/**
* Copyright 2008,2009 Ivan SZKIBA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package net.sourceforge.tink.model.impl;
import net.sourceforge.tink.model.Output;
import net.sourceforge.tink.model.TinkException;
import net.sourceforge.tink.model.TinkPublisher;
import org.apache.commons.vfs.AllFileSelector;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.auth.StaticUserAuthenticator;
import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;
import org.apache.commons.vfs.provider.ftp.FtpFileSystemConfigBuilder;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
public class TinkPublisherVFSImpl extends TinkPublisher
{
private FileObject remote;
@Override public void execute() throws TinkException
{
Output out = getOutput();
out.info("Publishing %s to %s", getConfig().getLocal().toString(), getConfig().getRemote().toString());
try
{
login();
mirror(getConfig().getLocal(), remote);
logout();
}
catch (IOException x)
{
throw new TinkException(x);
}
}
protected void copy(File localFile, FileObject remoteFile) throws TinkException, IOException
{
OutputStream out = remoteFile.getContent().getOutputStream();
InputStream in = new FileInputStream(localFile);
byte[] buff = new byte[1024];
for (int n = in.read(buff); n > 0; n = in.read(buff))
{
out.write(buff, 0, n);
}
out.close();
in.close();
}
protected void keep(FileObject remoteDir, Set names) throws TinkException, IOException
{
for (FileObject child : remoteDir.getChildren())
{
if (!names.contains(child.getName().getBaseName()))
{
child.delete(new AllFileSelector());
}
}
}
protected void login() throws TinkException, IOException
{
URI location = getConfig().getRemote();
StaticUserAuthenticator auth = new StaticUserAuthenticator(null, getConfig().getUser(), getConfig().getPassword());
FileSystemOptions opts = new FileSystemOptions();
FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
remote = VFS.getManager().resolveFile(location.toString(), opts);
}
protected void logout() throws TinkException, IOException
{
VFS.getManager().closeFileSystem(remote.getFileSystem());
}
protected void mirror(File localFile, FileObject remoteFile) throws TinkException, IOException
{
if (localFile.isFile())
{
copy(localFile, remoteFile);
}
else
{
remoteFile.createFolder();
Set names = new HashSet();
for (File f : localFile.listFiles())
{
mirror(f, remoteFile.resolveFile(f.getName()));
names.add(f.getName());
}
keep(remoteFile, names);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy