1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.logicblaze.lingo.example;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import java.util.Set;
24
25 import junit.framework.Assert;
26
27 /***
28 * @version $Revision: 1.4 $
29 */
30 public class ExampleServiceImpl extends Assert implements ExampleService {
31 private static final transient Log log = LogFactory.getLog(ExampleServiceImpl.class);
32
33 private static volatile String lastMethod;
34 private Object[] lastArguments;
35 private int delay = 1000;
36 private Set cheese;
37
38 public synchronized void someOneWayMethod(String name, int age) {
39 System.out.println("#### starting server side method for: " + name + " with age: " + age + " on instance: " + this);
40
41
42
43 System.out.println("####Ęsleeping for: " + delay + " millis to simulate slow server");
44
45 try {
46 Thread.sleep(delay);
47 }
48 catch (InterruptedException e) {
49 log.error("Caught: " + e, e);
50 }
51 lastMethod = "someOneWayMethod";
52 lastArguments = new Object[]{name, new Integer(age)};
53
54 System.out.println("#### completed server side method for: " + name + " with age: " + age);
55 }
56
57 public int regularRPC(String name) {
58 lastMethod = "regularRPC";
59 lastArguments = new Object[]{name};
60 return 55;
61 }
62
63 public void anotherRPC() throws Exception {
64 lastMethod = "anotherRPC";
65 lastArguments = new Object[0];
66 }
67
68 public void asyncRequestResponse(String stock, ResultListener listener) {
69 lastMethod = "asyncRequestResponse";
70 lastArguments = new Object[]{stock, listener};
71
72
73 try {
74 System.out.println("##### inside asyncRequestResponse() and about to call onResult()");
75 System.out.println("Remote proxy is: " + listener);
76
77 listener.onResult("Price for " + stock + " is 10");
78 try {
79 Thread.sleep(delay);
80 }
81 catch (InterruptedException e) {
82
83 }
84 listener.onResult("Price for " + stock + " is 11");
85 listener.stop();
86 }
87 catch (Exception e) {
88 System.out.println("#### error: " + e);
89 e.printStackTrace();
90 }
91 }
92
93
94
95 public String getLastMethod() {
96 return lastMethod;
97 }
98
99 public Object[] getLastArguments() {
100 return lastArguments;
101 }
102
103 public void assertOneWayCalled() {
104 assertNotNull("lastMethod should not be null if we have been invoked on instance: " + this, lastMethod);
105 }
106
107 public void assertOneWayNotCompletedYet() {
108 assertEquals("lastMethod should be null as we should not have waited for the method to complete for instance: " + this, null, lastMethod);
109 }
110
111 public int getDelay() {
112 return delay;
113 }
114
115 public void setDelay(int delay) {
116 this.delay = delay;
117 }
118
119
120 public Set getCheese() {
121 return cheese;
122 }
123
124 public void setCheese(Set cheese) {
125 this.cheese = cheese;
126 }
127
128 public void clear() {
129 lastMethod = null;
130 }
131 }