1 29 30 package hero.util; 31 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 import java.util.Map ; 36 import java.util.HashMap ; 37 38 import javax.rmi.PortableRemoteObject ; 39 import javax.mail.Session ; 40 41 public class BonitaServiceLocator { 42 private static BonitaServiceLocator msl; 43 private Map cache; 44 private static Context initial; 45 46 47 private BonitaServiceLocator() throws BonitaServiceException { 48 try { 49 initial=new InitialContext (); 50 cache = new HashMap (); 51 } catch (javax.naming.NamingException ne) { 52 throw new BonitaServiceException(ne.getMessage()); 53 } 54 } 55 56 static public BonitaServiceLocator getInstance() throws BonitaServiceException { 57 58 if (msl==null) { 59 msl=new BonitaServiceLocator(); 60 } 61 return msl; 62 } 63 64 66 public class Services { 67 final public static int TOPIC_CONNECTION_FACTORY=0; 68 final public static int TOPIC=1; 69 final public static int MAIL_SERVICE=2; 70 } 71 72 74 final static String TOPIC_CONNECTION_FACTORY_NAME = "TCF"; 75 final static String TOPIC_NAME = "testTopic"; 76 77 final static String MAIL_SERVICE_NAME = "Mail"; 79 80 82 83 static private String getServiceName(int service) { 84 switch (service) { 85 case Services.TOPIC_CONNECTION_FACTORY: return TOPIC_CONNECTION_FACTORY_NAME; 86 case Services.TOPIC: return TOPIC_NAME; 87 case Services.MAIL_SERVICE: return MAIL_SERVICE_NAME; 88 } 89 return null; 90 } 91 92 95 public Object getResource(int s) throws BonitaServiceException { 96 Object resource=null; 97 try { 98 String serv = getServiceName(s); 99 if (cache.containsKey(serv)) { 100 resource = (Object ) cache.get(serv); 101 } else { 102 Object objref = initial.lookup(serv); 103 resource=objref; 104 cache.put(serv, resource); 105 } 106 } catch (NamingException ex) { 107 throw new BonitaServiceException("ServiceException"); 108 } catch (Exception ex) { 109 System.out.println("ServiceLocator Exception"); 110 ex.printStackTrace(); 111 } 112 113 return resource; 114 } 115 116 public Object getMailSession(int s) throws BonitaServiceException { 117 Object mail=null; 118 try { 119 String serv = getServiceName(s); 120 if (cache.containsKey(serv)) { 121 mail = (Object ) cache.get(serv); 122 } else { 123 Object objref = initial.lookup(serv); 124 mail= PortableRemoteObject.narrow(objref,Session .class); 125 cache.put(serv, mail); 126 } 127 } catch (NamingException ex) { 128 throw new BonitaServiceException("ServiceException"); 129 } catch (Exception ex) { 130 System.out.println("ServiceLocator Exception"); 131 ex.printStackTrace(); 132 } 133 134 return mail; 135 } 136 137 } 138 | Popular Tags |