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

📄 servicelocator.java~1~

📁 Spring +Web 的完整 MyEclipse 项目源码,使用者可以作为入门材料可以在此基础上深入学习
💻 JAVA~1~
字号:
package loginejb;

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.naming.NamingException;
import javax.naming.Context;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import java.util.Properties;

public class ServiceLocator {
  private static ServiceLocator serviceLocator;
  private static Context context;
  protected ServiceLocator() throws ServiceLocatorException {
    try {
      context = getInitialContext();
    }
    catch (Exception e) {
      throw new ServiceLocatorException(e.getMessage());
    }
  }

  public static EJBHome getEjbHome(String ejbName, Class ejbClass) throws
      ServiceLocatorException {
    try {
      Object object = context.lookup(ejbName);
      EJBHome ejbHome = null;
      ejbHome = (EJBHome) PortableRemoteObject.narrow(object, ejbClass);
      if (ejbHome == null) {
        throw new ServiceLocatorException("Could not get home for " + ejbName);
      }
      return ejbHome;
    }
    catch (NamingException ne) {
      throw new ServiceLocatorException(ne.getMessage());
    }
  }

  public static EJBLocalHome getEjbLocalHome(String ejbName) throws
      ServiceLocatorException {
    try {
      Context localContext = new InitialContext();
      Object object = localContext.lookup(ejbName);
      EJBLocalHome ejbLocalHome = null;
      ejbLocalHome = (EJBLocalHome) object;
      if (ejbLocalHome == null) {
        throw new ServiceLocatorException("Could not get local home for " +
                                          ejbName);
      }
      return ejbLocalHome;
    }
    catch (NamingException ne) {
      throw new ServiceLocatorException(ne.getMessage());
    }
  }

  public static synchronized ServiceLocator getInstance() throws
      ServiceLocatorException {
    if (serviceLocator == null) {
      serviceLocator = new ServiceLocator();
    }
    return serviceLocator;
  }

  public Context getInitialContext() throws Exception {
    String url = "t3://标:7001";
    String user = null;
    String password = null;
    Properties properties;
    try {
      properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY,
                     "weblogic.jndi.WLInitialContextFactory");
      properties.put(Context.PROVIDER_URL, url);
      if (user != null) {
        properties.put(Context.SECURITY_PRINCIPAL, user);
        properties.put(Context.SECURITY_CREDENTIALS,
                       password == null ? "" : password);
      }
      return new javax.naming.InitialContext(properties);
    }
    catch (Exception e) {
      System.out.println("Unable to connect to WebLogic server at " + url);
      System.out.println("Please make sure that the server is running.");
      throw e;
    }
  }
}

⌨️ 快捷键说明

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