Search in sources :

Example 36 with NotCompliantMBeanException

use of javax.management.NotCompliantMBeanException 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 37 with NotCompliantMBeanException

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

the class NotCompliantCauseTest method test1.

/**
     * Test that NotCompliantMBeanException has a cause in case of
     * type mapping problems.
     **/
void test1() {
    try {
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        ObjectName oname = new ObjectName("domain:type=test");
        mbs.createMBean(NotCompliant.class.getName(), oname);
        System.err.println("ERROR: expected " + "NotCompliantMBeanException not thrown");
        throw new RuntimeTestException("NotCompliantMBeanException not thrown");
    } catch (RuntimeTestException e) {
        throw e;
    } catch (NotCompliantMBeanException e) {
        Throwable cause = e.getCause();
        if (cause == null)
            throw new RuntimeTestException("NotCompliantMBeanException " + "doesn't have any cause.", e);
        while (cause.getCause() != null) {
            if (cause instanceof OpenDataException)
                break;
            cause = cause.getCause();
        }
        if (!(cause instanceof OpenDataException))
            throw new RuntimeTestException("NotCompliantMBeanException " + "doesn't have expected cause (" + OpenDataException.class.getName() + "): " + cause, e);
        System.err.println("SUCCESS: Found expected cause: " + cause);
    } catch (Exception e) {
        System.err.println("Unexpected exception: " + e);
        throw new RuntimeException("Unexpected exception: " + e, e);
    }
}
Also used : OpenDataException(javax.management.openmbean.OpenDataException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) OpenDataException(javax.management.openmbean.OpenDataException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 38 with NotCompliantMBeanException

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

the class GetMBeanInfoExceptionTest method main.

public static void main(String[] args) throws Exception {
    int error = 0;
    // Instantiate the MBean server
    //
    System.out.println("Create the MBean server");
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    // Register the MBean
    //
    System.out.println("Create a TestDynamicMBean");
    TestDynamicMBean obj = new TestDynamicMBean();
    ObjectName n = new ObjectName("d:k=v");
    try {
        mbs.registerMBean(obj, n);
        System.out.println("Didn't get expected NotCompliantMBeanException");
        error++;
    } catch (NotCompliantMBeanException e) {
        boolean found = false;
        Throwable t = e.getCause();
        while (t != null) {
            if (t instanceof IllegalArgumentException && "GetMBeanInfoExceptionTest".equals(t.getMessage())) {
                found = true;
            }
            t = t.getCause();
        }
        if (found) {
            System.out.println("Found expected IllegalArgumentException");
        } else {
            System.out.println("Didn't find expected IllegalArgumentException");
            error++;
        }
    } catch (Exception e) {
        System.out.println("Got " + e.getClass().getName() + "instead of expected NotCompliantMBeanException");
        error++;
    }
    if (error > 0) {
        System.out.println("Test failed");
        throw new IllegalArgumentException("Test failed");
    } else {
        System.out.println("Test passed");
    }
}
Also used : NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) AttributeNotFoundException(javax.management.AttributeNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) ReflectionException(javax.management.ReflectionException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 39 with NotCompliantMBeanException

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

the class MBeanFallbackTest method testPrivate.

private static void testPrivate(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a private MBean " + iface.getName() + " ...");
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Compliant");
        mbs.registerMBean(bean, on);
        success("Registered a private MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("MBean not registered");
        } else {
            throw e;
        }
    }
}
Also used : NotCompliantMBeanException(javax.management.NotCompliantMBeanException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 40 with NotCompliantMBeanException

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

the class MBeanTest method testCompliant.

private static void testCompliant(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a compliant MBean " + iface.getName() + " ...");
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Compliant");
        mbs.registerMBean(bean, on);
        success("Registered a compliant MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("MBean not registered");
        } else {
            throw e;
        }
    }
}
Also used : NotCompliantMBeanException(javax.management.NotCompliantMBeanException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Aggregations

NotCompliantMBeanException (javax.management.NotCompliantMBeanException)58 ObjectName (javax.management.ObjectName)37 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)29 MBeanRegistrationException (javax.management.MBeanRegistrationException)29 MBeanServer (javax.management.MBeanServer)26 MalformedObjectNameException (javax.management.MalformedObjectNameException)24 StandardMBean (javax.management.StandardMBean)15 InstanceNotFoundException (javax.management.InstanceNotFoundException)9 MBeanException (javax.management.MBeanException)5 ReflectionException (javax.management.ReflectionException)5 AttributeNotFoundException (javax.management.AttributeNotFoundException)4 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)4 Logger (org.apache.aries.jmx.Logger)4 RegistrableStandardEmitterMBean (org.apache.aries.jmx.util.shared.RegistrableStandardEmitterMBean)4 IntrospectionException (javax.management.IntrospectionException)3 JMRuntimeException (javax.management.JMRuntimeException)3 ObjectInstance (javax.management.ObjectInstance)3 RuntimeOperationsException (javax.management.RuntimeOperationsException)3 PackageAdmin (org.osgi.service.packageadmin.PackageAdmin)3 IOException (java.io.IOException)2