1 53 package org.bsf.remoting.rmi; 54 55 import org.apache.commons.logging.*; 56 import org.bsf.remoting.ServiceFactory; 57 import org.bsf.remoting.EJBDefinition; 58 59 import javax.ejb.EJBHome ; 60 import javax.naming.Context ; 61 import javax.naming.InitialContext ; 62 import javax.naming.NamingException ; 63 import javax.rmi.PortableRemoteObject ; 64 import java.lang.reflect.Method ; 65 import java.util.Hashtable ; 66 67 public class ServiceFactoryImpl implements ServiceFactory { 68 69 private static Log log = LogFactory.getLog(ServiceFactoryImpl.class); 70 71 private Hashtable services = new Hashtable (); 72 73 private static Context context = null; 74 75 public ServiceFactoryImpl() { 76 } 77 78 public Object getService(EJBDefinition service) { 79 if (service == null) throw new RuntimeException (); 80 Object result = services.get(service); 81 if (result == null) { 82 result = createService(service); 83 services.put(service, result); 84 } 85 return result; 86 } 87 88 private Object createService(EJBDefinition service) { 89 Object result = null; 90 try { 91 String p_sEJBName = service.getJndiName(); 92 Object objref = getContext().lookup(p_sEJBName); 93 EJBHome home = (EJBHome ) PortableRemoteObject.narrow( objref, service.getHomeClass() ); 94 Method create = service.getHomeClass().getDeclaredMethod("create", new Class [0]); 95 result = create.invoke(home, new Object [0]); 96 } catch (Exception e) { 97 log.fatal("Unable to lookup the service " + service.toString(), e); 98 throw new RuntimeException (e.getLocalizedMessage()); 99 } 100 return result; 101 } 102 103 private static Context getContext(){ 104 if (context == null){ 105 try { 106 context = new InitialContext (); 107 } catch (NamingException e) { 108 log.fatal(e.getLocalizedMessage()); 109 throw new RuntimeException (e.getLocalizedMessage()); 110 } 111 } 112 113 return context; 114 } 115 116 117 118 119 } | Popular Tags |