Synchronized로 검색한 결과 :: 시소커뮤니티[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

회원가입 I 비밀번호 찾기


SSISO Community검색
SSISO Community메뉴
[카페목록보기]
[블로그등록하기]  
[블로그리스트]  
SSISO Community카페
블로그 카테고리
정치 경제
문화 칼럼
비디오게임 스포츠
핫이슈 TV
포토 온라인게임
PC게임 에뮬게임
라이프 사람들
유머 만화애니
방송 1
1 1
1 1
1 1
1 1
1

Synchronized로 검색한 결과
등록일:2008-06-09 10:37:18
작성자:
제목: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());
            }

        }
    }