Repository: camel Updated Branches: refs/heads/master a06988213 -> 408a39b48
CAMEL-8044: Camel commands useable for remote JVMs using jolokia Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/408a39b4 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/408a39b4 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/408a39b4 Branch: refs/heads/master Commit: 408a39b4812bcb6ca3c6d6479e03fc8c56290633 Parents: a069882 Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Dec 15 17:02:48 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Dec 15 17:21:34 2014 +0100 ---------------------------------------------------------------------- .../jolokia/DefaultJolokiaCamelController.java | 562 +++++++++++++++++++ .../jolokia/JolokiaCamelController.java | 544 +----------------- .../commands/jolokia/RemoteCamelController.java | 33 -- .../commands/jolokia/JolokiaCommandsTest.java | 14 +- .../commands/jolokia/JolokiaRemoteTest.java | 34 +- 5 files changed, 610 insertions(+), 577 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/408a39b4/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java b/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java new file mode 100644 index 0000000..af9c985 --- /dev/null +++ b/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java @@ -0,0 +1,562 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.commands.jolokia; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.management.ObjectName; + +import org.apache.camel.commands.AbstractCamelController; +import org.apache.camel.util.LRUCache; +import org.apache.camel.util.StringHelper; +import org.jolokia.client.J4pClient; +import org.jolokia.client.exception.J4pException; +import org.jolokia.client.request.J4pExecRequest; +import org.jolokia.client.request.J4pExecResponse; +import org.jolokia.client.request.J4pReadRequest; +import org.jolokia.client.request.J4pReadResponse; +import org.jolokia.client.request.J4pSearchRequest; +import org.jolokia.client.request.J4pSearchResponse; +import org.jolokia.client.request.J4pVersionRequest; +import org.jolokia.client.request.J4pVersionResponse; +import org.json.simple.JSONObject; + +/** + * A {@link org.apache.camel.commands.CamelController} that uses Jolokia Client to connect to remote JVMs which + * has an Jolokia agent running. + */ +public class DefaultJolokiaCamelController extends AbstractCamelController implements JolokiaCamelController { + + private Map<String, ObjectName> cache = new LRUCache<String, ObjectName>(1000); + + private J4pClient jolokia; + private String url; + + private ObjectName lookupCamelContext(String camelContextName) throws Exception { + ObjectName on = cache.get(camelContextName); + if (on == null) { + ObjectName found = null; + J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("*:type=context,*")); + if (sr != null) { + for (ObjectName name : sr.getObjectNames()) { + String id = name.getKeyProperty("name"); + id = StringHelper.removeLeadingAndEndingQuotes(id); + if (camelContextName.equals(id)) { + found = name; + break; + } + } + } + if (found != null) { + on = found; + cache.put(camelContextName, on); + } + } + return on; + } + + @Override + public void connect(String url, String username, String password) throws Exception { + this.jolokia = JolokiaClientFactory.createJolokiaClient(url, username, password); + this.url = url; + } + + @Override + public boolean ping() { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + J4pVersionResponse vr = null; + try { + vr = jolokia.execute(new J4pVersionRequest()); + return vr != null && vr.getValue() != null; + } catch (J4pException e) { + return false; + } + } + + @Override + public Map<String, Object> getCamelContextInformation(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + Map<String, Object> answer = new LinkedHashMap<String, Object>(); + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + J4pReadResponse rr = jolokia.execute(new J4pReadRequest(found)); + if (rr != null) { + for (String key : rr.getAttributes()) { + answer.put(asKey(key), rr.getValue(key)); + } + } + } + + return answer; + } + + @Override + public List<Map<String, String>> getCamelContexts() throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); + + J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("*:type=context,*")); + + List<J4pReadRequest> list = new ArrayList<J4pReadRequest>(); + for (ObjectName on : sr.getObjectNames()) { + list.add(new J4pReadRequest(on, "CamelId", "State", "Uptime")); + } + + List<J4pReadResponse> lrr = jolokia.execute(list); + for (J4pReadResponse rr : lrr) { + Map<String, String> row = new LinkedHashMap<String, String>(); + row.put("name", rr.getValue("CamelId").toString()); + row.put("state", rr.getValue("State").toString()); + row.put("uptime", rr.getValue("Uptime").toString()); + answer.add(row); + } + + return answer; + } + + @Override + public String getCamelContextStatsAsXml(String camelContextName, boolean fullStats, boolean includeProcessors) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + J4pExecResponse er = jolokia.execute(new J4pExecRequest(found, "dumpRoutesStatsAsXml(boolean,boolean)", fullStats, includeProcessors)); + if (er != null) { + String xml = er.getValue(); + return xml; + } + } + + return null; + } + + @Override + public void startContext(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + jolokia.execute(new J4pExecRequest(found, "start")); + } + } + + @Override + public void stopContext(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + jolokia.execute(new J4pExecRequest(found, "stop")); + } + } + + @Override + public void suspendContext(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + jolokia.execute(new J4pExecRequest(found, "suspend")); + } + } + + @Override + public void resumeContext(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + jolokia.execute(new J4pExecRequest(found, "resume")); + } + } + + @Override + public List<Map<String, String>> getRoutes(String camelContextName) throws Exception { + return getRoutes(camelContextName, null); + } + + @Override + public List<Map<String, String>> getRoutes(String camelContextName, String filter) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); + + if (camelContextName != null) { + + J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("*:type=routes,*")); + + List<J4pReadRequest> list = new ArrayList<J4pReadRequest>(); + for (ObjectName on : sr.getObjectNames()) { + list.add(new J4pReadRequest(on, "CamelId", "RouteId", "State")); + } + + List<J4pReadResponse> lrr = jolokia.execute(list); + for (J4pReadResponse rr : lrr) { + String routeId = rr.getValue("RouteId").toString(); + if (filter == null || routeId.matches(filter)) { + Map<String, String> row = new LinkedHashMap<String, String>(); + row.put("camelContextName", rr.getValue("CamelId").toString()); + row.put("routeId", routeId); + row.put("state", rr.getValue("State").toString()); + answer.add(row); + } + } + + } else { + List<Map<String, String>> camelContexts = this.getCamelContexts(); + for (Map<String, String> row : camelContexts) { + List<Map<String, String>> routes = getRoutes(row.get("name"), filter); + answer.addAll(routes); + } + } + + // sort the list + Collections.sort(answer, new Comparator<Map<String, String>>() { + @Override + public int compare(Map<String, String> o1, Map<String, String> o2) { + // group by camel context first, then by route name + String c1 = o1.get("camelContextName"); + String c2 = o2.get("camelContextName"); + + int answer = c1.compareTo(c2); + if (answer == 0) { + // okay from same camel context, then sort by route id + answer = o1.get("routeId").compareTo(o2.get("routeId")); + } + return answer; + } + }); + return answer; + } + + @Override + public void resetRouteStats(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + + if (found != null) { + String pattern = String.format("%s:context=%s,type=routes,name=*", found.getDomain(), found.getKeyProperty("context")); + J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest(pattern)); + + List<J4pExecRequest> list = new ArrayList<J4pExecRequest>(); + for (ObjectName on : sr.getObjectNames()) { + list.add(new J4pExecRequest(on, "reset(boolean)", true)); + } + + jolokia.execute(list); + } + } + + @Override + public void startRoute(String camelContextName, String routeId) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); + ObjectName on = ObjectName.getInstance(pattern); + jolokia.execute(new J4pExecRequest(on, "start()")); + } + } + + @Override + public void stopRoute(String camelContextName, String routeId) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); + ObjectName on = ObjectName.getInstance(pattern); + jolokia.execute(new J4pExecRequest(on, "stop()")); + } + } + + @Override + public void suspendRoute(String camelContextName, String routeId) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); + ObjectName on = ObjectName.getInstance(pattern); + jolokia.execute(new J4pExecRequest(on, "suspend()")); + } + } + + @Override + public void resumeRoute(String camelContextName, String routeId) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); + ObjectName on = ObjectName.getInstance(pattern); + jolokia.execute(new J4pExecRequest(on, "resume()")); + } + } + + @Override + public String getRouteModelAsXml(String camelContextName, String routeId) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); + ObjectName on = ObjectName.getInstance(pattern); + J4pExecResponse response = jolokia.execute(new J4pExecRequest(on, "dumpRouteAsXml()")); + if (response != null) { + String xml = response.getValue(); + return xml; + } + } + + return null; + } + + @Override + public String getRouteStatsAsXml(String camelContextName, String routeId, boolean fullStats, boolean includeProcessors) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); + ObjectName on = ObjectName.getInstance(pattern); + J4pExecResponse response = jolokia.execute(new J4pExecRequest(on, "dumpRouteStatsAsXml(boolean,boolean)", fullStats, includeProcessors)); + if (response != null) { + String xml = response.getValue(); + return xml; + } + } + + return null; + } + + @Override + public String getRestModelAsXml(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + J4pExecResponse response = jolokia.execute(new J4pExecRequest(found, "dumpRestsAsXml()")); + if (response != null) { + String xml = response.getValue(); + return xml; + } + } + + return null; + } + + @Override + public List<Map<String, String>> getEndpoints(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("*:type=endpoints,*")); + + List<J4pReadRequest> list = new ArrayList<J4pReadRequest>(); + for (ObjectName on : sr.getObjectNames()) { + list.add(new J4pReadRequest(on, "CamelId", "EndpointUri", "State")); + } + + List<J4pReadResponse> lrr = jolokia.execute(list); + for (J4pReadResponse rr : lrr) { + Map<String, String> row = new LinkedHashMap<String, String>(); + row.put("camelContextName", rr.getValue("CamelId").toString()); + row.put("uri", rr.getValue("EndpointUri").toString()); + row.put("state", rr.getValue("State").toString()); + answer.add(row); + } + } + + return answer; + } + + @Override + public List<Map<String, String>> getRestServices(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + String pattern = String.format("%s:context=%s,type=services,name=DefaultRestRegistry", found.getDomain(), found.getKeyProperty("context")); + ObjectName on = ObjectName.getInstance(pattern); + + J4pExecResponse response = jolokia.execute(new J4pExecRequest(on, "listRestServices()")); + if (response != null) { + JSONObject data = response.getValue(); + for (Object obj : data.values()) { + JSONObject data2 = (JSONObject) obj; + JSONObject service = (JSONObject) data2.values().iterator().next(); + + Map<String, String> row = new LinkedHashMap<String, String>(); + row.put("basePath", asString(service.get("basePath"))); + row.put("baseUrl", asString(service.get("baseUrl"))); + row.put("consumes", asString(service.get("consumes"))); + row.put("description", asString(service.get("description"))); + row.put("inType", asString(service.get("inType"))); + row.put("method", asString(service.get("method"))); + row.put("outType", asString(service.get("outType"))); + row.put("produces", asString(service.get("produces"))); + row.put("routeId", asString(service.get("routeId"))); + row.put("state", asString(service.get("state"))); + row.put("uriTemplate", asString(service.get("uriTemplate"))); + row.put("url", asString(service.get("url"))); + answer.add(row); + } + } + + // sort the list + Collections.sort(answer, new Comparator<Map<String, String>>() { + @Override + public int compare(Map<String, String> service1, Map<String, String> service2) { + String url1 = service1.get("url"); + String url2 = service2.get("url"); + return url1.compareTo(url2); + } + }); + } + + return answer; + } + + @Override + public String explainEndpointAsJSon(String camelContextName, String uri, boolean allOptions) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + J4pExecResponse response = jolokia.execute(new J4pExecRequest(found, "explainEndpointJson(java.lang.String,boolean)", uri, allOptions)); + if (response != null) { + String json = response.getValue(); + return json; + } + } + + return null; + } + + @Override + public List<Map<String, String>> listComponents(String camelContextName) throws Exception { + if (jolokia == null) { + throw new IllegalStateException("Need to connect to remote jolokia first"); + } + + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); + + ObjectName found = lookupCamelContext(camelContextName); + if (found != null) { + J4pExecResponse response = jolokia.execute(new J4pExecRequest(found, "listComponents()")); + if (response != null) { + JSONObject data = response.getValue(); + for (Object obj : data.values()) { + JSONObject component = (JSONObject) obj; + + Map<String, String> row = new LinkedHashMap<String, String>(); + row.put("artifactId", asString(component.get("artifactId"))); + row.put("description", asString(component.get("description"))); + row.put("groupId", asString(component.get("groupId"))); + row.put("label", asString(component.get("label"))); + row.put("name", asString(component.get("name"))); + row.put("status", asString(component.get("status"))); + row.put("type", asString(component.get("type"))); + row.put("version", asString(component.get("version"))); + answer.add(row); + } + } + + // sort the list + Collections.sort(answer, new Comparator<Map<String, String>>() { + @Override + public int compare(Map<String, String> component1, Map<String, String> component2) { + String name1 = component1.get("name"); + String name2 = component2.get("name"); + return name1.compareTo(name2); + } + }); + } + + return answer; + } + + private static String asKey(String attributeKey) { + char ch = Character.toLowerCase(attributeKey.charAt(0)); + return ch + attributeKey.substring(1); + } + + private static String asString(Object basePath) { + if (basePath == null) { + return null; + } else { + return basePath.toString(); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/408a39b4/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/JolokiaCamelController.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/JolokiaCamelController.java b/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/JolokiaCamelController.java index 43dc921..7f993e1 100644 --- a/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/JolokiaCamelController.java +++ b/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/JolokiaCamelController.java @@ -16,529 +16,25 @@ */ package org.apache.camel.commands.jolokia; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import javax.management.ObjectName; - -import org.apache.camel.commands.AbstractCamelController; -import org.apache.camel.util.LRUCache; -import org.apache.camel.util.StringHelper; -import org.jolokia.client.J4pClient; -import org.jolokia.client.request.J4pExecRequest; -import org.jolokia.client.request.J4pExecResponse; -import org.jolokia.client.request.J4pReadRequest; -import org.jolokia.client.request.J4pReadResponse; -import org.jolokia.client.request.J4pSearchRequest; -import org.jolokia.client.request.J4pSearchResponse; -import org.json.simple.JSONObject; - -/** - * A {@link org.apache.camel.commands.CamelController} that uses Jolokia Client to connect to remote JVMs which - * has an Jolokia agent running. - */ -public class JolokiaCamelController extends AbstractCamelController implements RemoteCamelController { - - private Map<String, ObjectName> cache = new LRUCache<String, ObjectName>(1000); - - private J4pClient jolokia; - private String url; - - private ObjectName lookupCamelContext(String camelContextName) throws Exception { - ObjectName on = cache.get(camelContextName); - if (on == null) { - ObjectName found = null; - J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("*:type=context,*")); - if (sr != null) { - for (ObjectName name : sr.getObjectNames()) { - String id = name.getKeyProperty("name"); - id = StringHelper.removeLeadingAndEndingQuotes(id); - if (camelContextName.equals(id)) { - found = name; - break; - } - } - } - if (found != null) { - on = found; - cache.put(camelContextName, on); - } - } - return on; - } - - @Override - public void connect(String url, String username, String password) throws Exception { - this.jolokia = JolokiaClientFactory.createJolokiaClient(url, username, password); - this.url = url; - } - - @Override - public Map<String, Object> getCamelContextInformation(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - Map<String, Object> answer = new LinkedHashMap<String, Object>(); - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - J4pReadResponse rr = jolokia.execute(new J4pReadRequest(found)); - if (rr != null) { - for (String key : rr.getAttributes()) { - answer.put(asKey(key), rr.getValue(key)); - } - } - } - - return answer; - } - - @Override - public List<Map<String, String>> getCamelContexts() throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); - - J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("*:type=context,*")); - - List<J4pReadRequest> list = new ArrayList<J4pReadRequest>(); - for (ObjectName on : sr.getObjectNames()) { - list.add(new J4pReadRequest(on, "CamelId", "State", "Uptime")); - } - - List<J4pReadResponse> lrr = jolokia.execute(list); - for (J4pReadResponse rr : lrr) { - Map<String, String> row = new LinkedHashMap<String, String>(); - row.put("name", rr.getValue("CamelId").toString()); - row.put("state", rr.getValue("State").toString()); - row.put("uptime", rr.getValue("Uptime").toString()); - answer.add(row); - } - - return answer; - } - - @Override - public String getCamelContextStatsAsXml(String camelContextName, boolean fullStats, boolean includeProcessors) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - J4pExecResponse er = jolokia.execute(new J4pExecRequest(found, "dumpRoutesStatsAsXml(boolean,boolean)", fullStats, includeProcessors)); - if (er != null) { - String xml = er.getValue(); - return xml; - } - } - - return null; - } - - @Override - public void startContext(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - jolokia.execute(new J4pExecRequest(found, "start")); - } - } - - @Override - public void stopContext(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - jolokia.execute(new J4pExecRequest(found, "stop")); - } - } - - @Override - public void suspendContext(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - jolokia.execute(new J4pExecRequest(found, "suspend")); - } - } - - @Override - public void resumeContext(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - jolokia.execute(new J4pExecRequest(found, "resume")); - } - } - - @Override - public List<Map<String, String>> getRoutes(String camelContextName) throws Exception { - return getRoutes(camelContextName, null); - } - - @Override - public List<Map<String, String>> getRoutes(String camelContextName, String filter) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); - - if (camelContextName != null) { - - J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("*:type=routes,*")); - - List<J4pReadRequest> list = new ArrayList<J4pReadRequest>(); - for (ObjectName on : sr.getObjectNames()) { - list.add(new J4pReadRequest(on, "CamelId", "RouteId", "State")); - } - - List<J4pReadResponse> lrr = jolokia.execute(list); - for (J4pReadResponse rr : lrr) { - String routeId = rr.getValue("RouteId").toString(); - if (filter == null || routeId.matches(filter)) { - Map<String, String> row = new LinkedHashMap<String, String>(); - row.put("camelContextName", rr.getValue("CamelId").toString()); - row.put("routeId", routeId); - row.put("state", rr.getValue("State").toString()); - answer.add(row); - } - } - - } else { - List<Map<String, String>> camelContexts = this.getCamelContexts(); - for (Map<String, String> row : camelContexts) { - List<Map<String, String>> routes = getRoutes(row.get("name"), filter); - answer.addAll(routes); - } - } - - // sort the list - Collections.sort(answer, new Comparator<Map<String, String>>() { - @Override - public int compare(Map<String, String> o1, Map<String, String> o2) { - // group by camel context first, then by route name - String c1 = o1.get("camelContextName"); - String c2 = o2.get("camelContextName"); - - int answer = c1.compareTo(c2); - if (answer == 0) { - // okay from same camel context, then sort by route id - answer = o1.get("routeId").compareTo(o2.get("routeId")); - } - return answer; - } - }); - return answer; - } - - @Override - public void resetRouteStats(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - - if (found != null) { - String pattern = String.format("%s:context=%s,type=routes,name=*", found.getDomain(), found.getKeyProperty("context")); - J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest(pattern)); - - List<J4pExecRequest> list = new ArrayList<J4pExecRequest>(); - for (ObjectName on : sr.getObjectNames()) { - list.add(new J4pExecRequest(on, "reset(boolean)", true)); - } - - jolokia.execute(list); - } - } - - @Override - public void startRoute(String camelContextName, String routeId) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); - ObjectName on = ObjectName.getInstance(pattern); - jolokia.execute(new J4pExecRequest(on, "start()")); - } - } - - @Override - public void stopRoute(String camelContextName, String routeId) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); - ObjectName on = ObjectName.getInstance(pattern); - jolokia.execute(new J4pExecRequest(on, "stop()")); - } - } - - @Override - public void suspendRoute(String camelContextName, String routeId) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); - ObjectName on = ObjectName.getInstance(pattern); - jolokia.execute(new J4pExecRequest(on, "suspend()")); - } - } - - @Override - public void resumeRoute(String camelContextName, String routeId) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); - ObjectName on = ObjectName.getInstance(pattern); - jolokia.execute(new J4pExecRequest(on, "resume()")); - } - } - - @Override - public String getRouteModelAsXml(String camelContextName, String routeId) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); - ObjectName on = ObjectName.getInstance(pattern); - J4pExecResponse response = jolokia.execute(new J4pExecRequest(on, "dumpRouteAsXml()")); - if (response != null) { - String xml = response.getValue(); - return xml; - } - } - - return null; - } - - @Override - public String getRouteStatsAsXml(String camelContextName, String routeId, boolean fullStats, boolean includeProcessors) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - String pattern = String.format("%s:context=%s,type=routes,name=\"%s\"", found.getDomain(), found.getKeyProperty("context"), routeId); - ObjectName on = ObjectName.getInstance(pattern); - J4pExecResponse response = jolokia.execute(new J4pExecRequest(on, "dumpRouteStatsAsXml(boolean,boolean)", fullStats, includeProcessors)); - if (response != null) { - String xml = response.getValue(); - return xml; - } - } - - return null; - } - - @Override - public String getRestModelAsXml(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - J4pExecResponse response = jolokia.execute(new J4pExecRequest(found, "dumpRestsAsXml()")); - if (response != null) { - String xml = response.getValue(); - return xml; - } - } - - return null; - } - - @Override - public List<Map<String, String>> getEndpoints(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("*:type=endpoints,*")); - - List<J4pReadRequest> list = new ArrayList<J4pReadRequest>(); - for (ObjectName on : sr.getObjectNames()) { - list.add(new J4pReadRequest(on, "CamelId", "EndpointUri", "State")); - } - - List<J4pReadResponse> lrr = jolokia.execute(list); - for (J4pReadResponse rr : lrr) { - Map<String, String> row = new LinkedHashMap<String, String>(); - row.put("camelContextName", rr.getValue("CamelId").toString()); - row.put("uri", rr.getValue("EndpointUri").toString()); - row.put("state", rr.getValue("State").toString()); - answer.add(row); - } - } - - return answer; - } - - @Override - public List<Map<String, String>> getRestServices(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - String pattern = String.format("%s:context=%s,type=services,name=DefaultRestRegistry", found.getDomain(), found.getKeyProperty("context")); - ObjectName on = ObjectName.getInstance(pattern); - - J4pExecResponse response = jolokia.execute(new J4pExecRequest(on, "listRestServices()")); - if (response != null) { - JSONObject data = response.getValue(); - for (Object obj : data.values()) { - JSONObject data2 = (JSONObject) obj; - JSONObject service = (JSONObject) data2.values().iterator().next(); - - Map<String, String> row = new LinkedHashMap<String, String>(); - row.put("basePath", asString(service.get("basePath"))); - row.put("baseUrl", asString(service.get("baseUrl"))); - row.put("consumes", asString(service.get("consumes"))); - row.put("description", asString(service.get("description"))); - row.put("inType", asString(service.get("inType"))); - row.put("method", asString(service.get("method"))); - row.put("outType", asString(service.get("outType"))); - row.put("produces", asString(service.get("produces"))); - row.put("routeId", asString(service.get("routeId"))); - row.put("state", asString(service.get("state"))); - row.put("uriTemplate", asString(service.get("uriTemplate"))); - row.put("url", asString(service.get("url"))); - answer.add(row); - } - } - - // sort the list - Collections.sort(answer, new Comparator<Map<String, String>>() { - @Override - public int compare(Map<String, String> service1, Map<String, String> service2) { - String url1 = service1.get("url"); - String url2 = service2.get("url"); - return url1.compareTo(url2); - } - }); - } - - return answer; - } - - @Override - public String explainEndpointAsJSon(String camelContextName, String uri, boolean allOptions) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - J4pExecResponse response = jolokia.execute(new J4pExecRequest(found, "explainEndpointJson(java.lang.String,boolean)", uri, allOptions)); - if (response != null) { - String json = response.getValue(); - return json; - } - } - - return null; - } - - @Override - public List<Map<String, String>> listComponents(String camelContextName) throws Exception { - if (jolokia == null) { - throw new IllegalStateException("Need to connect to remote jolokia first"); - } - - List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); - - ObjectName found = lookupCamelContext(camelContextName); - if (found != null) { - J4pExecResponse response = jolokia.execute(new J4pExecRequest(found, "listComponents()")); - if (response != null) { - JSONObject data = response.getValue(); - for (Object obj : data.values()) { - JSONObject component = (JSONObject) obj; - - Map<String, String> row = new LinkedHashMap<String, String>(); - row.put("artifactId", asString(component.get("artifactId"))); - row.put("description", asString(component.get("description"))); - row.put("groupId", asString(component.get("groupId"))); - row.put("label", asString(component.get("label"))); - row.put("name", asString(component.get("name"))); - row.put("status", asString(component.get("status"))); - row.put("type", asString(component.get("type"))); - row.put("version", asString(component.get("version"))); - answer.add(row); - } - } - - // sort the list - Collections.sort(answer, new Comparator<Map<String, String>>() { - @Override - public int compare(Map<String, String> component1, Map<String, String> component2) { - String name1 = component1.get("name"); - String name2 = component2.get("name"); - return name1.compareTo(name2); - } - }); - } - - return answer; - } - - private static String asKey(String attributeKey) { - char ch = Character.toLowerCase(attributeKey.charAt(0)); - return ch + attributeKey.substring(1); - } - - private static String asString(Object basePath) { - if (basePath == null) { - return null; - } else { - return basePath.toString(); - } - } +import org.apache.camel.commands.CamelController; + +public interface JolokiaCamelController extends CamelController { + + /** + * Connects to the remote JVM using the given url to the remote jolokia agent + * + * @param url the url for the remote jolokia agent + * @param username optional username + * @param password optional password + * @throws Exception can be thrown + */ + void connect(String url, String username, String password) throws Exception; + + /** + * After connecting the ping command can be used to check if the connection works. + * + * @return <tt>true</tt> if connection works, <tt>false</tt> otherwise + */ + boolean ping(); } http://git-wip-us.apache.org/repos/asf/camel/blob/408a39b4/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/RemoteCamelController.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/RemoteCamelController.java b/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/RemoteCamelController.java deleted file mode 100644 index 3a2ba3b..0000000 --- a/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/RemoteCamelController.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.commands.jolokia; - -import org.apache.camel.commands.CamelController; - -public interface RemoteCamelController extends CamelController { - - /** - * Connects to the remote JVM using the given url to the remote jolokia agent - * - * @param url the url for the remote jolokia agent - * @param username optional username - * @param password optional password - * @throws Exception can be thrown - */ - void connect(String url, String username, String password) throws Exception; - -} http://git-wip-us.apache.org/repos/asf/camel/blob/408a39b4/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaCommandsTest.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaCommandsTest.java b/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaCommandsTest.java index e5f9fb1..c7d3710 100644 --- a/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaCommandsTest.java +++ b/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaCommandsTest.java @@ -26,22 +26,30 @@ public class JolokiaCommandsTest { private String url = "http://localhost:8080/jolokia"; - private RemoteCamelController controller; + private JolokiaCamelController controller; @Test public void testContextList() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); + if (!controller.ping()) { + throw new IllegalArgumentException("Error connecting to " + url); + } + ContextListCommand cmd = new ContextListCommand(); cmd.execute(controller, System.out, System.err); } @Test public void testContextInfo() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); + if (!controller.ping()) { + throw new IllegalArgumentException("Error connecting to " + url); + } + ContextInfoCommand cmd = new ContextInfoCommand("myCamel", true); cmd.setStringEscape(new NoopStringEscape()); cmd.execute(controller, System.out, System.err); http://git-wip-us.apache.org/repos/asf/camel/blob/408a39b4/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java ---------------------------------------------------------------------- diff --git a/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java b/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java index bdfa8fa..5be597b 100644 --- a/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java +++ b/platforms/commands/commands-jolokia/src/test/java/org/apache/camel/commands/jolokia/JolokiaRemoteTest.java @@ -27,11 +27,11 @@ public class JolokiaRemoteTest { private String url = "http://localhost:8080/jolokia"; - private RemoteCamelController controller; + private JolokiaCamelController controller; @Test public void testRemoteCamelContexts() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); List<Map<String, String>> data = controller.getCamelContexts(); @@ -40,7 +40,7 @@ public class JolokiaRemoteTest { @Test public void testRemoteCamelContextInformation() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); Map<String, Object> data = controller.getCamelContextInformation("myCamel"); @@ -49,7 +49,7 @@ public class JolokiaRemoteTest { @Test public void testRemoteCamelContextStatsAsXml() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); String data = controller.getCamelContextStatsAsXml("myCamel", true, false); @@ -58,7 +58,7 @@ public class JolokiaRemoteTest { @Test public void testRemoteCamelContextControl() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); controller.suspendContext("myCamel"); @@ -74,7 +74,7 @@ public class JolokiaRemoteTest { @Test public void testRemoteGetAllRoutes() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); List<Map<String, String>> data = controller.getRoutes(null); @@ -83,7 +83,7 @@ public class JolokiaRemoteTest { @Test public void testRemoteGetRoutes() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); List<Map<String, String>> data = controller.getRoutes("myCamel"); @@ -92,7 +92,7 @@ public class JolokiaRemoteTest { @Test public void testRemoteGetRoutesFilter() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); List<Map<String, String>> data = controller.getRoutes(null, "route2"); @@ -101,7 +101,7 @@ public class JolokiaRemoteTest { @Test public void testRemoteResetRouteStats() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); controller.resetRouteStats("myCamel"); @@ -109,7 +109,7 @@ public class JolokiaRemoteTest { @Test public void testRemoteRouteControl() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); controller.suspendRoute("myCamel", "route2"); @@ -125,7 +125,7 @@ public class JolokiaRemoteTest { @Test public void testRouteModel() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); String data = controller.getRouteModelAsXml("myCamel", "route2"); @@ -134,7 +134,7 @@ public class JolokiaRemoteTest { @Test public void testRouteStats() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); String data = controller.getRouteStatsAsXml("myCamel", "route2", true, true); @@ -143,7 +143,7 @@ public class JolokiaRemoteTest { @Test public void testRestsModel() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); String data = controller.getRestModelAsXml("myCamel"); @@ -152,7 +152,7 @@ public class JolokiaRemoteTest { @Test public void testGetEndpoints() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); List<Map<String, String>> data = controller.getEndpoints("myCamel"); @@ -161,7 +161,7 @@ public class JolokiaRemoteTest { @Test public void testGetRestServices() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); List<Map<String, String>> data = controller.getRestServices("myCamel"); @@ -170,7 +170,7 @@ public class JolokiaRemoteTest { @Test public void testExplainEndpointJson() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); String data = controller.explainEndpointAsJSon("myCamel", "log:foo", true); @@ -179,7 +179,7 @@ public class JolokiaRemoteTest { @Test public void testListComponents() throws Exception { - controller = new JolokiaCamelController(); + controller = new DefaultJolokiaCamelController(); controller.connect(url, null, null); List<Map<String, String>> data = controller.listComponents("myCamel");