Example usage for javax.management ObjectName ObjectName

List of usage examples for javax.management ObjectName ObjectName

Introduction

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

Prototype

public ObjectName(String name) throws MalformedObjectNameException 

Source Link

Document

Construct an object name from the given string.

Usage

From source file:catalina.mbeans.MBeanUtils.java

/**
* Create an <code>ObjectName</code> for this
* <code>ContextResourceLink</code> object.
*
* @param domain Domain in which this name is to be created
* @param resourceLink The ContextResourceLink to be named
*
* @exception MalformedObjectNameException if a name cannot be created
*//*from   w w  w .  j  a  v a2  s. c o  m*/
public static ObjectName createObjectName(String domain, ContextResourceLink resourceLink)
        throws MalformedObjectNameException {

    ObjectName name = null;
    String encodedResourceLinkName = encodeStr(resourceLink.getName());
    Object container = resourceLink.getNamingResources().getContainer();
    if (container instanceof Server) {
        name = new ObjectName(domain + ":type=ResourceLink" + ",resourcetype=Global,class="
                + resourceLink.getType() + ",name=" + encodedResourceLinkName);
    } else if (container instanceof Context) {
        String path = ((Context) container).getPath();
        if (path.length() < 1)
            path = "/";
        Host host = (Host) ((Context) container).getParent();
        Engine engine = (Engine) host.getParent();
        Service service = engine.getService();
        name = new ObjectName(domain + ":type=ResourceLink" + ",resourcetype=Context,path=" + path + ",host="
                + host.getName() + ",service=" + service.getName() + ",class=" + resourceLink.getType()
                + ",name=" + encodedResourceLinkName);
    } else if (container instanceof DefaultContext) {
        container = ((DefaultContext) container).getParent();
        if (container instanceof Host) {
            Host host = (Host) container;
            Service service = ((Engine) host.getParent()).getService();
            name = new ObjectName(domain + ":type=ResourceLink" + ",resourcetype=HostDefaultContext,host="
                    + host.getName() + ",service=" + service.getName() + ",class=" + resourceLink.getType()
                    + ",name=" + encodedResourceLinkName);
        } else if (container instanceof Engine) {
            Engine engine = (Engine) container;
            Service service = engine.getService();
            name = new ObjectName(domain + ":type=ResourceLink" + ",resourcetype=ServiceDefaultContext,service="
                    + service.getName() + ",class=" + resourceLink.getType() + ",name="
                    + encodedResourceLinkName);
        }
    }

    return (name);

}

From source file:JDBCPool.dbcp.demo.sourcecode.BaseGenericObjectPool.java

/**
 * Registers the pool with the platform MBean server.
 * The registered name will be/*from   w  w w .j a va  2 s .  c  om*/
 * <code>jmxNameBase + jmxNamePrefix + i</code> where i is the least
 * integer greater than or equal to 1 such that the name is not already
 * registered. Swallows MBeanRegistrationException, NotCompliantMBeanException
 * returning null.
 *
 * @param config Pool configuration
 * @param jmxNameBase default base JMX name for this pool
 * @param jmxNamePrefix name prefix
 * @return registered ObjectName, null if registration fails
 */
private ObjectName jmxRegister(BaseObjectPoolConfig config, String jmxNameBase, String jmxNamePrefix) {
    ObjectName objectName = null;
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    int i = 1;
    boolean registered = false;
    String base = config.getJmxNameBase();
    if (base == null) {
        base = jmxNameBase;
    }
    while (!registered) {
        try {
            ObjectName objName;
            // Skip the numeric suffix for the first pool in case there is
            // only one so the names are cleaner.
            if (i == 1) {
                objName = new ObjectName(base + jmxNamePrefix);
            } else {
                objName = new ObjectName(base + jmxNamePrefix + i);
            }
            mbs.registerMBean(this, objName);
            objectName = objName;
            registered = true;
        } catch (MalformedObjectNameException e) {
            if (BaseObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX.equals(jmxNamePrefix)
                    && jmxNameBase.equals(base)) {
                // Shouldn't happen. Skip registration if it does.
                registered = true;
            } else {
                // Must be an invalid name. Use the defaults instead.
                jmxNamePrefix = BaseObjectPoolConfig.DEFAULT_JMX_NAME_PREFIX;
                base = jmxNameBase;
            }
        } catch (InstanceAlreadyExistsException e) {
            // Increment the index and try again
            i++;
        } catch (MBeanRegistrationException e) {
            // Shouldn't happen. Skip registration if it does.
            registered = true;
        } catch (NotCompliantMBeanException e) {
            // Shouldn't happen. Skip registration if it does.
            registered = true;
        }
    }
    return objectName;
}

From source file:com.jagornet.dhcp.server.JagornetDhcpServer.java

/**
 * Register Log4J in JMX to allow dynamic configuration
 * of server logging using JMX client (e.g. jconsole).
 *///from www. j a va 2 s  .  co m
@SuppressWarnings("unchecked")
public static void registerLog4jInJmx() {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {
        // Create and Register the top level Log4J MBean
        HierarchyDynamicMBean hdm = new HierarchyDynamicMBean();
        ObjectName mbo = new ObjectName("log4j:hiearchy=default");
        mbs.registerMBean(hdm, mbo);

        // Add the root logger to the Hierarchy MBean
        org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
        hdm.addLoggerMBean(rootLogger.getName());

        // Get each logger from the Log4J Repository and add it to
        // the Hierarchy MBean created above.
        LoggerRepository r = LogManager.getLoggerRepository();
        Enumeration<Logger> loggers = r.getCurrentLoggers();
        if (loggers != null) {
            while (loggers.hasMoreElements()) {
                org.apache.log4j.Logger logger = (org.apache.log4j.Logger) loggers.nextElement();
                hdm.addLoggerMBean(logger.getName());
            }
        }
    } catch (Exception ex) {
        log.error("Failure registering Log4J in JMX: " + ex);
    }
}

From source file:org.red5.server.undertow.UndertowLoader.java

protected void registerJMX() {
    // register with jmx
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {/*from   w w w . ja  v a2 s  . c  om*/
        ObjectName oName = new ObjectName("org.red5.server:type=UndertowLoader");
        // check for existing registration before registering
        if (!mbs.isRegistered(oName)) {
            mbs.registerMBean(this, oName);
        } else {
            log.debug("ContextLoader is already registered in JMX");
        }
    } catch (Exception e) {
        log.warn("Error on jmx registration", e);
    }
}

From source file:org.red5.server.undertow.UndertowLoader.java

protected void unregisterJMX() {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {// www.  j a  v  a 2 s.  c  om
        ObjectName oName = new ObjectName("org.red5.server:type=UndertowLoader");
        mbs.unregisterMBean(oName);
    } catch (Exception e) {
        log.warn("Exception unregistering", e);
    }
}

From source file:org.red5.server.scope.Scope.java

protected void registerJMX() {
    // register with jmx
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {/*from   w  w w  .j  av a  2s  . co m*/
        String cName = this.getClass().getName();
        if (cName.indexOf('.') != -1) {
            cName = cName.substring(cName.lastIndexOf('.')).replaceFirst("[\\.]", "");
        }
        oName = new ObjectName(String.format("org.red5.server:type=%s,name=%s", cName, name));
        // don't reregister
        if (!mbs.isRegistered(oName)) {
            mbs.registerMBean(new StandardMBean(this, ScopeMXBean.class, true), oName);
        }
    } catch (Exception e) {
        log.warn("Error on jmx registration", e);
    }
}

From source file:catalina.mbeans.MBeanUtils.java

/**
 * Create an <code>ObjectName</code> for this
 * <code>DefaultContext</code> object.
 *
 * @param domain Domain in which this name is to be created
 * @param context The DefaultContext to be named
 *
 * @exception MalformedObjectNameException if a name cannot be created
 *///w ww .  java 2 s  . co m
public static ObjectName createObjectName(String domain, DefaultContext context)
        throws MalformedObjectNameException {

    ObjectName name = null;
    Container container = context.getParent();
    if (container instanceof Host) {
        Host host = (Host) container;
        Service service = ((Engine) host.getParent()).getService();
        name = new ObjectName(
                domain + ":type=DefaultContext,host=" + host.getName() + ",service=" + service.getName());
    } else if (container instanceof Engine) {
        Engine engine = (Engine) container;
        Service service = engine.getService();
        name = new ObjectName(domain + ":type=DefaultContext,service=" + service.getName());
    }

    return (name);

}

From source file:catalina.mbeans.MBeanFactory.java

/**
 * Create a new System Error Logger./*w  w w.  j a v a 2  s. com*/
 *
 * @param parent MBean Name of the associated parent component
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createSystemErrLogger(String parent) throws Exception {

    // Create a new SystemErrLogger instance
    SystemErrLogger logger = new SystemErrLogger();

    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    String type = pname.getKeyProperty("type");
    Server server = ServerFactory.getServer();
    Service service = server.findService(pname.getKeyProperty("service"));
    Engine engine = (Engine) service.getContainer();
    if (type.equals("Context")) {
        Host host = (Host) engine.findChild(pname.getKeyProperty("host"));
        String pathStr = getPathStr(pname.getKeyProperty("path"));
        Context context = (Context) host.findChild(pathStr);
        context.setLogger(logger);
    } else if (type.equals("Engine")) {
        engine.setLogger(logger);
    } else if (type.equals("Host")) {
        Host host = (Host) engine.findChild(pname.getKeyProperty("host"));
        host.setLogger(logger);
    }

    // Return the corresponding MBean name
    ManagedBean managed = registry.findManagedBean("SystemErrLogger");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), logger);
    return (oname.toString());

}

From source file:net.lightbody.bmp.proxy.jetty.util.jmx.ModelMBeanImpl.java

public synchronized ObjectName uniqueObjectName(MBeanServer server, Object object, String objectName) {
    if (!objectName.endsWith("=")) {
        String className = object.getClass().getName();
        if (className.indexOf(".") > 0)
            className = className.substring(className.lastIndexOf(".") + 1);
        if (className.endsWith("MBean"))
            className = className.substring(0, className.length() - 5);
        if (!objectName.endsWith(":"))
            objectName += ",";
        objectName += className + "=";
    }//  w  w w  .j  av a2s  . c  o m

    ObjectName oName = null;
    try {
        while (true) {
            Integer id = (Integer) __objectId.get(objectName);
            if (id == null)
                id = new Integer(0);
            oName = new ObjectName(objectName + id);
            id = new Integer(id.intValue() + 1);
            __objectId.put(objectName, id);

            // If no server, this must be unique
            if (server == null)
                break;

            // Otherwise let's check it is unique
            // if not found then it is unique
            if (!server.isRegistered(oName))
                break;
        }
    } catch (Exception e) {
        log.warn(LogSupport.EXCEPTION, e);
    }

    return oName;
}

From source file:catalina.mbeans.MBeanUtils.java

/**
 * Create an <code>ObjectName</code> for this
 * <code>Engine</code> object.
 *
 * @param domain Domain in which this name is to be created
 * @param engine The Engine to be named//w ww .  ja va  2  s.com
 *
 * @exception MalformedObjectNameException if a name cannot be created
 */
public static ObjectName createObjectName(String domain, Engine engine) throws MalformedObjectNameException {

    ObjectName name = null;
    name = new ObjectName(domain + ":type=Engine,service=" + engine.getService().getName());
    return (name);

}