Example #1
0
  /**
   * 设置系统设置
   *
   * @param setting 系统设置
   */
  public static void set(Setting setting) {
    try {
      File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
      Document document = new SAXReader().read(shopxxXmlFile);
      List<Element> elements = document.selectNodes("/shopxx/setting");
      for (Element element : elements) {
        try {
          String name = element.attributeValue("name");
          String value = beanUtils.getProperty(setting, name);
          Attribute attribute = element.attribute("value");
          attribute.setValue(value);
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        } catch (InvocationTargetException e) {
          e.printStackTrace();
        } catch (NoSuchMethodException e) {
          e.printStackTrace();
        }
      }

      FileOutputStream fileOutputStream = null;
      XMLWriter xmlWriter = null;
      try {
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setEncoding("UTF-8");
        outputFormat.setIndent(true);
        outputFormat.setIndent("	");
        outputFormat.setNewlines(true);
        fileOutputStream = new FileOutputStream(shopxxXmlFile);
        xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
        xmlWriter.write(document);
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (xmlWriter != null) {
          try {
            xmlWriter.close();
          } catch (IOException e) {
          }
        }
        IOUtils.closeQuietly(fileOutputStream);
      }

      Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
      cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }