⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 jdk-6u10-docs java开发宝典
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            "(&" + "(objectClass=jmxConnector) " +            "(jmxServiceURL=*) " +            queryProtocol +            "(jmxAgentName=" + ((name!=null)?name:"*") + "))";        System.out.println("Looking up JMX Agents with filter: " + query );        SearchControls ctrls = new SearchControls();        // Want to get all jmxConnector objects, wherever they've been        // registered.        //        ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);        // Want to get only the jmxServiceUrl and jmxExpirationDate         // (comment these lines and all attributes will be returned).        //        // ctrls.setReturningAttributes(new String[] {         //           "jmxServiceURL",        //           "jmxExpirationDate"        //           });        // Search...        //        final NamingEnumeration results = root.search("", query, ctrls);        // Get the URL...        //        while (results.hasMore()) {            // Get node...            //            final SearchResult r = (SearchResult) results.nextElement();            debug("Found node: " + r.getName());            // Get attributes            //            final Attributes attrs = r.getAttributes();            // Get jmxServiceURL attribute            //            final Attribute  attr = attrs.get("jmxServiceURL");            if (attr == null) continue;            // Get jmxExpirationDate            //            final Attribute  exp = attrs.get("jmxExpirationDate");            // Check that URL has not expired.            //            if ((exp != null) && hasExpired((String)exp.get())) {                System.out.print(r.getName() + ": ");                System.out.println("URL expired since: " + exp.get());                continue;            }            // Get the URL string            //            final String urlStr = (String)attr.get();            if (urlStr.length() == 0) continue;            debug("Found URL: " + urlStr);            // Create a JMXServiceURL            //            final JMXServiceURL url  = new JMXServiceURL(urlStr);            // Create a JMXConnector            //            final JMXConnector conn =                JMXConnectorFactory.newJMXConnector(url,null);            // Add the connector to the result list            //            list.add(conn);            if (debug) listAttributes(root,r.getName());        }        return list;    }    /**     * List all MBeans and their attributes.     */    public static void listMBeans(MBeanServerConnection server)         throws IOException {        final Set names = server.queryNames(null,null);        for (final Iterator i=names.iterator(); i.hasNext(); ) {            ObjectName name = (ObjectName)i.next();            System.out.println("Got MBean: "+name);            try {                MBeanInfo info =                     server.getMBeanInfo((ObjectName)name);                MBeanAttributeInfo[] attrs = info.getAttributes();                if (attrs == null) continue;                for (int j=0; j<attrs.length; j++) {                    if (attrs[j].isReadable()) {                        try {                            Object o =                                 server.getAttribute(name,attrs[j].getName());                            System.out.println("\t\t" + attrs[j].getName() +                                               " = "+o);                        } catch (Exception x) {                            System.err.println("JmxClient failed to get " +                                               attrs[j].getName());                            x.printStackTrace(System.err);                        }                    }                }            } catch (Exception x) {                System.err.println("JmxClient failed to get MBeanInfo: "  + x);                x.printStackTrace(System.err);            }        }    }    /**     * Trace a debug message.     */    private static void debug(String msg) {        if (debug) System.out.println(msg);    }    /**     * Program Main.     * <p>     * Lookup all JMX agents in the LDAP Directory and list     * their MBeans and attributes.     * <p>     * You may wish to use the following properties on the Java command line:     * <ul>     * <li><code>-Dagent.name=&lt;AgentName&gt;</code>: specifies an      *     AgentName to lookup (default is null, meaning any agent).</li>     * <li><code>-Dprotocol=&lt;ProtocolType&gt;</code>: restrains the client     *     to lookup for a specific protocol type (default is null,      *     meaning any type).</li>     * <li><code>-Djava.naming.factory.initial=&lt;initial-context-factory&gt;     *     </code>: The initial context factory to use for accessing the      *     LDAP directory (see {@link Context#INITIAL_CONTEXT_FACTORY     *     Context.INITIAL_CONTEXT_FACTORY}) - default is      *     <code>"com.sun.jndi.ldap.LdapCtxFactory"</code>.</li>     * <li><code>-Djava.naming.provider.url=&lt;provider-url&gt;</code>:     *     The LDAP Provider URL (see {@link Context#PROVIDER_URL      *     Context.PROVIDER_URL}).</li>     * <li><code>-Djava.naming.security.principal=&lt;ldap-principal&gt;     *     </code>: The security principal (login) to use to connect with     *     the LDAP directory (see {@link Context#SECURITY_PRINCIPAL      *     Context.SECURITY_PRINCIPAL} - default is      *     <code>"cn=Directory Manager"</code>.</li>     * <li><code>-Djava.naming.security.credentials=&lt;ldap-credentials&gt;     *     </code>: The security credentials (password) to use to      *     connect with the LDAP directory (see      *     {@link Context#SECURITY_CREDENTIALS     *     Context.SECURITY_CREDENTIALS}).</li>     * <li><code>-Ddebug="true|false"</code>: switch the Server debug flag     *     on/off (default is "false")</li>     * </ul>     */    public static void main(String[] args) {        try {            // Get the value of the debug flag.            //            debug = (Boolean.valueOf(System.getProperty("debug","false"))).                booleanValue();            // Get a pointer to the LDAP Directory.            //            final DirContext root = getRootContext();            debug("root is: " + root.getNameInNamespace());            final String protocolType=System.getProperty("protocol");            final String agentName=System.getProperty("agent.name");            // Lookup all matching agents in the LDAP Directory.            //            List l = lookup(root,protocolType,agentName);            // Attempt to connect to retrieved agents            //            System.out.println("Number of agents found : " + l.size());            int j = 1;            for (Iterator i=l.iterator();i.hasNext();j++) {                JMXConnector c1 = (JMXConnector) i.next();                if (c1 != null) {                    // Connect                    //                    System.out.println(                      "----------------------------------------------------");                    System.out.println("\tConnecting to agent number "+j);                    System.out.println(                      "----------------------------------------------------");                    debug("JMXConnector is: " + c1);                    // Prepare the environment Map                    //                    final HashMap env = new HashMap();                    final String factory =                        System.getProperty(Context.INITIAL_CONTEXT_FACTORY);                    final String ldapServerUrl =                        System.getProperty(Context.PROVIDER_URL);                    final String ldapUser =                        System.getProperty(Context.SECURITY_PRINCIPAL);                    final String ldapPasswd =                        System.getProperty(Context.SECURITY_CREDENTIALS);                    // Transfer some system properties to the Map                    //                    if (factory!= null) // this should not be needed                        env.put(Context.INITIAL_CONTEXT_FACTORY,factory);                    if (ldapServerUrl!=null) // this should not be needed                        env.put(Context.PROVIDER_URL, ldapServerUrl);                    if (ldapUser!=null) // this is needed when LDAP is used                        env.put(Context.SECURITY_PRINCIPAL, ldapUser);                    if (ldapPasswd != null) // this is needed when LDAP is used                        env.put(Context.SECURITY_CREDENTIALS, ldapPasswd);                    try {                         c1.connect(env);                    } catch (IOException x) {                        System.err.println("Connection failed: " + x);                        x.printStackTrace(System.err);                        continue;                    }                    // Get MBeanServerConnection                    //                    MBeanServerConnection conn =                         c1.getMBeanServerConnection();                    debug("Connection is:" + conn);                    System.out.println("Server domain is: " +                                        conn.getDefaultDomain());                    // List all MBeans                    //                    try {                        listMBeans(conn);                    } catch (IOException x) {                        System.err.println("Failed to list MBeans: " + x);                        x.printStackTrace(System.err);                    }                    // Close connector                    //                    try {                        c1.close();                    } catch (IOException x) {                        System.err.println("Failed to close connection: " + x);                        x.printStackTrace(System.err);                    }                }            }        } catch (Exception x) {            System.err.println("Unexpected exception caught in main: " + x);            x.printStackTrace(System.err);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -