Search in sources :

Example 1 with MBeanPersister

use of mx4j.persist.MBeanPersister in project geode by apache.

the class MX4JModelMBean method findPersister.

private PersisterMBean findPersister() throws MBeanException, InstanceNotFoundException {
    Logger logger = getLogger();
    ModelMBeanInfo info = getModelMBeanInfo();
    if (info == null) {
        // Not yet initialized
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Can't find persister, ModelMBeanInfo is null");
        return null;
    }
    Descriptor mbeanDescriptor = info.getMBeanDescriptor();
    if (mbeanDescriptor == null) {
        // This is normally should not happen if ModelMBeanInfoSupport is used
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Can't find persister, MBean descriptor is null");
        return null;
    }
    String location = (String) mbeanDescriptor.getFieldValue("persistLocation");
    String name = (String) mbeanDescriptor.getFieldValue("persistName");
    String mbeanName = (String) mbeanDescriptor.getFieldValue("name");
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Persistence fields: location=" + location + ", name=" + name);
    if (mbeanName == null && name == null) {
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Persistence is not supported by this ModelMBean");
        return null;
    }
    // Try to see if this mbean should delegate to another mbean
    if (name != null) {
        try {
            ObjectName objectName = new ObjectName(name.trim());
            // OK, a valid object name
            MBeanServer server = getMBeanServer();
            if (server == null)
                throw new MBeanException(new IllegalStateException(LocalizedStrings.MX4JModelMBean_MX4JMODELMBEAN_IS_NOT_REGISTERED.toLocalizedString()));
            if (server.isRegistered(objectName) && server.isInstanceOf(objectName, PersisterMBean.class.getName())) {
                // OK, the given mbean is registered with this mbean server
                PersisterMBean persister = new MBeanPersister(server, objectName);
                if (logger.isEnabledFor(Logger.DEBUG))
                    logger.debug("Persistence is delegated to this MBean: " + objectName);
                return persister;
            } else {
                throw new InstanceNotFoundException(objectName.toString());
            }
        } catch (MalformedObjectNameException ignored) {
            // It does not delegates to another mbean, use default
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Persistence is not delegated to another MBean");
        }
        // Default is serialization to file
        FilePersister persister = new FilePersister(location, name);
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Persistence is realized through file system in " + persister.getFileName());
        return persister;
    } else {
        // Only location given, use MBean name
        FilePersister persister = new FilePersister(location, mbeanName);
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Persistence is realized through file system in " + persister.getFileName());
        return persister;
    }
}
Also used : MBeanPersister(mx4j.persist.MBeanPersister) MalformedObjectNameException(javax.management.MalformedObjectNameException) PersisterMBean(mx4j.persist.PersisterMBean) FilePersister(mx4j.persist.FilePersister) InstanceNotFoundException(javax.management.InstanceNotFoundException) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ObjectName(javax.management.ObjectName) Descriptor(javax.management.Descriptor) MBeanException(javax.management.MBeanException) MBeanServer(javax.management.MBeanServer)

Aggregations

Descriptor (javax.management.Descriptor)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanException (javax.management.MBeanException)1 MBeanServer (javax.management.MBeanServer)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 ObjectName (javax.management.ObjectName)1 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)1 FileLogger (mx4j.log.FileLogger)1 Logger (mx4j.log.Logger)1 MBeanLogger (mx4j.log.MBeanLogger)1 FilePersister (mx4j.persist.FilePersister)1 MBeanPersister (mx4j.persist.MBeanPersister)1 PersisterMBean (mx4j.persist.PersisterMBean)1