Example usage for org.apache.commons.vfs FileObject getName

List of usage examples for org.apache.commons.vfs FileObject getName

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject getName.

Prototype

public FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:com.panet.imeta.job.entries.sftpput.JobEntrySFTPPUT.java

public Result execute(Result previousResult, int nr, Repository rep, Job parentJob) throws KettleException {
    LogWriter log = LogWriter.getInstance();

    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    result.setResult(false);/*w  w w .j  a  v  a2s.  co  m*/

    if (log.isDetailed())
        log.logDetailed(toString(), Messages.getString("JobSFTPPUT.Log.StartJobEntry"));
    ArrayList<FileObject> myFileList = new ArrayList<FileObject>();

    if (copyprevious) {
        if (rows.size() == 0) {
            if (log.isDetailed())
                log.logDetailed(toString(), Messages.getString("JobSFTPPUT.ArgsFromPreviousNothing"));
            result.setResult(true);
            return result;
        }

        try {
            RowMetaAndData resultRow = null;
            // Copy the input row to the (command line) arguments
            for (int iteration = 0; iteration < rows.size(); iteration++) {
                resultRow = rows.get(iteration);

                // Get file names
                String file_previous = resultRow.getString(0, null);
                if (!Const.isEmpty(file_previous)) {
                    FileObject file = KettleVFS.getFileObject(file_previous);
                    if (!file.exists())
                        log.logError(toString(),
                                Messages.getString("JobSFTPPUT.Log.FilefromPreviousNotFound", file_previous));
                    else {
                        myFileList.add(file);
                        if (log.isDebug())
                            log.logDebug(toString(),
                                    Messages.getString("JobSFTPPUT.Log.FilenameFromResult", file_previous));
                    }
                }
            }
        } catch (Exception e) {
            log.logError(toString(), Messages.getString("JobSFTPPUT.Error.ArgFromPrevious"));
            result.setNrErrors(1);
            return result;
        }

    }
    SFTPClient sftpclient = null;

    // String substitution..
    String realServerName = environmentSubstitute(serverName);
    String realServerPort = environmentSubstitute(serverPort);
    String realUsername = environmentSubstitute(userName);
    String realPassword = environmentSubstitute(password);
    String realSftpDirString = environmentSubstitute(sftpDirectory);
    String realWildcard = environmentSubstitute(wildcard);
    String realLocalDirectory = environmentSubstitute(localDirectory);

    try {
        // Create sftp client to host ...
        sftpclient = new SFTPClient(InetAddress.getByName(realServerName), Const.toInt(realServerPort, 22),
                realUsername);
        if (log.isDetailed())
            log.logDetailed(toString(), Messages.getString("JobSFTPPUT.Log.OpenedConnection", realServerName,
                    "" + realServerPort, realUsername));

        // login to ftp host ...
        sftpclient.login(realPassword);
        // Don't show the password in the logs, it's not good for security
        // audits
        // log.logDetailed(toString(), "logged in using password
        // "+realPassword); // Logging this seems a bad idea! Oh well.

        // move to spool dir ...
        if (!Const.isEmpty(realSftpDirString)) {
            sftpclient.chdir(realSftpDirString);
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobSFTPPUT.Log.ChangedDirectory", realSftpDirString));
        } // end if

        if (!copyprevious) {
            // Get all the files in the local directory...
            myFileList = new ArrayList<FileObject>();

            FileObject localFiles = KettleVFS.getFileObject(realLocalDirectory);
            FileObject[] children = localFiles.getChildren();
            if (children != null) {
                for (int i = 0; i < children.length; i++) {
                    // Get filename of file or directory
                    if (children[i].getType().equals(FileType.FILE)) {
                        // myFileList.add(children[i].getAbsolutePath());
                        myFileList.add(children[i]);
                    }
                } // end for
            }
        }

        if (myFileList == null || myFileList.size() == 0) {
            log.logError(toString(), Messages.getString("JobSFTPPUT.Error.NoFileToSend"));
            result.setNrErrors(1);
            return result;
        }

        if (log.isDetailed())
            log.logDetailed(toString(),
                    Messages.getString("JobSFTPPUT.Log.RowsFromPreviousResult", "" + myFileList.size()));

        Pattern pattern = null;
        if (!copyprevious) {
            if (!Const.isEmpty(realWildcard)) {
                pattern = Pattern.compile(realWildcard);
            }
        }

        // Get the files in the list and execute sftp.put() for each file
        for (int i = 0; i < myFileList.size() && !parentJob.isStopped(); i++) {
            FileObject myFile = myFileList.get(i);
            String localFilename = myFile.toString();
            String destinationFilename = myFile.getName().getBaseName();
            boolean getIt = true;

            // First see if the file matches the regular expression!
            if (pattern != null) {
                Matcher matcher = pattern.matcher(destinationFilename);
                getIt = matcher.matches();
            }

            if (getIt) {
                if (log.isDebug())
                    log.logDebug(toString(),
                            Messages.getString("JobSFTPPUT.Log.PuttingFile", localFilename, realSftpDirString));

                sftpclient.put(myFile, destinationFilename);

                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobSFTPPUT.Log.TransferedFile", localFilename));

                // Delete the file if this is needed!
                if (remove) {
                    myFile.delete();
                    if (log.isDetailed())
                        log.logDetailed(toString(),
                                Messages.getString("JobSFTPPUT.Log.DeletedFile", localFilename));
                } else {
                    if (addFilenameResut) {
                        // Add to the result files...
                        ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, myFile,
                                parentJob.getJobname(), toString());
                        result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages
                                    .getString("JobSFTPPUT.Log.FilenameAddedToResultFilenames", localFilename));
                    }
                }
            }
        } // end for

        result.setResult(true);
        // JKU: no idea if this is needed...!
        // result.setNrFilesRetrieved(filesRetrieved);
    } // end try
    catch (Exception e) {
        result.setNrErrors(1);
        log.logError(toString(), Messages.getString("JobSFTPPUT.Exception", e.getMessage()));
        log.logError(toString(), Const.getStackTracker(e));
    } finally {
        // close connection, if possible
        try {
            if (sftpclient != null)
                sftpclient.disconnect();
        } catch (Exception e) {
            // just ignore this, makes no big difference
        } // end catch
    } // end finallly

    return result;
}

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>//ww  w.  j a va 2  s. c  o m
 *  ?   ?? .
 * </p>
 * @param dir
 *        <code>FileObject</code>
 * @param recursive
 *        <code>boolean</code>
 * @param prefix
 *        <code>String</code>
 * @return  ?
 * @throws FileSystemException
 */
private StringBuffer listChildren(final FileObject dir, final boolean recursive, final String prefix)
        throws FileSystemException {
    StringBuffer line = new StringBuffer();
    final FileObject[] children = dir.getChildren();

    for (int i = 0; i < children.length; i++) {
        final FileObject child = children[i];
        // log.info(prefix +
        // child.getName().getBaseName());
        line.append(prefix).append(child.getName().getBaseName());

        if (child.getType() == FileType.FOLDER) {
            // log.info("/");
            line.append("/");
            if (recursive) {
                line.append(listChildren(child, recursive, prefix + "    "));
            }
        } else {
            line.append("");
        }
    }

    return line;
}

From source file:com.newatlanta.appengine.nio.file.GaePath.java

GaePath(FileSystem fileSystem, FileObject fileObject) {
    this.fileSystem = fileSystem;
    this.fileObject = fileObject;
    path = fileObject.getName().getPath();
}

From source file:com.panet.imeta.trans.steps.sqlfileoutput.SQLFileOutput.java

public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (SQLFileOutputMeta) smi;/*w  w w .j av a2s  .  c  o m*/
    data = (SQLFileOutputData) sdi;

    if (super.init(smi, sdi)) {
        try {
            if (meta.getDatabaseMeta() == null) {
                throw new KettleStepException("The connection is not defined (empty)");
            }

            data.db = new Database(meta.getDatabaseMeta());
            data.db.shareVariablesWith(this);

            logBasic("Connected to database [" + meta.getDatabaseMeta() + "]");

            if (meta.isCreateParentFolder()) {
                // Check for parent folder
                FileObject parentfolder = null;
                try {
                    // Get parent folder
                    String filename = environmentSubstitute(meta.getFileName());
                    parentfolder = KettleVFS.getFileObject(filename).getParent();
                    if (!parentfolder.exists()) {
                        log.logBasic("Folder parent",
                                "Folder parent " + parentfolder.getName() + " does not exist !");
                        parentfolder.createFolder();
                        log.logBasic("Folder parent", "Folder parent was created.");
                    }
                } catch (Exception e) {
                    logError("Couldn't created parent folder " + parentfolder.getName());
                    setErrors(1L);
                    stopAll();
                } finally {
                    if (parentfolder != null) {
                        try {
                            parentfolder.close();
                        } catch (Exception ex) {
                        }
                        ;
                    }
                }
            }

            if (!meta.isDoNotOpenNewFileInit()) {
                if (!openNewFile()) {
                    logError("Couldn't open file [" + buildFilename() + "]");
                    setErrors(1L);
                    stopAll();
                }
            }

            tableName = environmentSubstitute(meta.getTablename());
            schemaName = environmentSubstitute(meta.getSchemaName());

            if (Const.isEmpty(tableName)) {
                throw new KettleStepException("The tablename is not defined (empty)");
            }

            schemaTable = data.db.getDatabaseMeta().getQuotedSchemaTableCombination(schemaName, tableName);

        } catch (Exception e) {
            logError("An error occurred intialising this step: " + e.getMessage());
            stopAll();
            setErrors(1);
        }

        return true;
    }
    return false;
}

From source file:com.newatlanta.appengine.vfs.provider.GaeFileObject.java

/**
 * Override the superclass implementation to make sure GaeVFS "shadows"
 * exist for local directories.//from www  .  j av  a2 s  .c  o m
 */
@Override
public FileObject getParent() throws FileSystemException {
    FileObject parent = super.getParent();
    if ((parent != null) && !parent.exists()) {
        // check for existing local directory
        FileSystemManager manager = getFileSystem().getFileSystemManager();
        FileObject localDir = manager.resolveFile("file://"
                + GaeFileNameParser.getRootPath(manager.getBaseFile().getName()) + parent.getName().getPath());

        if (localDir.exists() && localDir.getType().hasChildren()) {
            parent.createFolder(); // make sure GaeVFS "shadow" folder exists
        }
    }
    return parent;
}

From source file:mondrian.spi.impl.ApacheVfsVirtualFileHandler.java

public InputStream readVirtualFile(String url) throws FileSystemException {
    // Treat catalogUrl as an Apache VFS (Virtual File System) URL.
    // VFS handles all of the usual protocols (http:, file:)
    // and then some.
    FileSystemManager fsManager = VFS.getManager();
    if (fsManager == null) {
        throw Util.newError("Cannot get virtual file system manager");
    }//ww w. j  ava  2s.  c  om

    // Workaround VFS bug.
    if (url.startsWith("file://localhost")) {
        url = url.substring("file://localhost".length());
    }
    if (url.startsWith("file:")) {
        url = url.substring("file:".length());
    }

    //work around for VFS bug not closing http sockets
    // (Mondrian-585)
    if (url.startsWith("http")) {
        try {
            return new URL(url).openStream();
        } catch (IOException e) {
            throw Util.newError("Could not read URL: " + url);
        }
    }

    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        // Because of VFS caching, make sure we refresh to get the latest
        // file content. This refresh may possibly solve the following
        // workaround for defect MONDRIAN-508, but cannot be tested, so we
        // will leave the work around for now.
        file.refresh();

        // Workaround to defect MONDRIAN-508. For HttpFileObjects, verifies
        // the URL of the file retrieved matches the URL passed in.  A VFS
        // cache bug can cause it to treat URLs with different parameters
        // as the same file (e.g. http://blah.com?param=A,
        // http://blah.com?param=B)
        if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) {
            fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName());

            file = fsManager.resolveFile(userDir, url);
        }

        if (!file.isReadable()) {
            throw Util.newError("Virtual file is not readable: " + url);
        }

        fileContent = file.getContent();
    } finally {
        file.close();
    }

    if (fileContent == null) {
        throw Util.newError("Cannot get virtual file content: " + url);
    }

    return fileContent.getInputStream();
}

From source file:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

@Test
public void testCaching1() throws Exception {
    String testFolder = "d:/workspace/java/e-gov/eGovFramework/RTE/DEV/trunk/Foundation/egovframework.rte.fdl.filehandling/test";

    FileSystemManager manager = VFS.getManager();

    EgovFileUtil.writeFile(testFolder + "/file1.txt", text, "UTF-8");

    /*/*from www  .  ja  va 2  s  . c  o m*/
     * ? Manager ?
     * CacheStrategy.MANUAL      : Deal with cached data manually. Call FileObject.refresh() to refresh the object data.
     * CacheStrategy.ON_RESOLVE : Refresh the data every time you request a file from FileSystemManager.resolveFile
     * CacheStrategy.ON_CALL   : Refresh the data every time you call a method on the fileObject. You'll use this only if you really need the latest info as this setting is a major performance loss. 
     */
    DefaultFileSystemManager fs = new DefaultFileSystemManager();
    fs.setFilesCache(manager.getFilesCache());

    // zip, jar, tgz, tar, tbz2, file
    if (!fs.hasProvider("file")) {
        fs.addProvider("file", new DefaultLocalFileProvider());
    }
    //       StandardFileSystemManager fs = new StandardFileSystemManager();

    fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
    fs.init();

    // ? ? ?
    //FileObject foBase2 = fs.resolveFile(testFolder);
    log.debug("####1");
    FileObject cachedFile = fs.toFileObject(new File(testFolder + "/file1.txt"));
    log.debug("####2");

    FilesCache filesCache = fs.getFilesCache();
    log.debug("####3");
    filesCache.putFile(cachedFile);
    FileObject obj = filesCache.getFile(cachedFile.getFileSystem(), cachedFile.getName());

    //FileObject baseFile = fs.getBaseFile();
    //        log.debug("### cachedFile.getContent().getSize() is " + cachedFile.getContent().getSize());

    //        long fileSize = cachedFile.getContent().getSize();
    //        log.debug("#########size is " + fileSize);
    //FileObject cachedFile1 = cachedFile.resolveFile("file2.txt");

    //       FileObject scratchFolder = manager.resolveFile(testFolder);
    //       scratchFolder.delete(Selectors.EXCLUDE_SELF);

    EgovFileUtil.delete(new File(testFolder + "/file1.txt"));

    //       obj.createFile();

    //        log.debug("#########obj is " + obj.toString());
    //        log.debug("#########size is " + obj.getContent().getSize());
    log.debug("#########file is " + obj.exists());

    fs.close();
}

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>/*  w  w w .j  ava2 s  .  co  m*/
 *  ? ??? .
 * </p>
 * @param cmd
 *        <code>String[]</code>
 * @return ? ? ?
 * @throws FileSystemException
 */
public List ls(final String[] cmd) throws FileSystemException {
    List list = new ArrayList();

    int pos = 1;
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
        recursive = true;
        pos++;
    } else {
        recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
        file = manager.resolveFile(basefile, cmd[pos]);
    } else {
        file = basefile;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        log.info("Contents of " + file.getName());
        log.info(listChildren(file, recursive, ""));
        // list.add(file.getName());
    } else {
        // Stat the file
        log.info(file.getName());
        final FileContent content = file.getContent();
        log.info("Size: " + content.getSize() + " bytes.");
        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
        log.info("Last modified: " + lastMod);
    }

    return list;
}

From source file:com.thinkberg.moxo.dav.PropFindHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    SAXReader saxReader = new SAXReader();
    try {// w  ww  .j  av a  2 s.  c  o m
        Document propDoc = saxReader.read(request.getInputStream());
        // log(propDoc);
        Element propFindEl = propDoc.getRootElement();
        Element propEl = (Element) propFindEl.elementIterator().next();
        String propElName = propEl.getName();

        List<String> requestedProperties = new ArrayList<String>();
        boolean ignoreValues = false;
        if (TAG_PROP.equals(propElName)) {
            for (Object id : propEl.elements()) {
                requestedProperties.add(((Element) id).getName());
            }
        } else if (TAG_ALLPROP.equals(propElName)) {
            requestedProperties = DavResource.ALL_PROPERTIES;
        } else if (TAG_PROPNAMES.equals(propElName)) {
            requestedProperties = DavResource.ALL_PROPERTIES;
            ignoreValues = true;
        }

        FileObject object = getResourceManager().getFileObject(request.getPathInfo());
        if (object.exists()) {
            // respond as XML encoded multi status
            response.setContentType("text/xml");
            response.setCharacterEncoding("UTF-8");
            response.setStatus(SC_MULTI_STATUS);

            Document multiStatusResponse = getMultiStatusRespons(object, requestedProperties,
                    getBaseUrl(request), getDepth(request), ignoreValues);
            //log(multiStatusResponse);

            // write the actual response
            XMLWriter writer = new XMLWriter(response.getWriter(), OutputFormat.createCompactFormat());
            writer.write(multiStatusResponse);
            writer.flush();
            writer.close();

        } else {
            log("!! " + object.getName().getPath() + " NOT FOUND");
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } catch (DocumentException e) {
        log("!! inavlid request: " + e.getMessage());
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    }
}

From source file:com.panet.imeta.job.entries.copymoveresultfilenames.JobEntryCopyMoveResultFilenames.java

private boolean ProcessFile(FileObject sourcefile, String destinationFolder, LogWriter log, Result result,
        Job parentJob) {//from   w  w  w.  ja va2 s  . c  om
    boolean retval = false;
    boolean filexists = false;
    try {
        // return destination short filename
        String shortfilename = getDestinationFilename(sourcefile.getName().getBaseName());
        // build full destination filename
        String destinationFilename = destinationFolder + Const.FILE_SEPARATOR + shortfilename;
        FileObject destinationfile = KettleVFS.getFileObject(destinationFilename);
        filexists = destinationfile.exists();
        if (filexists) {
            if (log.isDetailed())
                log.logDetailed(toString(), Messages.getString("JobEntryCopyMoveResultFilenames.Log.FileExists",
                        destinationFilename));
        }
        if ((!filexists) || (filexists && isOverwriteFile())) {
            if (getAction().equals("copy")) {
                // Copy file
                FileUtil.copyContent(sourcefile, destinationfile);
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobEntryCopyMoveResultFilenames.log.CopiedFile",
                                    sourcefile.toString(), destinationFolder));
            } else {
                // Move file
                sourcefile.moveTo(destinationfile);
                if (log.isDetailed())
                    log.logDetailed(toString(),
                            Messages.getString("JobEntryCopyMoveResultFilenames.log.MovedFile",
                                    sourcefile.toString(), destinationFolder));
            }

            if (isRemovedSourceFilename()) {
                // Remove source file from result files list
                result.getResultFiles().remove(sourcefile.toString());
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString(
                            "JobEntryCopyMoveResultFilenames.RemovedFileFromResult", sourcefile.toString()));
            }
            if (isAddDestinationFilename()) {
                // Add destination filename to Resultfilenames ...
                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL,
                        KettleVFS.getFileObject(destinationfile.toString()), parentJob.getJobname(),
                        toString());
                result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString(
                            "JobEntryCopyMoveResultFilenames.AddedFileToResult", destinationfile.toString()));
            }
        }
        retval = true;
    } catch (Exception e) {
        log.logError(toString(),
                Messages.getString("JobEntryCopyMoveResultFilenames.Log.ErrorProcessing", e.toString()));
    }

    return retval;
}