KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > poa > POAManager


1 package org.jacorb.poa;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-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 import java.util.Enumeration JavaDoc;
24 import java.util.Vector JavaDoc;
25 import org.omg.CORBA.INTERNAL JavaDoc;
26 import org.omg.PortableServer.POAManagerPackage.AdapterInactive JavaDoc;
27 import org.omg.PortableServer.POAManagerPackage.State JavaDoc;
28
29 /**
30  * The poa manager class, an implementation of org.omg.PortableServer.POAManager
31  *
32  * @author Reimo Tiedemann, FU Berlin
33  * @version $Id: POAManager.java,v 1.15 2004/10/29 07:43:00 simon.mcqueen Exp $
34  */

35
36 public class POAManager
37     extends org.omg.PortableServer._POAManagerLocalBase
38 {
39     public State JavaDoc state = State.HOLDING;
40     private org.jacorb.orb.ORB orb;
41     private Vector JavaDoc poas = new Vector JavaDoc();
42     private POAManagerMonitor monitor;
43     protected boolean poaCreationFailed;
44
45
46     protected POAManager(org.jacorb.orb.ORB _orb)
47     {
48         orb = _orb;
49         monitor = new POAManagerMonitorLightImpl();
50         try
51         {
52             monitor.configure(orb.getConfiguration());
53         }
54         catch (org.apache.avalon.framework.configuration.ConfigurationException ce)
55         {
56             // Never thrown
57
}
58         monitor.init(this);
59         monitor.openMonitor();
60         monitor.printMessage("ready");
61     }
62
63
64     public void activate() throws AdapterInactive JavaDoc
65     {
66         checkCreation ();
67
68         switch (state.value())
69         {
70             case State._INACTIVE :
71             throw new AdapterInactive JavaDoc();
72             case State._ACTIVE :
73             break;
74             default:
75             state = State.ACTIVE;
76             monitor.setToActive();
77
78             final POA [] poaArray;
79
80             synchronized (this)
81             {
82                 poaArray = new POA[poas.size()];
83                 poas.copyInto(poaArray);
84             }
85             // notify all registered poas
86
Thread JavaDoc thread = new Thread JavaDoc()
87             {
88                 public void run()
89                 {
90                     for (int i=0; i<poaArray.length; i++)
91                     {
92                         try {
93                             poaArray[i].changeToActive();
94                         } catch (Throwable JavaDoc e) {}
95                     }
96                 }
97             };
98             thread.start();
99         }
100     }
101
102
103     public void deactivate
104         (boolean etherealize_objects, boolean wait_for_completion)
105         throws AdapterInactive JavaDoc
106     {
107         checkCreation ();
108
109         if (wait_for_completion && isInInvocationContext())
110         {
111             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc();
112         }
113
114         switch (state.value())
115         {
116             case State._INACTIVE :
117             throw new AdapterInactive JavaDoc();
118             default :
119             state = State.INACTIVE;
120             monitor.setToInactive(wait_for_completion, etherealize_objects);
121
122             final boolean etherealize = etherealize_objects;
123             final POA [] poaArray;
124
125             synchronized (this)
126             {
127                 poaArray = new POA[poas.size()];
128                 poas.copyInto(poaArray);
129             }
130             // notify all registered poas
131
Thread JavaDoc thread = new Thread JavaDoc()
132             {
133                 public void run()
134                 {
135                     for (int i=poaArray.length-1; i>=0; i--)
136                     {
137                         try {
138                             poaArray[i].changeToInactive(etherealize);
139                         } catch (Throwable JavaDoc e) {}
140                     }
141                 }
142             };
143             thread.start();
144             if (wait_for_completion)
145             {
146                 try {
147                     thread.join();
148                 } catch (InterruptedException JavaDoc e) {}
149             }
150         }
151     }
152
153
154     public void discard_requests(boolean wait_for_completion)
155         throws AdapterInactive JavaDoc
156     {
157         checkCreation ();
158
159         if (wait_for_completion && isInInvocationContext())
160         {
161             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc();
162         }
163
164         switch (state.value())
165         {
166             case State._INACTIVE :
167             throw new AdapterInactive JavaDoc();
168             case State._DISCARDING :
169             break;
170             default :
171             state = State.DISCARDING;
172             monitor.setToDiscarding(wait_for_completion);
173
174             final POA [] poaArray;
175
176             synchronized (this)
177             {
178                 poaArray = new POA[poas.size()];
179                 poas.copyInto(poaArray);
180             }
181             // notify all registered poas
182
Thread JavaDoc thread = new Thread JavaDoc()
183             {
184                 public void run()
185                 {
186                     for (int i=poaArray.length-1; i>=0; i--)
187                     {
188                         try {
189                             poaArray[i].changeToDiscarding();
190                         } catch (Throwable JavaDoc e) {}
191                     }
192                 }
193             };
194             thread.start();
195             if (wait_for_completion)
196             {
197                 try {
198                     thread.join();
199                 } catch (InterruptedException JavaDoc e) {}
200             }
201         }
202     }
203
204
205     public State JavaDoc get_state()
206     {
207         return state;
208     }
209
210
211     protected synchronized POA getRegisteredPOA(String JavaDoc name)
212     {
213         POA result;
214         Enumeration JavaDoc en = poas.elements();
215         while (en.hasMoreElements())
216         {
217             result = (POA) en.nextElement();
218             if (name.equals(result._getQualifiedName()))
219             {
220                 return result;
221             }
222         }
223         throw new INTERNAL JavaDoc
224         (
225             "POA not registered: " +
226             POAConstants.ROOT_POA_NAME+
227             POAConstants.OBJECT_KEY_SEPARATOR+
228             name
229         );
230     }
231
232
233     public void hold_requests(boolean wait_for_completion)
234         throws AdapterInactive JavaDoc
235     {
236         checkCreation ();
237
238         if (wait_for_completion && isInInvocationContext())
239         {
240             throw new org.omg.CORBA.BAD_INV_ORDER JavaDoc();
241         }
242         switch (state.value())
243         {
244             case State._INACTIVE :
245             throw new AdapterInactive JavaDoc();
246             case State._HOLDING :
247             break;
248             default :
249             state = State.HOLDING;
250             monitor.setToHolding(wait_for_completion);
251
252             final POA [] poaArray;
253
254             synchronized (this)
255             {
256                 poaArray = new POA[poas.size()];
257                 poas.copyInto(poaArray);
258             }
259             // notify all registered poas
260
Thread JavaDoc thread = new Thread JavaDoc()
261             {
262                 public void run()
263                 {
264                     for (int i=poaArray.length-1; i>=0; i--)
265                     {
266                         try {
267                             poaArray[i].changeToHolding();
268                         } catch (Throwable JavaDoc e) {}
269                     }
270                 }
271             };
272             thread.start();
273             if (wait_for_completion)
274             {
275                 try {
276                     thread.join();
277                 } catch (InterruptedException JavaDoc e) {}
278             }
279         }
280     }
281
282
283     /**
284      * it returns true if the current thread is not in an invocation
285      * context dispatched by some POA belonging to the same ORB as this POAManager.
286      */

287     private boolean isInInvocationContext()
288     {
289         try {
290             if (orb.getPOACurrent().getORB() == orb) return true;
291
292         } catch (org.omg.PortableServer.CurrentPackage.NoContext JavaDoc e) {}
293         return false;
294     }
295
296
297     protected synchronized void registerPOA(POA poa)
298     {
299         if (!poas.contains(poa))
300         {
301             poas.addElement(poa);
302             monitor.addPOA(poa._getQualifiedName());
303         }
304     }
305
306
307     protected void setMonitor(POAManagerMonitor _monitor)
308     {
309         monitor = _monitor;
310     }
311
312
313     protected synchronized void unregisterPOA(POA poa)
314     {
315         poas.removeElement(poa);
316         monitor.removePOA(poa._getQualifiedName());
317     }
318
319
320     private void checkCreation ()
321     {
322         if (poaCreationFailed)
323         {
324             throw new org.omg.CORBA.INTERNAL JavaDoc ("POA Creation failed; unable to deactive");
325         }
326     }
327 }
328
Popular Tags