Search in sources :

Example 6 with J4pReadRequest

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;
}
Also used : J4pReadRequest(org.jolokia.client.request.J4pReadRequest) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ObjectName(javax.management.ObjectName) J4pRemoteException(org.jolokia.client.exception.J4pRemoteException) J4pReadResponse(org.jolokia.client.request.J4pReadResponse) TimeUnit(java.util.concurrent.TimeUnit) JSONObject(org.json.simple.JSONObject)

Example 7 with J4pReadRequest

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;
}
Also used : J4pReadRequest(org.jolokia.client.request.J4pReadRequest) ArrayList(java.util.ArrayList) J4pSearchRequest(org.jolokia.client.request.J4pSearchRequest) ObjectName(javax.management.ObjectName) J4pSearchResponse(org.jolokia.client.request.J4pSearchResponse) LinkedHashMap(java.util.LinkedHashMap) J4pReadResponse(org.jolokia.client.request.J4pReadResponse) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with J4pReadRequest

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");
    }
}
Also used : J4pRemoteException(org.jolokia.client.exception.J4pRemoteException) J4pReadRequest(org.jolokia.client.request.J4pReadRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) J4pConnectException(org.jolokia.client.exception.J4pConnectException) J4pException(org.jolokia.client.exception.J4pException) J4pSearchRequest(org.jolokia.client.request.J4pSearchRequest) MalformedObjectNameException(javax.management.MalformedObjectNameException) J4pRemoteException(org.jolokia.client.exception.J4pRemoteException) ArtifactDeploymentException(org.apache.maven.artifact.deployer.ArtifactDeploymentException) J4pConnectException(org.jolokia.client.exception.J4pConnectException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) J4pException(org.jolokia.client.exception.J4pException) J4pSearchResponse(org.jolokia.client.request.J4pSearchResponse)

Example 9 with J4pReadRequest

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;
    }
}
Also used : J4pReadRequest(org.jolokia.client.request.J4pReadRequest) J4pWriteRequest(org.jolokia.client.request.J4pWriteRequest) J4pExecRequest(org.jolokia.client.request.J4pExecRequest) J4pException(org.jolokia.client.exception.J4pException) List(java.util.List) AbtractJ4pMBeanRequest(org.jolokia.client.request.AbtractJ4pMBeanRequest) J4pResponse(org.jolokia.client.request.J4pResponse)

Example 10 with J4pReadRequest

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;
    }
}
Also used : J4pReadRequest(org.jolokia.client.request.J4pReadRequest) J4pWriteRequest(org.jolokia.client.request.J4pWriteRequest) J4pExecRequest(org.jolokia.client.request.J4pExecRequest) J4pException(org.jolokia.client.exception.J4pException) List(java.util.List) AbtractJ4pMBeanRequest(org.jolokia.client.request.AbtractJ4pMBeanRequest) J4pResponse(org.jolokia.client.request.J4pResponse)

Aggregations

J4pReadRequest (org.jolokia.client.request.J4pReadRequest)11 ObjectName (javax.management.ObjectName)7 J4pReadResponse (org.jolokia.client.request.J4pReadResponse)6 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 J4pSearchRequest (org.jolokia.client.request.J4pSearchRequest)5 J4pSearchResponse (org.jolokia.client.request.J4pSearchResponse)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 J4pException (org.jolokia.client.exception.J4pException)4 J4pRemoteException (org.jolokia.client.exception.J4pRemoteException)3 J4pExecRequest (org.jolokia.client.request.J4pExecRequest)3 List (java.util.List)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 J4pClient (org.jolokia.client.J4pClient)2 J4pConnectException (org.jolokia.client.exception.J4pConnectException)2 AbtractJ4pMBeanRequest (org.jolokia.client.request.AbtractJ4pMBeanRequest)2 J4pResponse (org.jolokia.client.request.J4pResponse)2 J4pWriteRequest (org.jolokia.client.request.J4pWriteRequest)2 Container (io.fabric8.kubernetes.api.model.Container)1