org.p_vcd.model.Parameters.java Source code

Java tutorial

Introduction

Here is the source code for org.p_vcd.model.Parameters.java

Source

/*
 * Copyright (C) 2012-2015, Juan Manuel Barrios <http://juan.cl/>
 * All rights reserved.
 *
 * This file is part of P-VCD. http://p-vcd.org/
 * P-VCD is made available under the terms of the BSD 2-Clause License.
 */
package org.p_vcd.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Arrays;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import org.p_vcd.process.ProcessTest;
import org.p_vcd.ui.InternalVlcViewerDialog;

public class Parameters {

    private static Parameters singleton = null;

    public static synchronized Parameters get() {
        if (singleton == null)
            throw new RuntimeException("Parameters not loaded");
        return singleton;
    }

    public static synchronized void initParameters(File systemInstallPath, int system_bitness) throws Exception {
        singleton = new Parameters(systemInstallPath, system_bitness);
        singleton.reload();
    }

    private static final String KEY_USERDATA = "userdata.dir";
    private static final String KEY_PVCD = "pvcd.dir";
    private static final String KEY_MAXCORES = "max_cores.int";
    private static final String KEY_VLCBIN = "3rd_party.vlc.bin";
    private static final String KEY_VLCLIB_BITNESS = "3rd_party.vlc.lib";
    private static final String KEY_WGET = "3rd_party.wget.bin";
    private static final String KEY_YOUTUBEDL = "3rd_party.youtube-dl.bin";
    private static final String KEY_DIR_DBS = "DefaultDir.DatabaseFiles";
    private static final String KEY_DIR_QUERIES = "DefaultDir.QuerySelection";

    // system data
    private final String systemVersionName = "{VERSION_NAME}";
    private final File systemInstallPath;
    private final int systemBitness;
    private final String systemExeExtension;
    private final String systemLibExtension;

    // generated data
    private File userDataPath;
    private File databasesPath;
    private File queriesPath;
    private File searchesPath;
    private File downloadsPath;

    // p-vcd command line
    private String pvcdPath;
    private int pvcdMaxCores;

    // Third-party dependencies
    private String vlcExePath;
    private String libVlcPath;
    private String wgetExePath;
    private String youtubedlExePath;

    // Default file extensions
    private String[] videoExtensions;
    private String[] imageExtensions;
    private String[] imageAndVideoExtensions;

    // Default folders on file selection
    private File defaultDir_DatabaseFiles;
    private File defaultDir_QuerySelection;

    // log
    private File log_file;
    private PrintStream log_output;

    private Parameters(File systemInstallPath, int system_bitness) {
        // system data
        this.systemInstallPath = systemInstallPath;
        this.systemBitness = system_bitness;
        if (SystemUtils.IS_OS_WINDOWS) {
            this.systemExeExtension = ".exe";
            this.systemLibExtension = ".dll";
        } else {
            this.systemExeExtension = "";
            this.systemLibExtension = ".so";
        }
    }

    private void createUserDataDir() throws Exception {
        if (!this.userDataPath.exists() || !this.userDataPath.isDirectory()) {
            FileUtils.forceMkdir(this.userDataPath);
        }
        if (this.userDataPath.exists() && this.userDataPath.isDirectory()) {
            File readmeFile = new File(this.userDataPath, "README.txt");
            File logFile = new File(this.userDataPath, "console.log");
            if (!readmeFile.exists()) {
                String l1 = "This directory contains data generated by P-VCD.";
                String l2 = "You can move it to other destination by modifying P-VCD preferences.";
                FileUtils.writeLines(readmeFile, Arrays.asList(new String[] { l1, l2 }));
            }
            setGlobalLogFile(logFile);
        }
    }

    private void reload() throws Exception {
        // generated data
        String dir = MyPreferences.getParameter(KEY_USERDATA, "userdata");
        this.userDataPath = (new File(dir).isAbsolute()) ? new File(dir) : new File(this.systemInstallPath, dir);
        this.databasesPath = new File(this.userDataPath, "databases-data");
        this.queriesPath = new File(this.userDataPath, "queries-data");
        this.searchesPath = new File(this.userDataPath, "searches-data");
        this.downloadsPath = new File(this.userDataPath, "download-files");
        createUserDataDir();

        // p-vcd command line
        this.pvcdPath = MyPreferences.getParameter(KEY_PVCD, "");
        int numThreads = 0;
        try {
            String stringNum = MyPreferences.getParameter(KEY_MAXCORES, null);
            if (stringNum != null && stringNum.length() > 0)
                numThreads = Integer.parseInt(stringNum, 10);
            if (numThreads <= 0) {
                String val = System.getenv("NUMBER_OF_PROCESSORS");
                if (val != null && val.length() > 0)
                    numThreads = Integer.parseInt(val, 10);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.pvcdMaxCores = Math.min(Math.max(numThreads, 1), 64);

        // Third-party dependencies
        this.vlcExePath = MyPreferences.getParameter(KEY_VLCBIN, "vlc" + this.systemExeExtension);
        this.libVlcPath = MyPreferences.getParameter(KEY_VLCLIB_BITNESS + this.systemBitness,
                "libvlc" + this.systemLibExtension);
        this.wgetExePath = MyPreferences.getParameter(KEY_WGET, "wget" + this.systemExeExtension);
        this.youtubedlExePath = MyPreferences.getParameter(KEY_YOUTUBEDL, "youtube-dl" + this.systemExeExtension);

        // Default file extensions
        String videoExts = "mp4,mpg,avi,mov,flv,mkv,wmv,webm,ogv";
        String imgsExts = "png,jpg,jpeg,gif,bmp,pgm,ppm";
        this.videoExtensions = videoExts.split(",");
        this.imageExtensions = imgsExts.split(",");
        this.imageAndVideoExtensions = (videoExts + "," + imgsExts).split(",");

        // Default folders on file selection
        this.defaultDir_DatabaseFiles = MyPreferences.getParameterFile(KEY_DIR_DBS);
        this.defaultDir_QuerySelection = MyPreferences.getParameterFile(KEY_DIR_QUERIES);
    }

    public String validate() {
        try {
            System.out.println(MyUtil.getFormateDate() + "verifying P-VCD configuration.");
            createUserDataDir();
            if (!this.userDataPath.exists() || !this.userDataPath.isDirectory()) {
                return "Cannot create user path \"" + this.userDataPath + "\".";
            }
            // tests pvcd, wget
            String msg = ProcessTest.validateAll();
            if (msg != null)
                return msg;
            // VLC_BIN
            if (this.vlcExePath == null || this.vlcExePath.length() == 0)
                return "Invalid VideoLAN command \"" + this.vlcExePath + "\".";
            // VLC_LIB
            if (this.libVlcPath == null || this.libVlcPath.length() == 0)
                return "Invalid VideoLAN libvlc path";
            if (!new File(this.libVlcPath).exists())
                return "VideoLAN libvlc file \"" + this.libVlcPath + "\" does not exist.";
            InternalVlcViewerDialog.initLibVlc();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }
    }

    public int getSystemBitness() {
        return systemBitness;
    }

    public File getUserDataPath() {
        return userDataPath;
    }

    public File getDatabasesPath() {
        return databasesPath;
    }

    public File getQueriesPath() {
        return queriesPath;
    }

    public File getSearchesPath() {
        return searchesPath;
    }

    public File getDownloadsPath() {
        return downloadsPath;
    }

    public String getPvcdPath() {
        return pvcdPath;
    }

    public int getPvcdMaxCores() {
        return pvcdMaxCores;
    }

    public String getVlcExePath() {
        return vlcExePath;
    }

    public String getLibVlcPath() {
        return libVlcPath;
    }

    public String getWgetExePath() {
        return wgetExePath;
    }

    public String getYoutubedlExePath() {
        return youtubedlExePath;
    }

    public String[] getVideoExtensions() {
        return videoExtensions;
    }

    public String[] getImageExtensions() {
        return imageExtensions;
    }

    public String[] getImageAndVideoExtensions() {
        return imageAndVideoExtensions;
    }

    public File getDefaultDir_DatabaseFiles() {
        return defaultDir_DatabaseFiles;
    }

    public File getDefaultDir_QuerySelection() {
        return defaultDir_QuerySelection;
    }

    public String getSystemVersionName() {
        return systemVersionName;
    }

    public String getSystemExeExtension() {
        return systemExeExtension;
    }

    public String getSystemLibExtension() {
        return systemLibExtension;
    }

    public void updateUserDataPath(String val) {
        this.userDataPath = new File(val);
        MyPreferences.setParameterFile(KEY_USERDATA, this.userDataPath);
    }

    public void updatePvcdPath(String val) {
        this.pvcdPath = val;
        MyPreferences.setParameter(KEY_PVCD, val);
    }

    public void updatePvcdMaxCores(int val) {
        this.pvcdMaxCores = val;
        MyPreferences.setParameter(KEY_MAXCORES, "" + val);
    }

    public void updateVlcExePath(String val) {
        this.vlcExePath = val;
        MyPreferences.setParameter(KEY_VLCBIN, val);
    }

    public void updateLibVlcPath(String val) {
        this.libVlcPath = val;
        MyPreferences.setParameter(KEY_VLCLIB_BITNESS + this.systemBitness, val);
    }

    public void updateWgetExePath(String val) {
        this.wgetExePath = val;
        MyPreferences.setParameter(KEY_WGET, val);
    }

    public void updateYoutubedlExePath(String val) {
        this.youtubedlExePath = val;
        MyPreferences.setParameter(KEY_YOUTUBEDL, val);
    }

    public void updateDefaultDir_DatabaseFiles(File dir) {
        this.defaultDir_DatabaseFiles = dir;
        MyPreferences.setParameterFile(KEY_DIR_DBS, dir);
    }

    public void updateDefaultDir_QuerySelection(File dir) {
        this.defaultDir_QuerySelection = dir;
        MyPreferences.setParameterFile(KEY_DIR_QUERIES, dir);
    }

    private void setGlobalLogFile(File logFile) throws Exception {
        // no changes
        if ((this.log_file == null && logFile == null) || (this.log_file != null && logFile != null
                && this.log_file.getAbsolutePath().equals(logFile.getAbsolutePath())))
            return;
        // closing old file
        if (this.log_output != null) {
            this.log_output.close();
            this.log_output = null;
            this.log_file = null;
        }
        // redirecting standard output
        if (logFile != null) {
            this.log_file = logFile;
            this.log_output = new PrintStream(new FileOutputStream(this.log_file, true), true);
            System.setOut(this.log_output);
            System.setErr(this.log_output);
        }
    }

}