KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.logger.Logger;
24 import org.apache.avalon.framework.configuration.*;
25
26 import org.jacorb.poa.except.*;
27 import org.jacorb.poa.util.*;
28 import org.jacorb.poa.gui.*;
29
30 import org.jacorb.orb.dsi.ServerRequest;
31
32 import org.omg.PortableServer.*;
33 import org.omg.PortableServer.POAManagerPackage.State JavaDoc;
34
35 import java.util.Enumeration JavaDoc;
36
37 /**
38  * This class extends the POA with a monitoring gui. It implements all
39  * poa related listener interfaces and will set up and update the gui.
40  *
41  * @author Reimo Tiedemann, FU Berlin
42  * @version 1.06, 12/08/99, RT
43  */

44
45 public class POAMonitorImpl
46     extends POAAdapter
47     implements POAMonitor, POAMonitorController, Configurable
48 {
49     private POA poaModel;
50     private AOM aomModel;
51     private RequestQueue queueModel;
52     private RPPoolManager pmModel;
53
54     private POAMonitorView view;
55     private String JavaDoc prefix;
56
57     private int aomSize;
58     private int queueSize;
59     private int poolCount;
60     private int poolSize;
61
62     private boolean terminate;
63     private boolean aomChanged;
64     private boolean queueChanged;
65     private boolean pmChanged;
66
67     /** the configuration object for this POA instance */
68     private org.jacorb.config.Configuration configuration = null;
69     private Logger logger;
70     private int threadPoolMin = 0;
71     private int threadPoolMax = 0;
72
73
74     public void configure(Configuration myConfiguration)
75         throws ConfigurationException
76     {
77         this.configuration = (org.jacorb.config.Configuration)myConfiguration;
78         logger = configuration.getNamedLogger("jacorb.poa.monitor");
79
80         threadPoolMin =
81             configuration.getAttributeAsInteger("jacorb.poa.thread_pool_min", 5);
82
83         threadPoolMax =
84             configuration.getAttributeAsInteger("jacorb.poa.thread_pool_max", 20);
85
86     }
87
88
89     public void actionCloseView() {
90         closeMonitor();
91     }
92
93     public void actionDeactivateObject(String JavaDoc oidStr) {
94
95         if (poaModel != null) {
96             try {
97                 poaModel.deactivate_object( oidStr.getBytes() );
98             } catch (Throwable JavaDoc e) {
99                 printMessage("Exception occurred in deactivateObject() of POAMonitor: "+e);
100             }
101         }
102     }
103
104     public void actionRemoveRequestFromQueue(String JavaDoc ridStr) {
105
106         if (queueModel != null && poaModel != null) {
107             try {
108                 ServerRequest request = queueModel.getElementAndRemove(Integer.parseInt(ridStr));
109                 if (request == null) throw new ApplicationError("error: rid " + ridStr + " is not contained in queue");
110                 poaModel.getRequestController().rejectRequest(request, new org.omg.CORBA.OBJ_ADAPTER JavaDoc());
111             } catch (Throwable JavaDoc e) {
112                 printMessage("Exception occurred in removeRequestFromQueue() of POAMonitor: "+e);
113             }
114         }
115     }
116
117     public StringPair[] actionRetrieveAOMContent() {
118
119         if (aomModel != null) {
120             try {
121                 return aomModel != null ? aomModel.deliverContent() : null;
122             } catch (Throwable JavaDoc e) {
123                 printMessage("Exception occurred in retrieveAOMContent() of POAMonitor: "+e);
124             }
125         }
126         return null;
127     }
128
129     public StringPair[] actionRetrieveQueueContent()
130     {
131         if (queueModel != null)
132         {
133             try
134             {
135                 return queueModel.deliverContent();
136             }
137             catch (Throwable JavaDoc e)
138             {
139                 printMessage("Exception during retrieveQueueContent() of POAMonitor: "+e);
140             }
141         }
142         return null;
143     }
144
145
146     public synchronized void changeState(String JavaDoc state)
147     {
148         if (view != null)
149         {
150             try
151             {
152                 view._setState(state);
153             }
154             catch (Throwable JavaDoc exception)
155             {
156                 if (logger.isWarnEnabled())
157                 {
158                     logger.warn("Exception during changeState() of POAMonitor" +
159                                 exception.getMessage());
160                 }
161             }
162         }
163     }
164
165
166     public synchronized void closeMonitor()
167     {
168         if (view != null)
169         {
170             try
171             {
172                 terminate = true;
173                 poaModel._removePOAEventListener(this);
174                 POAMonitor newMonitor =
175                     (POAMonitor)Class.forName("org.jacorb.poa.POAMonitorLightImpl").newInstance();
176                 newMonitor.init(poaModel, aomModel, queueModel, pmModel, prefix );
177                 newMonitor.configure(configuration);
178                 poaModel.setMonitor(newMonitor);
179                 POAMonitorView tmp = view;
180                 view = null;
181                 tmp._destroy();
182
183             }
184             catch (Throwable JavaDoc exception)
185             {
186                 if (logger.isWarnEnabled())
187                 {
188                     logger.warn("Exception during closeMonitor() of POAMonitorImpl" +
189                                 exception.getMessage());
190                 }
191             }
192         }
193     }
194
195
196     public void init(POA poa, AOM aom,
197                      RequestQueue queue, RPPoolManager pm,
198                      String JavaDoc _prefix )
199     {
200         poaModel = poa;
201         aomModel = aom;
202         queueModel = queue;
203         pmModel = pm;
204         prefix = prefix;
205     }
206
207
208     private void initView()
209     {
210         if (view != null)
211         {
212             try
213             {
214                 String JavaDoc name = poaModel._getQualifiedName();
215                 view._setName(name.equals("") ? POAConstants.ROOT_POA_NAME :
216                               POAConstants.ROOT_POA_NAME+POAConstants.OBJECT_KEY_SEPARATOR+name);
217
218                 view._setState(POAUtil.convert(poaModel.getState()));
219
220                 view._setPolicyThread(POAUtil.convert(poaModel.threadPolicy, THREAD_POLICY_ID.value));
221                 view._setPolicyLifespan(POAUtil.convert(poaModel.lifespanPolicy, LIFESPAN_POLICY_ID.value));
222                 view._setPolicyIdUniqueness(POAUtil.convert(poaModel.idUniquenessPolicy, ID_UNIQUENESS_POLICY_ID.value));
223                 view._setPolicyIdAssignment(POAUtil.convert(poaModel.idAssignmentPolicy, ID_ASSIGNMENT_POLICY_ID.value));
224                 view._setPolicyServantRetention(POAUtil.convert(
225                                                     poaModel.servantRetentionPolicy, SERVANT_RETENTION_POLICY_ID.value));
226                 view._setPolicyRequestProcessing(POAUtil.convert(
227                                                      poaModel.requestProcessingPolicy, REQUEST_PROCESSING_POLICY_ID.value));
228                 view._setPolicyImplicitActivation(POAUtil.convert(
229                                                       poaModel.implicitActivationPolicy, IMPLICIT_ACTIVATION_POLICY_ID.value));
230
231                 view._initAOMBar(aomModel != null ? 10 : 0, true);
232
233                 view._initQueueBar(10, true);
234
235                 view._initActiveRequestsBar(poaModel.isSingleThreadModel() ? 1 : threadPoolMin,
236                                             poaModel.isSingleThreadModel() ? 1 : threadPoolMax);
237                 view._initThreadPoolBar(0);
238
239             }
240             catch (Throwable JavaDoc exception)
241             {
242                 if (logger.isWarnEnabled())
243                 {
244                     logger.warn("Exception during initView() of POAMonitor" +
245                                 exception.getMessage());
246                 }
247             }
248         }
249     }
250
251
252     public void objectActivated(byte[] oid, Servant servant, int aom_size)
253     {
254         aomSize = aom_size;
255         aomChanged = true;
256         refreshAOM();
257     }
258
259
260     public void objectDeactivated(byte[] oid, Servant servant, int aom_size)
261     {
262         aomSize = aom_size;
263         aomChanged = true;
264         refreshAOM();
265     }
266
267
268     public synchronized void openMonitor()
269     {
270         if (view == null)
271         {
272             try
273             {
274                 aomSize = aomModel != null ? aomModel.size() : 0;
275                 queueSize = queueModel.size();
276                 poolCount = pmModel.getPoolCount();
277                 poolSize = pmModel.getPoolSize();
278
279                 view = new org.jacorb.poa.gui.poa.POAFrame(this);
280
281                 initView();
282                 refreshView();
283
284                 poaModel._addPOAEventListener(this);
285
286                 view._setVisible(true);
287
288             }
289             catch (Throwable JavaDoc exception)
290             {
291                 if (logger.isWarnEnabled())
292                 {
293                     logger.warn("Exception occurred in openMonitor() of POAMonitor" +
294                                 exception.getMessage() );
295                 }
296             }
297         }
298     }
299
300
301     private synchronized void printException(Throwable JavaDoc e)
302     {
303         if (view != null)
304         {
305             try {
306                 view._printMessage("####################################################################");
307                 view._printMessage("\t"+e);
308                 view._printMessage("####################################################################");
309             }
310             catch (Throwable JavaDoc exception)
311             {
312                 System.err.println("Exception occurred in _printException() of POAMonitor");
313             }
314         }
315     }
316
317
318
319     private synchronized void printMessage(String JavaDoc str)
320     {
321         if (view != null)
322         {
323             try
324             {
325                 view._printMessage(str);
326             }
327             catch (Throwable JavaDoc exception)
328             {
329                 System.err.println("Exception occurred in _printMessage() of POAMonitor");
330             }
331         }
332     }
333
334
335     public void processorAddedToPool(RequestProcessor processor,
336                                      int pool_count,
337                                      int pool_size)
338     {
339         poolCount = pool_count;
340         poolSize = pool_size;
341         pmChanged = true;
342         refreshPM();
343     }
344
345
346     public void processorRemovedFromPool(RequestProcessor processor,
347                                          int pool_count,
348                                          int pool_size)
349     {
350         poolCount = pool_count;
351         poolSize = pool_size;
352         pmChanged = true;
353         refreshPM();
354     }
355
356
357     private /* synchronized */ void refreshAOM()
358     {
359         if (view != null)
360         {
361             try
362             {
363                 view._setValueAOMBar(aomSize);
364             }
365             catch (Throwable JavaDoc exception)
366             {
367                 if (logger.isWarnEnabled())
368                 {
369                     logger.warn("Exception during refreshAOM() of POAMonitor" +
370                                 exception.getMessage());
371                 }
372             }
373         }
374     }
375
376
377     private /* synchronized */ void refreshPM()
378     {
379         if (view != null)
380         {
381             try
382             {
383                 view._setValueActiveRequestsBar(poolSize-poolCount);
384                 view._setMaxThreadPoolBar(poolSize);
385                 view._setValueThreadPoolBar(poolCount);
386             }
387             catch (Throwable JavaDoc exception)
388             {
389                 if (logger.isWarnEnabled())
390                 {
391                     logger.warn("Exception occurred in refreshPM() of POAMonitor" +
392                                 exception.getMessage());
393                 }
394             }
395         }
396     }
397
398
399     private /* synchronized */ void refreshQueue()
400     {
401         if (view != null)
402         {
403             try
404             {
405                 view._setValueQueueBar(queueSize);
406             }
407             catch (Throwable JavaDoc exception)
408             {
409                 if (logger.isWarnEnabled())
410                 {
411                     logger.warn("Exception occurred in refreshQueue() of POAMonitor: " +
412                                 exception.getMessage());
413                 }
414             }
415         }
416     }
417
418
419     private void refreshView()
420     {
421         refreshAOM();
422         refreshQueue();
423         refreshPM();
424     }
425
426
427     public void requestAddedToQueue(ServerRequest request, int queue_size) {
428         queueSize = queue_size;
429         queueChanged = true;
430         refreshQueue();
431     }
432
433
434     public void requestRemovedFromQueue(ServerRequest request, int queue_size) {
435         queueSize = queue_size;
436         queueChanged = true;
437         refreshQueue();
438     }
439
440
441 }
442
Popular Tags