仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1098|回复: 19
打印 上一主题 下一主题

[学习教程] JAVA网站制作之一个毗连池的例子(来自JIVE)(6)

[复制链接]
不帅 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:39:27 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
你说是sun公司对她研究的透还是微软?针对自己工具开发的.net性能上肯定会站上风的。//文件:PropertyManager.java

//这个类实在没甚么用了,能够往失落,但必要往失落后面几个类中对这个类的援用。
packagecom.qingtuo.db.pool;

importjava.util.*;
importjava.io.*;

/**
*ManagespropertiesfortheentireJivesystem.Propertiesaremerely
*piecesofinformationthatneedtobesavedinbetweenserverrestarts.
*<p>
*Atthemoment,propertiesarestoredinaJavaPropertiesfile.Inaversion
*ofJivecomingsoon,thepropertiesfileformatwillmovetoXML.XML
*propertieswillallowhierarchicalpropertystructureswhichmaymeanthe
*APIofthisclasswillhavetochange.
*<p>
*JivepropertiesareonlymeanttobesetandretreviedbycoreJiveclasses.
*Therefore,skinwritersshouldprobablyignorethisclass.
*<p>
*Thisclassisimplementedasasingletonsincemanyclassloadersseemto
*takeissuewithdoingclasspathresourceloadingfromastaticcontext.
*/
publicclassPropertyManager{

privatestaticPropertyManagermanager=null;
privatestaticObjectmanagerLock=newObject();
privatestaticStringpropsName="/pcc_2000.properties";

/**
*ReturnsaJiveproperty
*
*@paramnamethenameofthepropertytoreturn.
*@returnsthepropertyvaluespecifiedbyname.
*/
publicstaticStringgetProperty(Stringname){
if(manager==null){
synchronized(managerLock){
if(manager==null){
Stringsysname=System.getProperty("os.name").toUpperCase();
if(sysname.indexOf("WIN")!=-1){
propsName=propsName2000;
}
else{
propsName=propsNameLinux;
}
manager=newPropertyManager(propsName);
}
}
}
returnmanager.getProp(name);
}

/**
*SetsaJiveproperty.
*
*@paramnamethenameofthepropertybeingset.
*@paramvaluethevalueofthepropertybeingset.
*/
publicstaticvoidsetProperty(Stringname,Stringvalue){
if(manager==null){
synchronized(managerLock){
if(manager==null){
manager=newPropertyManager(propsName);
}
}
}
manager.setProp(name,value);
}

/**
*Returnstrueifthepropertiesarereadable.Thismethodismainly
*valuableatsetuptimetoensurethatthepropertiesfileissetup
*correctly.
*/
publicstaticbooleanpropertyFileIsReadable(){
if(manager==null){
synchronized(managerLock){
if(manager==null){
manager=newPropertyManager(propsName);
}
}
}
returnmanager.propFileIsReadable();
}

/**
*Returnstrueifthepropertiesarewritable.Thismethodismainly
*valuableatsetuptimetoensurethatthepropertiesfileissetup
*correctly.
*/
publicstaticbooleanpropertyFileIsWritable(){
if(manager==null){
synchronized(managerLock){
if(manager==null){
manager=newPropertyManager(propsName);
}
}
}
returnmanager.propFileIsWritable();
}

/**
*Returnstrueifthejive.propertiesfileexistswherethepathproperty
*purportsthatitdoes.
*/
publicstaticbooleanpropertyFileExists(){
if(manager==null){
synchronized(managerLock){
if(manager==null){
manager=newPropertyManager(propsName);
}
}
}
returnmanager.propFileExists();
}

privatePropertiesproperties=null;
privateObjectpropertiesLock=newObject();
privateStringresourceURI;

/**
*Singletonaccessonly.
*/
privatePropertyManager(StringresourceURI){
this.resourceURI=resourceURI;
}

/**
*GetsaJiveproperty.Jivepropertiesarestoredinjive.properties.
*Thepropertiesfileshouldbeaccesiblefromtheclasspath.Additionally,
*itshouldhaveapathfieldthatgivesthefullpathtowherethe
*fileislocated.Gettingpropertiesisafastoperation.
*/
publicStringgetProp(Stringname){
//Ifpropertiesarentloadedyet.Wealsoneedtomakethisthread
//safe,sosynchronize...
if(properties==null){
synchronized(propertiesLock){
//Needanadditionalcheck
if(properties==null){
loadProps();
}
}
}
returnproperties.getProperty(name);
}

/**
*SetsaJiveproperty.Becausethepropertiesmustbesavedtodisk
*everytimeapropertyisset,propertysettingisrelativelyslow.
*/
publicvoidsetProp(Stringname,Stringvalue){
//Onlyonethreadshouldbewritingtothefilesystematonce.
synchronized(propertiesLock){
//Createthepropertiesobjectifnecessary.
if(properties==null){
loadProps();
}
properties.setProperty(name,value);
//Now,savethepropertiestodisk.Inorderforthistowork,theuser
//needstohavesetthepathfieldinthepropertiesfile.Trim
//theStringtomakesuretherearenoextraspaces.
Stringpath=properties.getProperty("path").trim();
OutputStreamout=null;
try{
out=newFileOutputStream(path);
properties.store(out,"jive.properties--"+(newjava.util.Date()));
}
catch(Exceptionioe){
System.err.println("Therewasanerrorwritingjive.propertiesto"+path+"."+
"EnsurethatthepathexistsandthattheJiveprocesshaspermission"+
"towritetoit--"+ioe);
ioe.printStackTrace();
}
finally{
try{
out.close();
}catch(Exceptione){}
}
}
}

/**
*LoadsJivepropertiesfromthedisk.
*/
privatevoidloadProps(){
properties=newProperties();
InputStreamin=null;
try{
in=getClass().getResourceAsStream(resourceURI);
properties.load(in);
}
catch(IOExceptionioe){
System.err.println("ErrorreadingPccproperties"+ioe);
ioe.printStackTrace();
}
finally{
try{
in.close();
}catch(Exceptione){}
}
}

/**
*Returnstrueifthepropertiesarereadable.Thismethodismainly
*valuableatsetuptimetoensurethatthepropertiesfileissetup
*correctly.
*/
publicbooleanpropFileIsReadable(){
try{
InputStreamin=getClass().getResourceAsStream(resourceURI);
returntrue;
}
catch(Exceptione){
returnfalse;
}
}

/**
*Returnstrueifthejive.propertiesfileexistswherethepathproperty
*purportsthatitdoes.
*/
publicbooleanpropFileExists(){
Stringpath=getProp("path");
Filefile=newFile(path);
if(file.isFile()){
returntrue;
}
else{
returnfalse;
}
}

/**
*Returnstrueifthepropertiesarewritable.Thismethodismainly
*valuableatsetuptimetoensurethatthepropertiesfileissetup
*correctly.
*/
publicbooleanpropFileIsWritable(){
Stringpath=getProp("path");
Filefile=newFile(path);
if(file.isFile()){
//Seeifwecanwritetothefile
if(file.canWrite()){
returntrue;
}
else{
returnfalse;
}
}
else{
returnfalse;
}
}

privatestaticStringpropsName2000="/pcc_2000.properties";
privatestaticStringpropsNameLinux="/pcc_linux.properties";
}
大型的应用一般不会用这些框架(因为性能考虑);开发人员根据需要选择用一些框架,也可以不选用框架;不用框架并不代表要自己写框架;修改框架的可能性更小。
不帅 该用户已被删除
沙发
 楼主| 发表于 2015-2-26 20:44:05 | 显示全部楼层
当然你也可以参加一些开源项目,一方面可以提高自己,另一方面也是为中国软件事业做贡献嘛!开发者在互联网上用CVS合作开发,用QQ,MSN,E-mail讨论联系,天南海北的程序员分散在各地却同时开发同一个软件,是不是很有意思呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-20 07:46

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表