KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > portableInterceptor > InterceptorManager


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.orb.portableInterceptor;
22
23 import org.omg.PortableInterceptor.*;
24 import org.apache.avalon.framework.logger.Logger;
25
26 import java.util.*;
27
28 /**
29  * This class "manages" the portable interceptors registered
30  * with the ORB, and controls the PICurrent.
31  *
32  * @author Nicolas Noffke
33  * @version $Id: InterceptorManager.java,v 1.17 2004/07/19 15:28:50 phil.mesnier Exp $
34  */

35
36 public class InterceptorManager
37 {
38     private Interceptor[] client_req_interceptors = null;
39     private Interceptor[] server_req_interceptors = null;
40     private Interceptor[] ior_interceptors = null;
41     private int[] profile_tags = null;
42
43     private org.omg.CORBA.ORB JavaDoc orb = null;
44     private int current_slots = 0;
45     private Logger logger;
46
47     private static ThreadLocal JavaDoc piCurrent = new ThreadLocal JavaDoc();
48
49     public static final PICurrentImpl EMPTY_CURRENT = new PICurrentImpl(null, 0);
50
51     public InterceptorManager(Vector client_interceptors,
52                               Vector server_interceptors,
53                               Vector ior_intercept,
54                               int slot_count,
55                               org.omg.CORBA.ORB JavaDoc orb)
56     {
57         logger =
58             ((org.jacorb.orb.ORB)orb).getConfiguration().getNamedLogger("jacorb.orb.interceptors");
59
60         if (logger.isInfoEnabled())
61         {
62             logger.info("InterceptorManager started with " +
63                         server_interceptors.size() +" SIs, "
64                         + client_interceptors.size() + " CIs and " +
65                         ior_intercept.size() + " IORIs");
66         }
67
68         //build sorted arrays of interceptors
69
client_req_interceptors =
70             new ClientRequestInterceptor[client_interceptors.size()];
71
72         //selection sort
73
for (int j = 0; j < client_req_interceptors.length; j++){
74             String JavaDoc min = ((ClientRequestInterceptor) client_interceptors.
75                           elementAt(0)).name();
76             int min_index = 0;
77
78             for(int _i = 1; _i < client_interceptors.size(); _i++)
79                 if (min.compareTo(((ClientRequestInterceptor) client_interceptors.
80                                    elementAt(_i)).name()) > 0){
81                     min = ((ClientRequestInterceptor) client_interceptors.
82                            elementAt(_i)).name();
83                     min_index = _i;
84                 }
85
86             client_req_interceptors[j] = (ClientRequestInterceptor)
87                 client_interceptors.elementAt(min_index);
88             client_interceptors.removeElementAt(min_index);
89         }
90
91         server_req_interceptors =
92             new ServerRequestInterceptor[server_interceptors.size()];
93         //selection sort
94
for (int j = 0; j < server_req_interceptors.length; j++){
95             String JavaDoc min = ((ServerRequestInterceptor) server_interceptors.
96                           elementAt(0)).name();
97             int min_index = 0;
98
99             for(int _i = 1; _i < server_interceptors.size(); _i++)
100                 if (min.compareTo(((ServerRequestInterceptor) server_interceptors.
101                                    elementAt(_i)).name()) > 0){
102                     min = ((ServerRequestInterceptor) server_interceptors.
103                            elementAt(_i)).name();
104                     min_index = _i;
105                 }
106
107             server_req_interceptors[j] = (ServerRequestInterceptor)
108                 server_interceptors.elementAt(min_index);
109
110             server_interceptors.removeElementAt(min_index);
111         }
112
113
114         ior_interceptors = new IORInterceptor[ior_intercept.size()];
115         //selection sort
116
for (int j = 0; j < ior_interceptors.length; j++){
117             String JavaDoc min = ((IORInterceptor) ior_intercept.elementAt(0)).name();
118             int min_index = 0;
119
120             for(int _i = 1; _i < ior_intercept.size(); _i++)
121                 if (min.compareTo(((IORInterceptor) ior_intercept.
122                                    elementAt(_i)).name()) > 0){
123                     min = ((IORInterceptor) ior_intercept.elementAt(_i)).name();
124                     min_index = _i;
125                 }
126
127             ior_interceptors[j] = (IORInterceptor) ior_intercept.
128                 elementAt(min_index);
129
130             ior_intercept.removeElementAt(min_index);
131         }
132
133         this.orb = orb;
134         current_slots = slot_count;
135     }
136
137     /**
138      * This method returns a thread specific PICurrent.
139      */

140     public Current getCurrent()
141     {
142         Current value = (Current)piCurrent.get();
143         if (value == null)
144         {
145             value = getEmptyCurrent();
146             piCurrent.set(value);
147         }
148         return value;
149     }
150
151     /**
152      * Sets the thread scope current, i.e. a server side current
153      * associated with the calling thread.
154      */

155     public void setTSCurrent(Current current)
156     {
157         piCurrent.set(current);
158     }
159
160     /**
161      * Removes the thread scope current, that is associated with the
162      * calling thread.
163      */

164     public void removeTSCurrent()
165     {
166         piCurrent.set(null);
167     }
168
169     /**
170      * Returns an empty current where no slot has been set.
171      */

172     public Current getEmptyCurrent()
173     {
174         return new PICurrentImpl(orb, current_slots);
175     }
176
177     /**
178      * Returns an iterator object that contains the ClientRequestInterceptors
179      * of this manager.
180      */

181     public ClientInterceptorIterator getClientIterator()
182     {
183         return new ClientInterceptorIterator(client_req_interceptors);
184     }
185
186     /**
187      * Returns an iterator object that contains the ServerRequestInterceptors
188      * of this manager.
189      */

190     public ServerInterceptorIterator getServerIterator()
191     {
192         return new ServerInterceptorIterator(server_req_interceptors);
193     }
194
195     /**
196      * Returns an iterator object that contains the IORInterceptors
197      * of this manager.
198      */

199     public IORInterceptorIterator getIORIterator()
200     {
201         return new IORInterceptorIterator(ior_interceptors, profile_tags);
202     }
203
204
205     /**
206      * Assign the array of profile tags to be passed to the IORInterceptors
207      */

208     public void setProfileTags (int [] ptags)
209     {
210         profile_tags = ptags;
211     }
212
213     /**
214      * Test, if the manager has ClientRequestInterceptors
215      */

216     public boolean hasClientRequestInterceptors()
217     {
218         return client_req_interceptors.length > 0;
219     }
220
221     /**
222      * Test, if the manager has ServerRequestInterceptors
223      */

224     public boolean hasServerRequestInterceptors()
225     {
226         return server_req_interceptors.length > 0;
227     }
228
229     /**
230      * Test, if the manager has IORInterceptors
231      */

232     public boolean hasIORInterceptors()
233     {
234         return ior_interceptors.length > 0;
235     }
236
237     public void destroy()
238     {
239         if( hasClientRequestInterceptors() )
240         {
241             for( int i = 0; i < client_req_interceptors.length; i++ )
242             {
243                 client_req_interceptors[ i ].destroy();
244             }
245         }
246
247         if( hasServerRequestInterceptors() )
248         {
249             for( int i = 0; i < server_req_interceptors.length; i++ )
250             {
251                 server_req_interceptors[ i ].destroy();
252             }
253         }
254
255         if( hasIORInterceptors() )
256         {
257             for( int i = 0; i < ior_interceptors.length; i++ )
258             {
259                 ior_interceptors[ i ].destroy();
260             }
261         }
262
263     }
264 } // InterceptorManager
265
Popular Tags