/sql-processor/src/main/java/org/sqlproc/engine/config/store/XmlEngineConfiguration.java

http://github.com/hudec/sql-processor · Java · 341 lines · 266 code · 67 blank · 8 comment · 8 complexity · 0f63864f13cd38d15f9df2013e766b3c MD5 · raw file

  1. package org.sqlproc.engine.config.store;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Map.Entry;
  6. import java.util.concurrent.ConcurrentHashMap;
  7. import java.util.concurrent.atomic.AtomicInteger;
  8. import javax.xml.bind.annotation.XmlAttribute;
  9. import javax.xml.bind.annotation.XmlElement;
  10. import javax.xml.bind.annotation.XmlElementWrapper;
  11. import javax.xml.bind.annotation.XmlRootElement;
  12. import org.sqlproc.engine.config.SqlEngineConfiguration;
  13. /**
  14. * This class is used only for the SQL Processor dynamic configuration serialization/deserialization.
  15. *
  16. * <p>
  17. * For more info please see the <a href="https://github.com/hudec/sql-processor/wiki">Tutorials</a>.
  18. *
  19. * @author <a href="mailto:Vladimir.Hudec@gmail.com">Vladimir Hudec</a>
  20. */
  21. @XmlRootElement(name = "sqlp-configuration")
  22. public class XmlEngineConfiguration {
  23. private List<EngineUsage> queryEngines = new ArrayList<EngineUsage>();
  24. private List<EngineUsage> crudEngines = new ArrayList<EngineUsage>();
  25. private List<EngineUsage> procedureEngines = new ArrayList<EngineUsage>();
  26. private List<EngineSql> dynamicQueryEngines = new ArrayList<EngineSql>();
  27. private List<EngineSql> dynamicCrudEngines = new ArrayList<EngineSql>();
  28. private List<EngineSql> dynamicProcedureEngines = new ArrayList<EngineSql>();
  29. private Boolean lazyInit;
  30. private Integer asyncInitThreads;
  31. private Integer initTreshold;
  32. private Boolean initInUsageOrder;
  33. private Boolean initClearUsage;
  34. private Boolean useProcessingCache;
  35. private Boolean useDynamicProcessingCache;
  36. private List<String> doProcessingCacheEngines = new ArrayList<>();
  37. private List<String> dontProcessingCacheEngines = new ArrayList<>();
  38. public XmlEngineConfiguration() {
  39. }
  40. public XmlEngineConfiguration(SqlEngineConfiguration config) {
  41. copyEng(config.getQueryEngines(), queryEngines);
  42. copyEng(config.getCrudEngines(), crudEngines);
  43. copyEng(config.getProcedureEngines(), procedureEngines);
  44. copyDyn(config.getDynamicQueryEngines(), dynamicQueryEngines);
  45. copyDyn(config.getDynamicCrudEngines(), dynamicCrudEngines);
  46. copyDyn(config.getDynamicProcedureEngines(), dynamicProcedureEngines);
  47. lazyInit = config.getLazyInit();
  48. asyncInitThreads = config.getAsyncInitThreads();
  49. initTreshold = config.getInitTreshold();
  50. initInUsageOrder = config.getInitInUsageOrder();
  51. initClearUsage = config.getInitClearUsage();
  52. useProcessingCache = config.getUseProcessingCache();
  53. useDynamicProcessingCache = config.getUseDynamicProcessingCache();
  54. doProcessingCacheEngines.addAll(config.getDoProcessingCacheEngines());
  55. dontProcessingCacheEngines.addAll(config.getDontProcessingCacheEngines());
  56. }
  57. public void toConfig(SqlEngineConfiguration config) {
  58. config.setQueryEngines(copyEng(queryEngines));
  59. config.setCrudEngines(copyEng(crudEngines));
  60. config.setProcedureEngines(copyEng(procedureEngines));
  61. config.setDynamicQueryEngines(copyDyn(dynamicQueryEngines));
  62. config.setDynamicCrudEngines(copyDyn(dynamicCrudEngines));
  63. config.setDynamicProcedureEngines(copyDyn(dynamicProcedureEngines));
  64. config.setLazyInit(getLazyInit());
  65. config.setAsyncInitThreads(getAsyncInitThreads());
  66. config.setInitTreshold(getInitTreshold());
  67. config.setInitInUsageOrder(getInitInUsageOrder());
  68. config.setInitClearUsage(getInitClearUsage());
  69. config.setUseProcessingCache(getUseProcessingCache());
  70. config.setUseDynamicProcessingCache(getUseDynamicProcessingCache());
  71. config.setDoProcessingCacheEngines(new HashSet<String>());
  72. config.getDoProcessingCacheEngines().addAll(doProcessingCacheEngines);
  73. config.setDontProcessingCacheEngines(new HashSet<String>());
  74. config.getDontProcessingCacheEngines().addAll(dontProcessingCacheEngines);
  75. }
  76. @XmlRootElement(name = "usage")
  77. public static class EngineUsage {
  78. private String name;
  79. private int usage;
  80. public EngineUsage() {
  81. }
  82. public EngineUsage(String name, AtomicInteger usage) {
  83. this.name = name;
  84. this.usage = usage.get();
  85. }
  86. @XmlElement
  87. public String getName() {
  88. return name;
  89. }
  90. public void setName(String name) {
  91. this.name = name;
  92. }
  93. @XmlAttribute
  94. public int getUsage() {
  95. return usage;
  96. }
  97. public void setUsage(int usage) {
  98. this.usage = usage;
  99. }
  100. @Override
  101. public String toString() {
  102. return "EngineUsage [name=" + name + ", usage=" + usage + "]";
  103. }
  104. }
  105. @XmlRootElement(name = "sql")
  106. public static class EngineSql {
  107. private String name;
  108. private String sql;
  109. public EngineSql() {
  110. }
  111. public EngineSql(String name, String sql) {
  112. this.name = name;
  113. this.sql = sql;
  114. }
  115. @XmlElement
  116. public String getName() {
  117. return name;
  118. }
  119. public void setName(String name) {
  120. this.name = name;
  121. }
  122. @XmlAttribute
  123. public String getSql() {
  124. return sql;
  125. }
  126. public void setSql(String sql) {
  127. this.sql = sql;
  128. }
  129. @Override
  130. public String toString() {
  131. return "EngineSql [name=" + name + ", sql=" + sql + "]";
  132. }
  133. }
  134. @XmlElementWrapper(name = "queries")
  135. public List<EngineUsage> getQueryEngines() {
  136. return queryEngines;
  137. }
  138. public void setQueryEngines(List<EngineUsage> queryEngines) {
  139. this.queryEngines = queryEngines;
  140. }
  141. @XmlElementWrapper(name = "cruds")
  142. public List<EngineUsage> getCrudEngines() {
  143. return crudEngines;
  144. }
  145. public void setCrudEngines(List<EngineUsage> crudEngines) {
  146. this.crudEngines = crudEngines;
  147. }
  148. @XmlElementWrapper(name = "procedures")
  149. public List<EngineUsage> getProcedureEngines() {
  150. return procedureEngines;
  151. }
  152. public void setProcedureEngines(List<EngineUsage> procedureEngines) {
  153. this.procedureEngines = procedureEngines;
  154. }
  155. @XmlElementWrapper(name = "dynamicQueries")
  156. public List<EngineSql> getDynamicQueryEngines() {
  157. return dynamicQueryEngines;
  158. }
  159. public void setDynamicQueryEngines(List<EngineSql> dynamicQueryEngines) {
  160. this.dynamicQueryEngines = dynamicQueryEngines;
  161. }
  162. @XmlElementWrapper(name = "dynamicCruds")
  163. public List<EngineSql> getDynamicCrudEngines() {
  164. return dynamicCrudEngines;
  165. }
  166. public void setDynamicCrudEngines(List<EngineSql> dynamicCrudEngines) {
  167. this.dynamicCrudEngines = dynamicCrudEngines;
  168. }
  169. @XmlElementWrapper(name = "dynamicProcedures")
  170. public List<EngineSql> getDynamicProcedureEngines() {
  171. return dynamicProcedureEngines;
  172. }
  173. public void setDynamicProcedureEngines(List<EngineSql> dynamicProcedureEngines) {
  174. this.dynamicProcedureEngines = dynamicProcedureEngines;
  175. }
  176. @XmlElement
  177. public Boolean getLazyInit() {
  178. return lazyInit;
  179. }
  180. public void setLazyInit(Boolean lazyInit) {
  181. this.lazyInit = lazyInit;
  182. }
  183. @XmlElement
  184. public Integer getAsyncInitThreads() {
  185. return asyncInitThreads;
  186. }
  187. public void setAsyncInitThreads(Integer asyncInitThreads) {
  188. this.asyncInitThreads = asyncInitThreads;
  189. }
  190. @XmlElement
  191. public Integer getInitTreshold() {
  192. return initTreshold;
  193. }
  194. public void setInitTreshold(Integer initTreshold) {
  195. this.initTreshold = initTreshold;
  196. }
  197. @XmlElement
  198. public Boolean getInitInUsageOrder() {
  199. return initInUsageOrder;
  200. }
  201. public void setInitInUsageOrder(Boolean initInUsageOrder) {
  202. this.initInUsageOrder = initInUsageOrder;
  203. }
  204. @XmlElement
  205. public Boolean getInitClearUsage() {
  206. return initClearUsage;
  207. }
  208. public void setInitClearUsage(Boolean initClearUsage) {
  209. this.initClearUsage = initClearUsage;
  210. }
  211. @XmlElement
  212. public Boolean getUseProcessingCache() {
  213. return useProcessingCache;
  214. }
  215. public void setUseProcessingCache(Boolean useProcessingCache) {
  216. this.useProcessingCache = useProcessingCache;
  217. }
  218. @XmlElement
  219. public Boolean getUseDynamicProcessingCache() {
  220. return useDynamicProcessingCache;
  221. }
  222. public void setUseDynamicProcessingCache(Boolean useDynamicProcessingCache) {
  223. this.useDynamicProcessingCache = useDynamicProcessingCache;
  224. }
  225. @XmlElementWrapper(name = "doProcessingCache")
  226. public List<String> getDoProcessingCacheEngines() {
  227. return doProcessingCacheEngines;
  228. }
  229. public void setDoProcessingCacheEngines(List<String> doProcessingCacheEngines) {
  230. this.doProcessingCacheEngines = doProcessingCacheEngines;
  231. }
  232. @XmlElementWrapper(name = "dontProcessingCache")
  233. public List<String> getDontProcessingCacheEngines() {
  234. return dontProcessingCacheEngines;
  235. }
  236. public void setDontProcessingCacheEngines(List<String> dontProcessingCacheEngines) {
  237. this.dontProcessingCacheEngines = dontProcessingCacheEngines;
  238. }
  239. @Override
  240. public String toString() {
  241. return "XmlEngineConfiguration [queryEngines=" + queryEngines + ", crudEngines=" + crudEngines
  242. + ", procedureEngines=" + procedureEngines + ", dynamicQueryEngines=" + dynamicQueryEngines
  243. + ", dynamicCrudEngines=" + dynamicCrudEngines + ", dynamicProcedureEngines=" + dynamicProcedureEngines
  244. + ", lazyInit=" + lazyInit + ", asyncInitThreads=" + asyncInitThreads + ", initTreshold=" + initTreshold
  245. + ", initInUsageOrder=" + initInUsageOrder + ", initClearUsage=" + initClearUsage
  246. + ", useProcessingCache=" + useProcessingCache + ", useDynamicProcessingCache="
  247. + useDynamicProcessingCache + ", doProcessingCacheEngines=" + doProcessingCacheEngines
  248. + ", dontProcessingCacheEngines=" + dontProcessingCacheEngines + "]";
  249. }
  250. private void copyEng(ConcurrentHashMap<String, AtomicInteger> from, List<EngineUsage> to) {
  251. for (Entry<String, AtomicInteger> e : from.entrySet())
  252. to.add(new EngineUsage(e.getKey(), e.getValue()));
  253. }
  254. private void copyDyn(ConcurrentHashMap<String, String> from, List<EngineSql> to) {
  255. for (Entry<String, String> e : from.entrySet())
  256. to.add(new EngineSql(e.getKey(), e.getValue()));
  257. }
  258. private ConcurrentHashMap<String, AtomicInteger> copyEng(List<EngineUsage> from) {
  259. ConcurrentHashMap<String, AtomicInteger> engines = new ConcurrentHashMap<String, AtomicInteger>();
  260. if (from == null)
  261. return engines;
  262. for (EngineUsage e : from)
  263. engines.put(e.getName(), new AtomicInteger(e.getUsage()));
  264. return engines;
  265. }
  266. private ConcurrentHashMap<String, String> copyDyn(List<EngineSql> from) {
  267. ConcurrentHashMap<String, String> engines = new ConcurrentHashMap<String, String>();
  268. if (from == null)
  269. return engines;
  270. for (EngineSql e : from)
  271. engines.put(e.getName(), e.getSql());
  272. return engines;
  273. }
  274. }