/src/main/java/org/graylog2/streams/Stream.java

https://github.com/meddah/graylog2-server · Java · 151 lines · 91 code · 27 blank · 33 comment · 14 complexity · 2828ee86f7a2b27e3b1987c964e3f0f8 MD5 · raw file

  1. /**
  2. * Copyright 2011 Lennart Koopmann <lennart@socketfeed.com>
  3. *
  4. * This file is part of Graylog2.
  5. *
  6. * Graylog2 is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Graylog2 is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Graylog2. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package org.graylog2.streams;
  21. import com.mongodb.*;
  22. import org.apache.log4j.Logger;
  23. import org.bson.types.ObjectId;
  24. import org.graylog2.database.MongoConnection;
  25. import org.graylog2.forwarders.ForwardEndpoint;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. /**
  29. * Stream.java: Mar 26, 2011 10:39:40 PM
  30. *
  31. * Representing a single stream from the streams collection. Also provides method
  32. * to get all streams of this collection.
  33. *
  34. * @author: Lennart Koopmann <lennart@socketfeed.com>
  35. */
  36. public class Stream {
  37. private static final Logger LOG = Logger.getLogger(Stream.class);
  38. private ObjectId id = null;
  39. private String title = null;
  40. private List<StreamRule> streamRules = null;
  41. private List<ForwardEndpoint> forwardedTo = null;
  42. private DBObject mongoObject = null;
  43. public Stream (DBObject stream) {
  44. this.id = (ObjectId) stream.get("_id");
  45. this.title = (String) stream.get("title");
  46. this.mongoObject = stream;
  47. }
  48. public static ArrayList<Stream> fetchAllEnabled() {
  49. if (StreamCache.getInstance().valid()) {
  50. return StreamCache.getInstance().get();
  51. }
  52. ArrayList<Stream> streams = new ArrayList<Stream>();
  53. DBCollection coll = MongoConnection.getInstance().getDatabase().getCollection("streams");
  54. DBObject query = new BasicDBObject();
  55. query.put("disabled", new BasicDBObject("$ne", true));
  56. DBCursor cur = coll.find(query);
  57. while (cur.hasNext()) {
  58. try {
  59. streams.add(new Stream(cur.next()));
  60. } catch (Exception e) {
  61. LOG.warn("Can't fetch stream. Skipping. " + e.getMessage(), e);
  62. }
  63. }
  64. StreamCache.getInstance().set(streams);
  65. return streams;
  66. }
  67. public List<StreamRule> getStreamRules() {
  68. if (this.streamRules != null) {
  69. return this.streamRules;
  70. }
  71. ArrayList<StreamRule> rules = new ArrayList<StreamRule>();
  72. BasicDBList rawRules = (BasicDBList) this.mongoObject.get("streamrules");
  73. if (rawRules != null && rawRules.size() > 0) {
  74. for (Object ruleObj : rawRules) {
  75. try {
  76. StreamRule rule = new StreamRule((DBObject) ruleObj);
  77. rules.add(rule);
  78. } catch (Exception e) {
  79. LOG.warn("Skipping stream rule in Stream.getStreamRules(): " + e.getMessage(), e);
  80. continue;
  81. }
  82. }
  83. }
  84. this.streamRules = rules;
  85. return rules;
  86. }
  87. public List<ForwardEndpoint> getForwardedTo() {
  88. if (this.forwardedTo != null) {
  89. return this.forwardedTo;
  90. }
  91. ArrayList<ForwardEndpoint> fwds = new ArrayList<ForwardEndpoint>();
  92. BasicDBList rawFwds = (BasicDBList) this.mongoObject.get("forwarders");
  93. if (rawFwds != null && rawFwds.size() > 0) {
  94. for (Object fwdObj : rawFwds) {
  95. try {
  96. ForwardEndpoint fwd = new ForwardEndpoint((DBObject) fwdObj);
  97. fwds.add(fwd);
  98. } catch (Exception e) {
  99. LOG.warn("Skipping forward endpoint in Stream.getForwardedTo(): " + e.getMessage(), e);
  100. continue;
  101. }
  102. }
  103. }
  104. this.forwardedTo = fwds;
  105. return fwds;
  106. }
  107. /**
  108. * @return the id
  109. */
  110. public ObjectId getId() {
  111. return id;
  112. }
  113. /**
  114. * @return the title
  115. */
  116. public String getTitle() {
  117. return title;
  118. }
  119. @Override
  120. public String toString() {
  121. this.getStreamRules();
  122. return this.id.toString() + ":" + this.title;
  123. }
  124. }