/spring/app_spring_service/src/main/java/org/cloudfoundry/canonical/apps/ReferenceDataRepository.java

https://github.com/remotesyssupport/vcap-test-assets · Java · 150 lines · 132 code · 18 blank · 0 comment · 3 complexity · 82348fa1d3f15dcbe4899682bddd140f MD5 · raw file

  1. package org.cloudfoundry.canonical.apps;
  2. import java.io.IOException;
  3. import java.net.UnknownHostException;
  4. import org.cloudfoundry.runtime.env.AbstractServiceInfo;
  5. import org.cloudfoundry.runtime.env.CloudEnvironment;
  6. import org.cloudfoundry.runtime.env.MongoServiceInfo;
  7. import org.cloudfoundry.runtime.env.RedisServiceInfo;
  8. import org.cloudfoundry.runtime.service.messaging.RabbitServiceCreator;
  9. import org.hibernate.SessionFactory;
  10. import org.springframework.amqp.rabbit.connection.Connection;
  11. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  12. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.orm.hibernate3.HibernateTemplate;
  15. import redis.clients.jedis.Jedis;
  16. import com.mongodb.BasicDBObject;
  17. import com.mongodb.DB;
  18. import com.mongodb.DBCollection;
  19. import com.mongodb.DBCursor;
  20. import com.mongodb.Mongo;
  21. import com.mongodb.MongoException;
  22. import com.rabbitmq.client.Channel;
  23. public class ReferenceDataRepository {
  24. private HibernateTemplate hibernateTemplate;
  25. public void setSessionFactory(SessionFactory sessionFactory) {
  26. this.hibernateTemplate = new HibernateTemplate(sessionFactory);
  27. }
  28. public void save(DataValue dataValue) {
  29. this.hibernateTemplate.saveOrUpdate(dataValue);
  30. }
  31. public DataValue find(String id) {
  32. return (DataValue) hibernateTemplate.find(
  33. "from DataValue where id='" + id + "'").get(0);
  34. }
  35. public void write_to_rabbitmq(String key, String value) throws IOException {
  36. ConnectionFactory rabbitConnectionFactory = new RabbitServiceCreator(environment()).createSingletonService().service;
  37. Connection conn = rabbitConnectionFactory.createConnection();
  38. Channel channel = conn.createChannel(true);
  39. channel.exchangeDeclare(key, "direct");
  40. channel.queueDeclare(key, true, false, false, null);
  41. RabbitTemplate amq = new RabbitTemplate(
  42. (ConnectionFactory) rabbitConnectionFactory);
  43. amq.convertAndSend(key, value);
  44. channel.close();
  45. conn.close();
  46. }
  47. public String read_from_rabbitmq(String key) throws IOException {
  48. ConnectionFactory rabbitConnectionFactory = new RabbitServiceCreator(environment()).createSingletonService().service;
  49. Connection conn = rabbitConnectionFactory.createConnection();
  50. Channel channel = conn.createChannel(true);
  51. channel.exchangeDeclare(key, "direct");
  52. channel.queueDeclare(key, true, false, false, null);
  53. RabbitTemplate amq = new RabbitTemplate(
  54. (ConnectionFactory) rabbitConnectionFactory);
  55. String value = (String) amq.receiveAndConvert(key);
  56. channel.close();
  57. conn.close();
  58. return value;
  59. }
  60. public void write_to_mongo(String key, String value) {
  61. MongoServiceInfo service = (MongoServiceInfo) getService(MongoServiceInfo.class);
  62. Mongo m = null;
  63. DBCollection coll = null;
  64. DB db = null;
  65. try {
  66. m = new Mongo(service.getHost(), service.getPort());
  67. } catch (UnknownHostException e) {
  68. e.printStackTrace();
  69. } catch (MongoException e) {
  70. e.printStackTrace();
  71. }
  72. db = m.getDB(service.getDatabase());
  73. if (db.authenticate(service.getUserName(), service.getPassword()
  74. .toCharArray())) {
  75. coll = db.getCollection(service.getServiceName());
  76. }
  77. BasicDBObject doc = new BasicDBObject();
  78. doc.put("key", key);
  79. doc.put("data_value", value);
  80. coll.insert(doc);
  81. m.close();
  82. }
  83. public String read_from_mongo(String key) {
  84. MongoServiceInfo service = (MongoServiceInfo) getService(MongoServiceInfo.class);
  85. Mongo m = null;
  86. DBCollection coll = null;
  87. DB db = null;
  88. try {
  89. m = new Mongo(service.getHost(), service.getPort());
  90. } catch (UnknownHostException e) {
  91. e.printStackTrace();
  92. } catch (MongoException e) {
  93. e.printStackTrace();
  94. }
  95. db = m.getDB(service.getDatabase());
  96. if (db.authenticate(service.getUserName(), service.getPassword()
  97. .toCharArray())) {
  98. coll = db.getCollection(service.getServiceName());
  99. }
  100. BasicDBObject query = new BasicDBObject();
  101. query.put("key", key);
  102. DBCursor cur = coll.find(query);
  103. String value = "";
  104. while (cur.hasNext()) {
  105. value = (String) cur.next().get("data_value");
  106. }
  107. cur.close();
  108. m.close();
  109. return value;
  110. }
  111. public void write_to_redis(String key, String value) {
  112. RedisServiceInfo service = (RedisServiceInfo) getService(RedisServiceInfo.class);
  113. Jedis jedis = new Jedis(service.getHost(), service.getPort());
  114. jedis.auth(service.getPassword());
  115. jedis.set(key, value);
  116. }
  117. public String read_from_redis(String key) {
  118. RedisServiceInfo service = (RedisServiceInfo) getService(RedisServiceInfo.class);
  119. Jedis jedis = new Jedis(service.getHost(), service.getPort());
  120. jedis.auth(service.getPassword());
  121. return jedis.get(key);
  122. }
  123. @Bean
  124. CloudEnvironment environment() {
  125. return new CloudEnvironment();
  126. }
  127. private <T extends AbstractServiceInfo> AbstractServiceInfo getService(
  128. Class<T> service) {
  129. CloudEnvironment env = environment();
  130. return env.getServiceInfos(service).get(0);
  131. }
  132. }