java使用 org.apache.commons.conf…

来源:互联网 发布:蒙泰怎么设置端口 编辑:程序博客网 时间:2024/06/02 06:57

所需的组件:

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 booleanreadAuthenticationWebServiceURL(StringBuilder pURL, ErrInfopErrInfo)
    {
        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=newXMLConfiguration(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 (Exceptione)
        {
           //异常错误
           pFlag = false;
           pErrInfo.getContent().append(e.toString());
           pErrInfo.setException(e);
        }
        finally
        {
           //拼接详细的错误描述信息,包括类名/方法名/错误位置
           if (pFlag == false &&pErrInfo.getContent().length() > 0)
           {
               StackTraceElement[] stackTraceElements =throwable.getStackTrace();
               StringBuilder tempBuilder = newStringBuilder(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;
    }

原创粉丝点击