1 17 18 package org.sape.carbon.services.ejb.remote; 19 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.ejb.EJBHome ; 25 import javax.naming.Context ; 26 import javax.naming.NamingException ; 27 import javax.rmi.PortableRemoteObject ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import org.sape.carbon.core.component.ComponentConfiguration; 33 import org.sape.carbon.core.component.lifecycle.Configurable; 34 import org.sape.carbon.core.config.InvalidConfigurationException; 35 36 import org.sape.carbon.services.ejb.EnterpriseBeanConfiguration; 37 import org.sape.carbon.services.ejb.HomeFactoryClassCastException; 38 import org.sape.carbon.services.ejb.HomeFactoryClassNotFoundException; 39 import org.sape.carbon.services.ejb.HomeFactoryException; 40 import org.sape.carbon.services.ejb.HomeFactoryNamingException; 41 42 61 public class RemoteHomeFactoryImpl implements Configurable, RemoteHomeFactory { 62 63 66 private Log log = LogFactory.getLog(this.getClass()); 67 68 71 private static final String EMPTY_STRING = ""; 72 73 private RemoteHomeFactoryConfiguration configuration; 74 75 79 private Map ejbMap; 80 81 82 86 private Map homeCache = new HashMap (); 87 88 89 92 public EJBHome lookup(String logicalName) throws HomeFactoryException { 93 94 Context context = null; 95 96 try { 97 context = this.configuration.getInitialContextFactory().getContext(); 98 99 return lookup(logicalName, context); 100 101 } catch (NamingException ne) { 102 throw new HomeFactoryNamingException(this.getClass(), 103 "javax.naming.NamingException occurred for EJB name = " 104 + "'" 105 + logicalName 106 + "'", ne); 107 108 } finally { 109 if (context != null) { 110 try { 111 context.close(); 112 } catch (NamingException ne) { 113 log.debug("Caught naming exception closing context: " + ne); 114 } 115 } 116 } 117 118 119 } 120 121 122 125 public EJBHome lookup(String logicalName, 126 String principal, 127 String credentials) 128 throws HomeFactoryException { 129 130 Context context = null; 131 132 try { 133 Map securityInfo = new HashMap (); 134 securityInfo.put(Context.SECURITY_PRINCIPAL, principal); 135 securityInfo.put(Context.SECURITY_CREDENTIALS, credentials); 136 context = this.configuration.getInitialContextFactory(). 137 getContext(securityInfo); 138 139 return lookup(logicalName, context); 140 141 } catch (NamingException ne) { 142 throw new HomeFactoryNamingException(this.getClass(), 143 "javax.naming.NamingException occurred for EJB name = " 144 + "'" 145 + logicalName 146 + "'", ne); 147 148 } finally { 149 if (context != null) { 150 try { 151 context.close(); 152 } catch (NamingException ne) { 153 log.debug("Caught naming exception closing context: " + ne); 154 } 155 } 156 } 157 } 158 159 160 163 public EJBHome lookup(String logicalName, Context context) 164 throws HomeFactoryException { 165 166 if (log.isTraceEnabled()) { 167 log.trace("Performing lookup for EJB with logical name: " 168 + logicalName); 169 } 170 171 EJBHome ejbHome = null; 173 174 EnterpriseBeanConfiguration ejbDetails = 175 (EnterpriseBeanConfiguration) this.ejbMap.get(logicalName); 176 177 if (ejbDetails == null) { 178 throw new HomeFactoryNamingException(this.getClass(), 179 "Unable to retrieve configuration details for EJB name = '" 180 + logicalName 181 + "'"); 182 } 183 184 if (ejbDetails.isCacheable()) { 185 186 if (log.isTraceEnabled()) { 187 log.trace("Checking home cache for EJB: " 188 + logicalName); 189 } 190 191 195 ejbHome = (EJBHome ) homeCache.get(logicalName); 196 } 197 198 if (ejbHome == null) { 199 200 Object homeObject = null; 202 203 try { 204 homeObject = context.lookup(logicalName); 205 } catch (NamingException ne) { 206 throw new HomeFactoryNamingException(this.getClass(), 207 "javax.naming.NamingException occurred for EJB name = '" 208 + logicalName 209 + "'", ne); 210 } 211 212 if (this.configuration.isRmiIiop()) { 214 215 220 221 Class homeClass; 222 223 String homeInterfaceClassName = ejbDetails.getHomeInterface(); 224 225 try { 226 228 homeClass = Class.forName(homeInterfaceClassName); 230 231 ejbHome = (EJBHome ) PortableRemoteObject.narrow( 233 homeObject, homeClass); 234 } catch (ClassNotFoundException cnfe) { 235 throw new HomeFactoryClassNotFoundException(this.getClass(), 236 "Unable to obtain class: for name = '" 237 + homeInterfaceClassName 238 + "'", cnfe); 239 } catch (ClassCastException cce) { 240 throw new HomeFactoryClassCastException(this.getClass(), 241 "java.lang.ClassCastException " 242 + "occurred while attempting to narrow the EJB home " 243 + "reference - logical name = '" 244 + logicalName 245 + "'", cce); 246 } 247 248 } else { 249 250 try { 252 ejbHome = (EJBHome ) homeObject; 253 } catch (ClassCastException cce) { 254 throw new HomeFactoryClassCastException(this.getClass(), 255 "java.lang.ClassCastException " 256 + " occurred while attempting to downcast the EJB home " 257 + "reference - logical name = '" 258 + logicalName 259 + "'", cce); 260 } 261 } 262 263 267 if (ejbDetails.isCacheable()) { 268 269 if (log.isTraceEnabled()) { 270 log.trace("Adding EJB: " 271 + logicalName 272 + " to the home interface cache"); 273 } 274 275 cacheHome(logicalName, ejbHome); 276 } 277 } 278 279 return ejbHome; 281 } 282 283 284 289 public void configure(ComponentConfiguration configuration) { 290 291 try { 292 this.configuration = (RemoteHomeFactoryConfiguration) configuration; 293 } catch (ClassCastException cce) { 294 throw new InvalidConfigurationException( 295 this.getClass(), 296 configuration.getConfigurationName(), 297 "ConfigurationInterface", 298 "Configuration interface must be of type " + 299 RemoteHomeFactoryConfiguration.class.getName()); 300 } 301 302 EnterpriseBeanConfiguration ejbs[]; 303 304 ejbs = (EnterpriseBeanConfiguration[]) 305 this.configuration.getEnterpriseBean(); 306 307 this.ejbMap = new HashMap (ejbs.length); 308 309 313 for (int i = 0; i < ejbs.length; i++) { 314 315 319 if (this.configuration.isRmiIiop() == true 320 && (ejbs[i].getHomeInterface().equals(EMPTY_STRING) 321 || ejbs[i].getHomeInterface() == null)) { 322 323 throw new InvalidConfigurationException(this.getClass(), 324 configuration.getConfigurationName(), 325 "Home interface for EJB " 326 + ejbs[i] 327 + "must be specified if RMI-IIOP is enabled"); 328 } 329 330 this.ejbMap.put(ejbs[i].getLogicalName(), ejbs[i]); 331 } 332 } 333 334 335 338 public EnterpriseBeanConfiguration getEJBDetails(String logicalName) { 339 return (EnterpriseBeanConfiguration) 340 this.ejbMap.get(logicalName); 341 } 342 343 344 352 protected void cacheHome(String logicalName, EJBHome ejbHome) { 353 this.homeCache.put(logicalName, ejbHome); 354 } 355 } 356 | Popular Tags |