KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > remoting > StatelessTestBean


1 package org.bsf.remoting;
2
3 import org.bsf.commons.ejb.SessionAdapterBean;
4 import org.bsf.commons.ejb.EJBFactory;
5
6 import javax.ejb.CreateException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Random JavaDoc;
12
13 /**
14  * This Test EJB is used by the JUnit test and by the BF remoting demo.
15  *
16  * @ejb:bean type="Stateless"
17  * name="StatelessTest"
18  * jndi-name="ejb/TestSession"
19  * generate="true"
20  * view-type="remote"
21  *
22  * @ejb:home extends="javax.ejb.EJBHome"
23  * generate="remote"
24  *
25  * @ejb:interface extends="javax.ejb.EJBObject"
26  * generate="remote"
27  *
28  * @ejb:transaction type="Required"
29  *
30  * @jonas.bean
31  * ejb-name="StatelessTest"
32  * jndi-name="ejb/TestSession"
33  */

34 public class StatelessTestBean extends SessionAdapterBean {
35
36     private static int exceptionCount = 0;
37
38     private List JavaDoc images = new ArrayList JavaDoc();
39     private Random JavaDoc random = null;
40
41     /**
42      * @ejb:interface-method
43      * @return the caller principal set on the server
44      */

45     public String JavaDoc getCallerPrincipal() {
46         String JavaDoc name = _ejbContext.getCallerPrincipal().getName();
47         logGraphBegin("getCallerPrincipal : " + name);
48         return name;
49     }
50
51
52     /**
53      * This method returns the given word in upper case to test a basic call.
54      * @ejb:interface-method
55      */

56     public String JavaDoc upper(String JavaDoc word){
57         return word.toUpperCase();
58     }
59
60     /**
61      * This method returns the given word in upper case to test a basic call.
62      * @ejb:interface-method
63      */

64     public int compute(int varA, int varB, int operator) {
65         int result = 0;
66         switch (operator) {
67             case RemoteService.OPERATOR_ADD :
68                 result = varA + varB;
69                 break;
70             case RemoteService.OPERATOR_MINUS:
71                 result = varA - varB;
72                 break;
73             case RemoteService.OPERATOR_MULT:
74                 result = varA * varB;
75                 break;
76         }
77         return result;
78     }
79
80
81     /**
82      * This method always throws an exception. The message varies based on
83      * the thrown exceptions count.
84      * @throws Exception
85      *
86      * @ejb:interface-method
87      */

88     public void throwsException() throws Exception JavaDoc {
89         throw new Exception JavaDoc("Exception "+ exceptionCount++);
90     }
91
92     /**
93      * This method always throws an SecurityException.
94      * As this exception is a RunTimeException, the container should
95      * encapsulate it in a EJBException
96      *
97      * @ejb:interface-method
98      */

99     public void throwsSecurityException() {
100         throw new SecurityException JavaDoc("No rights for this call");
101     }
102
103
104     /**
105      * This method is used to simulate the creation of a statefull bean and to
106      * test it.
107      * @ejb:interface-method
108      */

109     public StatelessTest createOtherBean(){
110         return (StatelessTest) getEJBContext().getEJBObject();
111     }
112
113
114     /**
115      * This method is used to simulate the creation of a statefull bean and to
116      * test it.
117      *
118      * @ejb:interface-method
119      */

120     public StatefulTest createStateful(){
121         StatefulTest result = null;
122
123         try {
124             result = ((StatefulTestHome)
125                     EJBFactory.getHome(StatefulTestHome.class)).create();
126         } catch (Exception JavaDoc e) {
127             throw new RuntimeException JavaDoc(e.getLocalizedMessage());
128         }
129         return result;
130     }
131
132
133     /**
134      * Loads the image from the ressources
135      * @return
136      *
137      * @ejb:interface-method
138      */

139     public BSFImage getBSFImage(){
140         int i = random.nextInt(images.size());
141         return (BSFImage) images.get(i);
142     }
143
144     private byte[] loadImageData(String JavaDoc resourceName){
145         //The max size must be 100ko.
146
byte[] buffer = new byte[1024*100];
147         byte[] data = null;
148         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
149         InputStream JavaDoc is = loader.getResourceAsStream(resourceName);
150         int i = 0;
151         while(true){
152             int nextByte = 0;
153             try {
154                 nextByte = is.read();
155             } catch (IOException JavaDoc e) {
156                 throw new RuntimeException JavaDoc(e.getLocalizedMessage());
157             }
158             if (nextByte == -1){
159                 data = new byte[i];
160                 System.arraycopy(buffer, 0, data, 0, i);
161                 break;
162             }
163             buffer[i] = (byte)nextByte;
164             i++;
165         }
166         return data;
167     }
168
169
170     /**
171      * @ejb.create-method
172      */

173     public void ejbCreate() throws CreateException JavaDoc {
174         BSFImage newImage =new BSFImage("BSF Architecture Team",
175                 loadImageData("images/TeamImage.jpg"));
176
177         images.add(newImage);
178
179         newImage = new BSFImage("Our President",
180                 loadImageData("images/Hendrix7.png"));
181         images.add(newImage);
182
183         newImage = new BSFImage("Our GUI Expert",
184                 loadImageData("images/keithRichards.jpg"));
185         images.add(newImage);
186
187         random = new Random JavaDoc();
188
189     }
190
191 }
192
Popular Tags