📄 ftpserviceimpl.java
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file. */package org.butor.net.ftp.service;import java.util.StringTokenizer;import java.util.Hashtable;import java.io.IOException;import org.butor.config.Config;import org.butor.config.lowlevel.IProperties;import org.butor.fwService.FwServiceImpl;import org.butor.helper.FileHelper;import org.butor.log.Log;import org.butor.net.ftp.FtpClient;import org.butor.net.ftp.IFTPInputStreamConsumer;import java.util.Enumeration;import java.net.Socket;/** * Implementation of the FTP service! * * @author Aiman Sawan */public class FtpServiceImpl extends FwServiceImpl implements IFtpService { public static final String FTP_SESSION_LIST = "ftp_session_list"; public static final String FTP_SESSION_HOST = "host"; public static final String FTP_SESSION_USER = "user"; public static final String FTP_SESSION_PASSWORD_FILE = "password_file"; public static final String FTP_SESSION_INITIAL_DIR = "initial_dir"; protected Hashtable f_ftpSessions = new Hashtable(); private FtpClient prepareFtpClient(String sessionName) { FtpClient ftpClient = getFtpClient(sessionName); if (ftpClient == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "isSessionValid", "Ftp Session not found: " + sessionName); return null; } boolean reconnect = false; // Test the connection, if it fails // reconnect to FTP server. try { ftpClient.testConnection(); } catch (IOException e) { reconnect = true; } // If FTP client is not logged in, proceed with login. if (!ftpClient.isLoggedIn()) { reconnect = true; } if (reconnect) { if (!connect(sessionName)) { return null; } } return ftpClient; } /** * Change Directory * * @param sessionName, name of the ftp session to use * @param dir String, name of remote directory. */ public boolean changeDirectory(String sessionName, String dir) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { ftpClient.changeDirectory(dir); return true; } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "changeDirectory", e); } } return false; } } /** * Init. */ protected synchronized void closeSessions() { if (f_ftpSessions == null) { return; } Enumeration sessionNames = f_ftpSessions.keys(); while (sessionNames.hasMoreElements()) { String sessionName = (String)sessionNames.nextElement(); closeSession(sessionName); } } protected synchronized void closeSession(String sessionName) { if (f_ftpSessions == null) { return; } FtpClient ftpSession = (FtpClient)f_ftpSessions.get(sessionName); if (ftpSession != null) { try { ftpSession.logOut(); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "closeSession(" + sessionName + ")", e); } } } /** * Init. */ protected boolean connect(String sessionName) { Log.logStr(this, Log.LOG_TYPE_INFO, "connect", "connection to " + sessionName); FtpClient ftpClient = getFtpClient(sessionName); if (ftpClient != null) { try { ftpClient.logIn(); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "connect", e); return false; } } return true; } /** * Stop the processing of the images of waiting IRIS clients. */ public boolean createDirectory(String sessionName, String dir) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { String curDir = ""; StringTokenizer tokens = new StringTokenizer(dir, "\\/"); while (tokens.hasMoreTokens()) { curDir = curDir + "/" + tokens.nextToken(); if (!existsDirectory(sessionName, curDir)) { ftpClient.createDirectory(curDir); } } return true; } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "createDirectory", e); } } return false; } } /** * get current Directory name * * @param sessionName, name of the ftp session to use */ public String currentDirectory(String sessionName) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { return ftpClient.currentDirectory(); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "currentDirectory", e); } } return null; } } /** * Stop the processing of the images of waiting IRIS clients. */ public boolean existsDirectory(String sessionName, String dir) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { return ftpClient.existsDirectory(dir); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "existsDirectory", e); } } return false; } } public boolean fileExists(String sessionName, String file) { String[] list = retrieveList(sessionName, file, false); if (list == null) { return false; } return (list.length > 0); } /** * Init. */ protected FtpClient getFtpClient(String sessionName) { FtpClient ftpClient = null; if (f_ftpSessions != null) { try { ftpClient = (FtpClient)f_ftpSessions.get(sessionName); } catch (ClassCastException e) { Log.logStr(this, Log.LOG_TYPE_ERROR, "getFtpClient", "Ftp Session not found: " +sessionName); } } return ftpClient; } /** * Gets Socket of stored file. * The file will be created on remote and a socket on it will be returned to * makes writes on its output stream. * * @param sessionName, name of the ftp session to use * @param remoteFile String, name of remote file to create. */ public Socket getSocketToStoreFile(String sessionName, String remoteFile) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { return ftpClient.getSocketToStoreFile(remoteFile); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "getSocketToStoreFile()", e); } } return null; } } /** * Init. */ public void init() { Log.logStr(this, Log.LOG_TYPE_INFO, "init()", "..."); reload(); } /** * Load all ftp sessions properties. */ public synchronized void reload() { closeSessions(); f_ftpSessions = new Hashtable(); // get service config. IProperties serviceProps = Config.getPropertyList(FwServiceImpl.PROPERTY_PREFIX_SERVICE + getServiceName()); if (serviceProps == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "reload", "Can't find " + FwServiceImpl.PROPERTY_PREFIX_SERVICE + getServiceName() + " in config"); return; } // Gets the ftp session list Object obj = serviceProps.getProperty(FTP_SESSION_LIST); if (obj == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "reload", "ftp_session_list is mandatory"); return; } if (!(obj instanceof IProperties)) { Log.logStr(this, Log.LOG_TYPE_ERROR, "reload", "ftp_session_list is not a list!"); return; } // Read each defined ftp sessions IProperties ftpSessionList = (IProperties)obj; for (int i = 0; i < ftpSessionList.size(); i++) { obj = ftpSessionList.getProperty(i); if (obj != null && (obj instanceof IProperties)) { IProperties ftpSession = (IProperties)obj; String sessionName = ftpSession.getName(); String host = ftpSession.getProperty(FTP_SESSION_HOST, ""); String user = ftpSession.getProperty(FTP_SESSION_USER, ""); String userPassFile = ftpSession.getProperty(FTP_SESSION_PASSWORD_FILE, ""); String userPass = FileHelper.getPasswordFromFile(userPassFile); String initialDir = ftpSession.getProperty(FTP_SESSION_INITIAL_DIR, null); if ((host == null) || (user == null) || (userPassFile == null) || (userPass == null)) { Log.logStr(this, Log.LOG_TYPE_ERROR, "reload", "Null property value in FTP session: " + sessionName); continue; } FtpClient ftpClient = new FtpClient(); ftpClient.setHost(host); ftpClient.setUserName(user); ftpClient.setPassword(userPass); ftpClient.setInitialDir(initialDir); f_ftpSessions.put(sessionName, ftpClient); } } } /** * Stop the processing of the images of waiting IRIS clients. */ public boolean retrieveFile(String sessionName, String localFile, String remoteFile, char type) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { ftpClient.retrieveFile(localFile, remoteFile, type); return true; } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "retrieveFile", e); } } return false; } } /** * Philip Whitford * @param sessionName, name of the ftp session to use * @param file, the file name * @param reader, the implementation that will read the requested file * @param transferType, Ex ASCII_TRANSFER_MODE, BINARY_TRANSFER_MODE...etc */ public void retrieveFileToConsumer(String sessionName, String path, String file, char transferType, IFTPInputStreamConsumer inStreamConsumer) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { ftpClient.retrieveFileToConsumer(path, file, transferType, inStreamConsumer); return; } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "retrieveFileToConsumer-" + sessionName, e); } } } } /** * Stop the processing of the images of waiting IRIS clients. */ //pw public StringBuffer retrieveFileSB(String sessionName, String localFile) { return retrieveFileSB(sessionName, localFile, FtpService.BINARY_TRANSFER_MODE); } public StringBuffer retrieveFileSB(String sessionName, String localFile, char transferType) { // Serialize requests on the same ftp client synchronized (sessionName) { transferType = FtpClient.BINARY_TRANSFER_MODE; FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { return ftpClient.retrieveFileSB(localFile, transferType); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "retrieveFileSB", e); } } return null; } } /** * @return a directory content list * * @param sessionName, name of the ftp session to use * @param dir, directory to list */ public String[] retrieveList(String sessionName, String dir, boolean detail) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { return ftpClient.retrieveList(dir, detail); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "retrieveList", e); } } return null; } } /** * Stop the processing of the images of waiting IRIS clients. */ public boolean storeFile(String sessionName, String localFile, String remoteFile, char type) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); boolean retryDone = false; if (ftpClient != null) { try { ftpClient.storeFile(localFile, remoteFile, type); return true; } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "storeFile", e); } } return false; } } /** * Stop the processing of the images of waiting IRIS clients. */ public boolean storeFileSB(String sessionName, StringBuffer buf, String remoteFile, char type) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { ftpClient.storeFile(buf, remoteFile, type); return true; } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "storeFileSB", e); } } return false; } } /** * */ public StringBuffer unzipFileSB(String sessionName, String remoteFile) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { return ftpClient.unzipFileSB(remoteFile); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "unzipFileSB", e); } } return null; } } /** * */ public boolean zipFileSB(String sessionName, StringBuffer buf, String remoteFile) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { ftpClient.zipFileSB(buf, remoteFile); return true; } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "zipFileSB", e); } } return false; } } /***************************** * pw * changeFilePermission ******************************/ public boolean changeFilePermission(String sessionName, String permission, String fileName) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { return ftpClient.changeFilePermission(permission, fileName); } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "changeFilePermission", e); } } return false; } } /** * @see org.butor.wak.service.IFwServiceMonitor#shutdown() */ public void shutdown() { } /** * @see org.butor.wak.ftpService.IFtpService#deleteFile(java.lang.String, java.lang.String) */ public boolean deleteFile(String sessionName, String remoteFile) { // Serialize requests on the same ftp client synchronized (sessionName) { FtpClient ftpClient = prepareFtpClient(sessionName); if (ftpClient != null) { try { ftpClient.deleteFile(remoteFile); return true; } catch (IOException e) { Log.logException(this, Log.LOG_TYPE_ERROR, "deleteFile", e); } } return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -