Search in sources :

Example 6 with J4pSearchRequest

use of org.jolokia.client.request.J4pSearchRequest 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 7 with J4pSearchRequest

use of org.jolokia.client.request.J4pSearchRequest 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 8 with J4pSearchRequest

use of org.jolokia.client.request.J4pSearchRequest in project syndesis by syndesisio.

the class PodMetricsReader method lookupCamelContext.

/**
 * Code borrowed from the DefaultCamelController: https://github.com/apache/camel/blob/master/platforms/commands/commands-jolokia/src/main/java/org/apache/camel/commands/jolokia/DefaultJolokiaCamelController.java
 * Slight modifications have been applied.
 * Credits to: Claus & Tomo
 */
private ObjectName lookupCamelContext(String camelContextName) throws Exception {
    ObjectName on = cache.get(camelContextName);
    if (on == null) {
        ObjectName found = null;
        J4pSearchResponse sr = jolokia.execute(new J4pSearchRequest("org.apache.camel:type=context,*"));
        if (sr != null) {
            for (ObjectName name : sr.getObjectNames()) {
                String id = name.getKeyProperty("name");
                id = removeLeadingAndEndingQuotes(id);
                if (camelContextName.equals(id)) {
                    found = name;
                    break;
                }
            }
        }
        if (found != null) {
            on = found;
            cache.put(camelContextName, on);
        }
    }
    return on;
}
Also used : J4pSearchRequest(org.jolokia.client.request.J4pSearchRequest) ObjectName(javax.management.ObjectName) J4pSearchResponse(org.jolokia.client.request.J4pSearchResponse)

Aggregations

J4pSearchRequest (org.jolokia.client.request.J4pSearchRequest)8 J4pSearchResponse (org.jolokia.client.request.J4pSearchResponse)8 ObjectName (javax.management.ObjectName)7 ArrayList (java.util.ArrayList)5 J4pReadRequest (org.jolokia.client.request.J4pReadRequest)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 J4pReadResponse (org.jolokia.client.request.J4pReadResponse)4 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 ArtifactDeploymentException (org.apache.maven.artifact.deployer.ArtifactDeploymentException)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 J4pConnectException (org.jolokia.client.exception.J4pConnectException)1 J4pException (org.jolokia.client.exception.J4pException)1 J4pRemoteException (org.jolokia.client.exception.J4pRemoteException)1 J4pExecRequest (org.jolokia.client.request.J4pExecRequest)1