KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > examples > statelessbean > 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.examples.statelessbean;
27
28 import javax.ejb.Local JavaDoc;
29 import javax.ejb.Remote JavaDoc;
30 import javax.ejb.Stateless JavaDoc;
31 import javax.interceptor.AroundInvoke;
32 import javax.interceptor.ExcludeClassInterceptors;
33 import javax.interceptor.Interceptors;
34 import javax.interceptor.InvocationContext;
35
36 /**
37  * Simple stateless bean.
38  * @author Florent Benoit
39  */

40 @Stateless JavaDoc
41 @Local JavaDoc(StatelessLocal.class)
42 @Remote JavaDoc(StatelessRemote.class)
43 @Interceptors({StatelessInterceptor.class})
44 public class StatelessBean implements StatelessRemote {
45
46     /**
47      * Hello world.
48      */

49     public void helloWorld() {
50         System.out.println("Hello world !");
51     }
52
53     /**
54      * Compute a + b.
55      * @param a first int
56      * @param b second int
57      * @return a + b
58      */

59     @Interceptors({StatelessOnlyAddMethodInterceptor.class})
60     public int add(final int a, final int b) {
61         return a + b;
62     }
63
64     /**
65      * Divide a by b.
66      * @param a first int
67      * @param b second int
68      * @return a / b
69      */

70     public int div(final int a, final int b) {
71         if (b == 0) {
72             throw new IllegalArgumentException JavaDoc("cannot divide by 0");
73         }
74         return a / b;
75     }
76
77     /**
78      * Methods without interceptors.
79      */

80     @ExcludeClassInterceptors
81     public void notInterceptedMethod() {
82
83     }
84
85
86     /**
87      * Trace method's time.
88      * @param invocationContext contains attributes of invocation
89      * @return method's invocation result
90      * @throws Exception if invocation fails
91      */

92     @AroundInvoke
93     public Object JavaDoc trace(final InvocationContext invocationContext) throws Exception JavaDoc {
94         System.out.println("TraceInterceptor : method '" + invocationContext.getMethod().getName()
95                 + "'.");
96         long startPeriod = System.nanoTime();
97         try {
98             return invocationContext.proceed();
99         } finally {
100             long elapsed = System.nanoTime() - startPeriod;
101             System.out.println("TraceInterceptor : Elapsed time = " + elapsed + " ns");
102         }
103     }
104
105 }
106
Popular Tags