PageRenderTime 39ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/PServiceBus.Java/pservicebus/Topic.java

#
Java | 274 lines | 227 code | 47 blank | 0 comment | 18 complexity | d0b80498af6d95da1f8851dc03871636 MD5 | raw file
  1. package pservicebus;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.lang.reflect.*;
  5. import pservicebus.*;
  6. import pservicebus.exceptions.*;
  7. import pservicebus.runtime.*;
  8. import pservicebus.extensions.*;
  9. import pservicebus.annotations.*;
  10. public class Topic {
  11. private String _topicName;
  12. private ObjectState _state;
  13. private Map<String, String> _contractDict;
  14. private Map<String, Object> _publishDict;
  15. private Long _topicID;
  16. private String _filter;
  17. private List<String> _filters;
  18. private Map<String, Object> _headers;
  19. private String _topicDescription;
  20. private int _expiresIn;
  21. public String getTopicFilter(){
  22. String[] filterArray = (String[])_filters.toArray(new String[0]);
  23. String[] array = !StringExtension.isNullOrEmpty(_filter) ? _filter.split(",") : new String[]{};
  24. return StringExtension.join(",", ArrayExtension.union(array, filterArray));
  25. }
  26. private void validateObjectState() throws TopicNotRegisteredException{
  27. if(_state == ObjectState.InValid) throw new TopicNotRegisteredException(ExceptionConstants.topicExceptionStr(_topicName));
  28. }
  29. private Topic(String name){
  30. _topicName = name;
  31. _state = ObjectState.Valid;
  32. _filters = new ArrayList<String>();
  33. _contractDict = new HashMap<String, String>();
  34. _publishDict = new HashMap<String, Object>();
  35. _headers = new HashMap<String, Object>();
  36. _topicDescription = StringExtension.empty;
  37. }
  38. public String getTopicName(){ return _topicName; }
  39. public ObjectState getState(){ return _state; }
  40. public Map<String, String> getContractDict(){ return _contractDict; }
  41. public Map<String, Object> getPublishDict(){ return _publishDict; }
  42. public Long getTopicID(){ return _topicID; }
  43. public List<String> getFilters(){ return _filters; }
  44. public Map<String, Object> getHeaders(){ return _headers; }
  45. public String getTopicDescription(){ return _topicDescription; }
  46. public int getExpiresIn(){ return _expiresIn; }
  47. public Topic filter(String topicFilter) throws TopicNotRegisteredException {
  48. validateObjectState();
  49. _filter = topicFilter;
  50. _filters = Arrays.asList(topicFilter.split(","));
  51. return this;
  52. }
  53. public Topic addParameter(String name, String description) throws TopicNotRegisteredException {
  54. validateObjectState();
  55. _contractDict.put(name, description);
  56. return this;
  57. }
  58. public Topic setParameter(String name, Object value) throws TopicNotRegisteredException {
  59. validateObjectState();
  60. _publishDict.put(name, value);
  61. return this;
  62. }
  63. public void register() throws TopicNotRegisteredException, ESBException, IOException {
  64. validateObjectState();
  65. String result =
  66. RestHelper.invoke("RegisterTopic",
  67. HashBuilder.create()
  68. .add("topicData",
  69. RestHelper.toJson(
  70. HashBuilder.create()
  71. .add("ContractDict", _contractDict)
  72. .add("TopicName", _topicName)
  73. .add("TopicDescription", _topicDescription)
  74. .getHash())
  75. ).getHash());
  76. ESBHelper.throwExceptionIfNeeded(result);
  77. }
  78. public void delete() throws TopicNotRegisteredException, ESBException, IOException {
  79. validateObjectState();
  80. String result = RestHelper.invoke("DeleteTopic", HashBuilder.create().add("name", _topicName).getHash());
  81. ESBHelper.throwExceptionIfNeeded(result);
  82. }
  83. public Topic setHeader(String key, Object value) throws TopicNotRegisteredException {
  84. validateObjectState();
  85. _headers.put(key, value);
  86. return this;
  87. }
  88. public Topic description(String desc) throws TopicNotRegisteredException {
  89. validateObjectState();
  90. _topicDescription = desc;
  91. return this;
  92. }
  93. public Topic setMessageExpiration(int expiresIn) throws TopicNotRegisteredException {
  94. validateObjectState();
  95. _expiresIn = expiresIn;
  96. return this;
  97. }
  98. public Topic message(Object msg) throws TopicNotRegisteredException, IllegalAccessException, InvocationTargetException {
  99. validateObjectState();
  100. Map<String, Object> dict = ESBHelper.toDict(msg);
  101. for(Map.Entry<String, Object> entry : dict.entrySet()) _publishDict.put(entry.getKey(), entry.getValue());
  102. return this;
  103. }
  104. public void publish(Object message) throws TopicNotRegisteredException, ESBException, IOException, IllegalAccessException, InvocationTargetException {
  105. validateObjectState();
  106. List<Object> list = new ArrayList<Object>();
  107. list.add(message);
  108. internalPublish(list);
  109. }
  110. public void publishMany(List<Object> messages) throws TopicNotRegisteredException, ESBException, IOException, IllegalAccessException, InvocationTargetException {
  111. validateObjectState();
  112. internalPublish(messages);
  113. }
  114. public void publish() throws TopicNotRegisteredException, ESBException, IOException, IllegalAccessException, InvocationTargetException {
  115. validateObjectState();
  116. internalPublish(null);
  117. }
  118. private void internalPublish(List<Object> messages) throws TopicNotRegisteredException, ESBException, IOException, IllegalAccessException, InvocationTargetException {
  119. List<Map<String, Object>> publishDicts = new ArrayList<Map<String, Object>>();
  120. if(messages != null){
  121. for(Object message : messages)
  122. publishDicts.add(ESBHelper.toDict(message));
  123. }
  124. if(_publishDict != null && _publishDict.size() > 0) publishDicts.add(_publishDict);
  125. String result = RestHelper.invoke("PublishTopic",
  126. HashBuilder.create()
  127. .add("topicName", _topicName)
  128. .add("topicData",
  129. RestHelper.toJson(
  130. HashBuilder.create()
  131. .add("Headers", _headers)
  132. .add("ExpiresIn", _expiresIn)
  133. .getHash()))
  134. .add("publishData", RestHelper.toJson(publishDicts))
  135. .getHash()
  136. );
  137. ESBHelper.throwExceptionIfNeeded(result);
  138. }
  139. private Topic filterByType(String propertyName, String value, String oper){
  140. _filters.add(String.format("%s %s %s", propertyName, oper, value));
  141. return this;
  142. }
  143. public Topic equal(String propertyName, String value) {
  144. return filterByType(propertyName, value, "=");
  145. }
  146. public Topic notEqual(String propertyName, String value) {
  147. return filterByType(propertyName, value, "!=");
  148. }
  149. public Topic lessThan(String propertyName, String value) {
  150. return filterByType(propertyName, value, "<");
  151. }
  152. public Topic lessThanOrEqual(String propertyName, String value) {
  153. return filterByType(propertyName, value, "<=");
  154. }
  155. public Topic greaterThan(String propertyName, String value) {
  156. return filterByType(propertyName, value, ">");
  157. }
  158. public Topic greaterThanOrEqual(String propertyName, String value) {
  159. return filterByType(propertyName, value, ">=");
  160. }
  161. public Topic contains(String propertyName, String value) {
  162. return filterByType(propertyName, value, "like");
  163. }
  164. public static Topic create(String name){
  165. return new Topic(name);
  166. }
  167. @SuppressWarnings("unchecked")
  168. public static void register(Class<?> type) throws TopicNotRegisteredException, ESBException, IOException {
  169. String typeName = type.getName();
  170. Topic topic = Topic.create(parseTopicName(typeName));
  171. Description typeAnnotation = type.getAnnotation(Description.class);
  172. String topicDesc = typeAnnotation != null ? typeAnnotation.description() : typeName;
  173. topic.description(topicDesc);
  174. Field[] fields = type.getFields();
  175. for(Field field : fields){
  176. String fieldName = field.getName();
  177. TopicHeader fieldTopicHeaderAnnotation = field.getAnnotation(TopicHeader.class);
  178. if(fieldTopicHeaderAnnotation != null) continue;
  179. Description fieldAnnotation = field.getAnnotation(Description.class);
  180. String fieldDesc = fieldAnnotation != null ? fieldAnnotation.description() : StringExtension.empty;
  181. topic.addParameter(fieldName, fieldDesc);
  182. }
  183. topic.register();
  184. }
  185. public static void publishMessage(Object message) throws TopicNotRegisteredException, ESBException, IOException, IllegalAccessException, InvocationTargetException {
  186. Class type = message.getClass();
  187. String topicName = type.getName();
  188. Topic topic = Topic.select(parseTopicName(topicName));
  189. topic.publish(message);
  190. }
  191. private static String parseTopicName(String name){
  192. String[] tokens = name.split("\\.");
  193. return tokens[tokens.length - 1];
  194. }
  195. public static void publishMessages(List<Object> messages) throws TopicNotRegisteredException, ESBException, IOException, IllegalAccessException, InvocationTargetException {
  196. Class type = messages.get(0).getClass();
  197. String topicName = type.getName();
  198. Topic topic = Topic.select(parseTopicName(topicName));
  199. topic.publishMany(messages);
  200. }
  201. public static Topic select(Class<?> type) throws IOException, TopicNotRegisteredException, ESBException {
  202. String topicName = type.getName();
  203. return select(parseTopicName(topicName));
  204. }
  205. protected Topic setState(ObjectState state){
  206. _state = state;
  207. return this;
  208. }
  209. @SuppressWarnings("unchecked")
  210. protected static Topic dictionaryToTopic(Map<String, Object> data) throws TopicNotRegisteredException{
  211. Map<String, String> contractDict = (Map<String, String>)data.get("ContractDict");
  212. Topic topic = Topic.create((String)data.get("TopicName")).description((String)data.get("TopicDescription"));
  213. topic._topicID = new Long(data.get("TopicID").toString());
  214. topic._state = ObjectState.get((Integer)data.get("State"));
  215. for(Map.Entry<String, String> entry : contractDict.entrySet())
  216. topic._contractDict.put(entry.getKey(), entry.getValue());
  217. return topic;
  218. }
  219. public static Topic select(String name) throws IOException, TopicNotRegisteredException, ESBException {
  220. Topic topic = null;
  221. String result = RestHelper.invoke("SelectTopic", HashBuilder.create().add("name", name).getHash());
  222. Map<String, Object> data = RestHelper.fromJsonToMap(result);
  223. if(data != null) topic = dictionaryToTopic(data);
  224. else ESBHelper.throwExceptionIfNeeded(result);
  225. return topic;
  226. }
  227. }