KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > ejb > local > test > LocalGatewayBean


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.ejb.local.test;
19
20
21 import java.rmi.RemoteException JavaDoc;
22
23 import javax.ejb.CreateException JavaDoc;
24 import javax.ejb.EJBLocalHome JavaDoc;
25 import javax.ejb.SessionBean JavaDoc;
26 import javax.ejb.SessionContext JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import javax.naming.NamingException JavaDoc;
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 /**
43  * <p>Bean implementation for the LocalGatewayBean
44  *
45  * Copyright 2002 Sapient
46  * @see LocalGateway
47  * @see LocalGatewayHome
48  * @version $Revision: 1.10 $
49  * @author $Author: dvoet $ $Date: 2003/11/05 17:45:16 $
50  */

51 public class LocalGatewayBean implements SessionBean JavaDoc {
52
53     /**
54      * Path of the remote home factory test component
55      */

56     public static final String JavaDoc TEST_INITIAL_CONTEXT_FACTORY =
57         "/ejb/test/TestInitialContextFactory";
58
59     /**
60      * Provides a handle to Apache-commons logger
61      */

62     private Log log = LogFactory.getLog(this.getClass());
63
64     /**
65      * Contains the SessionContext attribute for this instance
66      */

67     protected SessionContext JavaDoc ctx;
68
69     private LocalHomeFactory homeFactory;
70
71     public void ejbCreate() throws CreateException JavaDoc {
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     /**
92      * Returns the SessionContext associated with this instance.
93      *
94      * @return javax.ejb.SessionContext
95      */

96     public SessionContext JavaDoc getSessionContext() {
97         return ctx;
98     }
99
100
101     /**
102      * Assigns the SessionContext of the current instance to the provided
103      * parameter.
104      *
105      * @param ctx the SessionContext to assign to this instance.
106      */

107     public void setSessionContext(SessionContext JavaDoc ctx) {
108         this.ctx = ctx;
109     }
110
111
112     /**
113      * Returns the name of the class of this object, used for debugging
114      * purposes.
115      *
116      * @return String Identification
117      */

118     public String JavaDoc id() {
119         return (this.getClass().getName());
120     }
121
122
123     /**
124      * @see Tester
125      */

126     public void testLocalHomeFactoryNamingException()
127         throws HomeFactoryException, RemoteException JavaDoc {
128
129         EJBLocalHome JavaDoc ejbHome = null;
130
131         // Perform the test using the LocalHomeFactory
132
ejbHome = this.homeFactory.lookup(
133             LocalHomeFactoryTest.TEST_NONEXISTANT_EJB);
134     }
135
136
137     /**
138      * @see Tester
139      */

140     public void testLocalHomeFactoryLookup()
141         throws HomeFactoryException, RemoteException JavaDoc {
142
143         EJBLocalHome JavaDoc ejbHome = null;
144
145         ejbHome = this.homeFactory.lookup(LocalHomeFactoryTest.TEST_LOCAL_EJB);
146     }
147
148
149     /**
150      * @see Tester
151      */

152     public void testLocalHomeFactoryLookupWithContext()
153         throws HomeFactoryException, NamingException JavaDoc, RemoteException JavaDoc {
154
155         EJBLocalHome JavaDoc ejbHome = null;
156
157         InitialContextFactory factory = (InitialContextFactory)
158             Lookup.getInstance().fetchComponent(TEST_INITIAL_CONTEXT_FACTORY);
159         Context JavaDoc context = factory.getContext();
160
161         ejbHome = this.homeFactory.lookup(
162             LocalHomeFactoryTest.TEST_LOCAL_EJB, context);
163     }
164
165
166     /**
167      * @see Tester
168      */

169     public void testLocalHomeFactoryLookupWithCredentials()
170         throws HomeFactoryException, RemoteException JavaDoc {
171
172         InitialContextFactoryConfiguration factoryConfig =
173             (InitialContextFactoryConfiguration)
174             Config.getInstance().fetchConfiguration(TEST_INITIAL_CONTEXT_FACTORY);
175
176         EJBLocalHome JavaDoc 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     /**
185      * @see Tester
186      */

187     public void testLocalHomeCachePerformance()
188         throws HomeFactoryException, RemoteException JavaDoc {
189
190         long startTime = 0;
191         long elapsedTime = 0;
192
193         EJBLocalHome JavaDoc ejbHome = null;
194
195         if (log.isInfoEnabled()) {
196             log.info("Testing EJB home cache performance");
197
198             // Measure non-cached performance
199
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         // Set the start time
207
startTime = System.currentTimeMillis();
208
209         // Loop
210
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         // Print the elapsed time
218
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         // Measure cached lookup performance
231
EnterpriseBeanConfiguration ejbDetails =
232             homeFactory.getEJBDetails(LocalHomeFactoryTest.TEST_LOCAL_EJB);
233
234         if (ejbDetails != null) {
235             ejbDetails.setCacheable(true);
236         } else {
237             throw new RuntimeException JavaDoc(
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         // Set the start time
252
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         // Print the elapsed time
262
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