|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2017 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + ******************************************************************************/ |
| 16 | +package com.ibm.javametrics.web; |
| 17 | + |
| 18 | +import java.io.StringReader; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import javax.json.Json; |
| 26 | +import javax.json.JsonException; |
| 27 | +import javax.json.JsonObject; |
| 28 | +import javax.json.JsonReader; |
| 29 | + |
| 30 | +import com.ibm.javametrics.Javametrics; |
| 31 | +import com.ibm.javametrics.JavametricsListener; |
| 32 | + |
| 33 | +/** |
| 34 | + * Registers as a JavametricsListener to receive metrics data, processes the |
| 35 | + * data and sends the output to any registered emitters |
| 36 | + * |
| 37 | + */ |
| 38 | +public class DataHandler implements JavametricsListener { |
| 39 | + |
| 40 | + private static DataHandler instance = null; |
| 41 | + private Set<Emitter> emitters = new HashSet<Emitter>(); |
| 42 | + |
| 43 | + private HttpDataAggregator aggregateHttpData; |
| 44 | + |
| 45 | + private static DataHandler getInstance() { |
| 46 | + if (instance == null) { |
| 47 | + instance = new DataHandler(); |
| 48 | + } |
| 49 | + return instance; |
| 50 | + } |
| 51 | + |
| 52 | + protected DataHandler() { |
| 53 | + this.aggregateHttpData = new HttpDataAggregator(); |
| 54 | + } |
| 55 | + |
| 56 | + public void addEmitter(Emitter emitter) { |
| 57 | + emitters.add(emitter); |
| 58 | + /* |
| 59 | + * adding as listener has the side effect of sending history which is |
| 60 | + * required for any newly registered emitter to see the Environment daya |
| 61 | + */ |
| 62 | + Javametrics.getInstance().addListener(this); |
| 63 | + } |
| 64 | + |
| 65 | + public static void registerEmitter(Emitter emitter) { |
| 66 | + getInstance().addEmitter(emitter); |
| 67 | + } |
| 68 | + |
| 69 | + public void removeEmitter(Emitter emitter) { |
| 70 | + emitters.remove(emitter); |
| 71 | + if (emitters.isEmpty()) { |
| 72 | + Javametrics.getInstance().removeListener(this); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public static void deregisterEmitter(Emitter emitter) { |
| 77 | + getInstance().removeEmitter(emitter); |
| 78 | + } |
| 79 | + |
| 80 | + public void emit(String message) { |
| 81 | + emitters.forEach((emitter) -> { |
| 82 | + emitter.emit(message); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void receive(String pluginName, String data) { |
| 88 | + if (pluginName.equals("api")) { |
| 89 | + List<String> split = splitIntoJSONObjects(data); |
| 90 | + for (Iterator<String> iterator = split.iterator(); iterator.hasNext();) { |
| 91 | + String jsonStr = iterator.next(); |
| 92 | + JsonReader jsonReader = Json.createReader(new StringReader(jsonStr)); |
| 93 | + try { |
| 94 | + JsonObject jsonObject = jsonReader.readObject(); |
| 95 | + String topicName = jsonObject.getString("topic", null); |
| 96 | + if (topicName != null) { |
| 97 | + if (topicName.equals("http")) { |
| 98 | + synchronized (aggregateHttpData) { |
| 99 | + aggregateHttpData.aggregate(jsonObject.getJsonObject("payload")); |
| 100 | + } |
| 101 | + } else { |
| 102 | + emit(jsonObject.toString()); |
| 103 | + } |
| 104 | + } |
| 105 | + } catch (JsonException je) { |
| 106 | + // Skip this object, log the exception and keep trying with |
| 107 | + // the rest of the list |
| 108 | + je.printStackTrace(); |
| 109 | + } |
| 110 | + } |
| 111 | + emitHttp(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private void emitHttp() { |
| 116 | + HttpDataAggregator httpData; |
| 117 | + String httpUrlData; |
| 118 | + synchronized (aggregateHttpData) { |
| 119 | + httpData = aggregateHttpData.getCurrent(); |
| 120 | + if (aggregateHttpData.total == 0) { |
| 121 | + httpData.time = System.currentTimeMillis(); |
| 122 | + } |
| 123 | + httpUrlData = aggregateHttpData.urlDatatoJsonString(); |
| 124 | + aggregateHttpData.clear(); |
| 125 | + } |
| 126 | + emit(httpData.toJsonString()); |
| 127 | + emit(httpUrlData); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Split a string of JSON objects into multiple strings |
| 132 | + * |
| 133 | + * @param data |
| 134 | + * @return |
| 135 | + */ |
| 136 | + private List<String> splitIntoJSONObjects(String data) { |
| 137 | + List<String> strings = new ArrayList<String>(); |
| 138 | + int index = 0; |
| 139 | + // Find first opening bracket |
| 140 | + while (index < data.length() && data.charAt(index) != '{') { |
| 141 | + index++; |
| 142 | + } |
| 143 | + int closingBracket = index + 1; |
| 144 | + int bracketCounter = 1; |
| 145 | + while (index < data.length() - 1 && closingBracket < data.length()) { |
| 146 | + // Find the matching bracket for the bracket at location 'index' |
| 147 | + boolean found = false; |
| 148 | + if (data.charAt(closingBracket) == '{') { |
| 149 | + bracketCounter++; |
| 150 | + } else if (data.charAt(closingBracket) == '}') { |
| 151 | + bracketCounter--; |
| 152 | + if (bracketCounter == 0) { |
| 153 | + // found matching bracket |
| 154 | + found = true; |
| 155 | + } |
| 156 | + } |
| 157 | + if (found) { |
| 158 | + strings.add(data.substring(index, closingBracket + 1)); |
| 159 | + index = closingBracket + 1; |
| 160 | + // Find next opening bracket and reset counters |
| 161 | + while (index < data.length() && data.charAt(index) != '{') { |
| 162 | + index++; |
| 163 | + } |
| 164 | + closingBracket = index + 1; |
| 165 | + bracketCounter = 1; |
| 166 | + } else { |
| 167 | + closingBracket++; |
| 168 | + } |
| 169 | + } |
| 170 | + return strings; |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments