Example usage for javax.management ObjectName getKeyProperty

List of usage examples for javax.management ObjectName getKeyProperty

Introduction

In this page you can find the example usage for javax.management ObjectName getKeyProperty.

Prototype

public String getKeyProperty(String property) 

Source Link

Document

Obtains the value associated with a key in a key property.

Usage

From source file:org.nuxeo.ecm.core.management.jtajca.JtajcaManagementFeature.java

public static <T> T getInstanceNamedWithPrefix(Class<T> type, String prefix) {
    MBeanServer mbs = Framework.getService(ServerLocator.class).lookupServer();
    Set<String> names = new HashSet<>();
    for (ObjectName objectName : mbs.queryNames(nameOf(type), null)) {
        String name = objectName.getKeyProperty("name");
        names.add(name); // for error case
        if (name.startsWith(prefix)) {
            return JMX.newMXBeanProxy(mbs, objectName, type);
        }//w w  w. j  a  v a  2s. c  om
    }
    throw new RuntimeException("Found no bean with name prefix: " + prefix + " in available names: " + names);
}

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/*from ww w.jav a2  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: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);//www  .  jav a 2  s  .co  m
        }
    }
    return endPoints;
}

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   ww  w .  j  ava 2  s. c o 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.rhq.plugins.jbossas5.WebApplicationContextDiscoveryComponent.java

private static String getWebApplicationComponentVirtualHost(ManagedComponent webApplicationComponent) {
    ObjectName objectName;
    try {//from   w  w w  . j  a v  a  2 s. c  o m
        objectName = new ObjectName(webApplicationComponent.getName());
    } catch (MalformedObjectNameException e) {
        throw new IllegalStateException(
                "'" + webApplicationComponent.getName() + "' is not a valid JMX ObjectName.");
    }
    // name like "//localhost/foo/bar", return just the host portion, e.g. "localhost".
    String hostName = objectName.getKeyProperty("name").substring(2);
    hostName = hostName.substring(0, hostName.indexOf("/"));
    return hostName;
}

From source file:org.rhq.plugins.jbossas.util.DeploymentUtility.java

/**
 * Retrieves all the discovery information for a War resources. We are retrieving all the information
 * so that there is only ever one call to the MBeanServer to get the deployed mbeans, therefore saving
 * some performance if it did this for each and every war resource one at a time.
 *
 * @param connection EmsConnection to get the mbean information
 * @param jbossManMBeanNames Name of the main jboss.management mbeans for a collection of wars.
 * @return map holds all the war deployment information for the objects passed in the objectNames collection
 *///from  w  ww .  ja  v  a  2  s. c o  m
public static Map<String, List<WarDeploymentInformation>> getWarDeploymentInformation(EmsConnection connection,
        List<String> jbossManMBeanNames) {
    // We need a list of informations, as one jsr77 deployment can end up in multiple web apps in different vhosts
    HashMap<String, List<WarDeploymentInformation>> retDeploymentInformationMap = new HashMap<String, List<WarDeploymentInformation>>();

    // will contain information on all deployments
    Collection deploymentInfos;
    try {
        // NOTE: This is an expensive operation, since it returns a bunch of large objects.
        deploymentInfos = getDeploymentInformations(connection);
    } catch (Exception e) {
        return null;
    }

    String separator = System.getProperty("file.separator");
    boolean isOnWin = separator.equals("\\");

    // Loop through the deployment infos, and find the deployment infos corresponding to each of the
    // jboss.management/JSR77 MBean names that were passed into this method. From the deployment infos,
    // we can figure out the vhost(s) and context root for each WAR.
    for (Object deploymentInfo : deploymentInfos) {
        try {
            // NOTE: There may be more than one jboss.web MBean,
            //       e.g. "jboss.web:J2EEApplication=none,J2EEServer=none,j2eeType=WebModule,name=//localhost/jmx-console",
            //       associated with a given WAR deployment, in which case, the "deployedObject" field will be
            //       arbitrarily set to the name of one of the jboss.web MBeans.
            ObjectName jbossWebObjectName = getFieldValue(deploymentInfo, "deployedObject", ObjectName.class);
            if (jbossWebObjectName != null) {
                // e.g. "jmx-console.war"
                String shortName = getFieldValue(deploymentInfo, "shortName", String.class);

                for (String jbossManMBeanName : jbossManMBeanNames) {
                    ObjectName jbossManObjectName = new ObjectName(jbossManMBeanName);
                    String jbossManWarName = jbossManObjectName.getKeyProperty("name");

                    if (shortName.equals(jbossManWarName)) {
                        log.debug("Found DeploymentInfo for WAR " + shortName + ".");
                        // The only reliable way to determine the vhosts associated with the WAR is to use
                        // the "mbeans" field, whose value is a list of all the Servlet MBeans,
                        // .e.g. "jboss.web:J2EEApplication=none,J2EEServer=none,WebModule=//localhost/jmx-console,j2eeType=Servlet,name=default",
                        // corresponding to the WAR (one per servlet per vhost).
                        List servletObjectNames = getFieldValue(deploymentInfo, "mbeans", List.class);
                        Set<String> webModuleNames = new HashSet();
                        for (Object servletObjectName : servletObjectNames) {
                            // e.g. Figure out the web module name, e.g. "//localhost/jmx-console".
                            // NOTE: We must use reflection when working with the returned ObjectNames, since EMS
                            //       loaded them using a different classloader. Attempting to access them directly
                            //       would cause ClassCastExceptions.
                            Class<? extends Object> objectNameClass = servletObjectName.getClass();
                            Method getKeyPropertyMethod = objectNameClass.getMethod("getKeyProperty",
                                    String.class);
                            String webModuleName = (String) getKeyPropertyMethod.invoke(servletObjectName,
                                    "WebModule");
                            webModuleNames.add(webModuleName);
                        }
                        log.debug("Found " + webModuleNames.size() + " Web modules for WAR " + shortName + ": "
                                + webModuleNames);
                        String path = getPath(isOnWin, deploymentInfo);
                        List<WarDeploymentInformation> infos = new ArrayList<WarDeploymentInformation>();
                        for (String webModuleName : webModuleNames) {
                            WebModule webModule = parseWebModuleName(webModuleName);
                            WarDeploymentInformation deploymentInformation = new WarDeploymentInformation();
                            deploymentInformation.setVHost(webModule.vhost);
                            deploymentInformation.setFileName(path);
                            deploymentInformation.setContextRoot(webModule.contextRoot);
                            String jbossWebMBeanName = String.format(JBOSS_WEB_MBEAN_NAME_TEMPLATE,
                                    webModuleName);
                            jbossWebObjectName = ObjectName.getInstance(jbossWebMBeanName);
                            jbossWebMBeanName = jbossWebObjectName.getCanonicalName();
                            deploymentInformation.setJbossWebModuleMBeanObjectName(jbossWebMBeanName);
                            infos.add(deploymentInformation);
                        }

                        retDeploymentInformationMap.put(jbossManMBeanName, infos);
                    }
                }
            }
        } catch (Exception evalError) {
            log.warn("Failed to determine if a deployment contains our MBean", evalError);
        }
    }
    return retDeploymentInformationMap;
}

From source file:io.hawt.osgi.jmx.RBACDecorator.java

/**
 * see: <code>org.apache.karaf.management.KarafMBeanServerGuard#getNameSegments(javax.management.ObjectName)</code>
 *
 * Assuming <strong>full</strong> {@link ObjectName} (not null, not containing wildcards and other funny stuff),
 * split objectName to elements used then co contruct ordered list of PIDs to check for MBean permissions.
 * @return/*from   ww w .  j ava2s  . co m*/
 */
public static List<String> nameSegments(ObjectName objectName) {
    List<String> segments = new ArrayList<>();
    segments.add(objectName.getDomain());
    for (String s : objectName.getKeyPropertyListString().split(",")) {
        int index = s.indexOf('=');
        if (index < 0) {
            continue;
        }
        String key = objectName.getKeyProperty(s.substring(0, index));
        if (s.substring(0, index).equals("type")) {
            segments.add(1, key);
        } else {
            segments.add(key);
        }
    }

    return segments;
}

From source file:org.hyperic.hq.plugin.weblogic.jmx.ApplicationQuery.java

public boolean getAttributes(MBeanServer mServer, ObjectName name) {
    String appName = name.getKeyProperty("Name");

    ServerQuery server = (ServerQuery) getParent();
    String serverName = server.getName();

    //for later use in getScope to find children
    this.mbeanName = appName;

    if ((appName.startsWith(serverName)) && (appName.length() > (serverName.length() + 1))) {
        appName = appName.substring(serverName.length() + 1);
    }/* ww  w  .  j  a v a 2  s  .  c o m*/

    if ((appName.startsWith(serverName)) && (appName.length() > serverName.length())) {
        appName = appName.substring(serverName.length());
    }
    log.debug("[getAttributes] mbeanName = '" + this.mbeanName + "' => '" + appName + "'");

    if (server.getDiscover().isInternalApp(appName)) {
        log.debug(appName + " is a internal Application");
        return false;
    }

    setName(appName);
    super.getAttributes(mServer, name);

    return true;
}

From source file:com.atolcd.alfresco.audit.web.scripts.LuceneIndexesInfoGet.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {

    try {/*  w ww.  j  av  a 2s  .com*/
        // Map that will be passed to the template
        Map<String, Object> model = new HashMap<String, Object>();

        ObjectName query = new ObjectName("Alfresco:Name=LuceneIndexes,*");
        MBeanServer mbs = getMBeanServerWithQuery(query);
        Set<ObjectName> luceneIndexesName = mbs.queryNames(query, null);
        List<LuceneIndex> luceneIndexes = new ArrayList<LuceneIndex>();

        for (ObjectName luceneIndexName : luceneIndexesName) {

            luceneIndexes.add(new LuceneIndex(luceneIndexName.getKeyProperty("Index"),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "ActualSize")),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "NumberOfDocuments")),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "NumberOfFields")),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "NumberOfIndexedFields")),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "UsedSize"))));
        }

        model.put("luceneIndexes", luceneIndexes);

        return model;
    } catch (Exception e) {
        throw new WebScriptException(
                "[LuceneIndexesInfosGet] Error in executeImpl function : \n" + e.getMessage());
    }
}

From source file:com.cognifide.aet.runner.util.MessagesManager.java

private Set<ObjectName> filter(ObjectName[] queuesObjects) {
    Set<ObjectName> queues = new HashSet<>();
    for (ObjectName queueObject : queuesObjects) {
        if (queueObject.getKeyProperty(DESTINATION_NAME_PROPERTY).startsWith(AET_QUEUE_DOMAIN)) {
            queues.add(queueObject);//from   w  w  w  .ja v  a 2s.c  om
        }
    }
    return queues;
}