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

📄 server.java

📁 jdk-6u10-docs java开发宝典
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		"contain the jmxConnector objectclass";	    throw new NamingException(msg);	}        // Now need to replace jmxConnector attributes.        //        final Attributes newattrs = new BasicAttributes();        newattrs.put("jmxAgentName",name);        newattrs.put("jmxServiceURL",jmxUrl.toString());        newattrs.put("jmxAgentHost",InetAddress.getLocalHost().getHostName());        newattrs.put("jmxProtocolType",jmxUrl.getProtocol());        newattrs.put("jmxExpirationDate",                      getExpirationDate(JMX_DEFAULT_LEASE));        root.modifyAttributes(mydn,DirContext.REPLACE_ATTRIBUTE,newattrs);    }    /**     * Creates an RMI Connector Server, starts it, and registers it     * with the LDAP directory.     * <p>     * This method will transfer a fixed set of System Properties to      * the Map given to the RMIConnectorServer constructor. Some     * JNDI properties, if defined, are transfered to the Map so     * that they may be used when LDAP is used as external directory      * to register the RMI Stub (see {@link javax.management.remote.rmi}      * Javadoc). Note that even if LDAP is used as external directory     * the {@link Context#INITIAL_CONTEXT_FACTORY      *            Context.INITIAL_CONTEXT_FACTORY} and      * {@link Context#PROVIDER_URL Context.PROVIDER_URL} properties     * usually don't need to be passed.     * <p>     * The following System properties, if defined, are transfered to     * the Map given to the RMIConnectorServer constructor.     * <ul><li>{@link Context#INITIAL_CONTEXT_FACTORY      *           Context.INITIAL_CONTEXT_FACTORY}</li>     *     <li>{@link Context#PROVIDER_URL      *           Context.PROVIDER_URL}</li>     *     <li>{@link Context#SECURITY_PRINCIPAL      *           Context.SECURITY_PRINCIPAL}</li>     *     <li>{@link Context#SECURITY_CREDENTIALS     *           Context.SECURITY_CREDENTIALS}</li>     *     <li>{@link RMIConnectorServer#JNDI_REBIND_ATTRIBUTE     *           RMIConnectorServer.JNDI_REBIND_ATTRIBUTE} - default     *           is <code>true</code>.</li>     * </ul>     *     * @param url A string representation of the JMXServiceURL.     * @return the created RMIConnectorServer.     */    public JMXConnectorServer rmi(String url)         throws IOException, JMException,               NamingException, ClassNotFoundException {        // Make a JMXServiceURL from the url string.        //        JMXServiceURL jurl = new JMXServiceURL(url);        // Prepare the environment Map        //        final HashMap env = new HashMap();        final String rprop = RMIConnectorServer.JNDI_REBIND_ATTRIBUTE;        final String rebind=System.getProperty(rprop,"true");        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);        env.put(rprop,rebind); // default is true.        // Create an RMIConnectorServer        //        System.out.println("Creating RMI Connector: " + jurl);        JMXConnectorServer rmis =            JMXConnectorServerFactory.newJMXConnectorServer(jurl, env, mbs);        // Get the AgentName for registering the Connector in the Lookup Service        //        final String agentName = System.getProperty("agent.name",                                                    "DefaultAgent");        // Start the connector and register it in the LDAP directory.        //        start(rmis,env,agentName);        return rmis;    }    /**     * Start a JMXConnectorServer and register it with the LDAP directory.     *     * @param server the JMXConnectorServer to start and register.     * @param env   the environment Map.     * @param agentName the AgentName with which the URL must be registered     *                  in the LDAP Directory. This is not a LDAP DN, but     *                  the value of the jmxAgentName attribute.     */    public void start(JMXConnectorServer server, Map env, String agentName)         throws IOException, NamingException {        // Start the JMXConnectorServer        //        server.start();        // Get a pointer to the LDAP directory.        //        final DirContext root = getRootContext();        // Create a JMX Service URL to register in the LDAP directory        //        final JMXServiceURL address = server.getAddress();        // Register the URL in the LDAP directory        //        register(root,address,agentName);    }    /**     * Returns a X.208 string representing the GMT date at now + sec.     *     * @param sec Number of seconds from now.     * @return an X.208 GMT GeneralizedTime (ending with Z).     */    public static String getExpirationDate(long sec) {        final SimpleDateFormat fmt =  new SimpleDateFormat("yyyyMMddHHmmss.S");        final Date date = new Date();        final Date gmtDate;        if (fmt.getCalendar().getTimeZone().inDaylightTime(date))            gmtDate = new Date(System.currentTimeMillis() -                               fmt.getCalendar().getTimeZone().getRawOffset() -                               fmt.getCalendar().getTimeZone().getDSTSavings() +                               1000*sec);        else            gmtDate =                new Date(System.currentTimeMillis() -                         fmt.getCalendar().getTimeZone().getRawOffset() +                         1000*sec);        return ((fmt.format(gmtDate))+"Z");    }    /**     * Trace a debug message.     */    private static void debug(String msg) {        if (debug) System.out.println(msg);    }    /**     * Program Main     * <p>     * Creates a server object, gets the JMX Service URL, and calls     * the method that will create and register the appropriate      * JMX Connector Server for that URL.     * <p>     * You may wish to use the following properties on the Java command line:     * <ul>     * <li><code>-Durl=&lt;jmxServiceURL&gt;</code>: specifies the URL of     *     the JMX Connector Server you wish to use. See README file for more     *     details.</li>     * <li><code>-Dagent.name=&lt;AgentName&gt;</code>: specifies an      *     AgentName to register with.</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();            // Create a new Server object.            //            final Server s = new Server();            // Get the JMXConnector URL            //            final String url =                System.getProperty("url", "service:jmx:rmi://");            // Build a JMXServiceURL            //            final JMXServiceURL jurl = new JMXServiceURL(url);            // Creates a JMX Connector Server            //            final JMXConnectorServer server;            debug("Creating Connector: " + jurl);            final String p = jurl.getProtocol();            if (p.equals("rmi"))        // Create an RMI Connector                s.rmi(url);            else if (p.equals("iiop"))  // Create an RMI/IIOP Connector                s.rmi(url);            else                        // Unsupported protocol                throw new MalformedURLException("Unsupported protocol: " + p);            System.out.println("\nService URL successfully registered " +                               "in the LDAP Lookup Service");        } 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 + -