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.movefiles.JobEntryMoveFiles.java

private boolean ProcessFileFolder(String sourcefilefoldername, String destinationfilefoldername,
        String wildcard, Job parentJob, Result result, String MoveToFolder, LogWriter log) {
    boolean entrystatus = false;
    FileObject sourcefilefolder = null;
    FileObject destinationfilefolder = null;
    FileObject movetofolderfolder = null;
    FileObject Currentfile = null;

    // Get real source, destination file and wildcard
    String realSourceFilefoldername = environmentSubstitute(sourcefilefoldername);
    String realDestinationFilefoldername = environmentSubstitute(destinationfilefoldername);
    String realWildcard = environmentSubstitute(wildcard);

    try {//from ww  w .  j  ava 2 s .c o  m

        // Here gc() is explicitly called if e.g. createfile is used in the
        // same
        // job for the same file. The problem is that after creating the
        // file the
        // file object is not properly garbaged collected and thus the file
        // cannot
        // be deleted anymore. This is a known problem in the JVM.

        System.gc();

        sourcefilefolder = KettleVFS.getFileObject(realSourceFilefoldername);
        destinationfilefolder = KettleVFS.getFileObject(realDestinationFilefoldername);
        if (!Const.isEmpty(MoveToFolder))
            movetofolderfolder = KettleVFS.getFileObject(MoveToFolder);

        if (sourcefilefolder.exists()) {

            // Check if destination folder/parent folder exists !
            // If user wanted and if destination folder does not exist
            // PDI will create it
            if (CreateDestinationFolder(destinationfilefolder)) {

                // Basic Tests
                if (sourcefilefolder.getType().equals(FileType.FOLDER) && destination_is_a_file) {
                    // Source is a folder, destination is a file
                    // WARNING !!! CAN NOT MOVE FOLDER TO FILE !!!

                    log.logError(Messages.getString("JobMoveFiles.Log.Forbidden"),
                            Messages.getString("JobMoveFiles.Log.CanNotMoveFolderToFile",
                                    realSourceFilefoldername, realDestinationFilefoldername));

                    // Update Errors
                    updateErrors();
                } else {
                    if (destinationfilefolder.getType().equals(FileType.FOLDER)
                            && sourcefilefolder.getType().equals(FileType.FILE)) {
                        // Source is a file, destination is a folder
                        // return destination short filename
                        String shortfilename = sourcefilefolder.getName().getBaseName();

                        try {
                            shortfilename = getDestinationFilename(sourcefilefolder.getName().getBaseName());
                        } catch (Exception e) {
                            log.logError(toString(),
                                    Messages.getString(Messages.getString("JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }
                        // Move the file to the destination folder

                        String destinationfilenamefull = destinationfilefolder.toString() + Const.FILE_SEPARATOR
                                + shortfilename;
                        FileObject destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, log, parentJob, result);

                    } else if (sourcefilefolder.getType().equals(FileType.FILE) && destination_is_a_file) {
                        // Source is a file, destination is a file

                        FileObject destinationfile = KettleVFS.getFileObject(realDestinationFilefoldername);

                        // return destination short filename
                        String shortfilename = destinationfile.getName().getBaseName();
                        try {
                            shortfilename = getDestinationFilename(destinationfile.getName().getBaseName());
                        } catch (Exception e) {
                            log.logError(toString(),
                                    Messages.getString(Messages.getString("JobMoveFiles.Error.GettingFilename",
                                            sourcefilefolder.getName().getBaseName(), e.toString())));
                            return entrystatus;
                        }

                        String destinationfilenamefull = destinationfilefolder.getParent().toString()
                                + Const.FILE_SEPARATOR + shortfilename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        entrystatus = MoveFile(shortfilename, sourcefilefolder, destinationfile,
                                movetofolderfolder, log, parentJob, result);

                    } else {
                        // Both source and destination are folders
                        if (log.isDetailed()) {
                            log.logDetailed(toString(), "  ");
                            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FetchFolder",
                                    sourcefilefolder.toString()));
                        }

                        FileObject[] fileObjects = sourcefilefolder.findFiles(new AllFileSelector() {
                            public boolean traverseDescendents(FileSelectInfo info) {
                                return true;
                            }

                            public boolean includeFile(FileSelectInfo info) {
                                FileObject fileObject = info.getFile();
                                try {
                                    if (fileObject == null)
                                        return false;
                                } catch (Exception ex) {
                                    // Upon error don't process the
                                    // file.
                                    return false;
                                }

                                finally {
                                    if (fileObject != null) {
                                        try {
                                            fileObject.close();
                                        } catch (IOException ex) {
                                        }
                                        ;
                                    }

                                }
                                return true;
                            }
                        });

                        if (fileObjects != null) {
                            for (int j = 0; j < fileObjects.length && !parentJob.isStopped(); j++) {
                                // Success condition broken?
                                if (successConditionBroken) {
                                    if (!successConditionBrokenExit) {
                                        log.logError(toString(), Messages.getString(
                                                "JobMoveFiles.Error.SuccessConditionbroken", "" + NrErrors));
                                        successConditionBrokenExit = true;
                                    }
                                    return false;
                                }
                                // Fetch files in list one after one ...
                                Currentfile = fileObjects[j];

                                if (!MoveOneFile(Currentfile, sourcefilefolder, realDestinationFilefoldername,
                                        realWildcard, log, parentJob, result, movetofolderfolder)) {
                                    // Update Errors
                                    updateErrors();
                                }

                            }
                        }
                    }

                }
                entrystatus = true;
            } // end if
            else {
                // Destination Folder or Parent folder is missing
                log.logError(toString(), Messages.getString("JobMoveFiles.Error.DestinationFolderNotFound",
                        realDestinationFilefoldername));
            }
        } // end if
        else {
            log.logError(toString(),
                    Messages.getString("JobMoveFiles.Error.SourceFileNotExists", realSourceFilefoldername));
        }
    } // end try
    catch (Exception e) {
        log.logError(toString(), Messages.getString("JobMoveFiles.Error.Exception.MoveProcess",
                realSourceFilefoldername.toString(), destinationfilefolder.toString(), e.getMessage()));
        // Update Errors
        updateErrors();
    } finally {
        if (sourcefilefolder != null) {
            try {
                sourcefilefolder.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (destinationfilefolder != null) {
            try {
                destinationfilefolder.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (Currentfile != null) {
            try {
                Currentfile.close();
            } catch (IOException ex) {
            }
            ;
        }
        if (movetofolderfolder != null) {
            try {
                movetofolderfolder.close();
            } catch (IOException ex) {
            }
            ;
        }
    }
    return entrystatus;
}

From source file:be.ibridge.kettle.trans.TransMeta.java

/**
     * This method sets various internal kettle variables that can be used by the transformation.
     *//*from  www  .j a v  a 2 s . com*/
    public void setInternalKettleVariables() {
        KettleVariables variables = KettleVariables.getInstance();

        if (filename != null) // we have a finename that's defined.
        {
            try {
                FileSystemManager fsManager = VFS.getManager();
                FileObject fileObject = fsManager.resolveFile(filename);
                FileName fileName = fileObject.getName();

                // The filename of the transformation
                variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, fileName.getBaseName());

                // The directory of the transformation
                FileName fileDir = fileName.getParent();
                variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, fileDir.getURI());
            } catch (IOException e) {
                variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, "");
                variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, "");
            }
        } else {
            variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, "");
            variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, "");
        }

        // The name of the transformation
        variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_NAME, Const.NVL(name, ""));

        // The name of the directory in the repository
        variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_REPOSITORY_DIRECTORY,
                directory != null ? directory.getPath() : "");
    }

From source file:com.panet.imeta.core.plugins.PluginLoader.java

private void fromXML(FileObject xml, FileObject parent)
        throws IOException, ClassNotFoundException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(KettleVFS.getInputStream(xml));
    Node plugin = XMLHandler.getSubNode(doc, Plugin.PLUGIN);
    String id = XMLHandler.getTagAttribute(plugin, Plugin.ID);
    String description = XMLHandler.getTagAttribute(plugin, Plugin.DESCRIPTION);
    String iconfile = XMLHandler.getTagAttribute(plugin, Plugin.ICONFILE);
    String tooltip = XMLHandler.getTagAttribute(plugin, Plugin.TOOLTIP);
    String classname = XMLHandler.getTagAttribute(plugin, Plugin.CLASSNAME);
    String category = XMLHandler.getTagAttribute(plugin, Plugin.CATEGORY);
    String errorHelpfile = XMLHandler.getTagAttribute(plugin, Plugin.ERRORHELPFILE);

    // Localized categories
    ///*from  ww  w  . j  av a 2 s  .  c o m*/
    Node locCatsNode = XMLHandler.getSubNode(plugin, Plugin.LOCALIZED_CATEGORY);
    int nrLocCats = XMLHandler.countNodes(locCatsNode, Plugin.CATEGORY);
    Map<String, String> localizedCategories = new Hashtable<String, String>();
    for (int j = 0; j < nrLocCats; j++) {
        Node locCatNode = XMLHandler.getSubNodeByNr(locCatsNode, Plugin.CATEGORY, j);
        String locale = XMLHandler.getTagAttribute(locCatNode, Plugin.LOCALE);
        String locCat = XMLHandler.getNodeValue(locCatNode);

        if (!Const.isEmpty(locale) && !Const.isEmpty(locCat)) {
            localizedCategories.put(locale.toLowerCase(), locCat);
        }
    }

    // Localized descriptions
    //
    Node locDescsNode = XMLHandler.getSubNode(plugin, Plugin.LOCALIZED_DESCRIPTION);
    int nrLocDescs = XMLHandler.countNodes(locDescsNode, Plugin.DESCRIPTION);
    Map<String, String> localizedDescriptions = new Hashtable<String, String>();
    for (int j = 0; j < nrLocDescs; j++) {
        Node locDescNode = XMLHandler.getSubNodeByNr(locDescsNode, Plugin.DESCRIPTION, j);
        String locale = XMLHandler.getTagAttribute(locDescNode, Plugin.LOCALE);
        String locDesc = XMLHandler.getNodeValue(locDescNode);

        if (!Const.isEmpty(locale) && !Const.isEmpty(locDesc)) {
            localizedDescriptions.put(locale.toLowerCase(), locDesc);
        }
    }

    // Localized tooltips
    //
    Node locTipsNode = XMLHandler.getSubNode(plugin, Plugin.LOCALIZED_TOOLTIP);
    int nrLocTips = XMLHandler.countNodes(locTipsNode, Plugin.TOOLTIP);
    Map<String, String> localizedTooltips = new Hashtable<String, String>();
    for (int j = 0; j < nrLocTips; j++) {
        Node locTipNode = XMLHandler.getSubNodeByNr(locTipsNode, Plugin.TOOLTIP, j);
        String locale = XMLHandler.getTagAttribute(locTipNode, Plugin.LOCALE);
        String locTip = XMLHandler.getNodeValue(locTipNode);

        if (!Const.isEmpty(locale) && !Const.isEmpty(locTip)) {
            localizedTooltips.put(locale.toLowerCase(), locTip);
        }
    }

    Node libsnode = XMLHandler.getSubNode(plugin, Plugin.LIBRARIES);
    int nrlibs = XMLHandler.countNodes(libsnode, Plugin.LIBRARY);
    String jarfiles[] = new String[nrlibs];
    for (int j = 0; j < nrlibs; j++) {
        Node libnode = XMLHandler.getSubNodeByNr(libsnode, Plugin.LIBRARY, j);
        String jarfile = XMLHandler.getTagAttribute(libnode, Plugin.NAME);
        jarfiles[j] = parent.resolveFile(jarfile).getURL().getFile();
        // System.out.println("jar files=" + jarfiles[j]);
    }

    // convert to URL
    List<URL> classpath = new ArrayList<URL>();
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(new FileSystemResourceLoader());
    for (int i = 0; i < jarfiles.length; i++) {
        try {
            Resource[] paths = resolver.getResources(jarfiles[i]);
            for (Resource path : paths) {
                classpath.add(path.getURL());
            }
        } catch (IOException e) {
            e.printStackTrace();
            continue;
        }
    }

    URL urls[] = classpath.toArray(new URL[classpath.size()]);

    URLClassLoader cl = new PDIClassLoader(urls, Thread.currentThread().getContextClassLoader());

    String iconFilename = parent.resolveFile(iconfile).getURL().getFile();

    Class<?> pluginClass = cl.loadClass(classname);

    // here we'll have to use some reflection in order to decide
    // which object we should instantiate!
    if (JobEntryInterface.class.isAssignableFrom(pluginClass)) {
        Set<JobPlugin> jps = (Set<JobPlugin>) this.plugins.get(Job.class);

        JobPlugin plg = new JobPlugin(Plugin.TYPE_PLUGIN, id, description, tooltip, parent.getName().getURI(),
                jarfiles, iconFilename, classname, category);
        plg.setClassLoader(cl);

        // Add localized information too...
        plg.setLocalizedCategories(localizedCategories);
        plg.setLocalizedDescriptions(localizedDescriptions);
        plg.setLocalizedTooltips(localizedTooltips);

        jps.add(plg);
    } else {
        String errorHelpFileFull = errorHelpfile;
        String path = parent.getName().getURI();
        if (!Const.isEmpty(errorHelpfile))
            errorHelpFileFull = (path == null) ? errorHelpfile : path + Const.FILE_SEPARATOR + errorHelpfile;

        StepPlugin sp = new StepPlugin(Plugin.TYPE_PLUGIN, new String[] { id }, description, tooltip, path,
                jarfiles, iconFilename, classname, category, errorHelpFileFull);

        // Add localized information too...
        sp.setLocalizedCategories(localizedCategories);
        sp.setLocalizedDescriptions(localizedDescriptions);
        sp.setLocalizedTooltips(localizedTooltips);

        Set<StepPlugin> sps = (Set<StepPlugin>) this.plugins.get(Step.class);
        sps.add(sp);
    }

}

From source file:com.panet.imeta.trans.TransMeta.java

public void setInternalKettleVariables(VariableSpace var) {
    if (!Const.isEmpty(filename)) // we have a finename that's defined.
    {/*from w ww .j  a  va 2  s  . c o  m*/
        try {
            FileObject fileObject = KettleVFS.getFileObject(filename);
            FileName fileName = fileObject.getName();

            // The filename of the transformation
            var.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, fileName.getBaseName());

            // The directory of the transformation
            FileName fileDir = fileName.getParent();
            var.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, fileDir.getURI());
        } catch (IOException e) {
            var.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, "");
            var.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, "");
        }
    } else {
        var.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, "");
        var.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, "");
    }

    // The name of the transformation
    //
    var.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_NAME, Const.NVL(name, ""));

    // The name of the directory in the repository
    //
    var.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_REPOSITORY_DIRECTORY,
            directory != null ? directory.getPath() : "");

    // Here we don't remove the job specific parameters, as they may come in
    // handy.
    //
    if (var.getVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_DIRECTORY) == null) {
        var.setVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_DIRECTORY, "Parent Job File Directory"); //$NON-NLS-1$
    }
    if (var.getVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_NAME) == null) {
        var.setVariable(Const.INTERNAL_VARIABLE_JOB_FILENAME_NAME, "Parent Job Filename"); //$NON-NLS-1$
    }
    if (var.getVariable(Const.INTERNAL_VARIABLE_JOB_NAME) == null) {
        var.setVariable(Const.INTERNAL_VARIABLE_JOB_NAME, "Parent Job Name"); //$NON-NLS-1$
    }
    if (var.getVariable(Const.INTERNAL_VARIABLE_JOB_REPOSITORY_DIRECTORY) == null) {
        var.setVariable(Const.INTERNAL_VARIABLE_JOB_REPOSITORY_DIRECTORY, "Parent Job Repository Directory"); //$NON-NLS-1$        
    }
}

From source file:com.panet.imeta.trans.TransMeta.java

public String exportResources(VariableSpace space, Map<String, ResourceDefinition> definitions,
        ResourceNamingInterface resourceNamingInterface) throws KettleException {
    try {//from w  w  w  .  ja v a2s.  c o m
        FileObject fileObject = KettleVFS.getFileObject(getFilename());
        String exportFileName = resourceNamingInterface.nameResource(fileObject.getName().getBaseName(),
                fileObject.getParent().getName().getPath(), "ktr"); //$NON-NLS-1$
        ResourceDefinition definition = definitions.get(exportFileName);
        if (definition == null) {
            // If we do this once, it will be plenty :-)
            //
            TransMeta transMeta = (TransMeta) this.realClone(false);
            // transMeta.copyVariablesFrom(space);

            // Add used resources, modify transMeta accordingly
            // Go through the list of steps, etc.
            // These critters change the steps in the cloned TransMeta
            // At the end we make a new XML version of it in "exported"
            // format...

            // loop over steps, databases will be exported to XML anyway.
            //
            for (StepMeta stepMeta : transMeta.getSteps()) {
                stepMeta.exportResources(space, definitions, resourceNamingInterface);
            }

            // Change the filename, calling this sets internal variables
            // inside of the transformation.
            //
            transMeta.setFilename(exportFileName);

            // At the end, add ourselves to the map...
            //
            String transMetaContent = transMeta.getXML();

            definition = new ResourceDefinition(exportFileName, transMetaContent);
            definitions.put(fileObject.getName().getPath(), definition);
        }
        return exportFileName;
    } catch (FileSystemException e) {
        throw new KettleException(
                Messages.getString("TransMeta.Exception.ErrorOpeningOrValidatingTheXMLFile", getFilename()), e); //$NON-NLS-1$
    } catch (IOException e) {
        throw new KettleException(
                Messages.getString("TransMeta.Exception.ErrorOpeningOrValidatingTheXMLFile", getFilename()), e); //$NON-NLS-1$
    }
}

From source file:org.ambud.marauder.source.ids.IDSFileSource.java

@Override
public synchronized void start() {
    super.start();
    if (isSequential) {
        this.threadPool = Executors.newFixedThreadPool(2);
    } else {/* ww  w  . j  a v a2  s .  c  o  m*/
        this.threadPool = Executors.newCachedThreadPool();
    }
    monitor.addFile(watchObject);
    monitor.setDelay(1000);
    monitor.setRecursive(false);
    monitor.start();
    logger.info("Listener activated");
    FileObject[] tempList = null;
    try {
        tempList = watchObject.getChildren();
    } catch (FileSystemException e) {
        Throwables.propagate(e);
    }
    Arrays.sort(tempList, new Comparator<FileObject>() {

        @Override
        public int compare(FileObject o1, FileObject o2) {
            return o1.getName().getBaseName().compareTo(o2.getName().getBaseName());
        }

    });
    if (tempList.length != 0) {
        logger.info("Now Spooling:" + Arrays.toString(tempList));
        processOldFiles(tempList);
        processFile(tempList[tempList.length - 1], true);
    } else {
        logger.info(
                "No suitable files to process. A listener will be activated to watch for new Snort log files in the directory");
    }

}

From source file:org.ambud.marauder.source.ids.IDSFileSource.java

protected MarauderIDSLogFileReader processFile(FileObject file, boolean isContinuous) {
    MarauderIDSLogFileReader temp = builderFileReader(file, isContinuous);
    HandlerRunTask runTempTask = new HandlerRunTask(temp);
    HandlerCloseTask closeTempTask = new HandlerCloseTask(temp);

    logger.info("Now processing:" + file.getName().getPath());
    getThreadPool().execute(runTempTask);
    getThreadPool().execute(closeTempTask);
    return temp;//from   w  w  w.  ja va2  s  .  c o  m
}

From source file:org.ambud.marauder.source.ids.pcap.PcapSource.java

@Override
protected MarauderIDSLogFileReader builderFileReader(FileObject file, boolean isContinuous) {
    MarauderIDSLogFileReader temp = null;
    try {//  ww  w .  ja v a2s  .  com
        temp = new LibpcapFileReader(new File(file.getName().getPath()), getHostAddress(), getOutputQueue());
    } catch (IOException e) {
        Throwables.propagate(e);
    }
    return temp;
}

From source file:org.ambud.marauder.source.snort.SnortSource.java

@Override
protected MarauderIDSLogFileReader builderFileReader(FileObject file, boolean isContinuous) {
    MarauderIDSLogFileReader temp = null;
    try {/*from ww w.j a  v  a 2 s  . c om*/
        temp = new Unified2IDSFileReader(new File(file.getName().getPath()), isContinuous, getHostAddress(),
                getOutputQueue());
    } catch (IOException e) {
        Throwables.propagate(e);
    }
    return temp;
}

From source file:org.ambud.marauder.source.snort.SnortSourceBack.java

protected MarauderIDSLogFileReader processFile(FileObject file, boolean isContinuous) {
    MarauderIDSLogFileReader temp = null;
    try {/*w ww .j a  v a  2s  .  co m*/
        temp = new Unified2IDSFileReader(new File(file.getName().getPath()), isContinuous, getHostAddress(),
                outputQueue);
    } catch (IOException e) {
        Throwables.propagate(e);
    }
    HandlerRunTask runTempTask = new HandlerRunTask(temp);
    HandlerCloseTask closeTempTask = new HandlerCloseTask(temp);

    logger.info("Now processing:" + file.getName().getPath());
    threadPool.execute(runTempTask);
    threadPool.execute(closeTempTask);

    return temp;
}