001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.ejb.local.test;
019:
020: import java.rmi.RemoteException;
021:
022: import javax.ejb.CreateException;
023: import javax.ejb.EJBLocalHome;
024: import javax.ejb.SessionBean;
025: import javax.ejb.SessionContext;
026: import javax.naming.Context;
027: import javax.naming.NamingException;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import org.sape.carbon.core.component.Lookup;
033: import org.sape.carbon.core.config.Config;
034:
035: import org.sape.carbon.services.ejb.EnterpriseBeanConfiguration;
036: import org.sape.carbon.services.ejb.HomeFactoryException;
037: import org.sape.carbon.services.ejb.local.LocalHomeFactory;
038: import org.sape.carbon.services.jndi.InitialContextFactory;
039: import org.sape.carbon.services.jndi.InitialContextFactoryConfiguration;
040:
041: /**
042: * <p>Bean implementation for the LocalGatewayBean
043: *
044: * Copyright 2002 Sapient
045: * @see LocalGateway
046: * @see LocalGatewayHome
047: * @version $Revision: 1.10 $
048: * @author $Author: dvoet $ $Date: 2003/11/05 17:45:16 $
049: */
050: public class LocalGatewayBean implements SessionBean {
051:
052: /**
053: * Path of the remote home factory test component
054: */
055: public static final String TEST_INITIAL_CONTEXT_FACTORY = "/ejb/test/TestInitialContextFactory";
056:
057: /**
058: * Provides a handle to Apache-commons logger
059: */
060: private Log log = LogFactory.getLog(this .getClass());
061:
062: /**
063: * Contains the SessionContext attribute for this instance
064: */
065: protected SessionContext ctx;
066:
067: private LocalHomeFactory homeFactory;
068:
069: public void ejbCreate() throws CreateException {
070:
071: this .homeFactory = (LocalHomeFactory) Lookup.getInstance()
072: .fetchComponent(
073: LocalHomeFactoryTest.TEST_LOCAL_HOME_FACTORY);
074: }
075:
076: public void ejbActivate() {
077:
078: }
079:
080: public void ejbPassivate() {
081:
082: }
083:
084: public void ejbRemove() {
085:
086: }
087:
088: /**
089: * Returns the SessionContext associated with this instance.
090: *
091: * @return javax.ejb.SessionContext
092: */
093: public SessionContext getSessionContext() {
094: return ctx;
095: }
096:
097: /**
098: * Assigns the SessionContext of the current instance to the provided
099: * parameter.
100: *
101: * @param ctx the SessionContext to assign to this instance.
102: */
103: public void setSessionContext(SessionContext ctx) {
104: this .ctx = ctx;
105: }
106:
107: /**
108: * Returns the name of the class of this object, used for debugging
109: * purposes.
110: *
111: * @return String Identification
112: */
113: public String id() {
114: return (this .getClass().getName());
115: }
116:
117: /**
118: * @see Tester
119: */
120: public void testLocalHomeFactoryNamingException()
121: throws HomeFactoryException, RemoteException {
122:
123: EJBLocalHome ejbHome = null;
124:
125: // Perform the test using the LocalHomeFactory
126: ejbHome = this .homeFactory
127: .lookup(LocalHomeFactoryTest.TEST_NONEXISTANT_EJB);
128: }
129:
130: /**
131: * @see Tester
132: */
133: public void testLocalHomeFactoryLookup()
134: throws HomeFactoryException, RemoteException {
135:
136: EJBLocalHome ejbHome = null;
137:
138: ejbHome = this .homeFactory
139: .lookup(LocalHomeFactoryTest.TEST_LOCAL_EJB);
140: }
141:
142: /**
143: * @see Tester
144: */
145: public void testLocalHomeFactoryLookupWithContext()
146: throws HomeFactoryException, NamingException,
147: RemoteException {
148:
149: EJBLocalHome ejbHome = null;
150:
151: InitialContextFactory factory = (InitialContextFactory) Lookup
152: .getInstance().fetchComponent(
153: TEST_INITIAL_CONTEXT_FACTORY);
154: Context context = factory.getContext();
155:
156: ejbHome = this .homeFactory.lookup(
157: LocalHomeFactoryTest.TEST_LOCAL_EJB, context);
158: }
159:
160: /**
161: * @see Tester
162: */
163: public void testLocalHomeFactoryLookupWithCredentials()
164: throws HomeFactoryException, RemoteException {
165:
166: InitialContextFactoryConfiguration factoryConfig = (InitialContextFactoryConfiguration) Config
167: .getInstance().fetchConfiguration(
168: TEST_INITIAL_CONTEXT_FACTORY);
169:
170: EJBLocalHome ejbHome = null;
171:
172: ejbHome = this .homeFactory.lookup(
173: LocalHomeFactoryTest.TEST_LOCAL_EJB, factoryConfig
174: .getEnvironment(Context.SECURITY_PRINCIPAL),
175: factoryConfig
176: .getEnvironment(Context.SECURITY_CREDENTIALS));
177: }
178:
179: /**
180: * @see Tester
181: */
182: public void testLocalHomeCachePerformance()
183: throws HomeFactoryException, RemoteException {
184:
185: long startTime = 0;
186: long elapsedTime = 0;
187:
188: EJBLocalHome ejbHome = null;
189:
190: if (log.isInfoEnabled()) {
191: log.info("Testing EJB home cache performance");
192:
193: // Measure non-cached performance
194: log
195: .info("Performing "
196: + LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS
197: + " home interface lookups for EJB: "
198: + LocalHomeFactoryTest.TEST_LOCAL_EJB
199: + " without using cache");
200: }
201:
202: // Set the start time
203: startTime = System.currentTimeMillis();
204:
205: // Loop
206: for (long i = 0; i < LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS; i++) {
207:
208: ejbHome = this .homeFactory
209: .lookup(LocalHomeFactoryTest.TEST_LOCAL_EJB);
210: }
211:
212: // Print the elapsed time
213: elapsedTime = System.currentTimeMillis() - startTime;
214:
215: if (log.isInfoEnabled()) {
216: log.info("Total elapsed time: " + elapsedTime);
217: log
218: .info("Lookups/Second: "
219: + ((double) LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS
220: / elapsedTime * 1000) + " lps");
221: }
222:
223: // Measure cached lookup performance
224: EnterpriseBeanConfiguration ejbDetails = homeFactory
225: .getEJBDetails(LocalHomeFactoryTest.TEST_LOCAL_EJB);
226:
227: if (ejbDetails != null) {
228: ejbDetails.setCacheable(true);
229: } else {
230: throw new RuntimeException(
231: "Test of EJB home cache performance failed; "
232: + "unable to retrieve EJB details for logical name: "
233: + LocalHomeFactoryTest.TEST_LOCAL_EJB);
234: }
235:
236: if (log.isInfoEnabled()) {
237: log
238: .info("Performing "
239: + LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS
240: + " home interface lookups for EJB: "
241: + LocalHomeFactoryTest.TEST_LOCAL_EJB
242: + " using cache");
243: }
244:
245: // Set the start time
246: startTime = System.currentTimeMillis();
247:
248: for (long i = 0; i < LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS; i++) {
249:
250: ejbHome = this .homeFactory
251: .lookup(LocalHomeFactoryTest.TEST_LOCAL_EJB);
252: }
253:
254: // Print the elapsed time
255: elapsedTime = System.currentTimeMillis() - startTime;
256:
257: if (log.isInfoEnabled()) {
258: log.info("Total elapsed time: " + elapsedTime);
259: log
260: .info("Lookups/Second: "
261: + ((double) LocalHomeCachePerformanceTest.TEST_LOOKUP_ITERATIONS
262: / elapsedTime * 1000) + " lps");
263: }
264: }
265: }
|