/MongoQL/src/com/julewan/mongo/impl/MongoLoader.java

https://github.com/huangbenhua/MongoQL · Java · 151 lines · 119 code · 9 blank · 23 comment · 34 complexity · 57dc35247feed4cf8a639247754259ab MD5 · raw file

  1. /**
  2. * Copyright (c) 2010-2011 julewa.com.
  3. * All rights reserved.
  4. *
  5. * @author Huang Benhua
  6. * @date 2011-8-20
  7. *
  8. **/
  9. package com.julewan.mongo.impl;
  10. import java.util.Collections;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.Properties;
  14. import java.util.Set;
  15. import com.julewan.mongo.Mongoes;
  16. import com.julewan.mongo.ql.Statement;
  17. import com.mongodb.DB;
  18. import com.mongodb.DBCollection;
  19. import com.mongodb.Mongo;
  20. public class MongoLoader {
  21. //这是唯一可用的方法
  22. public Mongoes getMongoes(String name){
  23. if(name == null)return null;
  24. name = name.toLowerCase();
  25. Mongoes mongoes = mongoesMap.get(name);
  26. if(mongoes != null)return mongoes;
  27. return _load_mongoes_(name, null);
  28. }
  29. //////////////////
  30. private Mongo mongo = null;
  31. private Map<String, DB> mongoDBs = Collections.synchronizedMap(new HashMap<String, DB>());
  32. private Map<String, Mongoes> mongoesMap = Collections.synchronizedMap(new HashMap<String, Mongoes>());
  33. public MongoLoader(Properties props){
  34. if(props == null)return;
  35. try {
  36. //TODO 先不处理登陆和连接池等问题
  37. _load_mongo_(props.getProperty("mongo.url", "127.0.0.1"));
  38. //boolean auth = db.authenticate(myUserName, myPassword);
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. mongo = null;
  42. }
  43. if(mongo == null)return;
  44. load(props);
  45. }
  46. public boolean isValid(){
  47. return mongo != null;
  48. }
  49. private DB _get_DB_(String name){
  50. //防止错误
  51. name = name.replace(".", "").replaceAll("\\s", "");
  52. DB db = mongoDBs.get(name);
  53. if(db == null){
  54. db = mongo.getDB(name);
  55. mongoDBs.put(name, db);
  56. }
  57. return db;
  58. }
  59. private Map<String, Map<String, Statement>> global_db_statements
  60. = Collections.synchronizedMap(new HashMap<String, Map<String, Statement>>());
  61. private Map<String, Map<String, Statement>> global_collection_statements
  62. = Collections.synchronizedMap(new HashMap<String, Map<String, Statement>>());
  63. private void load(Properties props){
  64. //collection statements
  65. Map<String, Map<String, Statement>> local_statements = new HashMap<String, Map<String, Statement>>();
  66. //load all statements
  67. Set<Object> keys = props.keySet();
  68. for(Object k:keys){
  69. String key = k.toString().replaceAll("\\s", "");
  70. if(!key.startsWith("q."))continue;
  71. key = key.substring("q.".length());
  72. int idx = key.indexOf("*.");
  73. if(idx < 0){
  74. //
  75. idx = key.lastIndexOf(".");
  76. if(idx < 1)continue;
  77. if(idx == key.indexOf("."))continue;
  78. //
  79. _create_statement_(local_statements, key, props.get(k).toString());
  80. }else{
  81. key = key.replace("*.", "");
  82. k = props.get(k);
  83. if(idx == 0){
  84. _create_statement_(global_collection_statements, key, k.toString());
  85. }else{
  86. _create_statement_(global_db_statements, key, k.toString());
  87. }
  88. }
  89. }
  90. //create default db collections
  91. for(String key:local_statements.keySet()){
  92. _load_mongoes_(key, local_statements.get(key));
  93. }
  94. }
  95. private void _create_statement_(Map<String, Map<String, Statement>> map, String key, String ql){
  96. int idx = key.lastIndexOf(".");
  97. String k = key.substring(0, idx);
  98. key = key.substring(idx + 1);
  99. //
  100. Map<String, Statement> m = map.get(k);
  101. if(m == null){
  102. m = Collections.synchronizedMap(new HashMap<String, Statement>());
  103. map.put(k, m);
  104. }
  105. m.put(key, new Statement(ql));
  106. }
  107. private void _load_mongo_(String url) throws Exception{
  108. url = url.replaceAll("\\s", "");
  109. int idx = url.indexOf(":");
  110. if(idx < 0){
  111. mongo = new Mongo(url);
  112. }else if(idx == 0){
  113. mongo = new Mongo("127.0.0.1", Integer.parseInt(url.substring(1)));
  114. }else{
  115. mongo = new Mongo(url.substring(0, idx), Integer.parseInt(url.substring(idx + 1)));
  116. }
  117. }
  118. private Mongoes _load_mongoes_(String name, Map<String, Statement> mymap){
  119. int idx = name.indexOf(".");
  120. if(idx < 1 || name.lastIndexOf(".") != idx){
  121. System.err.println("INVALID MONGO DB NAME:" + name);
  122. return null;
  123. }
  124. String dbname = name.substring(0, idx);
  125. String colname = name.substring(idx + 1);
  126. //
  127. Map<String, Statement> lmap, statements = new HashMap<String, Statement>();
  128. lmap = global_collection_statements.get(colname);
  129. if(lmap != null) statements.putAll(lmap);
  130. lmap = global_db_statements.get(dbname);
  131. if(lmap != null) statements.putAll(lmap);
  132. if(mymap != null) statements.putAll(mymap);
  133. //
  134. DB db = _get_DB_(dbname);
  135. DBCollection col = db.getCollection(colname);
  136. //
  137. Mongoes mg = new Mongoes(col, statements);
  138. //
  139. mongoesMap.put(name, mg);
  140. return mg;
  141. }
  142. }