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>Group</code> object.
 *
 * @param domain Domain in which this name is to be created
 * @param group The Group to be named//from  ww w .ja va 2s  .c om
 *
 * @exception MalformedObjectNameException if a name cannot be created
 */
public static ObjectName createObjectName(String domain, Group group) throws MalformedObjectNameException {

    ObjectName name = null;
    name = new ObjectName(domain + ":type=Group,groupname=" + group.getGroupname() + ",database="
            + group.getUserDatabase().getId());
    return (name);

}

From source file:catalina.mbeans.MBeanFactory.java

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

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

    // 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("SystemOutLogger");
    ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), logger);
    return (oname.toString());
}

From source file:catalina.mbeans.MBeanUtils.java

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

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

}

From source file:catalina.mbeans.MBeanUtils.java

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

    ObjectName name = null;
    Container container = loader.getContainer();

    if (container instanceof Engine) {
        Service service = ((Engine) container).getService();
        name = new ObjectName(domain + ":type=Loader,service=" + service.getName());
    } else if (container instanceof Host) {
        Engine engine = (Engine) container.getParent();
        Service service = engine.getService();
        name = new ObjectName(
                domain + ":type=Loader,host=" + container.getName() + ",service=" + service.getName());
    } else if (container instanceof Context) {
        String path = ((Context) container).getPath();
        if (path.length() < 1) {
            path = "/";
        }
        Host host = (Host) container.getParent();
        Engine engine = (Engine) host.getParent();
        Service service = engine.getService();
        name = new ObjectName(domain + ":type=Loader,path=" + path + ",host=" + host.getName() + ",service="
                + service.getName());
    } else if (container == null) {
        DefaultContext defaultContext = loader.getDefaultContext();
        if (defaultContext != null) {
            Container parent = defaultContext.getParent();
            if (parent instanceof Engine) {
                Service service = ((Engine) parent).getService();
                name = new ObjectName(domain + ":type=DefaultLoader,service=" + service.getName());
            } else if (parent instanceof Host) {
                Engine engine = (Engine) parent.getParent();
                Service service = engine.getService();
                name = new ObjectName(domain + ":type=DefaultLoader,host=" + parent.getName() + ",service="
                        + service.getName());
            }
        }
    }

    return (name);

}

From source file:catalina.mbeans.MBeanFactory.java

/**
 * Create a new  UserDatabaseRealm./*www. j  ava 2  s. c om*/
 *
 * @param parent MBean Name of the associated parent component
 * @param resourceName Global JNDI resource name of the associated
 *  UserDatabase
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createUserDatabaseRealm(String parent, String resourceName) throws Exception {

    // Create a new UserDatabaseRealm instance
    UserDatabaseRealm realm = new UserDatabaseRealm();
    realm.setResourceName(resourceName);
    // 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.setRealm(realm);
    } else if (type.equals("Engine")) {
        engine.setRealm(realm);
    } else if (type.equals("Host")) {
        Host host = (Host) engine.findChild(pname.getKeyProperty("host"));
        host.setRealm(realm);
    }

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

}

From source file:com.linkedin.d2.balancer.util.LoadBalancerClientCli.java

public static void resetTogglingStores(String host, boolean enabled) throws Exception {

    MonitoredHost _host = MonitoredHost.getMonitoredHost(new HostIdentifier(host));

    for (Object pidObj : _host.activeVms()) {
        int pid = (Integer) pidObj;

        System.out.println("checking pid: " + pid);

        JMXServiceURL jmxUrl = null;
        com.sun.tools.attach.VirtualMachine vm = com.sun.tools.attach.VirtualMachine.attach(pid + "");

        try {//from   w  w  w. j a  va2s  .c om
            // get the connector address
            String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
            // establish connection to connector server
            if (connectorAddress != null) {
                jmxUrl = new JMXServiceURL(connectorAddress);
            }
        } finally {
            vm.detach();
        }

        if (jmxUrl != null) {
            System.out.println("got jmx url: " + jmxUrl);

            // connect to jmx
            JMXConnector connector = JMXConnectorFactory.connect(jmxUrl);

            connector.connect();

            MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();

            // look for all beans in the d2 name space
            Set<ObjectInstance> objectInstances = mbeanServer.queryMBeans(new ObjectName("com.linkedin.d2:*"),
                    null);

            for (ObjectInstance objectInstance : objectInstances) {
                System.err.println("checking object: " + objectInstance.getObjectName());

                // if we've found a toggling store, then toggle it
                if (objectInstance.getObjectName().toString().endsWith("TogglingStore")) {
                    System.out.println("found toggling zk store, so toggling to: " + enabled);

                    mbeanServer.invoke(objectInstance.getObjectName(), "setEnabled", new Object[] { enabled },
                            new String[] { "boolean" });
                }
            }
        } else {
            System.out.println("pid is not a jmx process: " + pid);
        }
    }
}

From source file:catalina.mbeans.MBeanFactory.java

/**
 * Create a new Web Application Loader./*from w  w  w.j ava2s. c om*/
 *
 * @param parent MBean Name of the associated parent component
 *
 * @exception Exception if an MBean cannot be created or registered
 */
public String createWebappLoader(String parent) throws Exception {

    // Create a new WebappLoader instance
    WebappLoader loader = new WebappLoader();

    // Add the new instance to its parent component
    ObjectName pname = new ObjectName(parent);
    Server server = ServerFactory.getServer();
    String type = pname.getKeyProperty("type");
    Service service = server.findService(pname.getKeyProperty("service"));
    Engine engine = (Engine) service.getContainer();
    if ((type != null) && (type.equals("Context"))) {
        Host host = (Host) engine.findChild(pname.getKeyProperty("host"));
        String pathStr = getPathStr(pname.getKeyProperty("path"));
        Context context = (Context) host.findChild(pathStr);
        context.setLoader(loader);
    } else if ((type != null) && (type.equals("DefaultContext"))) {
        String hostName = pname.getKeyProperty("host");
        DefaultContext defaultContext = null;
        if (hostName == null) {
            defaultContext = engine.getDefaultContext();
        } else {
            Host host = (Host) engine.findChild(hostName);
            defaultContext = host.getDefaultContext();
        }
        if (defaultContext != null) {
            loader.setDefaultContext(defaultContext);
            defaultContext.setLoader(loader);
        }
    }

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

}

From source file:catalina.mbeans.MBeanUtils.java

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

    ObjectName name = null;
    Container container = logger.getContainer();

    if (container instanceof Engine) {
        Service service = ((Engine) container).getService();
        name = new ObjectName(domain + ":type=Logger,service=" + service.getName());
    } else if (container instanceof Host) {
        Engine engine = (Engine) container.getParent();
        Service service = engine.getService();
        name = new ObjectName(
                domain + ":type=Logger,host=" + container.getName() + ",service=" + service.getName());
    } else if (container instanceof Context) {
        String path = ((Context) container).getPath();
        if (path.length() < 1) {
            path = "/";
        }
        Host host = (Host) container.getParent();
        Engine engine = (Engine) host.getParent();
        Service service = engine.getService();
        name = new ObjectName(domain + ":type=Logger,path=" + path + ",host=" + host.getName() + ",service="
                + service.getName());
    }

    return (name);

}

From source file:catalina.mbeans.MBeanFactory.java

/**
 * Remove an existing Connector./*from   ww  w.  j a v  a 2  s .c  o m*/
 *
 * @param name MBean Name of the comonent to remove
 *
 * @param serviceName Service name of the connector to remove
 *
 * @exception Exception if a component cannot be removed
 */
public void removeConnector(String name) throws Exception {

    // Acquire a reference to the component to be removed
    ObjectName oname = new ObjectName(name);
    Server server = ServerFactory.getServer();
    String serviceName = oname.getKeyProperty("service");
    Service service = server.findService(serviceName);
    String port = oname.getKeyProperty("port");
    String address = oname.getKeyProperty("address");

    Connector conns[] = (Connector[]) service.findConnectors();

    for (int i = 0; i < conns.length; i++) {
        Class cls = conns[i].getClass();
        Method getAddrMeth = cls.getMethod("getAddress", null);
        Object addrObj = getAddrMeth.invoke(conns[i], null);
        String connAddress = null;
        if (addrObj != null) {
            connAddress = addrObj.toString();
        }
        Method getPortMeth = cls.getMethod("getPort", null);
        Object portObj = getPortMeth.invoke(conns[i], null);
        String connPort = new String();
        if (portObj != null) {
            connPort = portObj.toString();
        }
        if (((address.equals("null")) && (connAddress == null)) && port.equals(connPort)) {
            service.removeConnector(conns[i]);
            break;
        } else if (address.equals(connAddress) && port.equals(connPort)) {
            // Remove this component from its parent component
            service.removeConnector(conns[i]);
            break;
        }
    }

}

From source file:catalina.mbeans.MBeanUtils.java

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

    ObjectName name = null;
    Container container = manager.getContainer();

    if (container instanceof Engine) {
        Service service = ((Engine) container).getService();
        name = new ObjectName(domain + ":type=Manager,service=" + service.getName());
    } else if (container instanceof Host) {
        Engine engine = (Engine) container.getParent();
        Service service = engine.getService();
        name = new ObjectName(
                domain + ":type=Manager,host=" + container.getName() + ",service=" + service.getName());
    } else if (container instanceof Context) {
        String path = ((Context) container).getPath();
        if (path.length() < 1) {
            path = "/";
        }
        Host host = (Host) container.getParent();
        Engine engine = (Engine) host.getParent();
        Service service = engine.getService();
        name = new ObjectName(domain + ":type=Manager,path=" + path + ",host=" + host.getName() + ",service="
                + service.getName());
    } else if (container == null) {
        DefaultContext defaultContext = manager.getDefaultContext();
        if (defaultContext != null) {
            Container parent = defaultContext.getParent();
            if (parent instanceof Engine) {
                Service service = ((Engine) parent).getService();
                name = new ObjectName(domain + ":type=DefaultManager,service=" + service.getName());
            } else if (parent instanceof Host) {
                Engine engine = (Engine) parent.getParent();
                Service service = engine.getService();
                name = new ObjectName(domain + ":type=DefaultManager,host=" + parent.getName() + ",service="
                        + service.getName());
            }
        }
    }

    return (name);

}