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

📄 connectorfactory.java

📁 jmx codeJava源码
💻 JAVA
字号:

package book.jmx.examples;

import java.util.*;
import java.lang.reflect.*;
import java.rmi.*;

import javax.naming.*;
import javax.jms.*;

public class ConnectorFactory  {

  private static Properties jndiProps = null;

  public static RemoteMBeanServer createConnector(String transport,
      String jndiName) throws ConnectorException {
    
    Context ctx = null;
    MBeanHandle handle = null;
    
    try {
      if (jndiProps == null)
        ctx = new InitialContext();
      else 
        ctx = new InitialContext(jndiProps);
      
      handle = (MBeanHandle)ctx.lookup(jndiName);
    }
    catch (NamingException e) {
      throw new ConnectorException(
        "Unable to find the handle", e
      );
    }
    
    return createConnector(transport, handle.getProperties());   
  }
  
  public static RemoteMBeanServer createConnector(String transport,
      Properties props)  throws ConnectorException {
    
    if (transport.equalsIgnoreCase("RMI")) {
      try {  
        return (RemoteMBeanServer)Proxy.newProxyInstance(
            Thread.currentThread().getContextClassLoader(),
            new Class[] { RemoteMBeanServer.class },
            new RMIInvocationHandler(props)
        );
      }
      catch (Exception e) {
        throw new ConnectorException(
            "Unable to create proxy.", e
        );
      }
    }
    
    else if (transport.equalsIgnoreCase("SOAP")) {
      try {
        return (RemoteMBeanServer)Proxy.newProxyInstance(
            Thread.currentThread().getContextClassLoader(),
            new Class[] { RemoteMBeanServer.class },
            new SOAPInvocationHandler(props)
        );
      }
      catch (Exception e) {
        throw new ConnectorException(
            "Unable to create proxy.", e
        );
      }
    }
    
    throw new ConnectorException(
        "Unrecognized connector transport: " + transport
    );
  }
  
  
  public static AsyncMBeanServer createAsyncConnector(
      String transport, String jndiName) throws ConnectorException {
      
    try {
      Context ctx = null;
    
      if (jndiProps == null) 
        ctx = new InitialContext();
      else
        ctx = new InitialContext(jndiProps);
      
      MBeanHandle handle = (MBeanHandle)ctx.lookup(jndiName);
      
      return createAsyncConnector(transport, handle.getProperties());  
    }
    catch (NamingException e) {
      throw new ConnectorException(
          "Error connecting to Naming Service.", e
      );
    }
    
  }
      
  public static AsyncMBeanServer createAsyncConnector(
      String transport, Properties props) throws ConnectorException {
    
    if (transport.equalsIgnoreCase("jms")) {
      try {
        return (AsyncMBeanServer)Proxy.newProxyInstance(
            Thread.currentThread().getContextClassLoader(),
            new Class[] { AsyncMBeanServer.class },
            new JMSInvocationHandler(props)
        );
      }
      catch (JMSException e) {
        throw new ConnectorException(
            "Error connecting to Java Message Service", e
        );
      }
      catch (NamingException e) {
        throw new ConnectorException(
            "Error connecting to Naming Service", e
        );
      }
    }

    throw new ConnectorException("Unknown transport " +
        transport);
        
  }  


  public static void setJNDIProperties(Properties props) {
    jndiProps = props;  
  }
  
}

⌨️ 快捷键说明

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