242 lines
12 KiB
Java
242 lines
12 KiB
Java
package it.alescar.sftp.daemon.get;
|
|
|
|
import it.alescar.sftp.utils.ApplicationConstants;
|
|
import it.alescar.sftp.utils.Config;
|
|
import it.alescar.sftp.utils.Func;
|
|
import it.alescar.sftp.utils.SftpManager;
|
|
import org.apache.log4j.Logger;
|
|
|
|
import java.io.*;
|
|
import java.util.Set;
|
|
import java.util.StringTokenizer;
|
|
import java.util.TreeSet;
|
|
|
|
/**
|
|
* @version 2.0 DBCFX_RU_253 riduzione verbosita' log.
|
|
* Problema: a ciascuna chiamata la classe scandisce (ed elenca) TUTTI i files che trova
|
|
* nella cartella remota SFTP, da cui NON vengono mai cancellati,
|
|
* quindi scrive centinaia di righe di log "inutili" piu' volte ogni ora.
|
|
* Soluzione: eliminare i messaggi di log relativi a:
|
|
* 1. file ignorato in quanto non corrisponde al pattern della chiamata corrente (60% log)
|
|
* 2. ripetizione file ignorato in quanto pertinente ma gia' elaborato (20% log)
|
|
*
|
|
* @version 2.1 DBCFX_RU_256 gestione inventario download passati.
|
|
* Problema: nel caso che il processo non riesca a leggere l'inventario dei files gia' acquisiti,
|
|
* scarica tutto quello che trova, saturando il file system sia di dati che di log.
|
|
* Soluzione: se non riesce a leggere l'inventario, esce in errore senza scaricare dati.
|
|
* Suluzione accessoria: abbassato da info a debug i log di normale elaborazione file.
|
|
*
|
|
*/
|
|
public class SFTPGetDaemon {
|
|
|
|
private static Logger log = Logger.getLogger(SFTPGetDaemon.class);
|
|
|
|
//PARAMETRI PATH
|
|
private String localPath;
|
|
//PARAMETRI CONNESSIONE
|
|
private String identity;
|
|
private String passphrase;
|
|
private String username;
|
|
private String host;
|
|
private String port;
|
|
private String knownHosts;
|
|
private String remotePath;
|
|
private String[] fileExtension;
|
|
private boolean unZip;
|
|
private String nomeFilePattern;
|
|
private int timeout;
|
|
//MANAGER DI CONNESSIONE
|
|
private SftpManager ftpManager = null;
|
|
//FILENAME PER MEMORIZZARE I FILE LAVORATI
|
|
private String processedFilename = null;
|
|
|
|
public SFTPGetDaemon(String idSistemaRemoto) throws Exception {
|
|
if (!idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.PIT_REGOLATORIO)
|
|
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.PIT_XDSL)
|
|
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.OPENACCESS)
|
|
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.MPA)
|
|
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.DWHE2E)
|
|
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.NOW_REGOLATORIO)
|
|
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.DWHE2E_REPOSALDINETTI_CO)
|
|
&& !idSistemaRemoto.equalsIgnoreCase(ApplicationConstants.DWHE2E_REPOSALDINETTI_BU)){
|
|
log.fatal("Sistema remoto sconosciuto: " + idSistemaRemoto);
|
|
}
|
|
initConfig(idSistemaRemoto);
|
|
}
|
|
|
|
//SISTEMA=REG per pitagora regolatorio, XDSL per pitagora xdsl
|
|
private void initConfig(String sistema) throws Exception {
|
|
|
|
localPath = Config.getString(ApplicationConstants.GET_PROPERTIES.localPath + sistema);
|
|
identity = Config.getString(ApplicationConstants.GET_PROPERTIES.identity + sistema);
|
|
passphrase = Config.getString(ApplicationConstants.GET_PROPERTIES.passphrase + sistema);
|
|
username = Config.getString(ApplicationConstants.GET_PROPERTIES.username + sistema);
|
|
host = Config.getString(ApplicationConstants.GET_PROPERTIES.host + sistema);
|
|
port = Config.getString(ApplicationConstants.GET_PROPERTIES.port + sistema);
|
|
knownHosts = Config.getString(ApplicationConstants.GET_PROPERTIES.knownHosts + sistema);
|
|
remotePath = Config.getString(ApplicationConstants.GET_PROPERTIES.remotePath + sistema);
|
|
String cfgExtension = Config.getString(ApplicationConstants.GET_PROPERTIES.extension + sistema);
|
|
fileExtension = cfgExtension.split(";");
|
|
nomeFilePattern = Config.getString(ApplicationConstants.GET_PROPERTIES.nomefile + sistema);
|
|
unZip = Config.getYesOrNo(ApplicationConstants.GET_PROPERTIES.uncompress + sistema);
|
|
timeout = Config.getInt(ApplicationConstants.GET_PROPERTIES.timeout + sistema);
|
|
processedFilename = Config.getString(ApplicationConstants.GET_PROPERTIES.processedFilename + sistema);
|
|
|
|
log.info("** SYSTEM ID = [" + sistema + "]");
|
|
log.info("** LOCAL DESTINATION PATH = [" + localPath + "]");
|
|
log.info("** FILE EXTENSIONS = [" + cfgExtension + "]");
|
|
log.info("** FILENAME PATTERN = [" + nomeFilePattern + "]");
|
|
log.info("** UNCOMPRESS = [" + (unZip ? "YES" : "NO") + "]");
|
|
log.info("** TIMEOUT CONNESSIONE = [" + timeout + "]");
|
|
log.info("** PROCESSED FILENAME = [" + processedFilename + "]");
|
|
File f = new File(localPath);
|
|
if (!f.exists()) {
|
|
throw new Exception("Attenzione manca la directory locale di destinazione [" + localPath + "]");
|
|
}
|
|
}
|
|
|
|
public void doTheWork() throws Exception {
|
|
Func function = new Func(); // per l'unzip
|
|
//CONNESSIONE SFTP
|
|
try {
|
|
ftpManager = new SftpManager(identity, passphrase, username, host, port, knownHosts, timeout);
|
|
String[] listaFiles = ftpManager.getListaFiles(remotePath);
|
|
|
|
// Recupera dal file locale, i filename gia' lavorati
|
|
TreeSet<String> fileLavorati = readFromFile(processedFilename);
|
|
|
|
// DBCFX_RU_256 gestione inventario download passati.
|
|
// se non riesce a leggere l'inventario, esce in errore senza scaricare dati.
|
|
if (null == fileLavorati || fileLavorati.isEmpty()) {
|
|
log.error("FAIL - could not read download archive file ["+processedFilename+"] - if absent or empty, please initialize with dummy content [placeholder]");
|
|
throw new NullPointerException("could not read download archive file ["+processedFilename+"]");
|
|
}
|
|
// DBCFX_RU_256 END
|
|
|
|
|
|
for (String fileName : listaFiles) {
|
|
// aggiunta per X1 2013
|
|
StringTokenizer st = new StringTokenizer(nomeFilePattern, ";");
|
|
boolean isOk = false;
|
|
while (st.hasMoreTokens() && !isOk) {
|
|
String app = st.nextToken();
|
|
//isOk = fileName.substring(0, app.length()).equalsIgnoreCase(app);
|
|
|
|
// [S4 NOW] trasforma il pattern Unix like in una regular expression
|
|
String regexPattern = app.replace("?", ".?").replace("*", ".*?");
|
|
isOk = fileName.matches(regexPattern);
|
|
}
|
|
|
|
if (isOk) { //se inizia col pattern corretto
|
|
// DBCFX_RU_253 riduzione verbosita' log.
|
|
//log.info("Elaborazione del file : " + fileName);
|
|
int gotFile = 0;
|
|
for (String aFileExtension : fileExtension) {
|
|
|
|
String fileExt = fileName.substring(fileName.length() - aFileExtension.length());
|
|
//log.debug("fileExt="+fileExt);//fileName.substring(fileName.lastIndexOf("."));
|
|
|
|
if (fileExt.equalsIgnoreCase(aFileExtension)) { //e se l'estensione corrisponde a una di quelle configurate
|
|
if (!fileLavorati.contains(fileName)) { // [S4 NOW] Se non ho ancora lavorato il file
|
|
// DBCFX_RU_253 riduzione verbosita' log.
|
|
log.debug("Elaborazione file [" + localPath + "/" + fileName + "]");
|
|
File outFile = new File(localPath + "/" + fileName);
|
|
|
|
// [S4 NOW] Tolto il remove del file remoto
|
|
//int retCode = ftpManager.getAndRm(remotePath, fileName, outFile); //allora lo riceve
|
|
int retCode = ftpManager.get(remotePath, fileName, outFile); //allora lo riceve
|
|
|
|
gotFile = gotFile + retCode;
|
|
if (unZip //SE unzip abilitato e il file ricevuto e' un .gz
|
|
&& retCode > 0
|
|
&& aFileExtension.substring(aFileExtension.lastIndexOf(".") + 1).equalsIgnoreCase(ApplicationConstants.EXTENSION_GZ)
|
|
) {
|
|
function.unZipAndRm(new File(localPath + "/" + fileName), new File(localPath + "/" + fileName.substring(0, fileName.length() - 3)));
|
|
}
|
|
|
|
File ctrFile = new File(localPath + "/" + fileName.substring(0, fileName.length() - aFileExtension.length()) + "." + ApplicationConstants.EXTENSION_CTR);
|
|
ctrFile.createNewFile();
|
|
// DBCFX_RU_256 abbassato da info a debug
|
|
log.debug("Creato ctr [" + localPath + "/" + fileName.substring(0, fileName.length() - aFileExtension.length()) + "." + ApplicationConstants.EXTENSION_CTR + "]");
|
|
// Aggiorno la lista dei file lavorati
|
|
fileLavorati.add(fileName);
|
|
// Memorizzo subito nel file in caso di fault
|
|
saveToFile(fileLavorati, processedFilename);
|
|
} else {
|
|
gotFile = -1;
|
|
// DBCFX_RU_256 abbassato da info a debug
|
|
log.debug("SKIP - File [" + fileName + "] gia' lavorato!");
|
|
}
|
|
}
|
|
}
|
|
if (gotFile == 0) {
|
|
// DBCFX_RU_253 riduzione verbosita' log.
|
|
//log.warn("Il file " + fileName + " non verra' elaborato in quanto non rispetta la nomenclatura prevista");
|
|
}
|
|
} else {
|
|
// DBCFX_RU_253 riduzione verbosita' log.
|
|
//log.warn("Il file " + fileName + " non verra' elaborato in quanto non rispetta la nomenclatura prevista");
|
|
}
|
|
}
|
|
if (ftpManager != null) {
|
|
ftpManager.release();
|
|
}
|
|
} catch (Exception e) {
|
|
if (ftpManager != null) {
|
|
ftpManager.release();
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Legge i file lavorati precedentemente memorizzati nel file in input
|
|
* Ogni riga e' un nome file.
|
|
* Restituisce un TreeSet
|
|
*
|
|
* @param filename to be listed
|
|
* @pa
|
|
*/
|
|
|
|
public TreeSet<String> readFromFile(String filename) throws Exception {
|
|
TreeSet<String> output = new TreeSet<String>();
|
|
BufferedReader br = null;
|
|
try {
|
|
br = new BufferedReader(new FileReader(new File(filename)));
|
|
String line;
|
|
// if no more lines the readLine() returns null
|
|
while ((line = br.readLine()) != null) {
|
|
// reading lines until the end of the file
|
|
output.add(line);
|
|
}
|
|
return output;
|
|
} finally {
|
|
if (br != null) {
|
|
br.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Salva il TreeSet nel file indicato
|
|
*
|
|
* @param obj to be listed
|
|
* @param path
|
|
*/
|
|
|
|
public void saveToFile(Set<String> obj, String filename) throws Exception {
|
|
PrintWriter pw = null;
|
|
try {
|
|
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"));
|
|
for (String s : obj) {
|
|
pw.println(s);
|
|
}
|
|
pw.flush();
|
|
} finally {
|
|
if (pw != null) {
|
|
pw.close();
|
|
}
|
|
}
|
|
}
|
|
}
|