Example of a connection pool (from Jive) (6)

Source: Internet
Author: User
Tags file system readable thread trim
File: Propertymanager.java

This class is actually useless and can be removed, but you need to remove the reference to the class in the previous classes.
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 is 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 would move to XML. Xml
* Properties would allow hierarchical property structures which may mean the
* API of this class would have to the change.
* <p>
* Jive properties are only meant to is set and retrevied by core Jive classes.
* Therefore, skin writers should probably ignore this class.
* <p>
* This class are implemented as a singleton since many classloaders seem to
* Take issue with doing classpath resource loading to 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 is mainly
* Valuable at setup time to ensure so 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 is mainly
* Valuable at setup time to ensure so 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 is 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 is set, the property setting is relatively slow.
*/
public void SetProp (string name, String value) {
Only one thread should is 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 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 a error writing jive.properties to" + Path + "." +
"Ensure that's 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 is mainly
* Valuable at setup time to ensure so 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 is mainly
* Valuable at setup time to ensure so the properties file is setup
* correctly.
*/
public Boolean propfileiswritable () {
String Path = Getprop ("path");
File File = new file (path);
if (File.isfile ()) {
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";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.