⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertymanager.java

📁 这是学习Java必须读懂两套源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (manager == null) {
            synchronized(managerLock) {
                if (manager == null) {
                    manager = new PropertyManager(propsName);
                }
            }
        }
        return manager.propFileIsWritable();
    }

    /**
     * Returns true if the yazd.properties file exists where the path property
     * purports that it does.
     */
    public static boolean propertyFileExists() {
        if (manager == null) {
            synchronized(managerLock) {
                if (manager == null) {
                    manager = new PropertyManager(propsName);
                }
            }
        }
        return manager.propFileExists();
    }

    /**
     * Returns the version number of Yazd as a String. i.e. -- major.minor.revision
     */
    public static String getYazdVersion() {
        return MAJOR_VERSION + "." + MINOR_VERSION + "." + REVISION_VERSION;
    }

    /**
     * Returns the major version number of Yazd. i.e. -- 1.x.x
     */
    public static int getYazdVersionMajor() {
        return MAJOR_VERSION;
    }

    /**
     * Returns the minor version number of Yazd. i.e. -- x.1.x
     */
    public static int getYazdVersionMinor() {
        return MINOR_VERSION;
    }

    /**
     * Returns the revision version number of Yazd. i.e. -- x.x.1
     */
    public static int getYazdVersionRevision() {
        return REVISION_VERSION;
    }

    private Properties properties = null;
    private Object propertiesLock = new Object();
    private String resourceURI;

    /**
     * Creates a new PropertyManager. Singleton access only.
     */
    private PropertyManager(String resourceURI) {
        this.resourceURI = resourceURI;
    }

    /**
     * Gets a Yazd property. Yazd properties are stored in yazd.properties.
     * The properties file should be accesible from the classpath. Additionally,
     * it should have a path field that gives the full path to where the
     * file is located. Getting properties is a fast operation.
     *
     * @param name the name of the property to get.
     * @return the property specified by name.
     */
    protected String getProp(String name) {
        //If properties aren't loaded yet. We also need to make this thread
        //safe, so synchronize...
        if (properties == null) {
            synchronized(propertiesLock) {
                //Need an additional check
                if (properties == null) {
                    loadProps();
                }
            }
        }
        String property = properties.getProperty(name);
        if (property == null) {
            return null;
        }
        else {
            return property.trim();
        }
    }

    /**
     * Sets a Yazd property. Because the properties must be saved to disk
     * every time a property is set, property setting is relatively slow.
     */
    protected void setProp(String name, String value) {
        //Only one thread should be writing to the file system at once.
        synchronized (propertiesLock) {
            //Create the properties object if necessary.
            if (properties == null) {
                loadProps();
            }
            properties.setProperty(name, value);
            saveProps();
        }
    }

    protected void deleteProp(String name) {
        //Only one thread should be writing to the file system at once.
        synchronized (propertiesLock) {
            //Create the properties object if necessary.
            if (properties == null) {
                loadProps();
            }
            properties.remove(name);
            saveProps();
        }
    }

    protected Enumeration propNames() {
        //If properties aren't loaded yet. We also need to make this thread
        //safe, so synchronize...
        if (properties == null) {
            synchronized(propertiesLock) {
                //Need an additional check
                if (properties == null) {
                    loadProps();
                }
            }
        }
        return properties.propertyNames();
    }

    /**
     * Loads Yazd properties from the disk.
     */
    private void loadProps() {
        properties = new Properties();
        InputStream in = null;
        try {
            in = getClass().getResourceAsStream(resourceURI);
            properties.load(in);
        }
        catch (Exception e) {
            System.err.println("Error reading Yazd properties in PropertyManager.loadProps() " + e);
            e.printStackTrace();
        }
        finally {
            try {
                in.close();
            } catch (Exception e) { }
        }
    }

    /**
     * Saves Yazd properties to disk.
     */
    private void saveProps() {
        //Now, save the properties to disk. In order for this to work, the user
        //needs to have set the path field in the properties file. Trim
        //the String to make sure there are no extra spaces.
        String path = properties.getProperty("path").trim();
        OutputStream out = null;
        try {
            out = new FileOutputStream(path);
            properties.store(out, "yazd.properties -- " + (new java.util.Date()));
        }
        catch (Exception ioe) {
            System.err.println("There was an error writing yazd.properties to " + path + ". " +
                "Ensure that the path exists and that the Yazd process has permission " +
                "to write to it -- " + ioe);
            ioe.printStackTrace();
        }
        finally {
            try {
               out.close();
            } catch (Exception e) { }
        }
    }

    /**
     * Returns true if the properties are readable. This method is mainly
     * valuable at setup time to ensure that the properties file is setup
     * correctly.
     */
    public boolean propFileIsReadable() {
        try {
            InputStream in = getClass().getResourceAsStream(resourceURI);
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }

    /**
     * Returns true if the yazd.properties file exists where the path property
     * purports that it does.
     */
    public boolean propFileExists() {
        String path = getProp("path");
        if( path == null ) {
            return false;
        }
        File file = new File(path);
        if (file.isFile()) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * Returns true if the properties are writable. This method is mainly
     * valuable at setup time to ensure that the properties file is setup
     * correctly.
     */
    public boolean propFileIsWritable() {
        String path = getProp("path");
        File file = new File(path);
        if (file.isFile()) {
            //See if we can write to the file
            if (file.canWrite()) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -