01: package demo.interceptors;
02:
03: import org.omg.PortableInterceptor.*;
04: import org.omg.CosNaming.*;
05: import org.omg.CORBA.Any;
06:
07: /**
08: * This interceptor will silently redirect requests of a
09: * client to another target by throwing a ForwardRequest
10: * exception.
11: *
12: * @author Nicolas Noffke
13: */
14:
15: public class ClientForwardInterceptor extends org.omg.CORBA.LocalObject
16: implements ClientRequestInterceptor {
17: private MyServer grid = null;
18: private boolean in_loop = false;
19:
20: public ClientForwardInterceptor(NamingContextExt nc) {
21: try {
22: grid = MyServerHelper.narrow(nc.resolve(nc
23: .to_name("grid2.example")));
24: } catch (Exception e) {
25: e.printStackTrace();
26: }
27: }
28:
29: public String name() {
30: return "ClientForwardInterceptor";
31: }
32:
33: public void destroy() {
34:
35: }
36:
37: /**
38: * Throws a ForwardRequest, if target is wrong
39: */
40: public void send_request(ClientRequestInfo ri)
41: throws ForwardRequest {
42: // loop prevention, because _is_a will also land here
43: if (!in_loop) {
44: in_loop = true;
45:
46: if (ri.effective_target()._is_a(MyServerHelper.id())) {
47: if (!grid._is_equivalent(ri.effective_target())) {
48: System.out
49: .println("Interceptor: Throwing ForwardRequest");
50:
51: // setting in_loop not back, since the forward is permanent
52: throw new ForwardRequest(grid);
53: } else {
54: System.out.println("Interceptor: target is ok");
55: in_loop = false;
56: }
57: } else {
58: System.out
59: .println("Interceptor: ignoring, target has wrong type");
60: in_loop = false;
61: }
62: } else
63: System.out
64: .println("Interceptor: ignoring, loop prevention");
65: }
66:
67: public void send_poll(ClientRequestInfo ri) {
68: }
69:
70: public void receive_reply(ClientRequestInfo ri) {
71: }
72:
73: public void receive_exception(ClientRequestInfo ri)
74: throws ForwardRequest {
75: }
76:
77: public void receive_other(ClientRequestInfo ri)
78: throws ForwardRequest {
79: }
80: } // ClientForwardInterceptor
|