Search in sources :

Example 1 with FtpImportedFile

use of org.meveo.model.jobs.FtpImportedFile in project meveo by meveo-org.

the class FtpAdapterJobBean method createImportedFileHistory.

/**
 * Creates the imported file history.
 *
 * @param fileName the file name
 * @param lastModification the last modification
 * @param size the size
 * @param remoteServer the remote server
 * @param remotePort the remote port
 * @param ftpInputDirectory the ftp input directory
 * @throws NoSuchAlgorithmException the no such algorithm exception
 * @throws UnsupportedEncodingException the unsupported encoding exception
 * @throws BusinessException the business exception
 */
private void createImportedFileHistory(String fileName, Date lastModification, Long size, String remoteServer, int remotePort, String ftpInputDirectory) throws NoSuchAlgorithmException, UnsupportedEncodingException, BusinessException {
    FtpImportedFile ftpImportedFile = new FtpImportedFile();
    ftpImportedFile.setCode(getCode(remoteServer, remotePort, fileName, ftpInputDirectory, size, lastModification));
    ftpImportedFile.setDescription(fileName);
    ftpImportedFile.setLastModification(lastModification);
    ftpImportedFile.setSize(size);
    ftpImportedFile.setImportDate(new Date());
    ftpImportedFile.setUri(getUri(remoteServer, remotePort, fileName, ftpInputDirectory));
    ftpImportedFileService.create(ftpImportedFile);
}
Also used : FtpImportedFile(org.meveo.model.jobs.FtpImportedFile) Date(java.util.Date)

Example 2 with FtpImportedFile

use of org.meveo.model.jobs.FtpImportedFile in project meveo by meveo-org.

the class FtpAdapterJobBean method execute.

/**
 * Execute.
 *
 * @param result the result
 * @param jobInstance the job instance
 * @param distDirectory the dist directory
 * @param remoteServer the remote server
 * @param remotePort the remote port
 * @param removeDistantFile the remove distant file
 * @param ftpInputDirectory the ftp input directory
 * @param extention the extention
 * @param ftpUsername the ftp username
 * @param ftpPassword the ftp password
 * @param ftpProtocol the ftp protocol
 */
@Interceptors({ JobLoggingInterceptor.class, PerformanceInterceptor.class })
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void execute(JobExecutionResultImpl result, JobInstance jobInstance, String distDirectory, String remoteServer, int remotePort, boolean removeDistantFile, String ftpInputDirectory, String extention, String ftpUsername, String ftpPassword, String ftpProtocol) {
    log.debug("start ftpClient...");
    int cpOk = 0, cpKo = 0, cpAll = 0, cpWarn = 0;
    FileObject[] children;
    try {
        initialize(distDirectory, ftpUsername, ftpPassword, extention, "SFTP".equalsIgnoreCase(ftpProtocol));
        String ftpAddress = ftpProtocol.toLowerCase() + "://" + ftpUsername + ":" + ftpPassword + "@" + remoteServer + ":" + remotePort + ftpInputDirectory;
        log.debug("ftpAddress:" + ftpAddress);
        try {
            sftpFile = fsManager.resolveFile(ftpAddress, opts);
            log.debug("SFTP connection successfully established to " + ftpAddress);
        } catch (FileSystemException ex) {
            result.setReport(StringUtils.truncate(ex.getMessage(), 255, true));
            throw new RuntimeException("SFTP error parsing path " + ftpInputDirectory, ex);
        }
        try {
            children = sftpFile.getChildren();
        } catch (FileSystemException ex) {
            result.setReport(StringUtils.truncate(ex.getMessage(), 255, true));
            throw new RuntimeException("Error collecting directory listing of " + ftpInputDirectory, ex);
        }
        for (FileObject fileObject : children) {
            if (!jobExecutionService.isJobRunningOnThis(result.getJobInstance())) {
                break;
            }
            try {
                String fileName = fileObject.getName().getBaseName();
                String relativePath = File.separatorChar + fileName;
                if (fileObject.getType() != FileType.FILE) {
                    log.debug("Ignoring non-file " + fileObject.getName());
                    continue;
                }
                log.debug("Examining remote file " + fileName);
                log.debug("patern:" + filePattern.matcher(fileName));
                if (!filePattern.matcher(fileName).matches()) {
                    log.debug("Filename does not match, skipping file :" + fileName);
                    continue;
                }
                long size = fileObject.getContent().getSize();
                long lastModification = fileObject.getContent().getLastModifiedTime();
                String code = getCode(remoteServer, remotePort, fileName, ftpInputDirectory, size, new Date(lastModification));
                log.debug("code with sha:" + code);
                FtpImportedFile ftpImportedFile = ftpImportedFileService.findByCode(code);
                if (ftpImportedFile != null) {
                    log.debug("file already imported");
                    continue;
                }
                String localUrl = "file://" + distDirectory + relativePath;
                String standardPath = distDirectory + relativePath;
                log.debug("Standard local path is " + standardPath);
                LocalFile localFile = (LocalFile) fsManager.resolveFile(localUrl);
                log.debug("Resolved local file name: " + localFile.getName());
                if (!localFile.getParent().exists()) {
                    localFile.getParent().createFolder();
                }
                log.debug("Retrieving file");
                localFile.copyFrom(fileObject, new AllFileSelector());
                log.debug("get file ok");
                if (removeDistantFile) {
                    log.debug("deleting remote file...");
                    fileObject.delete();
                    log.debug("remote file deleted");
                }
                cpOk++;
                createImportedFileHistory(fileName, new Date(lastModification), size, remoteServer, remotePort, ftpInputDirectory);
            } catch (Exception ex) {
                log.error("Error getting file type for " + fileObject.getName(), ex);
                cpKo++;
                result.setReport(StringUtils.truncate(ex.getMessage(), 255, true));
            }
        }
        // Set src for cleanup in release()
        if (children != null && children.length > 0) {
            src = children[0];
        }
    } catch (Exception e) {
        log.error("", e);
    } finally {
        result.setDone(true);
        result.setNbItemsToProcess(cpAll);
        result.setNbItemsProcessedWithError(cpKo);
        result.setNbItemsProcessedWithWarning(cpWarn);
        result.setNbItemsCorrectlyProcessed(cpOk);
        release();
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileObject(org.apache.commons.vfs2.FileObject) Date(java.util.Date) FtpImportedFile(org.meveo.model.jobs.FtpImportedFile) FileSystemException(org.apache.commons.vfs2.FileSystemException) BusinessException(org.meveo.admin.exception.BusinessException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Interceptors(javax.interceptor.Interceptors) TransactionAttribute(javax.ejb.TransactionAttribute)

Aggregations

Date (java.util.Date)2 FtpImportedFile (org.meveo.model.jobs.FtpImportedFile)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 TransactionAttribute (javax.ejb.TransactionAttribute)1 Interceptors (javax.interceptor.Interceptors)1 AllFileSelector (org.apache.commons.vfs2.AllFileSelector)1 FileObject (org.apache.commons.vfs2.FileObject)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 LocalFile (org.apache.commons.vfs2.provider.local.LocalFile)1 BusinessException (org.meveo.admin.exception.BusinessException)1