PageRenderTime 62ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/ws-link/src/main/java/it/unisannio/ding/wslink/core/WSLinkRepository.java

https://bitbucket.org/alshabani/dyno4ws
Java | 262 lines | 152 code | 35 blank | 75 comment | 18 complexity | 095ce1493e6fa17190457d19a271c161 MD5 | raw file
  1. /**
  2. *
  3. */
  4. package it.unisannio.ding.wslink.core;
  5. import it.unisannio.ding.dyno4ws.OffloadedField;
  6. import it.unisannio.ding.dyno4ws.ProfileData;
  7. import it.unisannio.ding.dyno4ws.repository.OffloadingRepository;
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.IOException;
  10. import java.io.StringWriter;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import java.util.zip.Deflater;
  18. import javax.xml.bind.JAXB;
  19. import com.mongodb.BasicDBObject;
  20. import com.mongodb.DB;
  21. import com.mongodb.DBCollection;
  22. import com.mongodb.DBCursor;
  23. import com.mongodb.DBObject;
  24. import com.mongodb.Mongo;
  25. /**
  26. * @author Quirino Zagarese
  27. *
  28. */
  29. public class WSLinkRepository implements OffloadingRepository {
  30. private DBCollection coll;
  31. private String dbname = "wslink";
  32. private long minWait = 100;
  33. private long maxWait = 6400;
  34. private static Logger logger = Logger.getLogger(WSLinkRepository.class
  35. .getName());
  36. private Map<String, OffloadedField> references = new HashMap<String, OffloadedField>();
  37. private Deflater compresser = new Deflater();
  38. public WSLinkRepository() {
  39. Mongo m = null;
  40. try {
  41. m = new Mongo("localhost");
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. logger.log(
  45. Level.INFO,
  46. "No Mongo instance found; fallback on memory. Only @Consistency(onsistencyType.KEEP_REFERENCE) supported.");
  47. }
  48. DB db = m.getDB(dbname);
  49. coll = db.getCollection("offloadedfield");
  50. }
  51. /*
  52. * (non-Javadoc)
  53. *
  54. * @see
  55. * it.unisannio.ding.dyno4ws.repository.OffloadingRepository#retrieveFieldsById
  56. * (java.lang.String, it.unisannio.ding.dyno4ws.ProfileData)
  57. */
  58. public List<OffloadedField> retrieveFieldsById(String id, ProfileData data) {
  59. List<OffloadedField> result = new ArrayList<OffloadedField>();
  60. OffloadedField item = references.get(id);
  61. if (item == null) {
  62. // field has been kept as a copy, retrieve it from db
  63. DBCursor cur = null;
  64. long sleepTime = minWait;
  65. while (sleepTime <= maxWait){
  66. BasicDBObject query = new BasicDBObject();
  67. query.put("idkey", id);
  68. cur = coll.find(query);
  69. int size = cur.size();
  70. if(size > 0){
  71. sleepTime = maxWait + 1;
  72. } else {
  73. Thread.yield();
  74. try {
  75. Thread.sleep(sleepTime);
  76. } catch (InterruptedException e) {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. }
  80. sleepTime *= 2;
  81. }
  82. }
  83. while (cur.hasNext()) {
  84. DBObject next = cur.next();
  85. OffloadedField f = new OffloadedField();
  86. f.setName((String) next.get("name"));
  87. f.setTimestamp((Long) next.get("timestamp"));
  88. f.setType((String) next.get("type"));
  89. f.setData((String) next.get("data"));
  90. f.setEncoding((String) next.get("enc"));
  91. result.add(f);
  92. }
  93. } else {
  94. // field has been kept as a reference, thus it was in the table
  95. if (item.getData() == null) {
  96. serializeItem(item);
  97. }
  98. // references.remove(id);
  99. result.add(item);
  100. }
  101. // StringWriter xml = new StringWriter();
  102. // JAXB.marshal(result, xml);
  103. // System.out.println(xml.toString());
  104. return result;
  105. }
  106. /*
  107. * (non-Javadoc)
  108. *
  109. * @see
  110. * it.unisannio.ding.dyno4ws.repository.OffloadingRepository#offloadParamFields
  111. * (java.lang.String, java.lang.String, java.lang.String, java.util.List)
  112. */
  113. public List<String> offloadParamFields(String nodeId, String invocationId,
  114. String paramName, List<OffloadedField> fields) {
  115. List<String> result = new ArrayList<String>();
  116. List<DBObject> docs = new ArrayList<DBObject>();
  117. for (OffloadedField f : fields) {
  118. String key = createKey(nodeId, invocationId, paramName, f.getName());
  119. if (f.isCopy()) {
  120. if (f.getData() == null) {
  121. serializeItem(f);
  122. }
  123. DBObject object = new BasicDBObject();
  124. object.put("idkey", key);
  125. object.put("name", f.getName());
  126. object.put("timestamp", f.getTimestamp());
  127. object.put("enc", f.getEncoding());
  128. object.put("data", f.getData());
  129. object.put("type", f.getType());
  130. docs.add(object);
  131. } else {
  132. references.put(key, f);
  133. }
  134. result.add(key);
  135. }
  136. save(docs);
  137. return result;
  138. }
  139. /*
  140. * (non-Javadoc)
  141. *
  142. * @see
  143. * it.unisannio.ding.dyno4ws.repository.OffloadingRepository#setStrategyEndpoint
  144. * (java.lang.String)
  145. */
  146. public void setStrategyEndpoint(String endpoint) {
  147. // TODO Auto-generated method stub
  148. }
  149. protected void serializeItem(OffloadedField item) {
  150. if (item.getEncoding().equalsIgnoreCase("xml")) {
  151. toXML(item);
  152. } else if (item.getEncoding().equalsIgnoreCase("gzip_xml")) {
  153. toXML(item);
  154. compress(item);
  155. }
  156. // else if (item.getEncoding().equalsIgnoreCase("json")) {
  157. // toJSON(item);
  158. // } else if (item.getEncoding().equalsIgnoreCase("gzip_json")) {
  159. // toJSON(item);
  160. // compress(item);
  161. // }
  162. // else if (item.getEncoding().equalsIgnoreCase("hessian")) {
  163. // toHessian(item);
  164. // }
  165. }
  166. private void compress(OffloadedField item) {
  167. byte[] input = null;
  168. input = item.getData().getBytes();
  169. // Compress the bytes
  170. compresser.setInput(input);
  171. compresser.finish();
  172. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
  173. byte[] buf = new byte[1024];
  174. while (!compresser.finished()) {
  175. int count = compresser.deflate(buf);
  176. bos.write(buf, 0, count);
  177. }
  178. try {
  179. bos.close();
  180. } catch (IOException e) {
  181. }
  182. // Get the compressed data
  183. byte[] compressedData = bos.toByteArray();
  184. // String data = Base64.encodeBase64String(compressedData);
  185. item.setData(new String(compressedData));
  186. }
  187. // private void toHessian(OffloadedField item) {
  188. // ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  189. // Hessian2Output hessian = new Hessian2Output(buffer);
  190. // SerializerFactory factory = new SerializerFactory();
  191. // factory.setAllowNonSerializable(true);
  192. // try {
  193. //
  194. // hessian.startMessage();
  195. // Serializer objectSerializer = factory.getObjectSerializer(item
  196. // .getReference().getClass());
  197. // objectSerializer.writeObject(item.getReference(), hessian);
  198. // hessian.completeMessage();
  199. // hessian.flush();
  200. // String data = Base64.encodeBase64String(buffer.toByteArray());
  201. // item.setData(data);
  202. // buffer.close();
  203. // hessian.close();
  204. // } catch (IOException e) {
  205. // // TODO Auto-generated catch block
  206. // e.printStackTrace();
  207. // }
  208. // }
  209. private void toXML(OffloadedField item) {
  210. StringWriter writer = new StringWriter();
  211. JAXB.marshal(item.getReference(), writer);
  212. item.setData(writer.toString());
  213. }
  214. // private void toJSON(OffloadedField item) {
  215. // String json = gson.toJson(item.getReference(), item.getReference()
  216. // .getClass());
  217. // item.setData(json);
  218. // }
  219. protected void save(List<DBObject> docs) {
  220. coll.insert(docs);
  221. }
  222. private String createKey(String nodeId, String invocationId,
  223. String paramName, String name) {
  224. return "{nodeId:" + nodeId + ",cid:" + invocationId + ",param:"
  225. + paramName + ",name:" + name + "}";
  226. }
  227. }