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

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

Introduction

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

Prototype

FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:pl.otros.logview.api.io.UtilsTest.java

@Test
public void testGzipedFile() throws IOException {
    FileObject resolveFile = resolveFileObject("/jul_log.txt.gz");
    boolean checkIfIsGzipped = Utils.checkIfIsGzipped(resolveFile);
    AssertJUnit.assertTrue(resolveFile.getName() + " should be compressed", checkIfIsGzipped);
}

From source file:pl.otros.logview.api.io.UtilsTest.java

@Test
public void testNotGzipedFile() throws IOException {
    FileObject resolveFile = resolveFileObject("/jul_log.txt");
    boolean checkIfIsGzipped = Utils.checkIfIsGzipped(resolveFile);
    AssertJUnit.assertFalse(resolveFile.getName() + " should be not compressed", checkIfIsGzipped);
}

From source file:pl.otros.logview.api.io.UtilsTest.java

@Test
public void testSmallGzipedFile() throws IOException {
    FileObject resolveFile = resolveFileObject("/smallFile.txt.gz");
    boolean checkIfIsGzipped = Utils.checkIfIsGzipped(resolveFile);
    AssertJUnit.assertTrue(resolveFile.getName() + " should be compressed", checkIfIsGzipped);
}

From source file:pl.otros.logview.api.io.UtilsTest.java

@Test
private void testGetObjectShortName(String scheme, String url, String baseName, String output) {
    // given/*w ww.  j av a  2 s  .  c  om*/
    FileObject fileObjectMock = mock(FileObject.class);
    FileName fileNameMock = mock(FileName.class);

    when(fileObjectMock.getName()).thenReturn(fileNameMock);
    when(fileNameMock.getScheme()).thenReturn(scheme);
    when(fileNameMock.getURI()).thenReturn(url);
    when(fileNameMock.getBaseName()).thenReturn(baseName);

    // when
    String fileObjectShortName = Utils.getFileObjectShortName(fileObjectMock);

    // then
    AssertJUnit.assertEquals(output, fileObjectShortName);
}

From source file:pl.otros.logview.batch.BatchProcessor.java

public void process() throws Exception {
    StreamProcessingLogDataCollector logDataCollector = new StreamProcessingLogDataCollector(
            logDataParsedListener, batchProcessingContext);
    FileSystemManager manager = null;/*from   w  w  w. j  a  va  2 s  .c  o m*/
    try {
        manager = VFS.getManager();
    } catch (FileSystemException e1) {
        return;
    }
    int i = 0;
    ArrayList<FileObject> fileObjects = new ArrayList<FileObject>();
    for (String file : files) {
        i++;
        FileObject resolveFile = null;
        try {
            batchProcessingContext.printIfVerbose("Resolving file %s [%d of %d]", file, i, files.size());
            try {
                resolveFile = manager.resolveFile(file);
            } catch (Exception e) {
                file = new File(file).getAbsolutePath();
                resolveFile = manager.resolveFile(file);
            }
            if (resolveFile != null) {
                fileObjects.add(resolveFile);
            }
        } catch (Exception e) {
            System.err.printf("Error resolving %s: %s", file, e.getMessage());
        }
    }
    batchProcessingContext.setAllFiles(fileObjects);

    if (logDataParsedListener instanceof BatchProcessingListener) {
        ((BatchProcessingListener) logDataParsedListener).processingStarted(batchProcessingContext);
    }

    AutoDetectingImporterProvider importerProvider = new AutoDetectingImporterProvider(
            AllPluginables.getInstance().getLogImportersContainer().getElements());
    i = 0;
    for (FileObject resolveFile : fileObjects) {
        i++;

        String fileName = resolveFile.getName().getBaseName();
        try {
            batchProcessingContext.printIfVerbose("Opening file %s [%d of %d]", fileName, i,
                    fileObjects.size());
            batchProcessingContext.setCurrentFile(resolveFile);
            if (logDataParsedListener instanceof SingleFileBatchProcessingListener) {
                ((SingleFileBatchProcessingListener) logDataParsedListener)
                        .processingFileStarted(batchProcessingContext);
            }
            LoadingInfo openFileObject = Utils.openFileObject(resolveFile);
            LogImporter logImporter = importerProvider.getLogImporter(openFileObject);
            if (logImporter == null) {
                System.err.println("Can't find suitable log importer for " + fileName);
                continue;
            }
            batchProcessingContext.printIfVerbose("Will user log importer: %s [%s]", logImporter.getName(),
                    logImporter.getPluginableId());

            // TODO for HTTP, Attempted read on closed stream. issue related to checking if file is gziped
            // Utils.closeQuietly(resolveFile);
            // String fileUrl = resolveFile.getURL().toString();
            // resolveFile = manager.resolveFile(fileUrl);
            // openFileObject = Utils.openFileObject(resolveFile);

            batchProcessingContext.setCurrentFile(resolveFile);
            ParsingContext context = new ParsingContext();
            context.setLogSource(resolveFile.getName().getFriendlyURI());
            logImporter.initParsingContext(context);
            logImporter.importLogs(openFileObject.getContentInputStream(), logDataCollector, context);

            if (logDataParsedListener instanceof SingleFileBatchProcessingListener) {
                ((SingleFileBatchProcessingListener) logDataParsedListener)
                        .processingFileFinished(batchProcessingContext);
            }
            batchProcessingContext.printIfVerbose("File %s processed  [%d of %d]", fileName, i, files.size());
        } catch (Exception e) {
            batchProcessingContext.printIfVerbose("Error processing file %s: %s", fileName, e.getMessage());
            System.err.println("Can't resolve file " + fileName + ": " + e.getMessage());
            continue;
        }

    }
    if (logDataParsedListener instanceof BatchProcessingListener) {
        ((BatchProcessingListener) logDataParsedListener).processingFinished(batchProcessingContext);
    }
}

From source file:pl.otros.logview.gui.actions.ImportLogActionListener.java

public void actionPerformed(ActionEvent e) {
    JOtrosVfsBrowserDialog chooser = getOtrosApplication().getOtrosVfsBrowserDialog();
    initFileChooser(chooser);/*from  w  w  w. ja  v  a 2s  .c o  m*/
    JOtrosVfsBrowserDialog.ReturnValue result = chooser.showOpenDialog((Component) e.getSource(),
            "Open " + importer.getName() + " log");
    if (result != JOtrosVfsBrowserDialog.ReturnValue.Approve) {
        return;
    }
    final FileObject[] files = chooser.getSelectedFiles();
    for (int i = 0; i < files.length; i++) {
        final FileObject file = files[i];
        try {
            TableColumns[] tableColumnsToUse = TableColumns.ALL_WITHOUT_LOG_SOURCE;
            if (importer instanceof TableColumnNameSelfDescribable) {
                TableColumnNameSelfDescribable describable = (TableColumnNameSelfDescribable) importer;
                tableColumnsToUse = describable.getTableColumnsToUse();
            }

            final LoadingInfo openFileObject = Utils.openFileObject(file);
            final LogViewPanelWrapper panel = new LogViewPanelWrapper(files[i].getName().getBaseName(),
                    openFileObject.getObserableInputStreamImpl(), tableColumnsToUse, getOtrosApplication());
            String tabName = Utils.getFileObjectShortName(file);
            getOtrosApplication().addClosableTab(tabName, file.getName().getFriendlyURI(), Icons.FOLDER_OPEN,
                    panel, true);
            Runnable r = new Runnable() {

                @Override
                public void run() {
                    ProgressWatcher watcher = null;
                    final ProxyLogDataCollector collector = new ProxyLogDataCollector();
                    ParsingContext parsingContext = new ParsingContext(file.getName().getFriendlyURI(),
                            file.getName().getBaseName());
                    importStats = new LogImportStats(file.getName().getFriendlyURI());
                    panel.getStatsTable().setModel(importStats);
                    watcher = new ProgressWatcher(openFileObject.getObserableInputStreamImpl(), panel, file);
                    Thread t = new Thread(watcher, "Log loader: " + file.getName().toString());
                    t.setDaemon(true);
                    t.start();
                    panel.addHierarchyListener(
                            new ReadingStopperForRemove(openFileObject.getObserableInputStreamImpl()));
                    importer.initParsingContext(parsingContext);
                    importer.importLogs(openFileObject.getContentInputStream(), collector, parsingContext);
                    final LogDataTableModel dataTableModel = panel.getDataTableModel();
                    LOGGER.info("File " + file.getName().getFriendlyURI() + " loaded");
                    dataTableModel.add(collector.getLogData());
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            panel.switchToContentView();
                        }
                    });
                    watcher.updateFinish("Loaded");
                    Utils.closeQuietly(openFileObject.getFileObject());
                }
            };
            Thread t = new Thread(r);
            t.start();
        } catch (Exception e1) {
            LOGGER.severe("Error loading log (" + file.getName().getFriendlyURI() + "): " + e1.getMessage());
            JOptionPane.showMessageDialog(null, "Error loading log: " + e1.getMessage(), "Error",
                    JOptionPane.ERROR_MESSAGE);
            continue;
        }

    }
}

From source file:pl.otros.logview.gui.actions.read.LogFileInNewTabOpener.java

public void open(FileObject file) {
    try {/*w  w w.ja v a2  s . co  m*/
        boolean tailing = true;
        // Do not tail for http(s)
        if (file.getName().getScheme().startsWith("http")) {
            tailing = false;
        }
        final LoadingInfo openFileObject = Utils.openFileObject(file, tailing);

        LogImporter importer = chooseImporter(openFileObject);
        if (isInvalid(importer)) {
            handleInvalidImporter(file);
            return;
        }

        final LogViewPanelWrapper panel = createPanelForLog(file, openFileObject, importer);
        otrosApplication.addClosableTab(getTabName(file), file.getName().getFriendlyURI(), Icons.FOLDER_OPEN,
                panel, true);
        startThreadToImportLogDataFromFile(file, openFileObject, importer, panel);
    } catch (Exception e1) {
        LOGGER.severe("Error loading log (" + file.getName().getFriendlyURI() + "): " + e1.getMessage());
        JOptionPane.showMessageDialog(null, "Error loading log: " + e1.getMessage(), "Error",
                JOptionPane.ERROR_MESSAGE);
    }
}

From source file:pl.otros.logview.gui.actions.read.LogFileInNewTabOpener.java

private LogViewPanelWrapper createPanelForLog(FileObject file, final LoadingInfo openFileObject,
        LogImporter importer) {//  w ww.  j ava2  s.  co m
    TableColumns[] tableColumnsToUse = TableColumns.ALL_WITHOUT_LOG_SOURCE;
    if (importer instanceof TableColumnNameSelfDescribable) {
        TableColumnNameSelfDescribable describable = (TableColumnNameSelfDescribable) importer;
        tableColumnsToUse = describable.getTableColumnsToUse();
    }
    final LogViewPanelWrapper panel = new LogViewPanelWrapper(file.getName().getBaseName(),
            openFileObject.getObserableInputStreamImpl(), tableColumnsToUse, otrosApplication);

    return panel;
}

From source file:pl.otros.logview.gui.actions.read.LogFileInNewTabOpener.java

private void handleInvalidImporter(final FileObject file) {
    LOGGER.severe("Error loading log (" + file.getName().getFriendlyURI() + "): no suitable log parser found");

    String errorMessage = "Error loading log file: no suitable log parser found for "
            + file.getName().getFriendlyURI() + "\n"
            + "Go http://code.google.com/p/otroslogviewer/wiki/Log4jPatternLayout to check how to parse log4j custom pattern.";
    JOptionPane.showMessageDialog(null, errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
}

From source file:pl.otros.logview.gui.actions.TailLogActionListener.java

public void openFileObjectInTailMode(final FileObject file) {
    final LoadingInfo loadingInfo;
    try {//from   w  ww.j a  v  a2s .  c  o m
        loadingInfo = Utils.openFileObject(file, true);
    } catch (Exception e2) {
        LOGGER.severe("Cannot open tailing input stream for " + file.getName().getFriendlyURI() + ", "
                + e2.getMessage());
        JOptionPane.showMessageDialog(null, "Cannot open tailing input stream for: "
                + file.getName().getFriendlyURI() + ", " + e2.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }

    TableColumns[] tableColumnsToUse = determineTableColumnsToUse(loadingInfo, importer);

    final LogViewPanelWrapper panel = new LogViewPanelWrapper(file.getName().getBaseName(),
            loadingInfo.getObserableInputStreamImpl(), tableColumnsToUse, getOtrosApplication());
    panel.goToLiveMode();

    String tabName = Utils.getFileObjectShortName(file);
    getOtrosApplication().addClosableTab(tabName, loadingInfo.getFriendlyUrl(), Icons.ARROW_REPEAT, panel,
            true);

    BufferingLogDataCollectorProxy bufferingLogDataCollectorProxy = new BufferingLogDataCollectorProxy(
            panel.getDataTableModel(), 2000, panel.getConfiguration());
    openFileObjectInTailMode(panel, loadingInfo, bufferingLogDataCollectorProxy);
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            panel.switchToContentView();
        }
    });
}