KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.bsf.remoting;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 import org.bsf.remoting.BSFImage;
7 import org.bsf.remoting.RemoteService;
8
9 public class RemoteServiceMock implements RemoteService{
10
11     private int statefullIndex = 0;
12
13     public int compute(int varA, int varB, int operator) {
14         int result = 0;
15         switch (operator) {
16             case OPERATOR_ADD :
17                 result = varA + varB;
18                 break;
19             case OPERATOR_MINUS:
20                 result = varA - varB;
21                 break;
22             case OPERATOR_MULT:
23                 result = varA * varB;
24                 break;
25         }
26         try {
27             Thread.sleep(100);
28         } catch (InterruptedException JavaDoc e) {}
29         return result;
30     }
31
32     public void throwsException() throws Exception JavaDoc {
33         try {
34             Thread.sleep(100);
35         } catch (InterruptedException JavaDoc e) {}
36         throw new Exception JavaDoc("New Exception message");
37     }
38
39     public String JavaDoc upperWord(String JavaDoc word) {
40         if (word == null)
41             return null;
42         sleep100();
43         return word.toUpperCase();
44     }
45
46     private void sleep100() {
47         try {
48             Thread.sleep(100);
49         } catch (InterruptedException JavaDoc e) {}
50     }
51
52
53     /**
54      * Loads the image from the ressources
55      * @return
56      */

57     public BSFImage getBSFImage(){
58         //The max size must be 100ko.
59
byte[] buffer = new byte[1024*100];
60         byte[] data = null;
61         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
62         InputStream JavaDoc is = loader.getResourceAsStream("images/TeamImage.jpg");
63         int i = 0;
64         while(true){
65             int nextByte = 0;
66             try {
67                 nextByte = is.read();
68             } catch (IOException JavaDoc e) {
69                 throw new RuntimeException JavaDoc(e.getLocalizedMessage());
70             }
71             if (nextByte == -1){
72                 data = new byte[i];
73                 System.arraycopy(buffer, 0, data, 0, i);
74                 break;
75             }
76             buffer[i] = (byte)nextByte;
77             i++;
78         }
79         sleep100();
80         return new BSFImage("BSF Architecture Team", data);
81     }
82
83
84     public String JavaDoc createStatefull() {
85         statefullIndex++;
86         sleep100();
87         return "EJB" + statefullIndex;
88     }
89
90     public String JavaDoc remoteStatefulCall(){
91         sleep100();
92         return "CallOnEJB" + statefullIndex;
93     }
94 }
95
Popular Tags