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();
}
}
Aggregations