Java tutorial
/* * Copyright 2005-2015 tuling123.com. All rights reserved. * Support: http://www.tuling123.com * License: http://www.tuling123.com */ package cn.mario256.blog.util; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Date; import java.util.List; import cn.mario256.blog.EnumConverter; import cn.mario256.blog.TemplateConfig; import cn.mario256.blog.EnumConverter; import cn.mario256.blog.LogConfig; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; import cn.mario256.blog.CommonAttributes; import cn.mario256.blog.Setting; import cn.mario256.blog.TemplateConfig; import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.ConvertUtilsBean; import org.apache.commons.beanutils.Converter; import org.apache.commons.beanutils.converters.ArrayConverter; import org.apache.commons.beanutils.converters.DateConverter; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.springframework.core.io.ClassPathResource; import org.springframework.util.Assert; /** * Utils - * * @author TURINGROBOT Team * @version 4.0 */ public final class SystemUtils { /** CacheManager */ private static final CacheManager CACHE_MANAGER = CacheManager.create(); /** BeanUtilsBean */ private static final BeanUtilsBean BEAN_UTILS; static { ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean() { @Override public String convert(Object value) { if (value != null) { Class<?> type = value.getClass(); if (type.isEnum() && super.lookup(type) == null) { super.register(new EnumConverter(type), type); } else if (type.isArray() && type.getComponentType().isEnum()) { if (super.lookup(type) == null) { ArrayConverter arrayConverter = new ArrayConverter(type, new EnumConverter(type.getComponentType()), 0); arrayConverter.setOnlyFirstToString(false); super.register(arrayConverter, type); } Converter converter = super.lookup(type); return ((String) converter.convert(String.class, value)); } } return super.convert(value); } @SuppressWarnings("rawtypes") @Override public Object convert(String value, Class clazz) { if (clazz.isEnum() && super.lookup(clazz) == null) { super.register(new EnumConverter(clazz), clazz); } return super.convert(value, clazz); } @SuppressWarnings("rawtypes") @Override public Object convert(String[] values, Class clazz) { if (clazz.isArray() && clazz.getComponentType().isEnum() && super.lookup(clazz.getComponentType()) == null) { super.register(new EnumConverter(clazz.getComponentType()), clazz.getComponentType()); } return super.convert(values, clazz); } @SuppressWarnings("rawtypes") @Override public Object convert(Object value, Class targetType) { if (super.lookup(targetType) == null) { if (targetType.isEnum()) { super.register(new EnumConverter(targetType), targetType); } else if (targetType.isArray() && targetType.getComponentType().isEnum()) { ArrayConverter arrayConverter = new ArrayConverter(targetType, new EnumConverter(targetType.getComponentType()), 0); arrayConverter.setOnlyFirstToString(false); super.register(arrayConverter, targetType); } } return super.convert(value, targetType); } }; DateConverter dateConverter = new DateConverter(); dateConverter.setPatterns(CommonAttributes.DATE_PATTERNS); convertUtilsBean.register(dateConverter, Date.class); BEAN_UTILS = new BeanUtilsBean(convertUtilsBean); } /** * ?? */ private SystemUtils() { } /** * ? * * @return */ @SuppressWarnings("unchecked") public static Setting getSetting() { Ehcache cache = CACHE_MANAGER.getEhcache(Setting.CACHE_NAME); String cacheKey = "setting"; Element cacheElement = cache.get(cacheKey); if (cacheElement == null) { Setting setting = new Setting(); try { File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile(); Document document = new SAXReader().read(turingXmlFile); List<org.dom4j.Element> elements = document.selectNodes("/turing/setting"); for (org.dom4j.Element element : elements) { try { String name = element.attributeValue("name"); String value = element.attributeValue("value"); BEAN_UTILS.setProperty(setting, name, value); } catch (IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); } } } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (DocumentException e) { throw new RuntimeException(e.getMessage(), e); } cache.put(new Element(cacheKey, setting)); cacheElement = cache.get(cacheKey); } return (Setting) cacheElement.getObjectValue(); } /** * * * @param setting * */ @SuppressWarnings("unchecked") public static void setSetting(Setting setting) { Assert.notNull(setting); try { File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile(); Document document = new SAXReader().read(turingXmlFile); List<org.dom4j.Element> elements = document.selectNodes("/turing/setting"); for (org.dom4j.Element element : elements) { try { String name = element.attributeValue("name"); String value = BEAN_UTILS.getProperty(setting, name); Attribute attribute = element.attribute("value"); attribute.setValue(value); } catch (IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); } catch (NoSuchMethodException e) { throw new RuntimeException(e.getMessage(), e); } } XMLWriter xmlWriter = null; try { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setEncoding("UTF-8"); outputFormat.setIndent(true); outputFormat.setIndent(" "); outputFormat.setNewlines(true); xmlWriter = new XMLWriter(new FileOutputStream(turingXmlFile), outputFormat); xmlWriter.write(document); xmlWriter.flush(); } catch (FileNotFoundException e) { throw new RuntimeException(e.getMessage(), e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } finally { try { if (xmlWriter != null) { xmlWriter.close(); } } catch (IOException e) { } } Ehcache cache = CACHE_MANAGER.getEhcache(Setting.CACHE_NAME); String cacheKey = "setting"; cache.put(new Element(cacheKey, setting)); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (DocumentException e) { throw new RuntimeException(e.getMessage(), e); } } /** * ??? * * @param id * ID * @return ?? */ public static TemplateConfig getTemplateConfig(String id) { Assert.hasText(id); Ehcache cache = CACHE_MANAGER.getEhcache(TemplateConfig.CACHE_NAME); String cacheKey = "templateConfig_" + id; Element cacheElement = cache.get(cacheKey); if (cacheElement == null) { TemplateConfig templateConfig = null; try { File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile(); Document document = new SAXReader().read(turingXmlFile); org.dom4j.Element element = (org.dom4j.Element) document .selectSingleNode("/turing/templateConfig[@id='" + id + "']"); if (element != null) { templateConfig = getTemplateConfig(element); } } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (DocumentException e) { throw new RuntimeException(e.getMessage(), e); } cache.put(new Element(cacheKey, templateConfig)); cacheElement = cache.get(cacheKey); } return (TemplateConfig) cacheElement.getObjectValue(); } /** * ??? * * @param type * * @return ?? */ @SuppressWarnings("unchecked") public static List<TemplateConfig> getTemplateConfigs(TemplateConfig.Type type) { Ehcache cache = CACHE_MANAGER.getEhcache(TemplateConfig.CACHE_NAME); String cacheKey = "templateConfigs_" + type; Element cacheElement = cache.get(cacheKey); if (cacheElement == null) { List<TemplateConfig> templateConfigs = new ArrayList<TemplateConfig>(); try { File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile(); Document document = new SAXReader().read(turingXmlFile); List<org.dom4j.Element> elements = document.selectNodes( type != null ? "/turing/templateConfig[@type='" + type + "']" : "/turing/templateConfig"); for (org.dom4j.Element element : elements) { templateConfigs.add(getTemplateConfig(element)); } } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (DocumentException e) { throw new RuntimeException(e.getMessage(), e); } cache.put(new Element(cacheKey, templateConfigs)); cacheElement = cache.get(cacheKey); } return (List<TemplateConfig>) cacheElement.getObjectValue(); } /** * ??? * * @return ?? */ public static List<TemplateConfig> getTemplateConfigs() { return getTemplateConfigs(null); } /** * ?? * * @return ? */ @SuppressWarnings("unchecked") public static List<LogConfig> getLogConfigs() { Ehcache cache = CACHE_MANAGER.getEhcache(LogConfig.CACHE_NAME); String cacheKey = "logConfigs"; Element cacheElement = cache.get(cacheKey); if (cacheElement == null) { List<LogConfig> logConfigs = new ArrayList<LogConfig>(); try { File turingXmlFile = new ClassPathResource(CommonAttributes.TURING_XML_PATH).getFile(); Document document = new SAXReader().read(turingXmlFile); List<org.dom4j.Element> elements = document.selectNodes("/turing/logConfig"); for (org.dom4j.Element element : elements) { String operation = element.attributeValue("operation"); String urlPattern = element.attributeValue("urlPattern"); LogConfig logConfig = new LogConfig(); logConfig.setOperation(operation); logConfig.setUrlPattern(urlPattern); logConfigs.add(logConfig); } } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (DocumentException e) { throw new RuntimeException(e.getMessage(), e); } cache.put(new Element(cacheKey, logConfigs)); cacheElement = cache.get(cacheKey); } return (List<LogConfig>) cacheElement.getObjectValue(); } /** * ??? * * @param element * * @return ?? */ private static TemplateConfig getTemplateConfig(org.dom4j.Element element) { Assert.notNull(element); String id = element.attributeValue("id"); String type = element.attributeValue("type"); String name = element.attributeValue("name"); String templatePath = element.attributeValue("templatePath"); String staticPath = element.attributeValue("staticPath"); String description = element.attributeValue("description"); TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setId(id); templateConfig.setType(TemplateConfig.Type.valueOf(type)); templateConfig.setName(name); templateConfig.setTemplatePath(templatePath); templateConfig.setStaticPath(staticPath); templateConfig.setDescription(description); return templateConfig; } }