Search in sources :

Example 1 with CacheConfig

use of org.sagacity.sqltoy.config.model.CacheConfig in project sagacity-sqltoy by chenrenfei.

the class TranslateManager method parseTranslate.

/**
 * @todo 解析缓存翻译的配置文件
 * @param configFile
 * @throws Exception
 */
public void parseTranslate(String configFile) throws Exception {
    if (StringUtil.isBlank(configFile))
        return;
    InputStream fileIS = null;
    InputStreamReader ir = null;
    try {
        fileIS = CommonUtils.getFileInputStream(configFile);
        if (fileIS != null) {
            SAXReader saxReader = new SAXReader();
            saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            ir = new InputStreamReader(fileIS);
            Document doc = saxReader.read(ir);
            Element root = doc.getRootElement();
            List translates = root.elements("translate");
            if (translates == null || translates.isEmpty())
                return;
            Element translate;
            for (Iterator iter = translates.iterator(); iter.hasNext(); ) {
                translate = (Element) iter.next();
                TranslateCacheModel translateCacheModel = new TranslateCacheModel();
                // 对应缓存
                translateCacheModel.setCacheName(translate.attributeValue("cache"));
                // key对应的数据列
                if (translate.attribute("key-index") != null) {
                    translateCacheModel.setKeyIndex(Integer.parseInt(translate.attributeValue("key-index")));
                }
                // 查询数据的service
                if (translate.attribute("service") != null) {
                    translateCacheModel.setService(translate.attributeValue("service"));
                }
                // 查询数据的service对应的方法
                if (translate.attribute("method") != null) {
                    translateCacheModel.setServiceMethod(translate.attributeValue("method"));
                }
                // 设置缓存管理器
                if (translate.attribute("cache-manager") != null) {
                    translateCacheModel.setTranslateCacheManager(translate.attributeValue("cache-manager"));
                } else if (translate.attribute("manager") != null) {
                    translateCacheModel.setTranslateCacheManager(translate.attributeValue("manager"));
                }
                // 过期时长
                CacheConfig cacheConfig = new CacheConfig();
                cacheConfig.setExpireSeconds(SqlToyConstants.getCacheExpireSeconds());
                if (translate.attribute("expire-seconds") != null) {
                    cacheConfig.setExpireSeconds(Long.parseLong(translate.attributeValue("expire-seconds")));
                } else if (translate.attribute("keep-alive") != null) {
                    cacheConfig.setExpireSeconds(Long.parseLong(translate.attributeValue("keep-alive")));
                }
                // sql对应的dataSource
                if (translate.attribute("datasource") != null)
                    translateCacheModel.setDataSource(translate.attributeValue("datasource"));
                else if (translate.attribute("dataSource") != null)
                    translateCacheModel.setDataSource(translate.attributeValue("dataSource"));
                // 基于简单的sql查询对应的sql
                if (translate.attribute("sql") != null) {
                    translateCacheModel.setSql(translate.attributeValue("sql"));
                } else if (StringUtil.isNotBlank(translate.getTextTrim())) {
                    translateCacheModel.setSql(translate.getText());
                } else if (translate.element("sql") != null) {
                    translateCacheModel.setSql(translate.element("sql").getText());
                }
                translateMap.put(translateCacheModel.getCacheName(), translateCacheModel);
            }
        }
    } catch (DocumentException de) {
        de.printStackTrace();
        logger.error("读取translate对应的xml文件失败,对应文件={}", configFile, de);
        throw de;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        if (ir != null)
            ir.close();
        if (fileIS != null)
            fileIS.close();
    }
}
Also used : TranslateCacheModel(org.sagacity.sqltoy.config.model.TranslateCacheModel) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) Iterator(java.util.Iterator) List(java.util.List) Document(org.dom4j.Document) CacheConfig(org.sagacity.sqltoy.config.model.CacheConfig) DocumentException(org.dom4j.DocumentException)

Aggregations

InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Document (org.dom4j.Document)1 DocumentException (org.dom4j.DocumentException)1 Element (org.dom4j.Element)1 SAXReader (org.dom4j.io.SAXReader)1 CacheConfig (org.sagacity.sqltoy.config.model.CacheConfig)1 TranslateCacheModel (org.sagacity.sqltoy.config.model.TranslateCacheModel)1