Example usage for javax.management Query attr

List of usage examples for javax.management Query attr

Introduction

In this page you can find the example usage for javax.management Query attr.

Prototype

public static AttributeValueExp attr(String name) 

Source Link

Document

Returns a new attribute expression.

Usage

From source file:at.ac.tuwien.dsg.cloud.salsa.engine.utils.SystemFunctions.java

public static List<String> getEndPoints()
        throws MalformedObjectNameException, NullPointerException, UnknownHostException,
        AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),
            Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
    String hostname = InetAddress.getLocalHost().getHostName();
    InetAddress[] addresses = InetAddress.getAllByName(hostname);
    ArrayList<String> endPoints = new ArrayList<>();
    for (Iterator<ObjectName> i = objs.iterator(); i.hasNext();) {
        ObjectName obj = i.next();
        String scheme = mbs.getAttribute(obj, "scheme").toString();
        String port = obj.getKeyProperty("port");
        for (InetAddress addr : addresses) {
            String host = addr.getHostAddress();
            String ep = scheme + "://" + host + ":" + port;
            endPoints.add(ep);//from  w w w .  ja va 2  s.c  o m
        }
    }
    return endPoints;
}

From source file:at.ac.tuwien.dsg.cloud.salsa.engine.utils.SystemFunctions.java

/**
 * This command try to get Port which the service is listening to. The port should be in the parameter -httpPort when running, or Tomcat port
 *
 * @return/*w w  w.  ja  va2  s  .  c  o  m*/
 */
public static String getPort() {
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),
                Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        for (ObjectName obj : objs) {
            String port = obj.getKeyProperty("port");
            return port;
        }
    } catch (MalformedObjectNameException e) {
        EngineLogger.logger
                .error("Cannot get listening port of salsa-engine service. return 8080 as default. Error: "
                        + e.toString());
    }
    EngineLogger.logger.error("Cannot find listening port of salsa-engine service. return 8080 as default");
    return "8080";
}

From source file:org.alfresco.solr.servlet.Solr4X509ServletFilter.java

private int getHttpsPort() {
    try {/*from   w ww  . j  av  a 2  s.co m*/
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        QueryExp query = Query.eq(Query.attr("Scheme"), Query.value("https"));
        Set<ObjectName> objectNames = mBeanServer.queryNames(null, query);

        if (objectNames != null && objectNames.size() > 0) {
            for (ObjectName objectName : objectNames) {
                String name = objectName.toString();
                if (name.indexOf("port=") > -1) {
                    String[] parts = name.split("port=");
                    String port = parts[1];
                    try {
                        int portNum = Integer.parseInt(port);
                        return portNum;
                    } catch (NumberFormatException e) {
                        logger.error("Error parsing https port:" + port);
                        return -1;
                    }
                }
            }
        }
    } catch (Throwable t) {
        logger.error("Error getting https port:", t);
    }

    return -1;
}

From source file:org.alfresco.solr.SolrInformationServer.java

private String getHttpPort(String defaultPort) {
    try {/*from ww  w  .ja va2 s  . com*/
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        QueryExp query = Query.and(Query.eq(Query.attr("scheme"), Query.value("http")),
                Query.eq(Query.attr("protocol"), Query.value("HTTP/1.1")));
        Set<ObjectName> objectNames = mBeanServer.queryNames(null, query);

        if (objectNames != null && objectNames.size() > 0) {
            for (ObjectName objectName : objectNames) {
                String name = objectName.toString();
                if (name.indexOf("port=") > -1) {
                    String[] parts = name.split("port=");
                    String port = parts[1];
                    try {
                        Integer.parseInt(port);
                        return port;
                    } catch (NumberFormatException e) {
                        log.error("Error parsing http port:" + port);
                        return defaultPort;
                    }
                }
            }
        }
    } catch (Throwable t) {
        log.error("Error getting https port:", t);
    }

    return defaultPort;
}

From source file:org.apache.geode.internal.process.MBeanProcessController.java

/**
 * Builds the QueryExp used to identify the target MBean.
 * /*  w  w w  . j  a v a 2 s  .  c om*/
 * @param pidAttribute the name of the MBean attribute with the process id to compare against
 * @param attributes the names of additional MBean attributes to compare with expected values
 * @param values the expected values of the specified MBean attributes
 *
 * @return the main QueryExp for matching the target MBean
 */
private QueryExp buildQueryExp(final String pidAttribute, final String[] attributes, final Object[] values) {
    QueryExp optionalAttributes = buildOptionalQueryExp(attributes, values);
    QueryExp constraint;
    if (optionalAttributes != null) {
        constraint = Query.and(optionalAttributes, Query.eq(Query.attr(pidAttribute), Query.value(pid)));
    } else {
        constraint = Query.eq(Query.attr(pidAttribute), Query.value(pid));
    }
    return constraint;
}

From source file:org.apache.geode.internal.process.MBeanProcessController.java

/**
 * Builds an optional QueryExp to aid in matching the correct MBean using additional attributes
 * with the specified values. Returns null if no attributes and values were specified during
 * construction.//from   ww w.  j  a  v a  2  s .co m
 * 
 * @param attributes the names of additional MBean attributes to compare with expected values
 * @param values the expected values of the specified MBean attributes
 *
 * @return optional QueryExp to aid in matching the correct MBean
 */
private QueryExp buildOptionalQueryExp(final String[] attributes, final Object[] values) {
    QueryExp queryExp = null;
    for (int i = 0; i < attributes.length; i++) {
        if (values[i] instanceof Boolean) {
            if (queryExp == null) {
                queryExp = Query.eq(Query.attr(attributes[i]), Query.value((Boolean) values[i]));
            } else {
                queryExp = Query.and(queryExp,
                        Query.eq(Query.attr(attributes[i]), Query.value((Boolean) values[i])));
            }
        } else if (values[i] instanceof Number) {
            if (queryExp == null) {
                queryExp = Query.eq(Query.attr(attributes[i]), Query.value((Number) values[i]));
            } else {
                queryExp = Query.and(queryExp,
                        Query.eq(Query.attr(attributes[i]), Query.value((Number) values[i])));
            }
        } else if (values[i] instanceof String) {
            if (queryExp == null) {
                queryExp = Query.eq(Query.attr(attributes[i]), Query.value((String) values[i]));
            } else {
                queryExp = Query.and(queryExp,
                        Query.eq(Query.attr(attributes[i]), Query.value((String) values[i])));
            }
        }
    }
    return queryExp;
}

From source file:org.lobzik.home_sapiens.pi.AppData.java

public static List<String> getHTTPEndPoints() throws Exception { //tomcat-specific
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),
            Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
    String hostname = InetAddress.getLocalHost().getHostName();
    InetAddress[] addresses = InetAddress.getAllByName(hostname);
    ArrayList<String> endPoints = new ArrayList<String>();
    for (Iterator<ObjectName> i = objs.iterator(); i.hasNext();) {
        ObjectName obj = i.next();
        String scheme = mbs.getAttribute(obj, "scheme").toString();
        if (!scheme.toLowerCase().equals("http")) {
            continue;
        }/*from  w w w.  j  a  v  a 2 s .  co m*/
        String port = obj.getKeyProperty("port");
        for (InetAddress addr : addresses) {
            String host = addr.getHostAddress();
            String ep = scheme + "://" + host + ":" + port;
            endPoints.add(ep);
        }
    }
    return endPoints;
}

From source file:org.wso2.carbon.core.ServerManagement.java

/**
 * Wait till all service requests have been serviced. This method will only wait for a maximum
 * of {@link ServerManagement#TIMEOUT}//from www .  j  ava 2 s.  co  m
 *
 * @throws Exception If an error occurs while trying to connect to the Tomcat MBean
 */
public void waitForRequestCompletion() throws Exception {
    SecurityManager secMan = System.getSecurityManager();
    if (secMan != null) {
        secMan.checkPermission(new ManagementPermission("control"));
    }
    log.info("Waiting for request service completion...");
    /**
     * Get all MBeans with names such as Catalina:type=RequestProcessor,worker=http-9762,name=HttpRequest<n>
     * & Catalina:type=RequestProcessor,worker=http-9762,name=HttpsRequest<n>
     */
    MBeanServer mbs = ManagementFactory.getMBeanServer();
    boolean areRequestsInService;
    long start = System.currentTimeMillis();
    do {
        // Check whether there are any processors which are currently in the SERVICE stage (3)
        QueryExp query = Query.eq(Query.attr("stage"), Query.value(3)); // 3 = org.apache.coyote.Constants.STAGE_SERVICE
        Set set = mbs.queryNames(new ObjectName("Catalina:type=RequestProcessor,*"), query);
        if (set.size() > 0) {
            areRequestsInService = true;
            if (System.currentTimeMillis() - start > TIMEOUT) {
                log.warn("Timeout occurred even though there are active connections.");
                break;
            }
            Thread.sleep(2000);
        } else {
            areRequestsInService = false;
        }
    } while (areRequestsInService);
    log.info("All requests have been served.");
}