1 17 18 package org.sape.carbon.services.ejb.local; 19 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.ejb.EJBLocalHome ; 25 import javax.naming.Context ; 26 import javax.naming.NamingException ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.sape.carbon.core.component.ComponentConfiguration; 32 import org.sape.carbon.core.component.lifecycle.Configurable; 33 import org.sape.carbon.core.config.InvalidConfigurationException; 34 35 import org.sape.carbon.services.ejb.EnterpriseBeanConfiguration; 36 import org.sape.carbon.services.ejb.HomeFactoryClassCastException; 37 import org.sape.carbon.services.ejb.HomeFactoryConfiguration; 38 import org.sape.carbon.services.ejb.HomeFactoryException; 39 import org.sape.carbon.services.ejb.HomeFactoryNamingException; 40 41 59 public class LocalHomeFactoryImpl implements Configurable, LocalHomeFactory { 60 61 64 private Log log = LogFactory.getLog(this.getClass()); 65 66 private HomeFactoryConfiguration configuration; 67 68 72 private Map ejbMap; 73 74 75 79 private Map homeCache = new HashMap (); 80 81 84 public EJBLocalHome lookup(String logicalName) throws HomeFactoryException { 85 86 Context context = null; 87 88 try { 89 context = this.configuration.getInitialContextFactory().getContext(); 90 91 return lookup(logicalName, context); 92 93 } catch (NamingException ne) { 94 throw new HomeFactoryNamingException(this.getClass(), 95 "javax.naming.NamingException occurred for EJB name = " 96 + "'" 97 + logicalName 98 + "'", ne); 99 100 } finally { 101 if (context != null) { 102 try { 103 context.close(); 104 } catch (NamingException ne) { 105 log.debug("Caught naming exception closing context: " + ne); 106 } 107 } 108 } 109 110 111 } 112 113 114 117 public EJBLocalHome lookup(String logicalName, 118 String principal, 119 String credentials) 120 throws HomeFactoryException { 121 122 Context context = null; 123 124 try { 125 Map securityInfo = new HashMap (); 126 securityInfo.put(Context.SECURITY_PRINCIPAL, principal); 127 securityInfo.put(Context.SECURITY_CREDENTIALS, credentials); 128 context = this.configuration.getInitialContextFactory(). 129 getContext(securityInfo); 130 131 return lookup(logicalName, context); 132 133 } catch (NamingException ne) { 134 throw new HomeFactoryNamingException(this.getClass(), 135 "javax.naming.NamingException occurred for EJB name = " 136 + "'" 137 + logicalName 138 + "'", ne); 139 140 } finally { 141 if (context != null) { 142 try { 143 context.close(); 144 } catch (NamingException ne) { 145 log.debug("Caught naming exception closing context: " + ne); 146 } 147 } 148 } 149 150 151 } 152 153 154 157 public EJBLocalHome lookup(String logicalName, Context context) 158 throws HomeFactoryException { 159 160 if (log.isTraceEnabled()) { 161 log.trace("Performing lookup for EJB with logical name: " 162 + logicalName); 163 } 164 165 EJBLocalHome ejbHome = null; 167 168 EnterpriseBeanConfiguration ejbDetails = 169 (EnterpriseBeanConfiguration) this.ejbMap.get(logicalName); 170 171 if (ejbDetails == null) { 172 throw new HomeFactoryNamingException(this.getClass(), 173 "Unable to retrieve configuration details for EJB name = '" 174 + logicalName 175 + "'"); 176 } 177 178 if (ejbDetails.isCacheable()) { 179 180 if (log.isTraceEnabled()) { 181 log.trace("Checking home cache for EJB: " 182 + logicalName); 183 } 184 185 189 ejbHome = (EJBLocalHome ) homeCache.get(logicalName); 190 } 191 192 if (ejbHome == null) { 193 194 Object homeObject = null; 196 197 try { 198 homeObject = context.lookup(logicalName); 199 } catch (NamingException ne) { 200 throw new HomeFactoryNamingException(this.getClass(), 201 "javax.naming.NamingException occurred for EJB name = " 202 + "'" 203 + logicalName 204 + "'", ne); 205 } 206 207 209 try { 211 ejbHome = (EJBLocalHome ) homeObject; 212 } catch (ClassCastException cce) { 213 throw new HomeFactoryClassCastException(this.getClass(), 214 "java.lang.ClassCastException " 215 + "occurred while attempting to downcast the EJB home " 216 + "reference - logical name = '" 217 + logicalName 218 + "'", cce); 219 } 220 221 225 if (ejbDetails.isCacheable()) { 226 227 if (log.isTraceEnabled()) { 228 log.trace("Adding EJB: " + logicalName 229 + " to the home interface cache"); 230 } 231 cacheHome(logicalName, ejbHome); 232 } 233 } 234 235 return ejbHome; 237 } 238 239 240 246 public void configure(ComponentConfiguration configuration) { 247 248 try { 249 this.configuration = (HomeFactoryConfiguration) configuration; 250 } catch (ClassCastException cce) { 251 throw new InvalidConfigurationException( 252 this.getClass(), 253 configuration.getConfigurationName(), 254 "ConfigurationInterface", 255 "Configuration interface must be of type " + 256 HomeFactoryConfiguration.class.getName()); 257 } 258 259 263 EnterpriseBeanConfiguration ejbs[]; 264 265 ejbs = this.configuration.getEnterpriseBean(); 266 267 this.ejbMap = new HashMap (); 268 269 for (int i = 0; i < ejbs.length; i++) { 270 this.ejbMap.put(ejbs[i].getLogicalName(), ejbs[i]); 271 } 272 } 273 274 275 278 public EnterpriseBeanConfiguration getEJBDetails(String logicalName) { 279 return (EnterpriseBeanConfiguration) 280 this.ejbMap.get(logicalName); 281 } 282 283 284 292 protected void cacheHome(String logicalName, EJBLocalHome ejbHome) { 293 this.homeCache.put(logicalName, ejbHome); 294 } 295 } 296 | Popular Tags |