当前位置:   article > 正文

Java-WebServiceUtil工具类

java webservice 工具类
  1. /**
  2. * Program : WebServiceUtil.java
  3. * Author : leigq
  4. * Create : 2010-11-12 上午09:02:05
  5. *
  6. * Copyright 2010 by Embedded Internet Solutions Inc.,
  7. * All rights reserved.
  8. *
  9. * This software is the confidential and proprietary information
  10. * of Embedded Internet Solutions Inc.("Confidential Information").
  11. * You shall not disclose such Confidential Information and shall
  12. * use it only in accordance with the terms of the license agreement
  13. * you entered into with Embedded Internet Solutions Inc.
  14. *
  15. */
  16. package cn.ipanel.apps.portalBackOffice.util;
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.Enumeration;
  23. import java.util.List;
  24. import java.util.Properties;
  25. import java.util.Set;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;
  28. import org.apache.log4j.Logger;
  29. import cn.ipanel.apps.portalBackOffice.define.Defines;
  30. import cn.ipanel.apps.portalBackOffice.domain.WSAddress;
  31. /**
  32. * webService 工具类
  33. */
  34. public class WebServiceUtil {
  35. private Properties properties = new Properties();
  36. private static Logger logger = Logger.getLogger(WebServiceUtil.class);
  37. public WebServiceUtil() {
  38. properties = PropertyManager.getConfig();
  39. }
  40. /**
  41. * 获取WebService配置信息,返回的数据格式为
  42. * @return List<WSAddress>
  43. */
  44. public List<WSAddress> getWebServers() {
  45. List<WSAddress> result = new ArrayList<WSAddress>();
  46. Enumeration<?> enu = properties.propertyNames();
  47. Pattern pattern = Pattern.compile("^(wsAddress)X?");
  48. CONTINUE_POINT: while (enu.hasMoreElements()) {
  49. try {
  50. String key = (String) enu.nextElement();
  51. Matcher matcher = pattern.matcher(key);
  52. if (!matcher.find())
  53. continue;
  54. String propertityValue = (String) properties.get(key);
  55. // 如果不是以';'分隔的,则不处理
  56. if (propertityValue.indexOf(";") == -1)
  57. continue;
  58. String[] values = propertityValue.split(";");
  59. // 如果不是三段规则,则不处理
  60. if (values.length != 4)
  61. continue;
  62. for (int i = 0; i < values.length; i++)
  63. if (values[i] == null || values[i].trim().length() == 0)
  64. continue CONTINUE_POINT;
  65. result.add(new WSAddress(key, values[0], values[1], values[2],values[3]));
  66. } catch (Exception e) {
  67. logger.warn(e);
  68. }
  69. }
  70. return result;
  71. }
  72. /**
  73. * 检测此key值是否已经被使用,若重复使用相同key值,会以新的配置覆盖旧的配置
  74. * @param key
  75. * @return
  76. */
  77. public boolean checkKeyIsExist(String key) {
  78. Set<Object> keys = properties.keySet();
  79. if (keys.contains(key))
  80. return true;
  81. return false;
  82. }
  83. /**
  84. * 保存WebService配置到property文件
  85. * @param wsAddress
  86. * @return
  87. */
  88. public boolean storWSAddress(WSAddress wsAddress){
  89. if (wsAddress == null || checkWSAddressValue(wsAddress))
  90. throw new RuntimeException("参数不正确,请检查.");
  91. String wsProperty = wsAddress.getAddress() + ";" + wsAddress.getAccessFolder() + ";" + wsAddress.getPublishFolder() + ";" + wsAddress.getVisitURL();
  92. String wsKey = wsAddress.getWsName();
  93. properties.setProperty(wsKey, wsProperty);
  94. try {
  95. properties.store(new FileOutputStream(new File(Defines.CONFIG_FILE_PATH)), null);
  96. } catch (IOException e) {
  97. throw new RuntimeException("属性配置存储失败,请检查.");
  98. }
  99. return true;
  100. }
  101. /**
  102. * 移除webService配置
  103. * @param key
  104. * @return
  105. * @throws FileNotFoundException
  106. * @throws IOException
  107. */
  108. public boolean removeWSAddress(String key){
  109. try {
  110. properties.remove(key);
  111. properties.store(new FileOutputStream(new File(Defines.CONFIG_FILE_PATH)), "");
  112. return true;
  113. } catch (FileNotFoundException e) {
  114. throw new RuntimeException("配置文件未找到,请检查.");
  115. } catch (IOException e) {
  116. throw new RuntimeException("文件存储失败,请检查.");
  117. }
  118. }
  119. /**
  120. * 检测参数是否正确,任何错误或空值都抛异常
  121. * @param wsAddress
  122. */
  123. private boolean checkWSAddressValue(WSAddress wsAddress) {
  124. String wsName = wsAddress.getWsName();
  125. if (wsName == null || wsName.trim().length() == 0)
  126. throw new RuntimeException("参数: wsName为空,请检查.");
  127. String publishFolder = wsAddress.getPublishFolder();
  128. if (publishFolder == null || publishFolder.trim().length() == 0 || publishFolder.indexOf(";") != -1)
  129. throw new RuntimeException("参数: publishFolder为空或包含非法字符:';',请检查.");
  130. String address = wsAddress.getAddress();
  131. if (address == null || address.trim().length() == 0 || address.indexOf(";") != -1)
  132. throw new RuntimeException("参数: wsAddress为空或包含非法字符:';',请检查.");
  133. String accessFolder = wsAddress.getAccessFolder();
  134. if (accessFolder == null || accessFolder.trim().length() == 0 || accessFolder.indexOf(";") != -1)
  135. throw new RuntimeException("参数: accessFolder为空或包含非法字符:';',请检查.");
  136. String visitURL = wsAddress.getVisitURL();
  137. if (visitURL == null || visitURL.trim().length() == 0 || visitURL.indexOf(";") != -1)
  138. throw new RuntimeException("参数: visitURL为空或包含非法字符:';',请检查.");
  139. return false;
  140. }
  141. }

转载于:https://www.cnblogs.com/bilaisheng/p/10210963.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/187006
推荐阅读
相关标签
  

闽ICP备14008679号