Where The Streets Have No Name

WebLogic8.1 에서 EJB lookup 본문

Developement/Java

WebLogic8.1 에서 EJB lookup

highheat 2006. 4. 27. 20:02

ServiceLocator 를 사용함.

기존에 사용하던...

public InitialContext getInitialContext()  throws ServiceLocatorException //   throws NamingException
{
   try {

       if (ic == null) {
           ic = new InitialContext();
       }
   } catch (Exception e) {
      System.out.println(">>> Exception ServiceLocator.getInitialContext [" + e.toString() + "]");
      throw new ServiceLocatorException("can't get InitialContext because of " + e.toString());
   }
   return ic;
}

 


public EJBHome getRemoteHome(String jndiHomeName) throws ServiceLocatorException {
   EJBHome home = null;
   try {
     if (cache.containsKey(jndiHomeName)) {
         home = (EJBHome) cache.get(jndiHomeName);
     } else {
         Object objref = getInitialContext().lookup(jndiHomeName);
     Class clazz = objref.getClass();
         home = (EJBHome)PortableRemoteObject.narrow(objref, clazz);
         cache.put(jndiHomeName, home);
     }
   } catch(NamingException ne) {
    System.out.println(">>>> ServiceLocator NamingException!!!!!!!!");
    System.out.println("can't get RemoteHome interface because of "+ne.toString());
    throw new ServiceLocatorException("can't get RemoteHome interface because of "+ne.toString());
   } catch (Exception e) {
    System.out.println(">>>>>>>>> ServiceLocator Exception!!!!! ");
    System.out.println("can't get RemoteHome interface because of "+e.toString());      
         throw new ServiceLocatorException("can't get RemoteHome interface because of "+e.toString());
   }
  
   return home;
}

아래처럼 사용했는데 에러발생!!!!

CodeManagerHome home = (CodeManagerHome)
                ServiceLocator.getInstance().getRemoteHome("CodeManager");
        return home.create();


여기서 ClassCastException 이 발생.

 

다음처럼 수정하니 되네... 화나는군.

 

/*
* Created on 2005. 7. 1
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package myproject.common.util;

 

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

 

/**
* @author 

 *
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ServiceLocator {

    private InitialContext ic;
private Map cache; //used to hold references to EJBHomes/JMS Resources for re-use

    private static ServiceLocator me;

    static {
 try {
  me = new ServiceLocator();
 } catch(Exception se) {
  System.err.println(se);
         se.printStackTrace(System.err);
 }
}

    private ServiceLocator() {
 try {
         cache = Collections.synchronizedMap(new HashMap());
     } catch (Exception e) {
      System.err.println(">>>> Exception ServiceLocator " + e.toString());
     }
}

    public InitialContext getInitialContext()
   throws ServiceLocatorException //   throws NamingException
{
   try {

  if (ic == null) {
   String localurl = "t3://127.0.0.1:7001";
   Properties properties = new Properties();
  
   properties.put(InitialContext.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
   properties.put(InitialContext.PROVIDER_URL, localurl);
   ic = new InitialContext(properties);
}
} catch (Exception e) {
 System.out.println(">>> Exception ServiceLocator.getInitialContext [" + e.toString() + "]");
throw new ServiceLocatorException("can't get InitialContext because of " + e.toString());
   }
   return ic;
}

    static public ServiceLocator getInstance() {
   return me;
}

/**
  * will get the ejb Local home factory. If this ejb home factory has already been
  * clients need to cast to the type of EJBHome they desire
  *
  * @return the EJB Home corresponding to the homeName
  */
public EJBLocalHome getLocalHome(String jndiHomeName) throws ServiceLocatorException {

      EJBLocalHome home = null;
   try {
     if (cache.containsKey(jndiHomeName)) {
         home = (EJBLocalHome) cache.get(jndiHomeName);
     } else {
         home = (EJBLocalHome) (new InitialContext()).lookup(jndiHomeName);
         cache.put(jndiHomeName, home);
     }
    } catch (NamingException ne) {
         throw new ServiceLocatorException(ne.toString());
    } catch (Exception e) {
         throw new ServiceLocatorException(e.toString());
    }
    return home;
}

    /**
  * lookup(name, null);
  * @return EJBObject remote interface.
  * @param name The string representation of the Bean object.
  * @param args the array of arguments for calling EJBHome.create( args ).
  *               if args is null, it means that just invoke create().
  */
public EJBHome getRemoteHome(String jndiHomeName) throws ServiceLocatorException {
   EJBHome home = null;
   try {
     if (cache.containsKey(jndiHomeName)) {
         home = (EJBHome) cache.get(jndiHomeName);
     } else {
         Object objref = new InitialContext().lookup(jndiHomeName);
      Class clazz = objref.getClass();
         home = (EJBHome)PortableRemoteObject.narrow(objref, clazz);
         cache.put(jndiHomeName, home);
     }
   } catch(NamingException ne) {
       System.out.println(">>>> ServiceLocator NamingException!!!!!!!!");
       System.out.println("can't get RemoteHome interface because of "+ne.toString());
       throw new ServiceLocatorException("can't get RemoteHome interface because of "+ne.toString());
   } catch (Exception e) {
      System.out.println(">>>>>>>>> ServiceLocator Exception!!!!! ");
      System.out.println("can't get RemoteHome interface because of "+e.toString());         
         throw new ServiceLocatorException("can't get RemoteHome interface because of "+e.toString());
   }
  
   return home;
}
}

 

이렇게 해주어야 함.