欢迎光临
我们一直在努力

一个连接池的例子(来自JIVE)(6)-JSP教程,数据库相关

建站超值云服务器,限时71元/月

//文件:propertymanager.java

//这个类其实没什么用了,可以去掉,但需要去掉前面几个类中对这个类的引用。
package com.qingtuo.db.pool;

import java.util.*;
import java.io.*;

/**
* manages properties for the entire jive system. properties are merely
* pieces of information that need to be saved in between server restarts.
* <p>
* at the moment, properties are stored in a java properties file. in a version
* of jive coming soon, the properties file format will move to xml. xml
* properties will allow hierarchical property structures which may mean the
* api of this class will have to change.
* <p>
* jive properties are only meant to be set and retrevied by core jive classes.
* therefore, skin writers should probably ignore this class.
* <p>
* this class is implemented as a singleton since many classloaders seem to
* take issue with doing classpath resource loading from a static context.
*/
public class propertymanager {

    private static propertymanager manager = null;
    private static object managerlock = new object();
    private static string propsname = "/pcc_2000.properties";

    /**
     * returns a jive property
     *
     * @param name the name of the property to return.
     * @returns the property value specified by name.
     */
    public static string getproperty(string name) {
        if (manager == null) {
            synchronized(managerlock) {
                if (manager == null) {
                    string sysname=system.getproperty("os.name").touppercase();
                    if(sysname.indexof("win")!=-1){
                        propsname=propsname2000;
                    }
                    else{
                        propsname=propsnamelinux;
                    }
                    manager = new propertymanager(propsname);
                }
            }
        }
        return manager.getprop(name);
    }

    /**
     * sets a jive property.
     *
     * @param name the name of the property being set.
     * @param value the value of the property being set.
     */
    public static void setproperty(string name, string value) {
        if (manager == null) {
            synchronized(managerlock) {
                if (manager == null) {
                    manager = new propertymanager(propsname);
                }
            }
        }
        manager.setprop(name, value);
    }

    /**
     * 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 static boolean propertyfileisreadable() {
        if (manager == null) {
            synchronized(managerlock) {
                if (manager == null) {
                    manager = new propertymanager(propsname);
                }
            }
        }
        return manager.propfileisreadable();
    }

    /**
     * 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 static boolean propertyfileiswritable() {
        if (manager == null) {
            synchronized(managerlock) {
                if (manager == null) {
                    manager = new propertymanager(propsname);
                }
            }
        }
        return manager.propfileiswritable();
    }

    /**
     * returns true if the jive.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();
    }

    private properties properties = null;
    private object propertieslock = new object();
    private string resourceuri;

    /**
     * singleton access only.
     */
    private propertymanager(string resourceuri) {
        this.resourceuri = resourceuri;
    }
   
    /**
     * gets a jive property. jive properties are stored in jive.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.
     */
    public string getprop(string name) {
        //if properties arent 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.getproperty(name);
    }

    /**
     * sets a jive property. because the properties must be saved to disk
     * every time a property is set, property setting is relatively slow.
     */
    public 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);
            //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, "jive.properties — " + (new java.util.date()));
            }
            catch (exception ioe) {
                system.err.println("there was an error writing jive.properties to " + path + ". " +
                    "ensure that the path exists and that the jive process has permission " +
                    "to write to it — " + ioe);
                ioe.printstacktrace();
            }
            finally {
                try {
                    out.close();
                } catch (exception e) { }
            }
        }
    }

    /**
     * loads jive properties from the disk.
     */
    private void loadprops() {
        properties = new properties();
        inputstream in = null;
        try {
            in = getclass().getresourceasstream(resourceuri);
            properties.load(in);
        }
        catch (ioexception ioe) {
            system.err.println("error reading pcc properties" + ioe);
            ioe.printstacktrace();
        }
        finally {
            try {
                in.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 jive.properties file exists where the path property
     * purports that it does.
     */
    public boolean propfileexists() {
        string path = getprop("path");
        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;
        }
    }

    private static string propsname2000 = "/pcc_2000.properties";
    private static string propsnamelinux = "/pcc_linux.properties";
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个连接池的例子(来自JIVE)(6)-JSP教程,数据库相关
分享到: 更多 (0)

相关推荐

  • 暂无文章