• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

一个连接池的例子(来自JIVE)(6)

servlet/jsp 搞代码 7年前 (2018-06-18) 231次浏览 已收录 0个评论

//文件:PropertyManager.java

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

import java.util.*;

http://www.gaodaima.com/41653.html一个连接池的例子(来自JIVE)(6)

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 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.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";
}

欢迎大家阅读《一个连接池的例子(来自JIVE)(6)》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:一个连接池的例子(来自JIVE)(6)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址