KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > servant > AbstractProxy


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

23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.logger.Logger;
30 import org.jacorb.notification.FilterManager;
31 import org.jacorb.notification.IContainer;
32 import org.jacorb.notification.OfferManager;
33 import org.jacorb.notification.SubscriptionManager;
34 import org.jacorb.notification.conf.Attributes;
35 import org.jacorb.notification.conf.Default;
36 import org.jacorb.notification.engine.TaskProcessor;
37 import org.jacorb.notification.interfaces.Disposable;
38 import org.jacorb.notification.interfaces.FilterStage;
39 import org.jacorb.notification.util.DisposableManager;
40 import org.jacorb.notification.util.QoSPropertySet;
41 import org.omg.CORBA.NO_IMPLEMENT JavaDoc;
42 import org.omg.CORBA.OBJECT_NOT_EXIST JavaDoc;
43 import org.omg.CORBA.ORB JavaDoc;
44 import org.omg.CosEventChannelAdmin.AlreadyConnected;
45 import org.omg.CosEventComm.Disconnected;
46 import org.omg.CosNotification.NamedPropertyRangeSeqHolder;
47 import org.omg.CosNotification.Property;
48 import org.omg.CosNotification.QoSAdminOperations;
49 import org.omg.CosNotification.UnsupportedQoS;
50 import org.omg.CosNotifyChannelAdmin.ConnectionAlreadyActive;
51 import org.omg.CosNotifyChannelAdmin.ConnectionAlreadyInactive;
52 import org.omg.CosNotifyChannelAdmin.NotConnected;
53 import org.omg.CosNotifyChannelAdmin.ProxyType;
54 import org.omg.CosNotifyFilter.Filter;
55 import org.omg.CosNotifyFilter.FilterAdminOperations;
56 import org.omg.CosNotifyFilter.FilterNotFound;
57 import org.omg.CosNotifyFilter.MappingFilter;
58 import org.omg.CosNotifyFilter.MappingFilterHelper;
59 import org.omg.PortableServer.POA JavaDoc;
60 import org.omg.PortableServer.Servant JavaDoc;
61 import org.picocontainer.PicoContainer;
62
63 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
64 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
65
66 /**
67  * @author Alphonse Bendt
68  * @version $Id: AbstractProxy.java,v 1.19 2005/05/01 21:00:58 alphonse.bendt Exp $
69  */

70
71 public abstract class AbstractProxy implements FilterAdminOperations, QoSAdminOperations,
72         FilterStage, Disposable, ManageableServant, Configurable
73 {
74     private final MappingFilter nullMappingFilterRef_;
75
76     protected final boolean isIDPublic_;
77
78     protected final Logger logger_;
79
80     private final SynchronizedBoolean connected_ = new SynchronizedBoolean(false);
81
82     protected final QoSPropertySet qosSettings_;
83
84     protected final Integer JavaDoc id_;
85
86     protected final OfferManager offerManager_;
87
88     protected final SubscriptionManager subscriptionManager_;
89
90     protected Servant JavaDoc thisServant_;
91
92     protected MappingFilter lifetimeFilter_;
93
94     protected MappingFilter priorityFilter_;
95
96     /**
97      * delegate for FilterAdminOperations
98      */

99     private final FilterManager filterManager_;
100
101     private final SynchronizedBoolean destroyed_ = new SynchronizedBoolean(false);
102
103     private final SynchronizedBoolean disposeInProgress_ = new SynchronizedBoolean(false);
104
105     private final SynchronizedInt errorCounter_ = new SynchronizedInt(0);
106
107     private final POA JavaDoc poa_;
108
109     private final ORB JavaDoc orb_;
110
111     private final TaskProcessor taskProcessor_;
112
113     private boolean isInterFilterGroupOperatorOR_;
114
115     private final boolean disposedProxyDisconnectsClient_;
116
117     private final SynchronizedBoolean active_ = new SynchronizedBoolean(true);
118
119     private final DisposableManager disposables_ = new DisposableManager();
120
121     private final PicoContainer container_;
122
123     ////////////////////////////////////////
124

125     protected AbstractProxy(IAdmin admin, ORB JavaDoc orb, POA JavaDoc poa, Configuration conf,
126             TaskProcessor taskProcessor, OfferManager offerManager,
127             SubscriptionManager subscriptionManager)
128     {
129         id_ = new Integer JavaDoc(admin.getProxyID());
130         isIDPublic_ = admin.isIDPublic();
131         container_ = admin.getContainer();
132
133         orb_ = orb;
134         poa_ = poa;
135         taskProcessor_ = taskProcessor;
136
137         offerManager_ = offerManager;
138         subscriptionManager_ = subscriptionManager;
139
140         filterManager_ = new FilterManager();
141
142         nullMappingFilterRef_ = MappingFilterHelper.narrow(orb.string_to_object(orb
143                 .object_to_string(null)));
144
145         logger_ = ((org.jacorb.config.Configuration) conf).getNamedLogger(getClass().getName());
146
147         disposedProxyDisconnectsClient_ = conf.getAttribute(
148                 Attributes.DISPOSE_PROXY_CALLS_DISCONNECT,
149                 Default.DEFAULT_DISPOSE_PROXY_CALLS_DISCONNECT).equals("on");
150
151         qosSettings_ = new QoSPropertySet(conf, QoSPropertySet.PROXY_QOS);
152
153         configure(conf);
154     }
155
156     public void configure(Configuration conf)
157     {
158         // no op
159
}
160
161     ////////////////////////////////////////
162

163     public void addDisposeHook(Disposable d)
164     {
165         disposables_.addDisposable(d);
166     }
167
168     public boolean isIDPublic()
169     {
170         return isIDPublic_;
171     }
172
173     protected POA JavaDoc getPOA()
174     {
175         return poa_;
176     }
177
178     protected ORB JavaDoc getORB()
179     {
180         return orb_;
181     }
182
183     protected TaskProcessor getTaskProcessor()
184     {
185         return taskProcessor_;
186     }
187
188     //////////////////////////////////////////////////////
189
// delegate FilterAdmin Operations to FilterManager //
190
//////////////////////////////////////////////////////
191

192     public final int add_filter(Filter filter)
193     {
194         return filterManager_.add_filter(filter);
195     }
196
197     public final void remove_filter(int n) throws FilterNotFound
198     {
199         filterManager_.remove_filter(n);
200     }
201
202     public final Filter get_filter(int n) throws FilterNotFound
203     {
204         return filterManager_.get_filter(n);
205     }
206
207     public final int[] get_all_filters()
208     {
209         return filterManager_.get_all_filters();
210     }
211
212     public final void remove_all_filters()
213     {
214         filterManager_.remove_all_filters();
215     }
216
217     ////////////////////////////////////////
218

219     // TODO implement
220
public void validate_event_qos(Property[] qosProps, NamedPropertyRangeSeqHolder propSeqHolder)
221     {
222         throw new NO_IMPLEMENT JavaDoc();
223     }
224
225     public final void validate_qos(Property[] props, NamedPropertyRangeSeqHolder propertyRange)
226             throws UnsupportedQoS
227     {
228         qosSettings_.validate_qos(props, propertyRange);
229     }
230
231     public final void set_qos(Property[] qosProps) throws UnsupportedQoS
232     {
233         if (qosSettings_ != null)
234         {
235             qosSettings_.set_qos(qosProps);
236         }
237     }
238
239     public final Property[] get_qos()
240     {
241         return qosSettings_.get_qos();
242     }
243
244     public final void priority_filter(MappingFilter filter)
245     {
246         priorityFilter_ = filter;
247     }
248
249     public final MappingFilter priority_filter()
250     {
251         if (priorityFilter_ == null)
252         {
253             return nullMappingFilterRef_;
254         }
255
256         return priorityFilter_;
257     }
258
259     public final MappingFilter lifetime_filter()
260     {
261         if (lifetimeFilter_ == null)
262         {
263             return nullMappingFilterRef_;
264         }
265
266         return lifetimeFilter_;
267     }
268
269     public final void lifetime_filter(MappingFilter filter)
270     {
271         lifetimeFilter_ = filter;
272     }
273
274     public final Integer JavaDoc getID()
275     {
276         return id_;
277     }
278
279     /**
280      * Override this method from the Servant baseclass. Fintan Bolton in his book "Pure CORBA"
281      * suggests that you override this method to avoid the risk that a servant object (like this
282      * one) could be activated by the <b>wrong </b> POA object.
283      */

284     public final POA JavaDoc _default_POA()
285     {
286         return getPOA();
287     }
288
289     public final List JavaDoc getFilters()
290     {
291         return filterManager_.getFilters();
292     }
293
294     public final void deactivate()
295     {
296         logger_.info("deactivate Proxy");
297
298         try
299         {
300             byte[] _oid = getPOA().servant_to_id(getServant());
301             getPOA().deactivate_object(_oid);
302         } catch (Exception JavaDoc e)
303         {
304             logger_.fatalError("Couldn't deactivate Proxy", e);
305         }
306     }
307
308     private void tryDisconnectClient()
309     {
310         try
311         {
312             if (disposedProxyDisconnectsClient_ && connected_.get())
313             {
314                 logger_.info("disconnect_client");
315
316                 disconnectClient();
317             }
318         } catch (Exception JavaDoc e)
319         {
320             logger_.error("disconnect_client raised an unexpected error: " + "ignore", e);
321         } finally
322         {
323             connected_.set(false);
324         }
325     }
326
327     public final boolean isDisposed()
328     {
329         return destroyed_.get();
330     }
331
332     protected void checkDestroyStatus() throws OBJECT_NOT_EXIST JavaDoc
333     {
334         if (!destroyed_.commit(false, true))
335         {
336             logger_.fatalError("dispose has been called twice");
337
338             throw new OBJECT_NOT_EXIST JavaDoc();
339         }
340     }
341
342     public final void destroy()
343     {
344         checkDestroyStatus();
345
346         container_.dispose();
347         
348         List JavaDoc list = container_.getComponentInstancesOfType(IContainer.class);
349         for (Iterator JavaDoc i = list.iterator(); i.hasNext();)
350         {
351             IContainer element = (IContainer) i.next();
352             element.destroy();
353         }
354     }
355
356     public void dispose()
357     {
358         logger_.info("Destroy Proxy " + id_);
359
360         disposeInProgress_.set(true);
361
362         //////////////////////////////
363

364         tryDisconnectClient();
365
366         //////////////////////////////
367

368         deactivate();
369
370         //////////////////////////////
371

372         removeListener();
373
374         //////////////////////////////
375

376         remove_all_filters();
377
378         //////////////////////////////
379

380         disposables_.dispose();
381     }
382
383     public abstract ProxyType MyType();
384
385     void setInterFilterGroupOperatorOR(boolean b)
386     {
387         isInterFilterGroupOperatorOR_ = b;
388     }
389
390     public final boolean hasInterFilterGroupOperatorOR()
391     {
392         return isInterFilterGroupOperatorOR_;
393     }
394
395     public final boolean isConnected()
396     {
397         return !disposeInProgress_.get() && connected_.get();
398     }
399
400     public final boolean hasLifetimeFilter()
401     {
402         return lifetimeFilter_ != null;
403     }
404
405     public final boolean hasPriorityFilter()
406     {
407         return priorityFilter_ != null;
408     }
409
410     public final MappingFilter getLifetimeFilter()
411     {
412         return lifetimeFilter_;
413     }
414
415     public final MappingFilter getPriorityFilter()
416     {
417         return priorityFilter_;
418     }
419
420     public void resetErrorCounter()
421     {
422         errorCounter_.set(0);
423     }
424
425     public final int getErrorCounter()
426     {
427         return errorCounter_.get();
428     }
429
430     public final int incErrorCounter()
431     {
432         return errorCounter_.increment();
433     }
434
435     protected boolean isSuspended()
436     {
437         return !active_.get();
438     }
439
440     public final void suspend_connection() throws NotConnected, ConnectionAlreadyInactive
441     {
442         checkIsConnected();
443
444         if (!active_.commit(true, false))
445         {
446             throw new ConnectionAlreadyInactive();
447         }
448
449         connectionSuspended();
450     }
451
452     /**
453      * this is an extension point.
454      */

455     protected void connectionSuspended()
456     {
457         // No Op
458
}
459
460     public final void resume_connection() throws NotConnected, ConnectionAlreadyActive
461     {
462         checkIsConnected();
463
464         if (!active_.commit(false, true))
465         {
466             throw new ConnectionAlreadyActive();
467         }
468
469         connectionResumed();
470     }
471
472     /**
473      * this is an extension point.
474      * invoked when resume_connection was called successfully.
475      */

476     protected void connectionResumed()
477     {
478         // NO OP
479
}
480
481     protected void checkIsConnected() throws NotConnected
482     {
483         if (!connected_.get())
484         {
485             throw new NotConnected();
486         }
487     }
488
489     protected void checkIsNotConnected() throws AlreadyConnected
490     {
491         if (connected_.get())
492         {
493             throw new AlreadyConnected();
494         }
495     }
496
497     protected void checkStillConnected() throws Disconnected
498     {
499         if (!connected_.get())
500         {
501             logger_.fatalError("access on a not connected proxy");
502
503             destroy();
504
505             throw new Disconnected();
506         }
507     }
508
509     protected void connectClient(org.omg.CORBA.Object JavaDoc client)
510     {
511         connected_.set(true);
512     }
513
514     /**
515      * invoke the proxy specific disconnect method.
516      */

517     protected abstract void disconnectClient();
518
519     protected abstract Servant JavaDoc getServant();
520
521     protected void handleDisconnected(Disconnected e)
522     {
523         logger_.fatalError("Illegal state: Client think it's disconnected. "
524                 + "Proxy thinks Client is still connected. The Proxy will be destroyed.", e);
525
526         destroy();
527         //container_.dispose();
528
}
529
530     protected abstract void removeListener();
531 }
Popular Tags