SSISO Community

시소당

EJB에서 사용도가 높은 ServiceLocator 1

EJB에서 사용도가 높은 ServiceLocator 1

 

ServiceLocator는 싱글톤 디자인 패턴을 이용하여 계속 반복되는 객체를 쉽게 찾는다.

 

또한 여러 객체를 만들지 않고 한번만 만들도록 해준다.

 

 Context localContext = new InitialContext();

 Object object = context.lookup(ejbName);
 XXXHome home = (XXXHome) PortableRemoteObject.narrow(object, XXXHome.class);

 

 EJB 빈 하나를 만들면 위 소스를 반복해야한다.

 

 항상반복되는 것을 숨기면서 이름만 넣고 쉽게 Home을 얻으려면 서비스 로케이터를 사용하라.

 

package stucmp3;

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://localhost: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;
        }
    }
}
//------------------

 

package stucmp3;

public class ServiceLocatorException extends Exception {
    public ServiceLocatorException(String message) {
        super(message);
    }
}

 

//----사용방법

private void findStudentHome() throws EJBException {
        final String ENTITY_NAME = "java:comp/env/ejb/StudentCMP3";
        if (studentHome == null) {
            try {
                ServiceLocator locator = ServiceLocator.getInstance();
                studentHome = (StudentHome) locator.getEjbLocalHome(ENTITY_NAME);
            } catch (ServiceLocatorException e) {
                throw new EJBException(e.getMessage());
            }
        }
    }

 

//-------------------사용방법

 

private void findStudentHome() throws EJBException {
        final String SESSION_NAME = "StudentSessionFacade3";

        if (home == null) {
            try {
                ServiceLocator locator = ServiceLocator.getInstance();
                home = (StudentSessionFacadeHome) locator.getEjbHome(
                        SESSION_NAME,StudentSessionFacadeHome.class);
                studentSessionFacade=home.create();
            } catch (ServiceLocatorException e) {
                throw new EJBException(e.getMessage());
            } catch (RemoteException ex) {
                 throw new EJBException(ex.getMessage());
            } catch (CreateException ex) {
                 throw new EJBException(ex.getMessage());
            }

        }
    }

2612 view

4.0 stars