Search in sources :

Example 61 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class DefaultMBeanServerInterceptor method queryNames.

public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
    Set<ObjectName> queryList;
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        // Check if the caller has the right to invoke 'queryNames'
        //
        checkMBeanPermission((String) null, null, null, "queryNames");
        // Perform query without "query".
        //
        Set<ObjectInstance> list = queryMBeansImpl(name, null);
        // Check if the caller has the right to invoke 'queryNames'
        // on each specific classname/objectname in the list.
        //
        Set<ObjectInstance> allowedList = new HashSet<ObjectInstance>(list.size());
        for (ObjectInstance oi : list) {
            try {
                checkMBeanPermission(oi.getClassName(), null, oi.getObjectName(), "queryNames");
                allowedList.add(oi);
            } catch (SecurityException e) {
            // OK: Do not add this ObjectInstance to the list
            }
        }
        // Apply query to allowed MBeans only.
        //
        Set<ObjectInstance> queryObjectInstanceList = filterListOfObjectInstances(allowedList, query);
        queryList = new HashSet<ObjectName>(queryObjectInstanceList.size());
        for (ObjectInstance oi : queryObjectInstanceList) {
            queryList.add(oi.getObjectName());
        }
    } else {
        // Perform query.
        //
        queryList = queryNamesImpl(name, query);
    }
    return queryList;
}
Also used : ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) HashSet(java.util.HashSet)

Example 62 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class DefaultMBeanServerInterceptor method filterListOfObjectInstances.

/**
     * Applies the specified queries to the set of ObjectInstances.
     */
private Set<ObjectInstance> filterListOfObjectInstances(Set<ObjectInstance> list, QueryExp query) {
    //
    if (query == null) {
        return list;
    } else {
        Set<ObjectInstance> result = new HashSet<ObjectInstance>();
        //
        for (ObjectInstance oi : list) {
            boolean res = false;
            MBeanServer oldServer = QueryEval.getMBeanServer();
            query.setMBeanServer(server);
            try {
                res = query.apply(oi.getObjectName());
            } catch (Exception e) {
                res = false;
            } finally {
                /*
                     * query.setMBeanServer is probably
                     * QueryEval.setMBeanServer so put back the old
                     * value.  Since that method uses a ThreadLocal
                     * variable, this code is only needed for the
                     * unusual case where the user creates a custom
                     * QueryExp that calls a nested query on another
                     * MBeanServer.
                     */
                query.setMBeanServer(oldServer);
            }
            if (res) {
                result.add(oi);
            }
        }
        return result;
    }
}
Also used : ObjectInstance(javax.management.ObjectInstance) IntrospectionException(javax.management.IntrospectionException) OperationsException(javax.management.OperationsException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RuntimeMBeanException(javax.management.RuntimeMBeanException) RuntimeErrorException(javax.management.RuntimeErrorException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) JMRuntimeException(javax.management.JMRuntimeException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) HashSet(java.util.HashSet) MBeanServer(javax.management.MBeanServer)

Example 63 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class MLet method getMBeansFromURL.

/**
      * Loads a text file containing MLET tags that define the MBeans to
      * be added to the MBean server. The location of the text file is specified by
      * a URL. The MBeans specified in the MLET file will be instantiated and
      * registered in the MBean server.
      *
      * @param url The URL of the text file to be loaded as String object.
      *
      * @return A set containing one entry per MLET tag in the m-let
      * text file loaded.  Each entry specifies either the
      * ObjectInstance for the created MBean, or a throwable object
      * (that is, an error or an exception) if the MBean could not be
      * created.
      *
      * @exception ServiceNotFoundException One of the following
      * errors has occurred: The m-let text file does not contain an
      * MLET tag, the m-let text file is not found, a mandatory
      * attribute of the MLET tag is not specified, the url is
      * malformed.
      * @exception IllegalStateException MLet MBean is not registered
      * with an MBeanServer.
      *
      */
public Set<Object> getMBeansFromURL(String url) throws ServiceNotFoundException {
    String mth = "getMBeansFromURL";
    if (server == null) {
        throw new IllegalStateException("This MLet MBean is not " + "registered with an MBeanServer.");
    }
    // Parse arguments
    if (url == null) {
        MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "URL is null");
        throw new ServiceNotFoundException("The specified URL is null");
    } else {
        url = url.replace(File.separatorChar, '/');
    }
    if (MLET_LOGGER.isLoggable(Level.FINER)) {
        MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "<URL = " + url + ">");
    }
    // Parse URL
    try {
        MLetParser parser = new MLetParser();
        mletList = parser.parseURL(url);
    } catch (Exception e) {
        final String msg = "Problems while parsing URL [" + url + "], got exception [" + e.toString() + "]";
        MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, msg);
        throw EnvHelp.initCause(new ServiceNotFoundException(msg), e);
    }
    // Check that the list of MLets is not empty
    if (mletList.size() == 0) {
        final String msg = "File " + url + " not found or MLET tag not defined in file";
        MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, msg);
        throw new ServiceNotFoundException(msg);
    }
    // Walk through the list of MLets
    Set<Object> mbeans = new HashSet<Object>();
    for (MLetContent elmt : mletList) {
        // Initialize local variables
        String code = elmt.getCode();
        if (code != null) {
            if (code.endsWith(".class")) {
                code = code.substring(0, code.length() - 6);
            }
        }
        String name = elmt.getName();
        URL codebase = elmt.getCodeBase();
        String version = elmt.getVersion();
        String serName = elmt.getSerializedObject();
        String jarFiles = elmt.getJarFiles();
        URL documentBase = elmt.getDocumentBase();
        // Display debug information
        if (MLET_LOGGER.isLoggable(Level.FINER)) {
            final StringBuilder strb = new StringBuilder().append("\n\tMLET TAG     = ").append(elmt.getAttributes()).append("\n\tCODEBASE     = ").append(codebase).append("\n\tARCHIVE      = ").append(jarFiles).append("\n\tCODE         = ").append(code).append("\n\tOBJECT       = ").append(serName).append("\n\tNAME         = ").append(name).append("\n\tVERSION      = ").append(version).append("\n\tDOCUMENT URL = ").append(documentBase);
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, strb.toString());
        }
        // Load classes from JAR files
        StringTokenizer st = new StringTokenizer(jarFiles, ",", false);
        while (st.hasMoreTokens()) {
            String tok = st.nextToken().trim();
            if (MLET_LOGGER.isLoggable(Level.FINER)) {
                MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "Load archive for codebase <" + codebase + ">, file <" + tok + ">");
            }
            //
            try {
                codebase = check(version, codebase, tok, elmt);
            } catch (Exception ex) {
                MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(), mth, "Got unexpected exception", ex);
                mbeans.add(ex);
                continue;
            }
            // URLs to search for classes and resources.
            try {
                if (!Arrays.asList(getURLs()).contains(new URL(codebase.toString() + tok))) {
                    addURL(codebase + tok);
                }
            } catch (MalformedURLException me) {
            // OK : Ignore jar file if its name provokes the
            // URL to be an invalid one.
            }
        }
        // Instantiate the class specified in the
        // CODE or OBJECT section of the MLet tag
        //
        Object o;
        ObjectInstance objInst;
        if (code != null && serName != null) {
            final String msg = "CODE and OBJECT parameters cannot be specified at the " + "same time in tag MLET";
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, msg);
            mbeans.add(new Error(msg));
            continue;
        }
        if (code == null && serName == null) {
            final String msg = "Either CODE or OBJECT parameter must be specified in " + "tag MLET";
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, msg);
            mbeans.add(new Error(msg));
            continue;
        }
        try {
            if (code != null) {
                List<String> signat = elmt.getParameterTypes();
                List<String> stringPars = elmt.getParameterValues();
                List<Object> objectPars = new ArrayList<Object>();
                for (int i = 0; i < signat.size(); i++) {
                    objectPars.add(constructParameter(stringPars.get(i), signat.get(i)));
                }
                if (signat.isEmpty()) {
                    if (name == null) {
                        objInst = server.createMBean(code, null, mletObjectName);
                    } else {
                        objInst = server.createMBean(code, new ObjectName(name), mletObjectName);
                    }
                } else {
                    Object[] parms = objectPars.toArray();
                    String[] signature = new String[signat.size()];
                    signat.toArray(signature);
                    if (MLET_LOGGER.isLoggable(Level.FINEST)) {
                        final StringBuilder strb = new StringBuilder();
                        for (int i = 0; i < signature.length; i++) {
                            strb.append("\n\tSignature     = ").append(signature[i]).append("\t\nParams        = ").append(parms[i]);
                        }
                        MLET_LOGGER.logp(Level.FINEST, MLet.class.getName(), mth, strb.toString());
                    }
                    if (name == null) {
                        objInst = server.createMBean(code, null, mletObjectName, parms, signature);
                    } else {
                        objInst = server.createMBean(code, new ObjectName(name), mletObjectName, parms, signature);
                    }
                }
            } else {
                o = loadSerializedObject(codebase, serName);
                if (name == null) {
                    server.registerMBean(o, null);
                } else {
                    server.registerMBean(o, new ObjectName(name));
                }
                objInst = new ObjectInstance(name, o.getClass().getName());
            }
        } catch (ReflectionException ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "ReflectionException", ex);
            mbeans.add(ex);
            continue;
        } catch (InstanceAlreadyExistsException ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "InstanceAlreadyExistsException", ex);
            mbeans.add(ex);
            continue;
        } catch (MBeanRegistrationException ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "MBeanRegistrationException", ex);
            mbeans.add(ex);
            continue;
        } catch (MBeanException ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "MBeanException", ex);
            mbeans.add(ex);
            continue;
        } catch (NotCompliantMBeanException ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "NotCompliantMBeanException", ex);
            mbeans.add(ex);
            continue;
        } catch (InstanceNotFoundException ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "InstanceNotFoundException", ex);
            mbeans.add(ex);
            continue;
        } catch (IOException ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "IOException", ex);
            mbeans.add(ex);
            continue;
        } catch (SecurityException ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "SecurityException", ex);
            mbeans.add(ex);
            continue;
        } catch (Exception ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "Exception", ex);
            mbeans.add(ex);
            continue;
        } catch (Error ex) {
            MLET_LOGGER.logp(Level.FINER, MLet.class.getName(), mth, "Error", ex);
            mbeans.add(ex);
            continue;
        }
        mbeans.add(objInst);
    }
    return mbeans;
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) URL(java.net.URL) HashSet(java.util.HashSet) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ObjectInstance(javax.management.ObjectInstance) IOException(java.io.IOException) ServiceNotFoundException(javax.management.ServiceNotFoundException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName) StringTokenizer(java.util.StringTokenizer) ServiceNotFoundException(javax.management.ServiceNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) MBeanRegistrationException(javax.management.MBeanRegistrationException)

Example 64 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class ScanManager method addDirectoryScanner.

// Creates and registers a new directory scanner.
// Called by applyConfiguration.
// throws IllegalStateException if state is not STOPPED or COMPLETED
// (you cannot change the config while scanning is scheduled or running).
//
private DirectoryScannerMXBean addDirectoryScanner(DirectoryScannerConfig bean) throws JMException {
    try {
        final DirectoryScannerMXBean scanner;
        final ObjectName scanName;
        synchronized (this) {
            if (state != STOPPED && state != COMPLETED)
                throw new IllegalStateException(state.toString());
            scanner = createDirectoryScanner(bean);
            scanName = makeDirectoryScannerName(bean.getName());
        }
        LOG.fine("server: " + mbeanServer);
        LOG.fine("scanner: " + scanner);
        LOG.fine("scanName: " + scanName);
        final ObjectInstance moi = mbeanServer.registerMBean(scanner, scanName);
        final ObjectName moiName = moi.getObjectName();
        final DirectoryScannerMXBean proxy = JMX.newMXBeanProxy(mbeanServer, moiName, DirectoryScannerMXBean.class, true);
        scanmap.put(moiName, proxy);
        return proxy;
    } catch (RuntimeException x) {
        final String msg = "Operation failed: " + x;
        if (LOG.isLoggable(Level.FINEST))
            LOG.log(Level.FINEST, msg, x);
        else
            LOG.fine(msg);
        throw x;
    } catch (JMException x) {
        final String msg = "Operation failed: " + x;
        if (LOG.isLoggable(Level.FINEST))
            LOG.log(Level.FINEST, msg, x);
        else
            LOG.fine(msg);
        throw x;
    }
}
Also used : ObjectInstance(javax.management.ObjectInstance) JMException(javax.management.JMException) ObjectName(javax.management.ObjectName)

Example 65 with ObjectInstance

use of javax.management.ObjectInstance in project jdk8u_jdk by JetBrains.

the class ScanManager method createOtherConfigurationMBean.

// See ScanManagerMXBean
public ScanDirConfigMXBean createOtherConfigurationMBean(String name, String filename) throws JMException {
    final ScanDirConfig profile = new ScanDirConfig(filename);
    final ObjectName profName = makeScanDirConfigName(name);
    final ObjectInstance moi = mbeanServer.registerMBean(profile, profName);
    final ScanDirConfigMXBean proxy = JMX.newMXBeanProxy(mbeanServer, profName, ScanDirConfigMXBean.class, true);
    configmap.put(moi.getObjectName(), proxy);
    return proxy;
}
Also used : ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName)

Aggregations

ObjectInstance (javax.management.ObjectInstance)75 ObjectName (javax.management.ObjectName)36 Test (org.junit.Test)27 InstanceNotFoundException (javax.management.InstanceNotFoundException)15 MBeanServer (javax.management.MBeanServer)12 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)10 JmxResultProcessor (com.googlecode.jmxtrans.model.JmxResultProcessor)9 Result (com.googlecode.jmxtrans.model.Result)9 HashSet (java.util.HashSet)7 Attribute (javax.management.Attribute)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)5 AttributeNotFoundException (javax.management.AttributeNotFoundException)5 MBeanRegistrationException (javax.management.MBeanRegistrationException)5 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)5 HashMap (java.util.HashMap)4 AttributeList (javax.management.AttributeList)4 MBeanException (javax.management.MBeanException)4 MBeanServerConnection (javax.management.MBeanServerConnection)4 ReflectionException (javax.management.ReflectionException)4