KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > filter > FilterFactoryImpl


1 package org.jacorb.notification.filter;
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.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.logger.Logger;
30 import org.jacorb.notification.conf.Attributes;
31 import org.jacorb.notification.conf.Default;
32 import org.jacorb.notification.interfaces.Disposable;
33 import org.jacorb.notification.interfaces.GCDisposable;
34 import org.jacorb.notification.servant.ManageableServant;
35 import org.jacorb.notification.util.DisposableManager;
36 import org.jacorb.notification.util.LogUtil;
37 import org.omg.CORBA.Any JavaDoc;
38 import org.omg.CORBA.ORB JavaDoc;
39 import org.omg.CosNotifyFilter.Filter;
40 import org.omg.CosNotifyFilter.FilterFactory;
41 import org.omg.CosNotifyFilter.FilterFactoryHelper;
42 import org.omg.CosNotifyFilter.FilterFactoryPOA;
43 import org.omg.CosNotifyFilter.FilterHelper;
44 import org.omg.CosNotifyFilter.InvalidGrammar;
45 import org.omg.CosNotifyFilter.MappingFilter;
46 import org.omg.CosNotifyFilter.MappingFilterHelper;
47 import org.omg.PortableServer.POA JavaDoc;
48 import org.omg.PortableServer.Servant JavaDoc;
49
50 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
51
52 /**
53  * @author Alphonse Bendt
54  * @version $Id: FilterFactoryImpl.java,v 1.3 2005/04/10 14:21:32 alphonse.bendt Exp $
55  */

56
57 public class FilterFactoryImpl extends FilterFactoryPOA implements Disposable, ManageableServant
58 {
59     private class GCThread extends Thread JavaDoc implements Disposable
60     {
61         private final SynchronizedBoolean active = new SynchronizedBoolean(true);
62
63         public GCThread()
64         {
65             super();
66             setName("NotificationService Filter GC");
67             setPriority(Thread.MIN_PRIORITY + 1);
68         }
69         
70         public void run()
71         {
72             while (active.get())
73             {
74                 try
75                 {
76                     Thread.sleep(1000);
77
78                     runLoop();
79                 } catch (InterruptedException JavaDoc e)
80                 {
81                     // ignore. will check active_
82
}
83             }
84
85             logger_.info("GCThread exits");
86         }
87
88         private void runLoop() throws InterruptedException JavaDoc
89         {
90             synchronized (allFiltersLock_)
91             {
92                 Iterator JavaDoc i = new ArrayList JavaDoc(allFilters_).iterator();
93
94                 while (i.hasNext())
95                 {
96                     GCDisposable item = (GCDisposable) i.next();
97
98                     try
99                     {
100                         item.attemptDispose();
101                     } catch (Exception JavaDoc e)
102                     {
103                         i.remove();
104                     }
105
106                     verifyIsActive();
107                 }
108             }
109         }
110
111         private void verifyIsActive() throws InterruptedException JavaDoc
112         {
113             if (!active.get())
114             {
115                 throw new InterruptedException JavaDoc();
116             }
117         }
118
119         public void dispose()
120         {
121             logger_.info("Shutdown GCThread");
122
123             active.set(false);
124         }
125     }
126
127     private final ORB JavaDoc orb_;
128
129     private final POA poa_;
130
131     private final DisposableManager disposeHooks_ = new DisposableManager();
132
133     private final List JavaDoc allFilters_ = new ArrayList JavaDoc();
134
135     private final Object JavaDoc allFiltersLock_ = new Object JavaDoc();
136
137     protected final Logger logger_;
138
139     private FilterFactory thisFilter_;
140
141     private final IFilterFactoryDelegate factoryDelegate_;
142
143     private final boolean useGarbageCollector_;
144
145     private final Configuration config_;
146
147     // //////////////////////////////////////
148

149     /**
150      * @param factoryDelegate
151      * this Factory assumes ownership over the delegate and will dispose it after use.
152      */

153     public FilterFactoryImpl(ORB JavaDoc orb, POA poa, Configuration config,
154             IFilterFactoryDelegate factoryDelegate)
155     {
156         super();
157
158         orb_ = orb;
159         poa_ = poa;
160        
161         factoryDelegate_ = factoryDelegate;
162
163         config_ = config;
164         logger_ = LogUtil.getLogger(config, getClass().getName());
165
166         addDisposeHook(factoryDelegate_);
167
168         useGarbageCollector_ = config.getAttributeAsBoolean(Attributes.USE_GC,
169                 Default.DEFAULT_USE_GC);
170         
171         if (useGarbageCollector_)
172         {
173             logger_.info("Enable Garbage Collection for Filters");
174
175             final GCThread gc = new GCThread();
176
177             addDisposeHook(gc);
178             
179             gc.start();
180         }
181     }
182
183     public final void addDisposeHook(Disposable d)
184     {
185         disposeHooks_.addDisposable(d);
186     }
187
188     public final Filter create_filter(String JavaDoc grammar) throws InvalidGrammar
189     {
190         final AbstractFilter _servant = factoryDelegate_.create_filter_servant(grammar);
191
192         registerFilter(_servant);
193
194         Filter _filter = FilterHelper.narrow(_servant.activate());
195
196         return _filter;
197     }
198
199     public MappingFilter create_mapping_filter(String JavaDoc grammar, Any JavaDoc any) throws InvalidGrammar
200     {
201         MappingFilterImpl _mappingFilterServant = factoryDelegate_.create_mapping_filter_servant(
202                 config_, grammar, any);
203
204         registerFilter(_mappingFilterServant);
205
206         MappingFilter _filter = MappingFilterHelper.narrow(_mappingFilterServant.activate());
207
208         return _filter;
209     }
210
211     private final void registerFilter(final GCDisposable filter)
212     {
213         if (useGarbageCollector_)
214         {
215             synchronized (allFiltersLock_)
216             {
217                 allFilters_.add(filter);
218
219                 filter.addDisposeHook(new Disposable()
220                 {
221                     public void dispose()
222                     {
223                         synchronized (allFiltersLock_)
224                         {
225                             allFilters_.remove(filter);
226                         }
227                     }
228                 });
229             }
230         }
231     }
232
233     public final void deactivate()
234     {
235         try
236         {
237             poa_.deactivate_object(poa_.servant_to_id(getServant()));
238         } catch (Exception JavaDoc e)
239         {
240             logger_.fatalError("cannot deactivate object", e);
241             throw new RuntimeException JavaDoc();
242         }
243     }
244
245     protected Servant getServant()
246     {
247         return this;
248     }
249
250     public synchronized org.omg.CORBA.Object JavaDoc activate()
251     {
252         if (thisFilter_ == null)
253         {
254             thisFilter_ = FilterFactoryHelper.narrow(getServant()._this_object(orb_));
255         }
256         
257         return thisFilter_;
258     }
259
260     public final void dispose()
261     {
262         try
263         {
264             deactivate();
265         } finally
266         {
267             disposeHooks_.dispose();
268         }
269     }
270
271     public final POA _default_POA()
272     {
273         return poa_;
274     }
275
276     protected final ORB JavaDoc getORB()
277     {
278         return orb_;
279     }
280 }
Popular Tags