KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > interceptors > ClientForwardInterceptor


1 package demo.interceptors;
2
3 import org.omg.PortableInterceptor.*;
4 import org.omg.CosNaming.*;
5 import org.omg.CORBA.Any JavaDoc;
6
7 /**
8  * This interceptor will silently redirect requests of a
9  * client to another target by throwing a ForwardRequest
10  * exception.
11  *
12  * @author Nicolas Noffke
13  */

14
15 public class ClientForwardInterceptor
16     extends org.omg.CORBA.LocalObject JavaDoc
17     implements ClientRequestInterceptor
18 {
19     private MyServer grid = null;
20     private boolean in_loop = false;
21
22     public ClientForwardInterceptor(NamingContextExt nc)
23     {
24         try
25         {
26             grid = MyServerHelper.narrow(nc.resolve(nc.to_name("grid2.example")));
27         }
28         catch (Exception JavaDoc e)
29         {
30             e.printStackTrace();
31         }
32     }
33
34     public String JavaDoc name()
35     {
36         return "ClientForwardInterceptor";
37     }
38
39     public void destroy()
40     {
41
42     }
43
44     /**
45      * Throws a ForwardRequest, if target is wrong
46      */

47     public void send_request(ClientRequestInfo ri)
48         throws ForwardRequest
49     {
50         // loop prevention, because _is_a will also land here
51
if (! in_loop)
52         {
53             in_loop = true;
54
55             if (ri.effective_target()._is_a(MyServerHelper.id()))
56             {
57                 if (! grid._is_equivalent(ri.effective_target()))
58                 {
59                     System.out.println("Interceptor: Throwing ForwardRequest");
60           
61                     // setting in_loop not back, since the forward is permanent
62
throw new ForwardRequest( grid );
63                 }
64                 else
65                 {
66                     System.out.println("Interceptor: target is ok");
67                     in_loop = false;
68                 }
69             }
70             else
71             {
72                 System.out.println("Interceptor: ignoring, target has wrong type");
73                 in_loop = false;
74             }
75         }
76         else
77             System.out.println("Interceptor: ignoring, loop prevention");
78     }
79
80     public void send_poll(ClientRequestInfo ri){
81     }
82
83     public void receive_reply(ClientRequestInfo ri){
84     }
85
86     public void receive_exception(ClientRequestInfo ri)
87         throws ForwardRequest{
88     }
89
90     public void receive_other(ClientRequestInfo ri)
91         throws ForwardRequest{
92     }
93 } // ClientForwardInterceptor
94
Popular Tags