Skip to content

Commit

Permalink
Adjusted for the new version of the monitor
Browse files Browse the repository at this point in the history
* NotFound Exceptions added
* New examples added to the main class
  • Loading branch information
Fruchuxs committed Jan 5, 2015
1 parent cec0914 commit 09f6b6e
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 92 deletions.
119 changes: 90 additions & 29 deletions client/src/main/java/de/hsos/ecs/richwps/wpsmonitor/client/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package de.hsos.ecs.richwps.wpsmonitor.client;

import de.hsos.ecs.richwps.wpsmonitor.client.exception.WpsMonitorClientCreateException;
import de.hsos.ecs.richwps.wpsmonitor.client.exception.WpsMonitorClientException;
import de.hsos.ecs.richwps.wpsmonitor.client.exception.WpsMonitorClientWpsProcessNotFoundException;
import de.hsos.ecs.richwps.wpsmonitor.client.exception.WpsMonitorClientWpsNotFoundException;
import de.hsos.ecs.richwps.wpsmonitor.client.resource.WpsMetricResource;
import de.hsos.ecs.richwps.wpsmonitor.client.resource.WpsProcessResource;
import de.hsos.ecs.richwps.wpsmonitor.client.resource.WpsResource;
Expand All @@ -24,42 +28,99 @@
import java.util.logging.Logger;

/**
* This is a simple Code Demonstration of the WpsMontiorClient. This Code also
* serves as a TestCode. To use this Code as a Test you must registred the WPS
* "http://localhost:8080/wps/WebProcessingService" and the Process
* "SimpleBuffer" in the WPSMonitor or simply change the static members below.
*
* Make sure the monitor is started and avaible (see the monitorUrl static
* Member).
*
* @author Florian Vogelpohl <[email protected]>
*/
public class Main {

private static URL monitorUrl;
private static URL registredWpsUrl;
private static URL nonExistsWpsUrl;
private static String registredWpsProcess;
private static String nonExistsWpsProcess;

static {
try {
monitorUrl = new URL("http://localhost:1111/");
registredWpsUrl = new URL("http://localhost:8080/wps/WebProcessingService");
nonExistsWpsUrl = new URL("http://example.com/this/wps/should/not/be/exists");
registredWpsProcess = "SimpleBuffer";
nonExistsWpsProcess = "AnyProcessWhichShouldNotExists";
} catch (MalformedURLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void listWps(final WpsMonitorClient client) throws WpsMonitorClientException {
for (final WpsResource wps : client.getAllWps()) {
System.out.println(wps);
}
}

public void wpsNotFound(final WpsMonitorClient client) throws WpsMonitorClientException {
try {
client.getWps(nonExistsWpsUrl);
} catch (WpsMonitorClientWpsNotFoundException ex) {
System.out.println(ex);
}
}

public void wpsNotFound2(final WpsMonitorClient client) throws WpsMonitorClientException {
try {
client.getWpsProcess(nonExistsWpsUrl, registredWpsProcess);
} catch (WpsMonitorClientWpsNotFoundException ex) {
System.out.println(ex);
}
}

public void wpsProcessNotFound(final WpsMonitorClient client) throws WpsMonitorClientException {
try {
client.getWpsProcess(registredWpsUrl, nonExistsWpsProcess);
} catch (WpsMonitorClientWpsProcessNotFoundException ex) {
System.out.println(ex);
}
}

public void findWpsProcess(final WpsMonitorClient client) throws WpsMonitorClientException {
WpsProcessResource wpsProcess = client.getWpsProcess(registredWpsUrl, registredWpsProcess);

System.out.println("Wps Process:");
System.out.println(wpsProcess.toString());
System.out.println("Metrics:");

for (final WpsMetricResource metric : wpsProcess.getMetricsAsList()) {
System.out.println(metric.toString());
}
}

public static void main(String[] args) {
try {
WpsMonitorClient wpsMonitorClient = new WpsMonitorClientFactory().create(new URL("http://localhost:1111/"));

System.out.println("List of WPS are available in the Monitor");
System.out.println("----");

for (final WpsResource wpsResource : wpsMonitorClient.getAllWps()) {
System.out.println(wpsResource);
}

System.out.println("----");

WpsResource pickup = wpsMonitorClient.getWps(new URL("http://localhost:8080/wps/WebProcessingService"));

if (pickup != null) {
WpsProcessResource wpsProcess = wpsMonitorClient.getWpsProcess(pickup, "Blubb");

if (wpsProcess != null) {
System.out.println("Metrics");

for(WpsMetricResource r : wpsProcess.getMetricsAsList()) {
System.out.println(r);
}
} else {
System.out.println("Can't demonstrate the getProcessMetrics method, because there are no WpsProcess with the name \"Blubb\" are registrated in the Monitor");
}
} else {
System.out.println("Can't demonstrate the metrics method, because there are no WPS registrated in the Monitor.");
}
} catch (MalformedURLException | WpsMonitorClientException | WpsMonitorClientCreateException ex) {
WpsMonitorClient wpsMonitorClient = new WpsMonitorClientFactory().create(monitorUrl);

Main main = new Main();
System.out.println("List WPS:");
main.listWps(wpsMonitorClient);

System.out.println("WPS Not Found:");
main.wpsNotFound(wpsMonitorClient);

System.out.println("WPS also Not Found:");
main.wpsNotFound2(wpsMonitorClient);

System.out.println("WPS Process not found but the WPS endpoint is registred:");
main.wpsProcessNotFound(wpsMonitorClient);

System.out.println("Show a registred WPS Process and the Metrics:");
main.findWpsProcess(wpsMonitorClient);

} catch (WpsMonitorClientException | WpsMonitorClientCreateException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package de.hsos.ecs.richwps.wpsmonitor.client;

import de.hsos.ecs.richwps.wpsmonitor.client.exception.WpsMonitorClientException;
import de.hsos.ecs.richwps.wpsmonitor.client.resource.WpsProcessResource;
import de.hsos.ecs.richwps.wpsmonitor.client.resource.WpsResource;
import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package de.hsos.ecs.richwps.wpsmonitor.client;

import de.hsos.ecs.richwps.wpsmonitor.client.exception.WpsMonitorClientCreateException;
import com.google.gson.Gson;
import de.hsos.ecs.richwps.wpsmonitor.client.http.HttpException;
import de.hsos.ecs.richwps.wpsmonitor.client.http.WpsMonitorRequester;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package de.hsos.ecs.richwps.wpsmonitor.client;

import de.hsos.ecs.richwps.wpsmonitor.client.exception.WpsMonitorClientException;
import de.hsos.ecs.richwps.wpsmonitor.client.exception.WpsMonitorClientWpsProcessNotFoundException;
import de.hsos.ecs.richwps.wpsmonitor.client.exception.WpsMonitorClientWpsNotFoundException;
import de.hsos.ecs.richwps.wpsmonitor.client.http.HttpException;
import de.hsos.ecs.richwps.wpsmonitor.client.http.WpsMonitorRequester;
import de.hsos.ecs.richwps.wpsmonitor.client.resource.WpsProcessResource;
Expand Down Expand Up @@ -71,7 +74,7 @@ private void initContext(final Boolean refresh) throws HttpException {
List<WpsEntity> wpsList = requester.getWpsList();

for (final WpsEntity entity : wpsList) {
wpsContext.put(entity.getUri(), ResourceConverter.WpsEntityToResource(entity));
wpsContext.put(entity.getEndpoint(), ResourceConverter.WpsEntityToResource(entity));
}
}
}
Expand Down Expand Up @@ -100,7 +103,13 @@ public WpsProcessResource getWpsProcess(final WpsResource wpsResource, final Str
public WpsProcessResource getWpsProcess(final WpsResource wpsResource, final String wpsProcessIdentifier, final Integer consider)
throws WpsMonitorClientException {
try {
return requester.getProcess(wpsResource, wpsProcessIdentifier, consider);
WpsProcessResource process = requester.getProcess(wpsResource, wpsProcessIdentifier, consider);

if(process == null) {
throw new WpsMonitorClientWpsProcessNotFoundException(wpsProcessIdentifier, wpsResource.getWpsEndPoint());
}

return process;
} catch (HttpException ex) {
throw new WpsMonitorClientException("Can't Request Process metrics.", ex);
}
Expand All @@ -120,7 +129,11 @@ public WpsResource getWps(final URL wpsEndPoint, final Boolean forceRefresh)
try {
initContext(forceRefresh);
WpsResource result = wpsContext.get(wpsEndPoint);


if(result == null) {
throw new WpsMonitorClientWpsNotFoundException(wpsEndPoint);
}

return result;
} catch (HttpException ex) {
throw new WpsMonitorClientException("Can't initalize WpsContent.", ex);
Expand All @@ -140,7 +153,7 @@ public List<WpsResource> getAllWps(final Boolean forceRefresh)
initContext(forceRefresh);
return new ArrayList<>(wpsContext.values());
} catch (HttpException ex) {
throw new WpsMonitorClientException("Can't request List of WPS.", ex);
throw new WpsMonitorClientException("Can't request List of WPS. Maybe the Monitor is offline (used endpoint: " + monitorEndpoint.toString() + ")", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.hsos.ecs.richwps.wpsmonitor.client;
package de.hsos.ecs.richwps.wpsmonitor.client.exception;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.hsos.ecs.richwps.wpsmonitor.client;
package de.hsos.ecs.richwps.wpsmonitor.client.exception;

/**
* WpsMonitorClientException capsule any Exceptions, e.g. HttpException.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.hsos.ecs.richwps.wpsmonitor.client.exception;

import java.net.URL;

/**
*
* @author Florian Vogelpohl <[email protected]>
*/
public class WpsMonitorClientWpsNotFoundException extends WpsMonitorClientException {

public WpsMonitorClientWpsNotFoundException(final URL wpsEndpoint) {
super("The WPS \"" + wpsEndpoint.toString() + "\" was not found within the WPSMonitor.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package de.hsos.ecs.richwps.wpsmonitor.client.exception;

import java.net.URL;

/**
*
* @author Florian Vogelpohl <[email protected]>
*/
public class WpsMonitorClientWpsProcessNotFoundException extends WpsMonitorClientException {

public WpsMonitorClientWpsProcessNotFoundException(final String processIdentifier, final URL wpsEndpoint) {
super("The Process \"" + processIdentifier + "\" of WPS \"" + wpsEndpoint.toString() + "\" was not found within the WPSMonitor.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class WpsMonitorJsonRequester {
private final static String MEASUREMENT_RESOURCE = "measurement";
private final static String WPS_RESOURCE = "wps";
private final static String WPS_PROCESS_RESOURCE = "process";
private final static String DISPLAY_OPTION = "display";
private final static String COUNT_OPTION = "count";

public static String getJson(final URL endPoint) throws HttpException {
Expand All @@ -46,7 +45,9 @@ public static String getJson(final URL endPoint) throws HttpException {
connection.setRequestMethod(REQUEST_METHOD);
connection.setRequestProperty("Accept", MIME_TYPE);

if (connection.getResponseCode() != 200) {
if(connection.getResponseCode() == 404) {
return null;
} else if (connection.getResponseCode() != 200) {
throw new HttpException("Request failed: HTTP Error Code was: " + connection.getResponseCode());
}

Expand All @@ -70,20 +71,18 @@ public static String getJson(final URL endPoint) throws HttpException {
return result;
}

public static URL buildWpsProcessMetricsURL(final URL monitorEndpoint, final String wpsIdentifier,
public static URL buildWpsProcessMetricsURL(final URL monitorEndpoint, final Long wpsId,
final String wpsProcessIdentifier, final Integer count)
throws HttpException {

try {
String buildUrl = concatUrlStr(new String[] {
WPS_RESOURCE,
wpsIdentifier,
wpsId.toString(),
WPS_PROCESS_RESOURCE,
wpsProcessIdentifier,
COUNT_OPTION,
count.toString(),
DISPLAY_OPTION,
"metric"
count.toString()
});

return new URL(monitorEndpoint, buildUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,39 @@ public WpsMonitorRequester(final URL monitorEndpoint, final Gson gson) throws Ht
this.monitorEndpoint = monitorEndpoint;
this.gson = gson;

this.wpsUrl = WpsMonitorJsonRequester.buildWpsURL(monitorEndpoint);
this.wpsUrl = buildWpsUrl();
}

public List<WpsEntity> getWpsList() throws HttpException {
String wpsListJson = WpsMonitorJsonRequester.getJson(wpsUrl);

String wpsListJson = getJson(wpsUrl);
return gson.fromJson(wpsListJson, new TypeToken<List<WpsEntity>>() {
}.getType());
}

public WpsProcessResource getProcess(final WpsResource wpsResource, final String wpsProcessIdentifier, final Integer considerMeasuredValue) throws HttpException {
Validate.notNull(wpsResource, "wpsResource");
Validate.notNull(wpsProcessIdentifier, "wpsProcessIdentifier");
Validate.notNull(considerMeasuredValue, "considerMeasuredValue");

/* Build REST endpoint URL and get the JSON Data */
URL processURL = WpsMonitorJsonRequester.buildWpsProcessMetricsURL(monitorEndpoint,
wpsResource.getWpsIdentifier(), wpsProcessIdentifier, considerMeasuredValue);
String metricJson = WpsMonitorJsonRequester.getJson(processURL);

/* Restore object structure */
Map<String, Map<String, MeasuredValue>> fromJson = gson.fromJson(metricJson, new TypeToken<Map<String, Map<String, MeasuredValue>>>() {
}.getType());
Map<String, WpsMetricResource> metrics = getMetrics(fromJson);
URL processURL = buildWpsProcessMetricsUrl(wpsResource.getWpsId(), wpsProcessIdentifier, considerMeasuredValue);
String metricJson = getJson(processURL);

WpsProcessResource result = null;

if (metricJson != null && !metricJson.isEmpty()) {

/* Restore object structure */
Map<String, Map<String, MeasuredValue>> fromJson = gson.fromJson(metricJson, new TypeToken<Map<String, Map<String, MeasuredValue>>>() {
}.getType());
Map<String, WpsMetricResource> metrics = getMetrics(fromJson);

result = new WpsProcessResource(wpsResource, wpsProcessIdentifier, metrics);
}

/* Return new WpsProcessResource instance */
return new WpsProcessResource(wpsResource, wpsProcessIdentifier, metrics);
return result;
}

private Map<String, WpsMetricResource> getMetrics(final Map<String, Map<String, MeasuredValue>> metricsMap) {
Expand All @@ -91,4 +101,19 @@ public URL getMonitorEndpoint() {
public Gson getGson() {
return gson;
}

/*
* Encapsulate Static helpers
*/
private String getJson(final URL endpoint) throws HttpException {
return WpsMonitorJsonRequester.getJson(endpoint);
}

private URL buildWpsUrl() throws HttpException {
return WpsMonitorJsonRequester.buildWpsURL(monitorEndpoint);
}

private URL buildWpsProcessMetricsUrl(final Long wpsId, final String processIdentifier, final Integer considerMeasuredValue) throws HttpException {
return WpsMonitorJsonRequester.buildWpsProcessMetricsURL(monitorEndpoint, wpsId, processIdentifier, considerMeasuredValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ public List<WpsMetricResource> getMetricsAsList() {
public WpsResource getWpsResource() {
return wpsResource;
}

@Override
public String toString() {
return "WpsProcessResource{" + "wpsResource=" + wpsResource + ", processIdentifier=" + processIdentifier + ", metrics=" + metrics + '}';
}
}
Loading

0 comments on commit 09f6b6e

Please sign in to comment.