| 1 17 18 package org.sape.carbon.services.ejb.local.test; 19 20 21 import java.rmi.RemoteException ; 22 23 import javax.ejb.CreateException ; 24 import javax.ejb.EJBLocalHome ; 25 import javax.ejb.SessionBean ; 26 import javax.ejb.SessionContext ; 27 import javax.naming.Context ; 28 import javax.naming.NamingException ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 import org.sape.carbon.core.component.Lookup; 34 import org.sape.carbon.core.config.Config; 35 36 import org.sape.carbon.services.ejb.EnterpriseBeanConfiguration; 37 import org.sape.carbon.services.ejb.HomeFactoryException; 38 import org.sape.carbon.services.ejb.local.LocalHomeFactory; 39 import org.sape.carbon.services.jndi.InitialContextFactory; 40 import org.sape.carbon.services.jndi.InitialContextFactoryConfiguration; 41 42 51 public class LocalGatewayBean implements SessionBean { 52 53 56 public static final String TEST_INITIAL_CONTEXT_FACTORY = 57 "/ejb/test/TestInitialContextFactory"; 58 59 62 private Log log = LogFactory.getLog(this.getClass()); 63 64 67 protected SessionContext ctx; 68 69 private LocalHomeFactory homeFactory; 70 71 public void ejbCreate() throws CreateException { 72 73 this.homeFactory = (LocalHomeFactory) 74 Lookup.getInstance().fetchComponent( 75 LocalHomeFactoryTest.TEST_LOCAL_HOME_FACTORY); 76 } 77 78 public void ejbActivate() { 79 80 } 81 82 public void ejbPassivate() { 83 84 } 85 86 public void ejbRemove() { 87 88 } 89 90 91 96 public SessionContext getSessionContext() { 97 return ctx; 98 } 99 100 101 107 public void setSessionContext(SessionContext ctx) { 108 this.ctx = ctx; 109 } 110 111 112 118 public String id() { 119 return (this.getClass().getName()); 120 } 121 122 123 126 public void testLocalHomeFactoryNamingException() 127 throws HomeFactoryException, RemoteException { 128 129 EJBLocalHome ejbHome = null; 130 131 ejbHome = this.homeFactory.lookup( 133 LocalHomeFactoryTest.TEST_NONEXISTANT_EJB); 134 } 135 136 137 140 public void testLocalHomeFactoryLookup() 141 throws HomeFactoryException, RemoteException { 142 143 EJBLocalHome ejbHome = null; 144 145 ejbHome = this.homeFactory.lookup(LocalHomeFactoryTest.TEST_LOCAL_EJB); 146 } 147 148 149 152 public void testLocalHomeFactoryLookupWithContext() 153 throws HomeFactoryException, NamingException , RemoteException { 154 155 EJBLocalHome ejbHome = null; 156 157 InitialContextFactory factory = (InitialContextFactory) 158 Lookup.getInstance().fetchComponent(TEST_INITIAL_CONTEXT_FACTORY); 159 Context context = factory.getContext(); 160 161 ejbHome = this.homeFactory.lookup( 162 LocalHomeFactoryTest.TEST_LOCAL_EJB, context); 163 } 164 165 166 169 public void testLocalHomeFactoryLookupWithCredentials() 170 throws HomeFactoryException, RemoteException { 171 172 InitialContextFactoryConfiguration factoryConfig = 173 (InitialContextFactoryConfiguration) 174 Config.getInstance().fetchConfiguration(TEST_INITIAL_CONTEXT_FACTORY); 175 176 EJBLocalHome ejbHome = null; 177 178 ejbHome = this.homeFactory.lookup(LocalHomeFactoryTest.TEST_LOCAL_EJB, 179 factoryConfig.getEnvironment(Context.SECURITY_PRINCIPAL), 180 factoryConfig.getEnvironment(Context.SECURITY_CREDENTIALS)); 181 } 182 183 184 187 public void testLocalHomeCachePerformance() 188 throws HomeFactoryException, RemoteException { 189 190 long startTime = 0; 191 long elapsedTime = 0; 192 193 EJBLocalHome ejbHome = null; 194 195 if (log.isInfoEnabled()) { 196 log.info("Testing EJB home cache performance"); 197 198 log.info("Performing " 200 + LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS 201 + " home interface lookups for EJB: " 202 + LocalHomeFactoryTest.TEST_LOCAL_EJB 203 + " without using cache"); 204 } 205 206 startTime = System.currentTimeMillis(); 208 209 for (long i = 0; 211 i < LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS; i++) { 212 213 ejbHome = this.homeFactory.lookup( 214 LocalHomeFactoryTest.TEST_LOCAL_EJB); 215 } 216 217 elapsedTime = System.currentTimeMillis() - startTime; 219 220 if (log.isInfoEnabled()) { 221 log.info("Total elapsed time: " 222 + elapsedTime); 223 log.info("Lookups/Second: " 224 + ((double) 225 LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS 226 / elapsedTime * 1000) 227 + " lps"); 228 } 229 230 EnterpriseBeanConfiguration ejbDetails = 232 homeFactory.getEJBDetails(LocalHomeFactoryTest.TEST_LOCAL_EJB); 233 234 if (ejbDetails != null) { 235 ejbDetails.setCacheable(true); 236 } else { 237 throw new RuntimeException ( 238 "Test of EJB home cache performance failed; " 239 + "unable to retrieve EJB details for logical name: " 240 + LocalHomeFactoryTest.TEST_LOCAL_EJB); 241 } 242 243 if (log.isInfoEnabled()) { 244 log.info("Performing " 245 + LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS 246 + " home interface lookups for EJB: " 247 + LocalHomeFactoryTest.TEST_LOCAL_EJB 248 + " using cache"); 249 } 250 251 startTime = System.currentTimeMillis(); 253 254 for (long i = 0; 255 i < LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS; i++) { 256 257 ejbHome = this.homeFactory.lookup( 258 LocalHomeFactoryTest.TEST_LOCAL_EJB); 259 } 260 261 elapsedTime = System.currentTimeMillis() - startTime; 263 264 if (log.isInfoEnabled()) { 265 log.info("Total elapsed time: " 266 + elapsedTime); 267 log.info("Lookups/Second: " 268 + ((double) 269 LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS 270 / elapsedTime * 1000) 271 + " lps"); 272 } 273 } 274 } 275 | Popular Tags |