Skip to content

Commit 4197ab1

Browse files
committed
maven refactor
1 parent aac5c45 commit 4197ab1

File tree

75 files changed

+6956
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+6956
-0
lines changed

dashboard/pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.ibm.runtimetools</groupId>
5+
<version>1.0.0</version>
6+
<artifactId>javametrics-dash</artifactId>
7+
<packaging>war</packaging>
8+
<name>javametrics-dash</name>
9+
<description>Application Metrics for Java (web UI)</description>
10+
11+
<distributionManagement>
12+
<snapshotRepository>
13+
<id>ossrh</id>
14+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
15+
</snapshotRepository>
16+
<repository>
17+
<id>ossrh</id>
18+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
19+
</repository>
20+
</distributionManagement>
21+
22+
23+
24+
<properties>
25+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
27+
</properties>
28+
<dependencies>
29+
<dependency>
30+
<groupId>javax.json</groupId>
31+
<artifactId>javax.json-api</artifactId>
32+
<version>1.1</version>
33+
<scope>compile</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.ibm.runtimetools</groupId>
37+
<artifactId>javametrics-agent</artifactId>
38+
<version>1.0.0</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>javax.websocket</groupId>
42+
<artifactId>javax.websocket-api</artifactId>
43+
<version>1.1</version>
44+
<scope>compile</scope>
45+
</dependency>
46+
</dependencies>
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.sonatype.plugins</groupId>
51+
<artifactId>nexus-staging-maven-plugin</artifactId>
52+
<version>1.6.7</version>
53+
<extensions>true</extensions>
54+
<configuration>
55+
<serverId>ossrh</serverId>
56+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
57+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
58+
</configuration>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-war-plugin</artifactId>
63+
<version>3.1.0</version>
64+
<configuration>
65+
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
66+
</configuration>
67+
</plugin>
68+
<plugin>
69+
<artifactId>maven-compiler-plugin</artifactId>
70+
<version>3.6.1</version>
71+
<configuration>
72+
<source>1.8</source>
73+
<target>1.8</target>
74+
</configuration>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
</project>
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
public interface Emitter {
19+
public void emit(String message);
20+
}

0 commit comments

Comments
 (0)