需求及背景1:在Web|Service中通常会配置自己的服务地址,加入某一个配置文件中配置了一个地址,那怎么读取这个地址。
例如:文件wsaddress.properites下的服务配置地址:address=http://127.0.0.1:8080/realware/services/AsspFinanceService,500000,2015
业务逻辑中需要得到地址:String address = WSClientHander.getDefaultAddress();
getDefaultAddress()实现:
private static String getDefaultAddress() { String str = null; if (str != null) return DEFAULT_ADDRESS; InputStream in = null; Properties prop = new Properties(); try { in = WSClientHander.class .getResourceAsStream("/wsaddress.properites"); prop.load(in); } catch (Exception e) { throw new RuntimeException(WSClientHander.PBCS035, e); } finally { if (in != null) try { in.close(); } catch (IOException e) { throw new RuntimeException(WSClientHander.PBCS036, e); } } str = prop.getProperty("address"); String[] array = str.split(","); DEFAULT_ADDRESS = array[0]; admDivCode = array[1]; stYear = Integer.parseInt(array[2]); if (null == DEFAULT_ADDRESS || DEFAULT_ADDRESS.length() == 0) throw new RuntimeException(WSClientHander.PBCS031); return DEFAULT_ADDRESS; }
配置文件evoucher.conf
##################################################################################################### # evoucher.switch 电子凭证库开关 # '0'关闭,'1'打开 ##################################################################################################### #签名服务开关,非调试不可关闭 evoucher.switch.SignService=1 #消息中间件开关,非调试不可关闭 evoucher.switch.MessageService=1 #印章服务器开关,非调试不可关闭 evoucher.switch.EStampService=1 #资金凭证双证书校验开关 #发送资金凭证时,需检验是否包含两个不同签名、签章证书 evoucher.switch.EStampFund=0 #接收验证开关 #接收外部凭证时,检验凭证对应的签名、签章 evoucher.switch.ReceiveVerfy=1 #回调接口开关 #打开此接口回调自动任务将启动,若业务系统未开发回调接口,开关应关闭 evoucher.switch.CallBackFuction=1 #业务报文检查开关 #检查凭证原文字段与模板配置的字段是否一致以及字段值是否合法,联调开启,上线应关闭 evoucher.switch.VoucherFieldCheck=0 #OCX是否弹出错误提示框 evoucher.switch.ErrMsgModeFlag=0 #绿色通道同步发送消息开关 #打开开关会接收同步发送的数据,并向业务系统请求数据,若业务系统没有此需求或未提供请求服务,开关应关闭 evoucher.switch.requestData=1 #绿色通道异步发送消息开关 #打开开关会接收异步发送的数据,若业务系统没有此需求,开关应关闭 evoucher.switch.sendData=1 #打开开关 当有异步数据发送过来时会向业务系统通知数据,若业务系统没有此需求或未提供通知服务,开关应关闭 evoucher.switch.sendData.CallBackFuncton=1 ##################################################################################################### # evoucher.database 数据库配置 ##################################################################################################### # 'ORACLE'ORACLE数据库;'POSTGRES'postgreSql数据库;'DB2'DB2数据库;'SYBASE'sybase数据库 evoucher.model.DataBaseService=ORACLE #设置数据库用户名 evoucher.database.user=ww #设置数据库密码 evoucher.database.password=ww #设置数据库ip evoucher.database.ip=192.168.1.241 #设置数据库端口 evoucher.database.port=1521 #数据库实例名oracle是实例名、postgres是数据库名、sybase是数据库名 evoucher.database.databasename=orcl ##################################################################################################### # evoucher.service.sign 签名服务配置 ###################################################################################################### # 'ZK'中科签名服务;'JD'吉大签名服务;'GEER'格尔签名服务;'JNKY'江南科友签名服务 evoucher.model.SignService=JD #—————————————————————————————————————————————————— # evoucher.service.sign.zk 中科签名服务配置 #—————————————————————————————————————————————————— #中科签名服务地址 evoucher.service.sign.zk.ip=192.168.1.250 #中科签名服务端口号 evoucher.service.sign.zk.port=8017 #中科签名证书 evoucher.service.sign.zk.signCertId=04 #中科数字信封加密证书 evoucher.service.sign.zk.envelopCertId=500000|011:05+500000|001:05+500000|102:05 #—————————————————————————————————————————————————— # evoucher.service.sign.jd 吉大签名服务配置 #—————————————————————————————————————————————————— #吉大签名服务地址,格式:http://地址:端口号/signserver/service/xml ServerURL=http://192.168.1.250:8000/signserver/service/xml #数字信封加密证书 #格式:行政区划|机构类型:证书ID AppID=500000|011:koal_env+500000|001:koal_env+500000|102:koal_env #吉大签名服务器必须设置对应的签名证书 SignCertID=500000:04+500000:koal_sign+500000:01010001 #以下参数为吉大签名服务器默认配置,不可修改 #以下参数为吉大签名服务器默认配置,不可修改 DigestAlg=SHA1 #des3/scb2 EncAlg=scb2 CertBaseInfo = version;issuerdn;subjectdn CertExtendInfo = CertAlias = BaseInfo =digestalg;plaindata SendMsgFormat=0 HashActionPosition=0 PrintLog=false Compatible=true #—————————————————————————————————————————————————— # evoucher.service.sign.geer 格尔签名服务配置 #—————————————————————————————————————————————————— #格尔签名服务地址 estamp.sign.geEr.ip=192.168.1.249 #格尔签名服务端口 estamp.sign.geEr.port=6000 #格尔签名服务超时时间 estamp.sign.geEr.timeOut=10000 #格尔签名服务证书 estamp.sign.geEr.certDN=koal_sign #格尔数字信封加密证书 #格式:行政区划|机构类型:证书ID geErAppID=500000|MOF:koal_env+500000|PBC:koal_env+500000|102:koal_env #—————————————————————————————————————————————————— # evoucher.service.sign.jnky 江南科友签名 服务配置 #—————————————————————————————————————————————————— #江南科友签名服务地址 estamp.sign.jnky.ipAddr=192.168.1.135 #江南科友签名服务端口 estamp.sign.jnky.port=20102 #江南科友签名服务证书 estamp.sign.jnky.certDN=01010001 #江南科友数字信封加密证书 estamp.sign.jnky.jnkyAppID=500000|MOF:01010002+500000|PBC:01010002+500000|102:01010002 ##################################################################################################### # evoucher.service.msg 消息中间件服务配置 ###################################################################################################### # 'MQ'MQ消息中间件;'TLQ'TLQ消息中间件 evoucher.model.MessageService=MQ #—————————————————————————————————————————————————— # evoucher.service.msg.mq MQ消息中间件服务配置 #—————————————————————————————————————————————————— #MQ服务地址 evoucher.service.msg.mq.HostName=127.0.0.1 #MQ监听端口号 evoucher.service.msg.mq.Port=6001 #MQ队列管理器名称 evoucher.service.msg.mq.QMgr=wfc1 #MQ服务器连接通道名称 evoucher.service.msg.mq.Channel=service #MQ字符集 #一般Unix、Linux平台中MQ默认的字符集为819,而Windows平台为1381 evoucher.service.msg.mq.CCSID=1381 #—————————————————————————————————————————————————— # evoucher.service.msg.tlq TLQ消息中间件服务配置 #—————————————————————————————————————————————————— #TLQ服务地址 evoucher.service.msg.tlq.HostName=192.168.1.38 #TLQ的JMS代理端口号 evoucher.service.msg.tlq.JMSPort=10024 ##################################################################################################### # evoucher.service.estamp 印章服务配置 ###################################################################################################### # 'ZK'中科印章服务;'JD'吉大电子印章服务 evoucher.model.EStampService=ZK #—————————————————————————————————————————————————— # evoucher.service.estamp.zk 中科印章服务配置 #—————————————————————————————————————————————————— #中科印章服务地址及端口,格式:http://地址:端口号/realware/services/AsspEStampService evoucher.service.estamp.zk.address=http://192.168.1.42:8001/realware/services/AsspEStampService #—————————————————————————————————————————————————— # evoucher.service.estamp.jd 吉大印章服务配置 #—————————————————————————————————————————————————— #吉大印章服务地址及端口,格式:http://地址:端口号 evoucher.service.estamp.jd.address=http://192.168.1.250:8000 ##################################################################################################### # evoucher.service.business.system 业务系统服务配置 ##################################################################################################### #————————————————————————————————————————————————————————————————————————— # evoucher.service.callback 回调业务系统服务地址及端口(voucherArrivedNotify) #————————————————————————————————————————————————————————————————————————— #格式:区划1&凭证类型1$凭证类型2&服务地址2||区划2&凭证类型1$凭证类型2&服务地址2 #不设置凭证类型表示此区划全部凭证类型均对应此地址,如:区划2&&服务地址1|区划2&&服务地址2 表示此区划的全部凭证类型 #服务地址格式:http://地址:端口号/realware/service #evoucher.service.callback.address=500000&&http://192.168.1.179:8090/realware_callback/services/TestService evoucher.service.callback.address=500000&1102&http://127.0.0.1:8090/realwareCallback_signTest/services/TestService #————————————————————————————————————————————————————————————————————————— # evoucher.service.requestData 数据请求、通知服务地址及端口 #————————————————————————————————————————————————————————————————————————— #服务地址格式:区划1&http://地址1:端口号1/realware/service1+区划2&http://地址2:端口号2/realware/service2 evoucher.service.requestData.address=500000&http://192.168.1.200:8080/realware_callback/services/TestService #################################################################################################### # evoucher.properties 凭证库运行参数配置 #################################################################################################### #自动解析线程间歇时间 #既待解析的接收凭证为空时下一次轮询等待时间,参数值为整型,精确到毫秒级 evoucher.model.ParsePause=5000 #凭证到达通知线程数(回调接口) evoucher.service.callback.thread.count=20 #接收凭证消息线程数 ,默认5个 evoucher.download.thread.count=5 #数据请求超时时间配置(单位:毫秒)(开发编写,其他人员不建议修改) evoucher.requestData.receiveTimeout=240000 #数据请求消息失效时间 (单位:毫秒)(开发编写,其他人员不建议修改) evoucher.requestData.timeToLive=40000 #接收数据消息线程数(绿色通道异步数据发送) evoucher.sendData.onMessage.concurrentConsumers=1 #原文被篡改错误编码配置(开发编写,其他人员不建议修改) evoucher.verfiySignStamp.code=50041,10702005,100100
怎么从配置文件中获取需要的配置
1)导入配置文件获得配置文件的实例evoucherConfPro 2)通过HashMap存储好我们需要的配置的Key,写死的,我们只需要些配置 3)通过getEVConfValue获得需要的配置
public class PropertiesHander { private static final Logger logger = Logger .getLogger(PropertiesHander.class); private static HashMap<String, String> File_MAPPINGS = new HashMap<String, String>(); private static final Properties evoucherConfPro = PropertiesHander .getProByCustomPath("/", BIZConstant.FILE_NAME_EVOUCHER_CONF); static { // 注册类字段与配置字段的映射关系 File_MAPPINGS.put("M_SIGN_SERVICE", "evoucher.model.SignService"); File_MAPPINGS.put("M_MESSAGE_SERVICE", "evoucher.model.MessageService"); File_MAPPINGS.put("M_ESTAMP_SERVICE", "evoucher.model.EStampService"); File_MAPPINGS.put("M_DATABASE_SERVICE", "evoucher.model.DataBaseService"); File_MAPPINGS.put("M_PARSE_PAUSE", "evoucher.model.ParsePause"); File_MAPPINGS.put("M_CALL_BACK_PARAMETER", "evoucher.service.callback.address"); File_MAPPINGS.put("M_REQUEST_NOTIFY", "evoucher.service.requestData.address"); File_MAPPINGS.put("M_CALL_BACK_THREAD_COUNT", "evoucher.service.callback.thread.count"); File_MAPPINGS.put("S_SIGN_SERVICE", "evoucher.switch.SignService"); File_MAPPINGS .put("S_MESSAGE_SERVICE", "evoucher.switch.MessageService"); File_MAPPINGS.put("S_ESTAMP_SERVICE", "evoucher.switch.EStampService"); File_MAPPINGS.put("S_ESTAMP_ALLSIGN", "evoucher.switch.EStampAllSign"); File_MAPPINGS.put("S_ESTAMP_FUND", "evoucher.switch.EStampFund"); File_MAPPINGS.put("S_RECEIVE_VIERFY", "evoucher.switch.ReceiveVerfy"); File_MAPPINGS.put("S_READ_IS_RETURN", "evoucher.switch.IsReturn"); File_MAPPINGS.put("S_CHECK_IS", "evoucher.switch.VoucherFieldCheck"); File_MAPPINGS.put("S_CALL_BACK_FUNCTION", "evoucher.switch.CallBackFuction"); File_MAPPINGS.put("S_ERROR_MSG_MODEL_FLAG", "evoucher.switch.ErrMsgModeFlag"); File_MAPPINGS.put("S_RECEIVE_MSG_REQUESTDATA_FLAG", "evoucher.switch.requestData"); File_MAPPINGS.put("S_RECEIVE_MSG_SENDDATA_FLAG", "evoucher.switch.sendData"); File_MAPPINGS.put("S_RECEIVE_MSG_SENDDATA_CALLBACK_FLAG", "evoucher.switch.sendData.CallBackFuncton"); /** 凭证库服务版本号 */ File_MAPPINGS.put("EVOUCHER_SERVICE_VERSION", "evoucher.service.version"); //原文篡改编码 File_MAPPINGS.put("M_EVOUCHER_VERFIY_SIGN_CODE", "evoucher.verfiySignStamp.code"); } public static String getEVConfValue(String key) { if(File_MAPPINGS.containsKey(key)) key=File_MAPPINGS.get(key); return evoucherConfPro.getProperty(key); } /** * 获取自定义包路径的配置文件 * * @param customPath * 自定义路径 * @param fileName * 文件名 * @return */ public static Properties getProByCustomPath(String customPath, String fileName) { InputStream in = null; Properties prop = new Properties(); try { in = PropertiesHander.class.getResourceAsStream(customPath + fileName); prop.load(in); } catch (Exception e) { throw new EVoucherException(ExceptionConstant.EVS601 + "获取文件[" + fileName + "]失败", e); } finally { if (in != null) try { in.close(); } catch (IOException e) { logger.warn("关闭流失败", e); } } return prop; } }
第三种情况:对于一些配置文件可以直接放在代码里面,例如:Boolean型的【可以作为开关】,设置其变量状态,然后初始化,在业务逻辑代码中去取【这样就实现了配置的作用】如下:
public class ControlParamsHandler { /** 签名服务器开关-:[true]打开 [false]关闭 */ private static boolean S_SIGN_SERVICE = false; /** 签名服务器---:[JD]吉大[ZK]中科 */ private static String M_SIGN_SERVICE = null; /** 消息中间件开关-:[true]打开 [false]关闭 */ private static boolean S_MESSAGE_SERVICE = false; /** 消息中间件服务-:[TLQ] TLQ [MQ]MQ MQ */ private static String M_MESSAGE_SERVICE = null; /** 印章服务器开关-:[true]打开 [false]关闭 */ private static boolean S_ESTAMP_SERVICE = false; /** 印章服务器-:[ZK]中科[JD]吉大 */ private static String M_ESTAMP_SERVICE = null; /** 自动解析线程间歇时间 既待解析的接收凭证为空时下一次轮询等待时间:int型毫秒级 */ private static int M_PARSE_PAUSE = 0; /** 资金凭证发送校验开关:[OPEN]打开 [CLOSE]关闭 发送时需有两岗不同证书的签章 */ private static boolean S_ESTAMP_FUND = false; /** 接收凭证时验章、验签开关:[OPEN]打开 [CLOSE]关闭*/ private static boolean S_RECEIVE_VIERFY = false; /**业务报文xml校验开关:[OPEN]打开 [CLOSE]关闭*/ private static boolean S_CHECK_IS = false; /** 数据库选择-:[ORACLE] oracle [POSTGRES] postgreSql */ private static String M_DATABASE_SERVICE = null; /** 回调接口是否启用开关:[OPEN]打开 [CLOSE]关闭 */ private static boolean S_CALL_BACK_FUNCTION = false; /** 区划1&凭证类型1$凭证类型2&服务地址2||区划2&凭证类型1$凭证类型2&服务地址2 <p> 区划2&&服务地址1||区划2&&服务地址2 表示此区划的全部凭证类型 */ private static String M_CALL_BACK_PARAMETER = null; /** #OCX是否弹出错误提示框 */ private static boolean S_ERROR_MSG_MODEL_FLAG = false; /** #绿色通道同步发送消息开关 */ private static boolean S_RECEIVE_MSG_REQUESTDATA_FLAG = false; /** #绿色通道异步发送消息开关 */ private static boolean S_RECEIVE_MSG_SENDDATA_FLAG = false; /** #绿色通道异步消息通知开关 */ private static boolean S_RECEIVE_MSG_SENDDATA_CALLBACK_FLAG = false; /** 数据请求、通知服务地址及端口 */ private static String M_REQUEST_NOTIFY = null; /** 凭证到达通知线程数 */ private static int M_CALL_BACK_THREAD_COUNT = 1; /** 原文被篡改错误编码配置 */ private static String M_EVOUCHER_VERFIY_SIGN_CODE = null; static { initParameters(); CacheRegister.registerCache(); } public static void initParameters() { final Logger logger = Logger.getLogger(ControlParamsHandler.class); // 初始化控制参数 String fieldName = null;// 字段名 Object fieldValue = null;// 字段值 try { Field[] fields = ControlParamsHandler.class.getDeclaredFields(); int a = 1; for (Field field : fields) { fieldName = field.getName(); fieldValue = PropertiesHander.getEVConfValue(fieldName); if (fieldName == null || (fieldName.indexOf("S_") == -1 && fieldName.indexOf("M_") == -1)) continue; // 取出空格 fieldValue = fieldValue == null ? null : fieldValue.toString() .trim(); if (StringUtils.isEmpty((String) fieldValue)) { logger.error(ExceptionConstant.EVS001 + ",参数[" + fieldName + "]为空"); throw new EVoucherException(ExceptionConstant.EVS001 + ",参数[" + fieldName + "]为空"); } // 打印控制参数 logger.info("控制参数(" + a + ")[" + fieldName + "]:[" + fieldValue + "]"); // 处理int类型 if (field.getType() == int.class) { fieldValue = Integer.parseInt((String) fieldValue); } // 处理boolean类型 if (field.getType() == boolean.class) { if (BIZConstant.EV_CONF_SWITCH_OPEN.equals(fieldValue)) { fieldValue = true; } else { fieldValue = false; } } // 赋值 field.set(ControlParamsHandler.class, fieldValue); a++; } } catch (EVoucherException ex) { logger.error(ex); throw ex; } catch (Exception ex) { logger.error(ExceptionConstant.EVS001 + ",参数[" + fieldName + "]异常", ex); throw new EVoucherException(ExceptionConstant.EVS001 + ",参数[" + fieldName + "]异常", ex); } // 打印第三方服务器状态 ServiceTestManager.print3rdPartyService(); } public static boolean isS_RECEIVE_VIERFY() { return S_RECEIVE_VIERFY; } public static void setS_RECEIVE_VIERFY(boolean s_receive_vierfy) { S_RECEIVE_VIERFY = s_receive_vierfy; } public static boolean isS_SIGN_SERVICE() { return S_SIGN_SERVICE; } public static String getM_SIGN_SERVICE() { return M_SIGN_SERVICE; } public static boolean isS_MESSAGE_SERVICE() { return S_MESSAGE_SERVICE; } public static String getM_MESSAGE_SERVICE() { return M_MESSAGE_SERVICE; } public static boolean isS_ESTAMP_SERVICE() { return S_ESTAMP_SERVICE; } public static String getM_ESTAMP_SERVICE() { return M_ESTAMP_SERVICE; } public static int getM_PARSE_PAUSE() { return M_PARSE_PAUSE; } public static boolean isS_ESTAMP_FUND() { return S_ESTAMP_FUND; } public static String getM_DATABASE_SERVICE() { return M_DATABASE_SERVICE; } public static void setM_DATABASE_SERVICE(String mDATABASESERVICE) { M_DATABASE_SERVICE = mDATABASESERVICE; } public static boolean isS_CALL_BACK_FUNCTION() { return S_CALL_BACK_FUNCTION; } public static void setS_CALL_BACK_FUNCTION(boolean sCALLBACKFUNCTION) { S_CALL_BACK_FUNCTION = sCALLBACKFUNCTION; } public static String getM_CALL_BACK_PARAMETER() { return M_CALL_BACK_PARAMETER; } public static void setM_CALL_BACK_PARAMETER(String mCALLBACKPARAMETER) { M_CALL_BACK_PARAMETER = mCALLBACKPARAMETER; } public static boolean isS_CHECK_IS() { return S_CHECK_IS; } public static void setS_CHECK_IS(boolean sCHECKIS) { S_CHECK_IS = sCHECKIS; } public static boolean isS_ERROR_MSG_MODEL_FLAG() { return S_ERROR_MSG_MODEL_FLAG; } public static void setS_ERROR_MSG_MODEL_FLAG(boolean sERRORMSGMODELFLAG) { S_ERROR_MSG_MODEL_FLAG = sERRORMSGMODELFLAG; } public static boolean isS_RECEIVE_MSG_REQUESTDATA_FLAG() { return S_RECEIVE_MSG_REQUESTDATA_FLAG; } public static void setS_RECEIVE_MSG_REQUESTDATA_FLAG( boolean sRECEIVEMSGREQUESTDATAFLAG) { S_RECEIVE_MSG_REQUESTDATA_FLAG = sRECEIVEMSGREQUESTDATAFLAG; } public static boolean isS_RECEIVE_MSG_SENDDATA_FLAG() { return S_RECEIVE_MSG_SENDDATA_FLAG; } public static void setS_RECEIVE_MSG_SENDDATA_FLAG( boolean sRECEIVEMSGSENDDATAFLAG) { S_RECEIVE_MSG_SENDDATA_FLAG = sRECEIVEMSGSENDDATAFLAG; } public static String getM_REQUEST_NOTIFY() { return M_REQUEST_NOTIFY; } public static int getM_CALL_BACK_THREAD_COUNT() { return M_CALL_BACK_THREAD_COUNT; } public static void setM_CALL_BACK_THREAD_COUNT(int mCALLBACKTHREADCOUNT) { M_CALL_BACK_THREAD_COUNT = mCALLBACKTHREADCOUNT; } public static String getM_EVOUCHER_VERFIY_SIGN_CODE() { return M_EVOUCHER_VERFIY_SIGN_CODE; } public static void setM_EVOUCHER_VERFIY_SIGN_CODE(String mEVOUCHERVERFIYSIGNCODE) { M_EVOUCHER_VERFIY_SIGN_CODE = mEVOUCHERVERFIYSIGNCODE; } public static boolean isS_RECEIVE_MSG_SENDDATA_CALLBACK_FLAG() { return S_RECEIVE_MSG_SENDDATA_CALLBACK_FLAG; } public static void setS_RECEIVE_MSG_SENDDATA_CALLBACK_FLAG( boolean sRECEIVEMSGSENDDATACALLBACKFLAG) { S_RECEIVE_MSG_SENDDATA_CALLBACK_FLAG = sRECEIVEMSGSENDDATACALLBACKFLAG; } }
原文地址:http://blog.csdn.net/itjavawfc/article/details/44055989