KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > enhancer > interceptors > business > bean > StatelessBean


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: StatelessBean.java 450 2006-05-12 15:30:25Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.tests.enhancer.interceptors.business.bean;
27
28 import static javax.ejb.TransactionAttributeType.NEVER JavaDoc;
29
30 import javax.ejb.Local JavaDoc;
31 import javax.ejb.Remote JavaDoc;
32 import javax.ejb.Stateless JavaDoc;
33 import javax.ejb.TransactionAttribute JavaDoc;
34 import javax.interceptor.AroundInvoke;
35 import javax.interceptor.ExcludeClassInterceptors;
36 import javax.interceptor.Interceptors;
37 import javax.interceptor.InvocationContext;
38
39 /**
40  * Simple class for testing interceptors.
41  * @author Florent Benoit
42  */

43 @Stateless JavaDoc(name = "HelloWorldbean")
44 @Remote JavaDoc(StatelessRemoteItf.class)
45 @Local JavaDoc({StatelessLocalItf.class, StatelessLocalBisItf.class})
46 @Interceptors({Interceptor.class})
47 @TransactionAttribute JavaDoc(NEVER)
48 public class StatelessBean implements StatelessRemoteItf, StatelessLocalItf, StatelessLocalBisItf {
49
50     /**
51      * Counter of intercepted methods.
52      */

53     private int counter;
54
55
56     /**
57      * Counter of intercepted methods by an external interceptor.
58      */

59     private int otherInterceptorCounter = 0;
60
61
62     /**
63      * Counter of intercepted calls on singleMethodIntercepted.
64      */

65     private int singleMethodInterceptedCounter = 0;
66
67
68
69     // =====================
70
// boolean
71
// =====================
72

73     /**
74      * Test method on boolean.
75      * @param b value to return
76      * @return given value
77      */

78     public boolean getBoolean(final boolean b) {
79         return b;
80     }
81
82
83     /**
84      * Test method on boolean.
85      * @param booleans array to return
86      * @return given value
87      */

88     public boolean[] getBooleans(final boolean[] booleans) {
89         return booleans;
90     }
91
92
93
94     // =====================
95
// byte
96
// =====================
97

98     /**
99      * Test method on byte.
100      * @param i value to return
101      * @return given value
102      */

103     public byte getByte(final byte i) {
104         return i;
105     }
106
107
108     /**
109      * Test method on byte.
110      * @param bytes array to return
111      * @return given value
112      */

113     public byte[] getBytes(final byte[] bytes) {
114         return bytes;
115     }
116
117     // =====================
118
// char
119
// =====================
120

121
122     /**
123      * Test method on char.
124      * @param c value to return
125      * @return given value
126      */

127     public char getChar(final char c) {
128         return c;
129     }
130
131
132     /**
133      * Test method on char.
134      * @param chars array to return
135      * @return given value
136      */

137     public char[] getChars(final char[] chars) {
138         return chars;
139     }
140
141     // =====================
142
// double
143
// =====================
144

145
146     /**
147      * Test method on double.
148      * @param d value to return
149      * @return given value
150      */

151     public double getDouble(final double d) {
152         return d;
153     }
154
155
156     /**
157      * Test method on double.
158      * @param doubles array to return
159      * @return given value
160      */

161     public double[] getDoubles(final double[] doubles) {
162         return doubles;
163     }
164
165
166     // =====================
167
// float
168
// =====================
169

170
171     /**
172      * Test method on float.
173      * @param f value to return
174      * @return given value
175      */

176     public float getFloat(final float f) {
177         return f;
178     }
179
180
181     /**
182      * Test method on float.
183      * @param floats array to return
184      * @return given value
185      */

186     public float[] getFloats(final float[] floats) {
187         return floats;
188     }
189
190
191
192     // =====================
193
// int
194
// =====================
195

196
197
198     /**
199      * Test method on int.
200      * @param i value to return
201      * @return given value
202      */

203     public int getInt(final int i) {
204         return i;
205     }
206
207     /**
208      * Adds two int.
209      * @param i first value
210      * @param j second value
211      * @return given value
212      */

213     public int addInt(final int i, final int j) {
214         return i + j;
215     }
216
217
218     /**
219      * Test method on int.
220      * @param ints array to return
221      * @return given value
222      */

223     public int[] getInts(final int[] ints) {
224         return ints;
225     }
226
227
228
229     // =====================
230
// long
231
// =====================
232

233
234     /**
235      * Test method on long.
236      * @param l value to return
237      * @return given value
238      */

239     public long getLong(final long l) {
240         return l;
241     }
242
243
244     /**
245      * Test method on long.
246      * @param longs array to return
247      * @return given value
248      */

249     public long[] getLongs(final long[] longs) {
250         return longs;
251     }
252
253
254     // =====================
255
// short
256
// =====================
257

258     /**
259      * Test method on short.
260      * @param s value to return
261      * @return given value
262      */

263     public short getShort(final short s) {
264         return s;
265     }
266
267
268     /**
269      * Test method on short.
270      * @param shorts array to return
271      * @return given value
272      */

273     public short[] getShorts(final short[] shorts) {
274         return shorts;
275     }
276
277
278
279     // =====================
280
// Mix of primitives
281
// =====================
282

283     /**
284      * Test method on primitive.
285      * @param flag value to return
286      * @param b value to return
287      * @param c value to return
288      * @param d value to return
289      * @param f value to return
290      * @param i value to return
291      * @param l value to return
292      * @param o value to return
293      * @return given values
294      */

295     public Object JavaDoc[] getPrimitive(final boolean flag, final byte b, final char c,
296             final double d, final float f, final int i, final long l, final Object JavaDoc o) {
297         // don't use autoboxing
298
return new Object JavaDoc[] {Boolean.valueOf(flag), Byte.valueOf(b), Character.valueOf(c),
299                 Double.valueOf(d), Float.valueOf(f), Integer.valueOf(i), Long.valueOf(l), o};
300     }
301
302
303
304
305
306
307
308     /**
309      * -----------------------------------------.
310      * - Method from another interface ---
311      * -----------------------------------------
312      */

313     public void methodNotInAllInterface() {
314
315     }
316
317
318
319     // =====================
320
// Exceptions
321
// =====================
322

323     /**
324      * Throws a user defined exception.
325      * @throws TestException an user defined exception
326      */

327     public void someCustomizedExceptions() throws TestException {
328         throw new TestException("someCustomizedExceptions");
329     }
330
331     /**
332      * Throws user defined exceptions.
333      * @param value depending of the value, throw different exceptions.
334      * @throws TestException an user defined exception
335      * @throws TestException2 another user defined exception
336      */

337     public void someCustomizedExceptions2(final int value) throws TestException, TestException2 {
338         switch (value) {
339             case 1:
340                 throw new TestException("someCustomizedExceptions2.TestException");
341             case 2:
342                 throw new TestException2("someCustomizedExceptions2.TestException2");
343             default:
344                 break;
345         }
346     }
347
348
349     /**
350      * Throws user defined exceptions.
351      * @param value depending of the value, throw different exceptions
352      * @throws Exception another exception
353      */

354     public void someCustomizedExceptions3(final int value) throws Exception JavaDoc {
355         final int one = 1;
356         final int two = 2;
357         final int three = 3;
358         switch (value) {
359             case one:
360                 throw new TestException("someCustomizedExceptions3.TestException");
361             case two:
362                 throw new TestException2("someCustomizedExceptions3.TestException2");
363             case three:
364                 throw new Exception JavaDoc("someCustomizedExceptions3.Exception");
365             default:
366                 throw new RuntimeException JavaDoc("someCustomizedExceptions3.RuntimeException");
367         }
368     }
369
370
371     /**
372      * Method do nothing but the interceptor will throw an exception.
373      */

374     public void throwExceptionByInterceptor() {
375
376     }
377
378
379     /**
380      * Change the return value by the interceptor.
381      * @param i value to be add twice.
382      * @return a value (mult * 2) of the given value
383      */

384     public int valueDoubledByInterceptor(final int i) {
385         return i;
386     }
387
388
389     /**
390     * Do some stuff while intercepting methods.
391     * @param invocationContext contains attributes of invocation
392     * @return method's invocation result
393     * @throws Exception if invocation fails
394     */

395    @AroundInvoke
396    public Object JavaDoc intercepted(final InvocationContext invocationContext) throws Exception JavaDoc {
397        // first, increase the counter
398
counter++;
399
400        // Check the method name
401
if (invocationContext.getMethod().getName().equals("throwExceptionByInterceptor")) {
402            throw new Exception JavaDoc("Throw an exception on throwExceptionByInterceptor");
403        }
404
405        if (invocationContext.getMethod().getName().equals("valueDoubledByInterceptor")) {
406            Object JavaDoc value = null;
407            try {
408                value = invocationContext.proceed();
409            } finally {
410                if (value instanceof Integer JavaDoc) {
411                 return Integer.valueOf(((Integer JavaDoc) value).intValue()* 2);
412                }
413            }
414        }
415
416            return invocationContext.proceed();
417    }
418
419
420     /**
421      * @return the counter (should be incremented by interceptor)
422      */

423     public int getCounter() {
424         return counter;
425     }
426
427
428     /**
429      * @return a counter used by other interceptors.
430      */

431     public int getOtherInterceptorCounter() {
432         return otherInterceptorCounter;
433     }
434
435     /**
436      * Increment the value of the counter used by other interceptors.
437      */

438     public void incrementOtherInterceptorCounter() {
439         otherInterceptorCounter++;
440     }
441
442
443     /**
444      * Test interceptor which is applied only on a single method.
445      */

446     @Interceptors({SingleMethodInterceptor.class})
447     public void singleMethodIntercepted() {
448
449     }
450
451     /**
452      * Increment the value of the counter used by single method interceptor.
453      */

454     public void incrementSingleMethodInterceptedCounter() {
455         singleMethodInterceptedCounter++;
456     }
457
458     /**
459      * @return the value of the counter used by single method interceptor.
460      */

461     public int getIncrementSingleMethodInterceptedCounter() {
462         return singleMethodInterceptedCounter;
463     }
464
465
466     /**
467      * Test that no interceptors are called on this method.
468      */

469     @ExcludeClassInterceptors
470     public void excludedInterceptorsMethod() {
471
472     }
473
474
475     /**
476      * Two methods with the same name but different parameters.
477      * First method.
478      * @param i dummy parameter.
479      */

480     public void sameMethodName(final int i) {
481
482     }
483
484     /**
485      * Two methods with the same name but different parameters.
486      * Second method.
487      * @param d dummy parameter.
488      */

489     public void sameMethodName(final double d) {
490
491     }
492 }
493
Popular Tags