Example usage for java.io File compareTo

List of usage examples for java.io File compareTo

Introduction

In this page you can find the example usage for java.io File compareTo.

Prototype

public int compareTo(File pathname) 

Source Link

Document

Compares two abstract pathnames lexicographically.

Usage

From source file:com.jaeksoft.searchlib.learning.StandardLearner.java

@Override
public void init(File instancesDir) throws SearchLibException {
    rwl.r.lock();/*from ww w  .ja  v  a2  s .  c o m*/
    try {
        if (instancesDir != null && indexDir != null)
            if (instancesDir.compareTo(indexDir) == 0)
                return;
    } finally {
        rwl.r.unlock();
    }
    rwl.w.lock();
    try {
        closeNoLock();
        this.indexDir = instancesDir;
    } finally {
        rwl.w.unlock();
    }
}

From source file:com.stormcloud.ide.api.ServicesController.java

private void walk(Item current, File dir, FilenameFilter filter) {

    /**//ww w.ja va2 s.  c  o m
     * @todo read pom for item label etc.
     *
     */
    File[] files = dir.listFiles(filter);

    if (files != null) {

        Comparator comp = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                File f1 = (File) o1;
                File f2 = (File) o2;
                if (f1.isDirectory() && !f2.isDirectory()) {
                    // Directory before non-directory
                    return -1;
                } else if (!f1.isDirectory() && f2.isDirectory()) {
                    // Non-directory after directory
                    return 1;
                } else {
                    // Alphabetic order otherwise
                    return f1.compareTo(f2);
                }
            }
        };

        Arrays.sort(files, comp);

        for (File file : files) {

            // create new item
            Item item = new Item();
            item.setId(file.getAbsolutePath());

            if (file.getName().endsWith(".jar")) {

                item.setType(ItemType.FILE);
                item.setStyle("jar");
                item.setLabel(file.getName());

            } else if (file.getName().endsWith(".pom")) {

                item.setType(ItemType.FILE);
                item.setStyle("xml");
                item.setLabel(file.getName());

            } else if (file.getName().endsWith(".sha1")) {

                item.setType(ItemType.FILE);
                item.setStyle("sha1");
                item.setLabel(file.getName());

            } else if (file.getName().endsWith(".xml")) {

                item.setType(ItemType.FILE);
                item.setStyle("xml");
                item.setLabel(file.getName());

            } else {

                item.setType(ItemType.FOLDER);
                item.setStyle("folder");
                item.setLabel(file.getName());

            }

            if (file.isDirectory()) {

                walk(item, file, filter);
            }

            if (current != null) {
                current.getChildren().add(item);
            }
        }
    }
}

From source file:net.sourceforge.seqware.common.util.workflowtools.WorkflowTools.java

private File[] getFailedJobLogs(String statusDir) {

    File dir = new File(statusDir);
    ArrayList<File> logFiles = new ArrayList<File>();

    if (dir.isDirectory()) {

        //System.out.println("Looking in directory for *.dagman.out: "+dir.getAbsolutePath());

        FileFilter fileFilter = new WildcardFileFilter("*.dagman.out");
        File[] files = dir.listFiles(fileFilter);
        if (files.length != 1) {
            Log.error("There are too many files matching *.dagman.out in statusDir: " + statusDir
                    + " expected just one but found " + files.length);
        } else {/*  www.  j  ava  2s .c o  m*/
            File dagLog = files[0];
            Log.info("EXAMINING ERROR LOGS:");
            Log.info(" + found dagman file: " + dagLog.getAbsolutePath());
            String[] logNames = findLogNamesFromDagLog(dagLog);
            for (String logFileName : logNames) {
                Log.info(" + found error log file prefix: " + logFileName);
                FileFilter logFileFilter = new WildcardFileFilter(logFileName + ".out*");
                File[] currLogFiles = dir.listFiles(logFileFilter);
                File newLogFile = null;
                for (File currLogFile : currLogFiles) {
                    Log.info(" + found error log file instance: " + currLogFile.getAbsolutePath());
                    if (newLogFile == null && currLogFile != null) {
                        newLogFile = currLogFile;
                    } else if (newLogFile.compareTo(currLogFile) < 0) {
                        newLogFile = currLogFile;
                    }
                }
                if (newLogFile != null && newLogFile.exists()) {
                    //System.out.println("Adding log file");
                    logFiles.add(newLogFile);
                } else if (newLogFile != null) {
                    Log.error("Log file: " + newLogFile.getAbsolutePath() + " does not exist!");
                } else {
                    Log.error("Log file was not found!");
                }
            }
            return (logFiles.toArray(new File[0]));
        }
    }

    Log.error("Was unable to find any error logs in statusDir: " + statusDir);
    return (logFiles.toArray(new File[0]));
}

From source file:org.jimcat.model.Image.java

/**
 * compares two Images in there natural order (file path)
 * /*  w  w  w  . ja  va2  s  .  com*/
 * @param o
 * @return the result of the compareTo method as specified in java
 * @see java.lang.Comparable#compareTo(java.lang.Object)
 */
public int compareTo(Image o) {
    // fast decision
    if (this == o) {
        return 0;
    }

    // get the data
    File path1 = (getMetadata() != null) ? getMetadata().getPath() : null;
    File path2 = (o.getMetadata() != null) ? o.getMetadata().getPath() : null;
    // test if first is null
    if (path1 == null) {
        // if yes test if second is null too
        if (path2 == null)
            return -1;
        // only first is null
        return -1;
    }
    // test if only second is null
    if (path2 == null)
        return 1;

    // none of them is null, so delegate to compareTo from String
    return path1.compareTo(path2);
}

From source file:com.metamx.druid.realtime.RealtimePlumberSchool.java

@Override
public Plumber findPlumber(final Schema schema, final FireDepartmentMetrics metrics) {
    verifyState();// w  w  w.j  a v  a2  s  .  c  o m
    initializeExecutors();

    computeBaseDir(schema).mkdirs();

    final Map<Long, Sink> sinks = Maps.newConcurrentMap();

    for (File sinkDir : computeBaseDir(schema).listFiles()) {
        Interval sinkInterval = new Interval(sinkDir.getName().replace("_", "/"));

        final File[] sinkFiles = sinkDir.listFiles();
        Arrays.sort(sinkFiles, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                try {
                    return Ints.compare(Integer.parseInt(o1.getName()), Integer.parseInt(o2.getName()));
                } catch (NumberFormatException e) {
                    log.error(e, "Couldn't compare as numbers? [%s][%s]", o1, o2);
                    return o1.compareTo(o2);
                }
            }
        });

        try {
            List<FireHydrant> hydrants = Lists.newArrayList();
            for (File segmentDir : sinkFiles) {
                log.info("Loading previously persisted segment at [%s]", segmentDir);
                hydrants.add(new FireHydrant(new QueryableIndexSegment(null, IndexIO.loadIndex(segmentDir)),
                        Integer.parseInt(segmentDir.getName())));
            }

            Sink currSink = new Sink(sinkInterval, schema, hydrants);
            sinks.put(sinkInterval.getStartMillis(), currSink);

            metadataUpdater.announceSegment(currSink.getSegment());
        } catch (IOException e) {
            log.makeAlert(e, "Problem loading sink[%s] from disk.", schema.getDataSource())
                    .addData("interval", sinkInterval).emit();
        }
    }

    serverView.registerSegmentCallback(persistExecutor, new ServerView.BaseSegmentCallback() {
        @Override
        public ServerView.CallbackAction segmentAdded(DruidServer server, DataSegment segment) {
            if ("realtime".equals(server.getType())) {
                return ServerView.CallbackAction.CONTINUE;
            }

            log.debug("Checking segment[%s] on server[%s]", segment, server);
            if (schema.getDataSource().equals(segment.getDataSource())) {
                final Interval interval = segment.getInterval();
                for (Map.Entry<Long, Sink> entry : sinks.entrySet()) {
                    final Long sinkKey = entry.getKey();
                    if (interval.contains(sinkKey)) {
                        final Sink sink = entry.getValue();
                        log.info("Segment matches sink[%s]", sink);

                        if (segment.getVersion().compareTo(sink.getSegment().getVersion()) >= 0) {
                            try {
                                metadataUpdater.unannounceSegment(sink.getSegment());
                                FileUtils.deleteDirectory(computePersistDir(schema, sink.getInterval()));
                                sinks.remove(sinkKey);
                            } catch (IOException e) {
                                log.makeAlert(e, "Unable to delete old segment for dataSource[%s].",
                                        schema.getDataSource()).addData("interval", sink.getInterval()).emit();
                            }
                        }
                    }
                }
            }

            return ServerView.CallbackAction.CONTINUE;
        }
    });

    final long truncatedNow = segmentGranularity.truncate(new DateTime()).getMillis();
    final long windowMillis = windowPeriod.toStandardDuration().getMillis();
    final RejectionPolicy rejectionPolicy = rejectionPolicyFactory.create(windowPeriod);
    log.info("Creating plumber using rejectionPolicy[%s]", rejectionPolicy);

    log.info("Expect to run at [%s]", new DateTime().plus(new Duration(System.currentTimeMillis(),
            segmentGranularity.increment(truncatedNow) + windowMillis)));

    ScheduledExecutors.scheduleAtFixedRate(scheduledExecutor,
            new Duration(System.currentTimeMillis(), segmentGranularity.increment(truncatedNow) + windowMillis),
            new Duration(truncatedNow, segmentGranularity.increment(truncatedNow)),
            new ThreadRenamingRunnable(String.format("%s-overseer", schema.getDataSource())) {
                @Override
                public void doRun() {
                    log.info("Starting merge and push.");

                    long minTimestamp = segmentGranularity.truncate(rejectionPolicy.getCurrMaxTime())
                            .getMillis() - windowMillis;

                    List<Map.Entry<Long, Sink>> sinksToPush = Lists.newArrayList();
                    for (Map.Entry<Long, Sink> entry : sinks.entrySet()) {
                        final Long intervalStart = entry.getKey();
                        if (intervalStart < minTimestamp) {
                            log.info("Adding entry[%s] for merge and push.", entry);
                            sinksToPush.add(entry);
                        }
                    }

                    for (final Map.Entry<Long, Sink> entry : sinksToPush) {
                        final Sink sink = entry.getValue();

                        final String threadName = String.format("%s-%s-persist-n-merge", schema.getDataSource(),
                                new DateTime(entry.getKey()));
                        persistExecutor.execute(new ThreadRenamingRunnable(threadName) {
                            @Override
                            public void doRun() {
                                final Interval interval = sink.getInterval();

                                for (FireHydrant hydrant : sink) {
                                    if (!hydrant.hasSwapped()) {
                                        log.info("Hydrant[%s] hasn't swapped yet, swapping. Sink[%s]", hydrant,
                                                sink);
                                        final int rowCount = persistHydrant(hydrant, schema, interval);
                                        metrics.incrementRowOutputCount(rowCount);
                                    }
                                }

                                final File mergedFile;
                                try {
                                    List<QueryableIndex> indexes = Lists.newArrayList();
                                    for (FireHydrant fireHydrant : sink) {
                                        Segment segment = fireHydrant.getSegment();
                                        final QueryableIndex queryableIndex = segment.asQueryableIndex();
                                        log.info("Adding hydrant[%s]", fireHydrant);
                                        indexes.add(queryableIndex);
                                    }

                                    mergedFile = IndexMerger.mergeQueryableIndex(indexes,
                                            schema.getAggregators(),
                                            new File(computePersistDir(schema, interval), "merged"));

                                    QueryableIndex index = IndexIO.loadIndex(mergedFile);

                                    DataSegment segment = segmentPusher.push(mergedFile,
                                            sink.getSegment().withDimensions(
                                                    Lists.newArrayList(index.getAvailableDimensions())));

                                    metadataUpdater.publishSegment(segment);
                                } catch (IOException e) {
                                    log.makeAlert(e, "Failed to persist merged index[%s]",
                                            schema.getDataSource()).addData("interval", interval).emit();
                                }
                            }
                        });
                    }
                }
            });

    return new Plumber() {
        @Override
        public Sink getSink(long timestamp) {
            if (!rejectionPolicy.accept(timestamp)) {
                return null;
            }

            final long truncatedTime = segmentGranularity.truncate(timestamp);

            Sink retVal = sinks.get(truncatedTime);

            if (retVal == null) {
                retVal = new Sink(new Interval(new DateTime(truncatedTime),
                        segmentGranularity.increment(new DateTime(truncatedTime))), schema);

                try {
                    metadataUpdater.announceSegment(retVal.getSegment());

                    sinks.put(truncatedTime, retVal);
                } catch (IOException e) {
                    log.makeAlert(e, "Failed to announce new segment[%s]", schema.getDataSource())
                            .addData("interval", retVal.getInterval()).emit();
                }
            }

            return retVal;
        }

        @Override
        public <T> QueryRunner<T> getQueryRunner(final Query<T> query) {
            final QueryRunnerFactory<T, Query<T>> factory = conglomerate.findFactory(query);
            final Function<Query<T>, ServiceMetricEvent.Builder> builderFn = new Function<Query<T>, ServiceMetricEvent.Builder>() {
                private final QueryToolChest<T, Query<T>> toolchest = factory.getToolchest();

                @Override
                public ServiceMetricEvent.Builder apply(@Nullable Query<T> input) {
                    return toolchest.makeMetricBuilder(query);
                }
            };

            return factory.mergeRunners(EXEC,
                    FunctionalIterable.create(sinks.values()).transform(new Function<Sink, QueryRunner<T>>() {
                        @Override
                        public QueryRunner<T> apply(@Nullable Sink input) {
                            return new MetricsEmittingQueryRunner<T>(emitter, builderFn, factory.mergeRunners(
                                    EXEC,
                                    Iterables.transform(input, new Function<FireHydrant, QueryRunner<T>>() {
                                        @Override
                                        public QueryRunner<T> apply(@Nullable FireHydrant input) {
                                            return factory.createRunner(input.getSegment());
                                        }
                                    })));
                        }
                    }));
        }

        @Override
        public void persist(final Runnable commitRunnable) {
            final List<Pair<FireHydrant, Interval>> indexesToPersist = Lists.newArrayList();
            for (Sink sink : sinks.values()) {
                if (sink.swappable()) {
                    indexesToPersist.add(Pair.of(sink.swap(), sink.getInterval()));
                }
            }

            log.info("Submitting persist runnable for dataSource[%s]", schema.getDataSource());

            persistExecutor.execute(new ThreadRenamingRunnable(
                    String.format("%s-incremental-persist", schema.getDataSource())) {
                @Override
                public void doRun() {
                    for (Pair<FireHydrant, Interval> pair : indexesToPersist) {
                        metrics.incrementRowOutputCount(persistHydrant(pair.lhs, schema, pair.rhs));
                    }
                    commitRunnable.run();
                }
            });
        }

        @Override
        public void finishJob() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:tvbrowser.core.Settings.java

/**
 * Reads the settings from settings file. If there is no settings file,
 * default settings are used.//from  w w  w  . j a v a2 s .co m
 */
public static void loadSettings() {
    String oldDirectoryName = System.getProperty("user.home", "") + File.separator + ".tvbrowser";
    String newDirectoryName = getUserSettingsDirName();

    File settingsFile = new File(newDirectoryName, SETTINGS_FILE);
    File firstSettingsBackupFile = new File(getUserSettingsDirName(), SETTINGS_FILE + "_backup1");
    File secondSettingsBackupFile = new File(getUserSettingsDirName(), SETTINGS_FILE + "_backup2");

    if (settingsFile.exists() || firstSettingsBackupFile.exists() || secondSettingsBackupFile.exists()) {
        try {
            mProp.readFromFile(settingsFile);

            if (((mProp.getProperty("subscribedchannels") == null
                    || mProp.getProperty("subscribedchannels").trim().length() < 1)
                    && (mProp.getProperty("channelsWereConfigured") != null
                            && mProp.getProperty("channelsWereConfigured").equals("true")))
                    && (firstSettingsBackupFile.isFile() || secondSettingsBackupFile.isFile())) {
                throw new IOException();
            } else {
                mLog.info("Using settings from file " + settingsFile.getAbsolutePath());
            }
        } catch (IOException evt) {

            if (firstSettingsBackupFile.isFile() || secondSettingsBackupFile.isFile()) {
                Localizer localizer = Localizer.getLocalizerFor(Settings.class);
                if (JOptionPane.showConfirmDialog(null, localizer.msg("settingBroken",
                        "Settings file broken.\nWould you like to load the backup file?\n\n(If you select No, the\ndefault settings are used)"),
                        Localizer.getLocalization(Localizer.I18N_ERROR), JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
                    boolean loadSecondBackup = !firstSettingsBackupFile.isFile();

                    if (firstSettingsBackupFile.isFile()) {
                        try {
                            mProp.readFromFile(firstSettingsBackupFile);

                            if ((mProp.getProperty("subscribedchannels") == null
                                    || mProp.getProperty("subscribedchannels").trim().length() < 1)
                                    && secondSettingsBackupFile.isFile()) {
                                loadSecondBackup = true;
                            } else {
                                mLog.info("Using settings from file "
                                        + firstSettingsBackupFile.getAbsolutePath());
                                loadSecondBackup = false;
                            }
                        } catch (Exception e) {
                            loadSecondBackup = true;
                        }
                    }
                    if (loadSecondBackup && secondSettingsBackupFile.isFile()) {
                        try {
                            mProp.readFromFile(secondSettingsBackupFile);
                            mLog.info("Using settings from file " + secondSettingsBackupFile.getAbsolutePath());
                            loadSecondBackup = false;
                        } catch (Exception e) {
                            loadSecondBackup = true;
                        }
                    }

                    if (loadSecondBackup) {
                        mLog.info("Could not read settings - using default user settings");
                    } else {
                        try {
                            loadWindowSettings();
                            storeSettings(true);
                        } catch (Exception e) {
                        }
                    }
                }
            } else {
                mLog.info("Could not read settings - using default user settings");
            }
        }
    }
    /*
     * If the settings file doesn't exist, we try to import the settings created
     * by a previous version of TV-Browser
     */
    else if (!oldDirectoryName.equals(newDirectoryName)) {
        File oldDir = null;
        File testFile = null;

        int countValue = 1;

        String firstDir = System.getProperty("user.home") + "/TV-Browser";

        if (Launch.isOsWindowsNtBranch()) {
            countValue = 3;
        }

        if (OperatingSystem.isWindows()) {
            File test = new File(System.getenv("appdata"), "TV-Browser");

            if (test.isDirectory()) {
                firstDir = test.getAbsolutePath();
            }
        }

        String[] directories = { getUserDirectoryName(), firstDir,
                System.getProperty("user.home") + "/TV-Browser",
                System.getProperty("user.home") + "/Library/Preferences/TV-Browser",
                System.getProperty("user.home") + "/.tvbrowser" };

        for (int j = 0; j < (TVBrowser.isTransportable() ? directories.length : countValue); j++) {
            String[] allVersions = TVBrowser.getAllVersionStrings();
            for (int i = (j == 0 ? 1 : 0); i < allVersions.length; i++) {
                testFile = new File(directories[j] + File.separator + allVersions[i], SETTINGS_FILE);

                if (testFile.isFile()) {
                    oldDir = new File(directories[j], allVersions[i]);
                    break;
                }
            }

            if (oldDir == null) {
                testFile = new File(directories[j], SETTINGS_FILE);

                if (testFile.isFile()) {
                    oldDir = new File(directories[j]);
                } else {
                    testFile = new File(oldDirectoryName, SETTINGS_FILE);

                    if (testFile.isFile()) {
                        oldDir = new File(oldDirectoryName);
                    }
                }
            }

            if (oldDir != null) {
                break;
            }
        }

        if (oldDir != null && oldDir.isDirectory() && oldDir.exists() && TVBrowser.isTransportable()
                && !oldDir.getAbsolutePath().startsWith(new File("settings").getAbsolutePath())) {
            try {
                UIManager.setLookAndFeel(getDefaultLookAndFeelClassName());
            } catch (Exception e) {
                /*ignore*/}

            String[] options = { MainFrame.mLocalizer.msg("import", "Import settings"),
                    MainFrame.mLocalizer.msg("configureNew", "Create new configuration") };
            String title = MainFrame.mLocalizer.msg("importInfoTitle", "Import settings?");
            String msg = MainFrame.mLocalizer.msg("importInfoMsg",
                    "TV-Browser has found settings for import.\nShould the settings be imported now?");

            if (JOptionPane.showOptionDialog(null, msg, title, JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE, null, options, options[0]) == JOptionPane.NO_OPTION) {
                oldDir = null;
            }
        }

        if (oldDir != null && oldDir.isDirectory() && oldDir.exists()) {
            startImportWaitingDlg();
            mLog.info("Try to load settings from a previous version of TV-Browser: " + oldDir);

            final File newDir = new File(getUserSettingsDirName());

            File oldTvDataDir = null;

            final Properties prop = new Properties();

            try {
                StreamUtilities.inputStream(testFile, new InputStreamProcessor() {
                    public void process(InputStream input) throws IOException {
                        prop.load(input);
                    }
                });
            } catch (Exception e) {
            }

            String versionString = prop.getProperty("version", null);
            Version testVersion = null;

            if (versionString != null) {
                try {
                    int asInt = Integer.parseInt(versionString);
                    int major = asInt / 100;
                    int minor = asInt % 100;
                    testVersion = new Version(major, minor);
                } catch (NumberFormatException exc) {
                    // Ignore
                }
            }

            String temp = prop.getProperty("dir.tvdata", null);
            boolean versionTest = !TVBrowser.isTransportable() && Launch.isOsWindowsNtBranch()
                    && testVersion != null && testVersion.compareTo(new Version(3, 0, true)) < 0
                    && (temp == null || temp.replace("/", "\\")
                            .equals(System.getProperty("user.home") + "\\TV-Browser\\tvdata"));

            if ((TVBrowser.isTransportable() || versionTest)
                    && !(new File(getUserDirectoryName(), "tvdata").isDirectory())) {
                try {
                    if (temp != null) {
                        oldTvDataDir = new File(temp);
                    } else if (new File(oldDir, "tvdata").isDirectory()) {
                        oldTvDataDir = new File(oldDir, "tvdata");
                    } else if (new File(oldDir.getParent(), "tvdata").isDirectory()) {
                        oldTvDataDir = new File(oldDir.getParent(), "tvdata");
                    }

                } catch (Exception e) {
                }
            }

            if (newDir.mkdirs()) {
                try {
                    IOUtilities.copy(oldDir.listFiles(new FilenameFilter() {
                        public boolean accept(File dir, String name) {
                            return !name.equalsIgnoreCase("tvdata") && !name.equals(newDir.getName())
                                    && !name.equalsIgnoreCase("backup") && !name.equalsIgnoreCase("lang");
                        }
                    }), newDir);

                    mShowSettingsCopyWaiting = false;

                    mLog.info("settings from previous version copied successfully");
                    File newSettingsFile = new File(newDir, SETTINGS_FILE);
                    mProp.readFromFile(newSettingsFile);
                    mLog.info("settings from previous version read successfully");

                    /*
                     * This is the .tvbrowser dir, if there are settings form version
                     * 1.0 change the name to start with java.
                     */
                    if (oldDirectoryName.equals(oldDir.getAbsolutePath())) {
                        File[] settings = newDir.listFiles(new FilenameFilter() {
                            public boolean accept(File dir, String name) {
                                return (name.toLowerCase().endsWith(".prop")
                                        && name.toLowerCase().indexOf("settings") == -1)
                                        || (name.toLowerCase().endsWith(".dat")
                                                && name.toLowerCase().indexOf("tv-data-inventory") == -1);
                            }
                        });

                        boolean version1 = false;

                        if (settings != null) {
                            for (int i = 0; i < settings.length; i++) {
                                String name = "java." + settings[i].getName();

                                if (!settings[i].getName().toLowerCase().startsWith("java.")) {
                                    version1 = true;
                                    settings[i].renameTo(new File(settings[i].getParent(), name));
                                }
                            }
                        }

                        if (version1 && !(new File(oldDirectoryName, newDir.getName())).isDirectory()) {
                            oldDir.renameTo(new File(
                                    System.getProperty("user.home", "") + File.separator + "tvbrowser_BACKUP"));
                        }
                    }

                    /*
                     * Test if and copy TV data for the portable version.
                     */
                    if (oldTvDataDir != null && oldTvDataDir.isDirectory()) {
                        final File targetDir = new File(getUserDirectoryName(), "tvdata");

                        if (!oldTvDataDir.equals(targetDir)) {
                            targetDir.mkdirs();

                            final CopyWaitingDlg waiting = new CopyWaitingDlg(new JFrame(),
                                    versionTest ? CopyWaitingDlg.APPDATA_MSG : CopyWaitingDlg.IMPORT_MSG);

                            mShowWaiting = true;

                            final File srcDir = oldTvDataDir;

                            Thread copyDataThread = new Thread("Copy TV data directory") {
                                public void run() {
                                    try {
                                        IOUtilities.copy(srcDir.listFiles(), targetDir, true);
                                    } catch (Exception e) {
                                    }

                                    mShowWaiting = false;
                                    waiting.setVisible(false);
                                }
                            };
                            copyDataThread.start();

                            waiting.setVisible(mShowWaiting);
                        }
                    }

                    /*
                     * Test if a settings file exist in the user directory, move the
                     * settings to backup.
                     */
                    if ((new File(getUserDirectoryName(), SETTINGS_FILE)).isFile()) {
                        final File backupDir = new File(getUserDirectoryName(), "BACKUP");
                        if (backupDir.mkdirs()) {
                            mLog.info("moving the settings of old settings dir to backup");
                            File[] files = oldDir.listFiles(new FileFilter() {
                                public boolean accept(File pathname) {
                                    return pathname.compareTo(newDir) != 0
                                            && pathname.getName().compareToIgnoreCase("tvdata") != 0
                                            && pathname.compareTo(backupDir) != 0;
                                }
                            });

                            if (files != null) {
                                for (File file : files) {
                                    file.renameTo(new File(backupDir, file.getName()));
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    mLog.log(Level.WARNING, "Could not import user settings from '" + oldDir.getAbsolutePath()
                            + "' to '" + newDir.getAbsolutePath() + "'", e);
                }
            } else {
                mLog.info("Could not create directory '" + newDir.getAbsolutePath()
                        + "' - using default user settings");
            }
        } else {
            mLog.info("No previous version of TV-Browser found - using default user settings");
        }
    }
    mShowSettingsCopyWaiting = false;

    File settingsDir = new File(newDirectoryName);

    if (!settingsDir.exists()) {
        mLog.info("Creating " + newDirectoryName);
        settingsDir.mkdir();
    }

    loadWindowSettings();
}

From source file:org.apache.james.filesystem.api.SieveFileRepository.java

/**
 * @see org.apache.james.managesieve.api.SieveRepository#listScripts(java.lang.String)
 *//*from w  w  w. j a  v a  2 s  . c o  m*/
public List<ScriptSummary> listScripts(final String user) throws UserNotFoundException {
    File[] files = getUserDirectory(user).listFiles();
    List<ScriptSummary> summaries = new ArrayList<ScriptSummary>(files.length);
    File activeFile = null;
    try {
        activeFile = getActiveFile(user);
    } catch (ScriptNotFoundException ex) {
        // no op
    }
    final File activeFile1 = activeFile;
    for (final File file : files) {
        if (!SYSTEM_FILES.contains(file.getName())) {
            summaries.add(new ScriptSummary() {

                public String getName() {
                    return file.getName();
                }

                public boolean isActive() {
                    boolean isActive = false;
                    if (null != activeFile1) {
                        isActive = 0 == activeFile1.compareTo(file);
                    }
                    return isActive;
                }
            });
        }
    }
    return summaries;
}

From source file:com.xtructure.xevolution.tool.impl.ReadPopulationsTool.java

/**
 * Creates a new {@link ReadPopulationsTool}.
 * /*w w  w.  j  a v  a 2 s .co  m*/
 * @param dataTracker
 */
public ReadPopulationsTool(DataTracker<?, ?> dataTracker) {
    super("readPopulations", //
            new BooleanXOption(HELP, "h", "help", "print usage"), //
            new BooleanXOption(CONTINUOUS_READ, "c", "continuous", "continue reading files"), //
            new BooleanXOption(VERBOSE, "v", "verbose", "print progess"), //
            new BooleanXOption(RESUME, "r", "resume",
                    "resume from previously built data (otherwise, all files in data directory will be erased)"), //
            new FileXOption(POPULATION_DIRECTORY, "p", "populations", "directory containing population files"), //
            new FileXOption(DATA_DIRECTORY, "d", "data", "directory to which data is written"));
    this.dataTracker = dataTracker;
    this.worker = new Thread(new Runnable() {
        private long latest;

        @Override
        public void run() {
            latest = readLatest();
            try {
                // load existing data
                if (verbose) {
                    System.out.println("Loading data...");
                }
                ReadPopulationsTool.this.dataTracker.load(dataDir);
                if (verbose) {
                    System.out.println("\tdone.");
                }
                while (true) {
                    // collect population files
                    File[] newFiles = populationDir.listFiles(new FileFilter() {
                        @Override
                        public boolean accept(File pathname) {
                            return pathname.lastModified() > latest && pathname.getName().endsWith(".xml");
                        }
                    });
                    if (newFiles.length == 0) {
                        if (verbose) {
                            System.out.println("no new files, waiting...");
                        }
                        try {
                            // polling for now...
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            break;
                        }
                        if (continuousRead) {
                            continue;
                        } else {
                            break;
                        }
                    }
                    Arrays.sort(newFiles, new Comparator<File>() {
                        @Override
                        public int compare(File o1, File o2) {
                            Matcher matcher = POPULATION_FILE_PATTERN.matcher(o1.getName());
                            if (matcher.matches()) {
                                int age1 = Integer.parseInt(matcher.group(1));
                                matcher = POPULATION_FILE_PATTERN.matcher(o2.getName());
                                if (matcher.matches()) {
                                    int age2 = Integer.parseInt(matcher.group(1));
                                    return age1 - age2;
                                }
                            }
                            return o1.compareTo(o2);
                        }
                    });
                    // process population files
                    for (File populationFile : newFiles) {
                        latest = Math.max(latest, populationFile.lastModified());
                        if (verbose) {
                            System.out.println("Processing population : " + populationFile.getName());
                        }
                        for (int j = 0; j < RETRIES; j++) {
                            popLock.lock();
                            try {
                                if (ReadPopulationsTool.this.dataTracker
                                        .processPopulation(populationFile) != null) {
                                    // success
                                    break;
                                }
                            } finally {
                                popLock.unlock();
                            }
                            // failed to read population, wait and try again
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            ReadPopulationsTool.this.dataTracker.write(dataDir);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    writeLatest(latest);
                    if (!continuousRead) {
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (org.json.simple.parser.ParseException e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:com.cyberway.issue.crawler.settings.XMLSettingsHandler.java

/** Creates a replica of the settings file structure in another directory
 * (fully recursive, includes all per host settings). The SettingsHandler
 * will then refer to the new files.//w  w w .j av a2  s.  c  om
 *
 * Observe that this method should only be called after the SettingsHandler
 * has been initialized.
 *
 * @param newOrderFileName where the new order file should be saved.
 * @param newSettingsDirectory the top level directory of the per host/domain
 *                          settings files.
 * @throws IOException
 */
public void copySettings(File newOrderFileName, String newSettingsDirectory) throws IOException {
    File oldSettingsDirectory = getSettingsDirectory();

    // Write new orderfile and point the settingshandler to it
    orderFile = newOrderFileName;
    try {
        getOrder().setAttribute(new Attribute(CrawlOrder.ATTR_SETTINGS_DIRECTORY, newSettingsDirectory));
    } catch (Exception e) {
        throw new IOException("Could not update settings with new location: " + e.getMessage());
    }
    writeSettingsObject(getSettingsObject(null));

    File newDir = getPathRelativeToWorkingDirectory(newSettingsDirectory);

    // Copy the per host files if src and dest directories are different.
    if (oldSettingsDirectory.compareTo(newDir) != 0) {
        FileUtils.copyFiles(oldSettingsDirectory, newDir);
    }
}

From source file:com.xtructure.xevolution.gui.XEvolutionGui.java

/**
 * Creates a background thread to watch the output directory where.
 * /*from w  w  w . j  a v  a2  s  .  co m*/
 * {@link Population} xml files are written. The thread polls the file
 * system once every two seconds for new files.
 */
public void watchDir() {
    watchDirThread = new Thread(new Runnable() {
        private long newest = 0l;

        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            if (OUTPUT_OPTION.hasValue()) {
                File watchDir = OUTPUT_OPTION.processValue();
                populationFiles.clear();
                populationPanel.removeAllItems();
                while (true) {
                    File[] newFiles = watchDir.listFiles(new FileFilter() {
                        @Override
                        public boolean accept(File pathname) {
                            return pathname.lastModified() > newest && pathname.getName().endsWith(".xml");
                        }
                    });
                    if (newFiles.length == 0) {
                        try {
                            // polling for now...
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            break;
                        }
                        continue;
                    }
                    List<File> newFilesList = Arrays.asList(newFiles);
                    Collections.sort(newFilesList, new Comparator<File>() {
                        @Override
                        public int compare(File o1, File o2) {
                            Matcher matcher = POPULATION_FILE_PATTERN.matcher(o1.getName());
                            if (matcher.matches()) {
                                int age1 = Integer.parseInt(matcher.group(1));
                                matcher = POPULATION_FILE_PATTERN.matcher(o2.getName());
                                if (matcher.matches()) {
                                    int age2 = Integer.parseInt(matcher.group(1));
                                    return age1 - age2;
                                }
                            }
                            return o1.compareTo(o2);
                        }
                    });
                    populationFiles.addAll(newFilesList);
                    populationPanel.addAllItems(newFilesList);
                    XEvolutionGui.this.statusBar.startProgressBar(0, newFilesList.size());
                    int i = 0;
                    for (File populationFile : newFilesList) {
                        newest = Math.max(newest, populationFile.lastModified());
                        statusBar.setProgressBar(i++);
                        statusBar.setMessage("Loading population : " + populationFile.getName());
                        Population<?> population = null;
                        for (int j = 0; j < RETRIES; j++) {
                            popLock.lock();
                            try {
                                population = dataTracker.processPopulation(populationFile);
                                for (@SuppressWarnings("rawtypes")
                                final XValId valueId : population.getGenomeAttributeIds()) {
                                    Graph graph = graphsMap.get(valueId);
                                    if (graph == null) {
                                        graph = addGraph(valueId);
                                    }
                                    graph.addData(
                                            //
                                            ((Number) population.getHighestGenomeByAttribute(valueId)
                                                    .getAttribute(valueId)).doubleValue(), //
                                            population.getAverageGenomeAttribute(valueId), //
                                            ((Number) population.getLowestGenomeByAttribute(valueId)
                                                    .getAttribute(valueId)).doubleValue());
                                }
                                // success
                                break;
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                popLock.unlock();
                            }
                            // failed to read population, wait and try again
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        if (population == null) {
                            // failed to read population. we know there was
                            // a population, so add dummy data as a place
                            // holder
                            for (XValId<?> valueId : graphsMap.keySet()) {
                                Graph graph = graphsMap.get(valueId);
                                if (graph == null) {
                                    graph = new Graph(valueId.getBase(), bufferSize, 3);
                                    graphsMap.put(valueId, graph);
                                    menuBar.addGraphCheckbox(graph, graphPanel, false);
                                }
                                graph.addData(0.0, 0.0, 0.0);
                            }
                        }
                        frame.repaint();
                    }
                    statusBar.clearMessage();
                    statusBar.clearProgressBar();
                }
            }
        }
    });
    watchDirThread.start();
}