use of org.jolokia.client.request.J4pReadRequest in project camel by apache.
the class DefaultJolokiaCamelController method getCamelContextInformation.
@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) {
String pattern = String.format("%s:context=%s,type=services,name=DefaultTypeConverter", found.getDomain(), found.getKeyProperty("context"));
ObjectName tc = ObjectName.getInstance(pattern);
String pattern2 = String.format("%s:context=%s,type=services,name=DefaultAsyncProcessorAwaitManager", found.getDomain(), found.getKeyProperty("context"));
ObjectName am = ObjectName.getInstance(pattern2);
List<J4pReadRequest> list = new ArrayList<J4pReadRequest>();
list.add(new J4pReadRequest(found));
list.add(new J4pReadRequest(tc));
list.add(new J4pReadRequest(am));
List<J4pReadResponse> rr = jolokia.execute(list);
if (rr != null && rr.size() > 0) {
// camel context attributes
J4pReadResponse first = rr.get(0);
for (String key : first.getAttributes()) {
answer.put(asKey(key), first.getValue(key));
}
// type converter attributes
if (rr.size() >= 2) {
J4pReadResponse second = rr.get(1);
for (String key : second.getAttributes()) {
answer.put("typeConverter." + asKey(key), second.getValue(key));
}
}
// async processor await manager attributes
if (rr.size() >= 3) {
J4pReadResponse second = rr.get(2);
for (String key : second.getAttributes()) {
answer.put("asyncProcessorAwaitManager." + asKey(key), second.getValue(key));
}
}
}
// would be great if there was an api in jolokia to read optional (eg ignore if an mbean does not exists)
answer.put("streamCachingEnabled", false);
try {
pattern = String.format("%s:context=%s,type=services,name=DefaultStreamCachingStrategy", found.getDomain(), found.getKeyProperty("context"));
ObjectName sc = ObjectName.getInstance(pattern);
// there is only a mbean if stream caching is enabled
J4pReadResponse rsc = jolokia.execute(new J4pReadRequest(sc));
if (rsc != null) {
for (String key : rsc.getAttributes()) {
answer.put("streamCaching." + asKey(key), rsc.getValue(key));
}
}
answer.put("streamCachingEnabled", true);
} catch (J4pRemoteException e) {
// ignore
boolean ignore = InstanceNotFoundException.class.getName().equals(e.getErrorType());
if (!ignore) {
throw e;
}
}
// store some data using special names as that is what the core-commands expects
answer.put("name", answer.get("camelId"));
answer.put("status", answer.get("state"));
answer.put("version", answer.get("camelVersion"));
answer.put("suspended", "Suspended".equals(answer.get("state")));
TimeUnit unit = TimeUnit.valueOf((String) answer.get("timeUnit"));
long timeout = (Long) answer.get("timeout");
answer.put("shutdownTimeout", "" + unit.toSeconds(timeout));
answer.put("applicationContextClassLoader", answer.get("applicationContextClassName"));
}
return answer;
}
use of org.jolokia.client.request.J4pReadRequest in project camel by apache.
the class DefaultJolokiaCamelController method getEndpoints.
@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) {
String pattern = String.format("%s:context=%s,type=endpoints,*", found.getDomain(), found.getKeyProperty("context"));
J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest(pattern));
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;
}
use of org.jolokia.client.request.J4pReadRequest in project fabric8 by jboss-fuse.
the class DeployToProfileMojo method getMavenUploadUri.
protected String getMavenUploadUri(J4pClient client) throws MalformedObjectNameException, J4pException, MojoExecutionException {
Exception exception = null;
try {
J4pSearchResponse searchResponse = client.execute(new J4pSearchRequest(FABRIC_MBEAN));
List<String> mbeanNames = searchResponse.getMBeanNames();
if (mbeanNames == null || mbeanNames.isEmpty()) {
getLog().warn("No MBean " + FABRIC_MBEAN + " found, are you sure you have created a fabric in this JVM?");
return null;
}
J4pResponse<J4pReadRequest> request = client.execute(new J4pReadRequest(FABRIC_MBEAN, "MavenRepoUploadURI"));
Object value = request.getValue();
if (value != null) {
String uri = value.toString();
if (uri.startsWith("http")) {
return uri;
} else {
getLog().warn("Could not find the Maven upload URI. Got: " + value);
}
} else {
getLog().warn("Could not find the Maven upload URI");
}
} catch (J4pConnectException e) {
String message = "Could not connect to jolokia on " + jolokiaUrl + " using user: " + fabricServer.getUsername() + ".\nAre you sure you are running a fabric8 container?";
getLog().error(message);
throw new MojoExecutionException(message, e);
} catch (J4pRemoteException e) {
int status = e.getStatus();
if (status == 401) {
String message = "Status 401: Unauthorized to access: " + jolokiaUrl + " using user: " + fabricServer.getUsername();
if (!customUsernameAndPassword) {
message += ".\nHave you created a Fabric?\nHave you setup your ~/.m2/settings.xml with the correct user and password for server ID: " + serverId + " and do the user/password match the server " + jolokiaUrl + "?";
}
getLog().error(message);
throw new MojoExecutionException(message, e);
} else if (status == 404) {
String message = "Status 404: Resource not found: " + jolokiaUrl + ".\nHave you created a Fabric?";
getLog().error(message);
throw new MojoExecutionException(message, e);
} else {
exception = e;
}
} catch (J4pException e) {
// it may be an empty response which is like a 404
boolean is404 = "Could not parse answer: Unexpected token END OF FILE at position 0.".equals(e.getMessage());
if (is404) {
String message = "Status 404: Resource not found: " + jolokiaUrl + ".\nHave you created a Fabric?";
getLog().error(message);
throw new MojoExecutionException(message, e);
} else {
exception = e;
}
}
if (exception != null) {
getLog().error("Failed to get maven repository URI from " + jolokiaUrl + ". " + exception, exception);
throw new MojoExecutionException("Could not find the Maven Upload Repository URI");
} else {
throw new MojoExecutionException("Could not find the Maven Upload Repository URI");
}
}
use of org.jolokia.client.request.J4pReadRequest in project fabric8 by jboss-fuse.
the class JolokiaInvocationHandler method invoke.
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
String attribute;
AbtractJ4pMBeanRequest request;
if ((attribute = getterAttributeName(method)) != null) {
request = new J4pReadRequest(objectName, attribute);
} else if ((attribute = setterAttributeName(method)) != null) {
request = new J4pWriteRequest(objectName, attribute, args[0]);
} else {
name = executeMethodName(method);
if (args == null | method.getParameterTypes().length == 0) {
request = new J4pExecRequest(objectName, name);
} else {
request = new J4pExecRequest(objectName, name, args);
}
}
try {
request.setPreferredHttpMethod("POST");
J4pResponse response = jolokia.execute(request);
Object value = response.getValue();
return JolokiaClients.convertJolokiaToJavaType(method.getReturnType(), value);
} catch (J4pException e) {
List<Object> argsList = args == null ? null : Arrays.asList(args);
LOG.warn("Failed to invoke " + objectName + " method: " + name + " with arguments: " + argsList + ". " + e, e);
throw e;
}
}
use of org.jolokia.client.request.J4pReadRequest in project fabric8 by fabric8io.
the class JolokiaInvocationHandler method invoke.
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
String attribute;
AbtractJ4pMBeanRequest request;
if ((attribute = getterAttributeName(method)) != null) {
request = new J4pReadRequest(objectName, attribute);
} else if ((attribute = setterAttributeName(method)) != null) {
request = new J4pWriteRequest(objectName, attribute, args[0]);
} else {
name = executeMethodName(method);
if (args == null | method.getParameterTypes().length == 0) {
request = new J4pExecRequest(objectName, name);
} else {
request = new J4pExecRequest(objectName, name, args);
}
}
try {
request.setPreferredHttpMethod("POST");
J4pResponse response = jolokia.execute(request);
Object value = response.getValue();
return JolokiaHelpers.convertJolokiaToJavaType(method.getReturnType(), value);
} catch (J4pException e) {
List<Object> argsList = args == null ? null : Arrays.asList(args);
LOG.warn("Failed to invoke " + objectName + " method: " + name + " with arguments: " + argsList + ". " + e, e);
throw e;
}
}
Aggregations