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

📄 servicelocator.java

📁 EJB+Struts+Webservice实现的开放式基金管理系统
💻 JAVA
字号:
package com.fund;

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;

/**
 *
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ServiceLocator {
        /**
         * serviceLocator
         */
        private static ServiceLocator serviceLocator;
        /**
     * context
     */
    private static Context context;
    /**
     *
     * @throws ServiceLocatorException e
     */
    private ServiceLocator() throws ServiceLocatorException {
    try {
            context = getInitialContext();
        } catch (Exception e) {
            throw new ServiceLocatorException(e.getMessage());
        }
    }

    /**
     *
     * @param ejbName String
     * @param ejbClass Class
     * @return EJBHome
     * @throws ServiceLocatorException e
     */
    public 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());
        }
    }

    /**
     *
     * @param ejbName String
     * @return EJBLocalHome
     * @throws ServiceLocatorException e
     */
    public 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());
        }
    }

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

    /**
     *
     * @return Context
     * @throws Exception e
     */
    public Context getInitialContext() throws Exception {
    String url = "t3://127.0.0.1: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 + -