Constant.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.pbsage.util;
  2. import java.io.File;
  3. import java.net.URL;
  4. import java.net.URLDecoder;
  5. import java.util.Iterator;
  6. import org.apache.log4j.Logger;
  7. import org.apache.log4j.PropertyConfigurator;
  8. import org.dom4j.Document;
  9. import org.dom4j.Element;
  10. import org.dom4j.io.SAXReader;
  11. @SuppressWarnings("rawtypes")
  12. public class Constant {
  13. public static String data_middle_url;
  14. public static String metadata_web_url;
  15. private static Logger logger;
  16. private static String path = null;
  17. public static String GetPath() {
  18. if (path == null) {
  19. String resource = "/log4j.properties";
  20. URL resourceUrl = Constant.class.getResource(resource);
  21. String resourcePath = null;
  22. if (resourceUrl == null) {
  23. resourcePath = System.getProperty("user.dir") + resource;
  24. } else {
  25. resourcePath = resourceUrl.getPath();
  26. }
  27. path = resourcePath.substring(0, resourcePath.length() - resource.length());
  28. }
  29. return path;
  30. }
  31. static {
  32. try {
  33. String filepathLog4j = Constant.GetPath() + "/log4j.properties";
  34. PropertyConfigurator.configure(URLDecoder.decode(filepathLog4j, "UTF-8"));
  35. logger = Logger.getLogger(Constant.class);
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. try {
  40. {
  41. File xmlFile = new File(Constant.GetPath() + "/config.xml");
  42. if (xmlFile.exists()) {
  43. SAXReader saxReader = new SAXReader();
  44. Document document = saxReader.read(xmlFile);
  45. Iterator iter;
  46. Element element;
  47. iter = document.selectNodes("/root/data_middle").iterator();
  48. if (iter.hasNext()) {
  49. element = (Element) iter.next();
  50. data_middle_url = element.attribute("url").getValue();
  51. if (!data_middle_url.endsWith("/")) {
  52. data_middle_url = data_middle_url + "/";
  53. }
  54. }
  55. iter = document.selectNodes("/root/metadata_web").iterator();
  56. if (iter.hasNext()) {
  57. element = (Element) iter.next();
  58. metadata_web_url = element.attribute("url").getValue();
  59. if (!metadata_web_url.endsWith("/")) {
  60. metadata_web_url = metadata_web_url + "/";
  61. }
  62. }
  63. }
  64. }
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. public synchronized static void debug(String message) {
  70. logger.debug(message);
  71. }
  72. public synchronized static void info(String message) {
  73. logger.info(message);
  74. }
  75. public synchronized static void warn(String message) {
  76. logger.warn(message);
  77. }
  78. public synchronized static void error(String message) {
  79. logger.error(message);
  80. }
  81. public synchronized static void fatal(String message) {
  82. logger.fatal(message);
  83. }
  84. }