KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > client > Interceptor


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.client;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import org.omg.CORBA.COMM_FAILURE JavaDoc;
11 import org.omg.CORBA.CompletionStatus JavaDoc;
12 import org.omg.CORBA.LocalObject JavaDoc;
13 import org.omg.CORBA.ORB JavaDoc;
14 import org.omg.CORBA.Object JavaDoc;
15 import org.omg.CORBA.SystemException JavaDoc;
16 import org.omg.CORBA.SystemExceptionHelper JavaDoc;
17 import org.omg.PortableInterceptor.ClientRequestInfo JavaDoc;
18 import org.omg.PortableInterceptor.ClientRequestInterceptor JavaDoc;
19 import org.omg.PortableInterceptor.ForwardRequest JavaDoc;
20 import org.omg.PortableInterceptor.SYSTEM_EXCEPTION JavaDoc;
21
22 import ve.luz.ica.jackass.ref.RefUtil;
23
24 /**
25  * This class implements the Jackass Client Interceptor.
26  * @author Guido Urdaneta, David Fernández
27  * @see ClientRequestInterceptor
28  */

29 public class Interceptor extends LocalObject JavaDoc implements ClientRequestInterceptor JavaDoc
30 {
31     private static final Log LOG = LogFactory.getLog(Interceptor.class);
32     private static final String JavaDoc NAME = "Jackass Client Interceptor";
33     private static final String JavaDoc COMPONENT_FAULTED = "Component Faulted";
34     private static final int COMPONENT_FAULTED_MINOR_CODE = 6;
35
36     private ORB JavaDoc orb;
37     private ProxyManager proxyMgr;
38     private RefUtil refUtil;
39
40     /**
41      * Returns the name of the Interceptor
42      * @return the name of the Interceptor
43      */

44     public String JavaDoc name()
45     {
46         return NAME;
47     }
48
49     /**
50      * Forwards requests to Jackass components to an appropriate proxy. If the
51      * reference is not a Jackass Component nothing is done.
52      * @param ri request information.
53      * @throws ForwardRequest When it is neccessary to forward a request to a proxy.
54      */

55     public void send_request(ClientRequestInfo JavaDoc ri) throws ForwardRequest JavaDoc
56     {
57         init();
58         Object JavaDoc ref = ri.target();
59         LOG.debug("Effective target: " + ri.effective_target());
60         LOG.debug("Target: " + ri.target());
61
62         if (refUtil.isJackassComponent(ref))
63         {
64             LOG.debug("IT IS a Jackass component: " + ref);
65             String JavaDoc compID = refUtil.getComponentID(ref);
66             LOG.debug("Component ID: " + compID);
67             Object JavaDoc proxy = proxyMgr.getProxy(compID);
68             LOG.debug("Forwarding request to target (Proxy) " + proxy);
69             throw new ForwardRequest JavaDoc(proxy);
70         }
71         else
72         {
73             LOG.debug("NOT a Jackass component: " + ref);
74         }
75     }
76
77     /**
78      * Empty implementation. Not needed by Jackass Client Interceptor.
79      * @param ri Request information.
80      */

81     public void send_poll(ClientRequestInfo JavaDoc ri)
82     {
83     }
84
85     /**
86      * Empty implementation. Not needed by Jackass Client Interceptor.
87      * @param ri Request information.
88      */

89     public void receive_reply(ClientRequestInfo JavaDoc ri)
90     {
91     }
92
93     /**
94      * Handles exceptions. If The exception received is a system exception, and the request
95      * is for a Jackass component and the CompletionStatus is NO, then another
96      * proxy will be tried. Otherwise the exception is passed to the client.
97      * If no more proxies are available (the component is faulted) a COMM_FAILURE is thrown.
98      * @param ri Request information
99      * @throws ForwardRequest when a request is forwarded to another proxy
100      * @throws COMM_FAILURE when the targer component is faulted
101      */

102     public void receive_exception(ClientRequestInfo JavaDoc ri) throws ForwardRequest JavaDoc
103     {
104         //Check the use of effective_target(). It is probably wrong.
105
init();
106         if (LOG.isDebugEnabled()) LOG.debug("Exception Received. Reply status = " + ri.reply_status());
107         if (ri.reply_status() == SYSTEM_EXCEPTION.value)
108         {
109             SystemException JavaDoc exception = SystemExceptionHelper.extract(ri.received_exception());
110             if (LOG.isDebugEnabled()) LOG.debug("Received system exception " + exception);
111             Object JavaDoc efTarget = ri.effective_target();
112             if (LOG.isDebugEnabled()) LOG.debug("Effective Target: " + efTarget);
113
114             if ((exception.completed == CompletionStatus.COMPLETED_NO) && (refUtil.isJackassComponent(efTarget)))
115             {
116                 String JavaDoc compID = refUtil.getComponentID(efTarget);
117                 if (proxyMgr.isComponentFaulted(compID))
118                 {
119                     if (LOG.isWarnEnabled()) LOG.warn("Target faulted, throwing COMM_FAILURE");
120                     throw new COMM_FAILURE JavaDoc(
121                         COMPONENT_FAULTED,
122                         COMPONENT_FAULTED_MINOR_CODE,
123                         CompletionStatus.COMPLETED_NO);
124                 }
125                 else
126                 {
127                     Object JavaDoc proxy = proxyMgr.getProxy(compID);
128                     if (LOG.isInfoEnabled()) LOG.info("Trying another proxy: " + proxy);
129                     throw new ForwardRequest JavaDoc(proxy);
130                 }
131             }
132         }
133     }
134
135     /**
136      * Empty implementation. Not needed by Jackass Client Interceptor.
137      * @param ri Request information.
138      */

139     public void receive_other(ClientRequestInfo JavaDoc ri) //throws ForwardRequest
140
{
141     }
142
143     /**
144      * Empty implementation. Not needed by Jackass Client Interceptor.
145      * @see org.omg.PortableInterceptor.InterceptorOperations#destroy()
146      */

147     public void destroy()
148     {
149     }
150
151     /**
152      * Initializaes the ProxyManager and the RefUtil.
153      */

154     private void init()
155     {
156         initProxyManager();
157         initRefUtil();
158     }
159
160     /**
161      * Initializes an aditional ORB to be use for coding/decoding reference data.
162      */

163     private void initOrb()
164     {
165         if (orb == null)
166         {
167             orb = ORB.init((String JavaDoc[]) null, null);
168         }
169     }
170
171     /**
172      * Initializes the ProxyManager.
173      */

174     private void initProxyManager()
175     {
176         initOrb();
177         if (proxyMgr == null)
178         {
179             proxyMgr = new SingleHostProxyManager(orb);
180         }
181     }
182
183     /**
184      * Initializes the RefUtil.
185      */

186     private void initRefUtil()
187     {
188         initOrb();
189         if (refUtil == null)
190         {
191             refUtil = new RefUtil(orb);
192         }
193     }
194
195 }
196
Popular Tags