java使用 org.apache.commons.conf…

所需的组件:

commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-lang-2.5.jar
xml-apis-1.0.b2.jar
commons-logging-1.1.1.jar

 

配置文件示例:

<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : config.xml
    Description:
        全文索引服务配置文件.
-->

<root>
    <!-- 身份验证服务的WebService访问地址,注意,该地址必须以 ?wsdl 结束 -->
    <!-- 例如:http://localhost:8088/AuthenticationWebService/services/AuthenticationService?wsdl -->
    <AuthenticationWebServiceURL>http://localhost:8088/AuthenticationWebService/services/AuthenticationService?wsdl</AuthenticationWebServiceURL>
</root>

 

java代码示例:


    public static boolean readAuthenticationWebServiceURL(StringBuilder pURL, ErrInfo pErrInfo)
    {
        boolean pFlag = true;
        int pErrPos = 0;
        Throwable throwable = new Throwable();

        try
        {
            //指定配置文件路径
            String configFilePath="config.xml";

            //判断配置文件是否存在
            File configFile = new File(configFilePath.trim());
            if (configFile.exists()==false)
            {
                pFlag = false;
                pErrInfo.getContent().append("系统配置文件不存在: "+configFilePath.trim());
            }

            //开始读取配置文件
            if (pFlag)
            {
                String value=null;//读取的配置值

                //装载配置文件
                XMLConfiguration xmlConfiguration=new XMLConfiguration(configFilePath.trim());

                //读取身份验证服务的WebService地址
                String serverName=null;
                value=xmlConfiguration.getString("AuthenticationWebServiceURL");
                if (value.trim().length()==0)
                {
                    pFlag = false;
                    pErrInfo.getContent().append("身份验证服务的WebService地址未配置。");
                }
                else
                {
                    pURL.append(value.trim());
                }

            }
        }
        catch (Exception e)
        {
            //异常错误
            pFlag = false;
            pErrInfo.getContent().append(e.toString());
            pErrInfo.setException(e);
        }
        finally
        {
            //拼接详细的错误描述信息,包括类名/方法名/错误位置
            if (pFlag == false && pErrInfo.getContent().length() > 0)
            {
                StackTraceElement[] stackTraceElements = throwable.getStackTrace();
                StringBuilder tempBuilder = new StringBuilder(stackTraceElements[0].getClassName());
                tempBuilder.append(".");
                tempBuilder.append(stackTraceElements[0].getMethodName());
                tempBuilder.append("-->");

                //如果属于异常错误,则需要在错误信息中加入错误位置标记信息
                if (pErrInfo.getException() != null)
                {
                    tempBuilder.append(" ErrPos: ");
                    tempBuilder.append(pErrPos);
                    tempBuilder.append(", ");
                }

                pErrInfo.getContent().insert(0, tempBuilder.toString());
                tempBuilder = null;
            }

            //销毁局部变量
            throwable = null;
        }

        return pFlag;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiamiaogogogo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值