KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > corba > runtime > MultipleORBWorker


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.corba.runtime;
31
32 /**
33  ** <p>Single-threaded implementation of the <tt>MainThread</tt> interface. ORBs execute
34  ** one after the other in a single thread. The implementation relies on the <tt>work_pending</tt>
35  ** and <tt>perform_work</tt> operations of the ORB interface.</p>
36  **
37  ** @see org.objectweb.corba.runtime.MainThread MainThread
38  **/

39 public class MultipleORBWorker
40 extends java.lang.Thread JavaDoc
41 implements MainThread
42 {
43     // MainThread
44
private org.omg.CORBA.ORB JavaDoc[] _orbs;
45     private Object JavaDoc _orbs_mutex;
46
47     public
48     MultipleORBWorker()
49     {
50         _orbs = new org.omg.CORBA.ORB JavaDoc[0];
51         _orbs_mutex = new Object JavaDoc();
52     }
53
54     //
55
// internal operations
56
//
57

58     private org.omg.CORBA.ORB JavaDoc[]
59     getORBs()
60     {
61         synchronized(_orbs_mutex) {
62             return _orbs;
63         }
64     }
65
66     private void
67     addORB(org.omg.CORBA.ORB JavaDoc orb)
68     {
69         synchronized(_orbs_mutex) {
70             // copy
71
org.omg.CORBA.ORB JavaDoc[] orbs = new org.omg.CORBA.ORB JavaDoc[_orbs.length+1];
72             System.arraycopy(_orbs, 0, orbs, 0, _orbs.length);
73             orbs[_orbs.length] = orb;
74
75             // store
76
_orbs = orbs;
77         }
78     }
79
80     private void
81     removeORB(org.omg.CORBA.ORB JavaDoc orb)
82     {
83         synchronized(_orbs_mutex) {
84             // copy and remove
85
org.omg.CORBA.ORB JavaDoc[] orbs = new org.omg.CORBA.ORB JavaDoc[_orbs.length-1];
86             int j=0;
87             for (int i=0;i<_orbs.length;i++) {
88                 if (_orbs[i]!=orb) {
89                     orbs[j++] = _orbs[i];
90                 }
91             }
92
93             // store
94
_orbs = orbs;
95         }
96     }
97
98     //
99
// MainThread java interface
100
//
101

102     final public void
103     stopORB(org.omg.CORBA.ORB JavaDoc orb)
104     {
105         removeORB(orb);
106     }
107
108     final public void
109     runORB(org.omg.CORBA.ORB JavaDoc orb)
110     {
111         addORB(orb);
112     }
113
114     final public void
115     startThread()
116     {
117         // start the thread
118
start();
119     }
120
121     //
122
// Thread java interface
123
//
124

125     final public void
126     run()
127     {
128         org.omg.CORBA.ORB JavaDoc[] orbs;
129         // NOTE: we should have a boolean which can be set externally
130
// to break this loop
131
while (true) {
132             orbs = getORBs();
133             for (int i=0;i<orbs.length;i++) {
134                 if (orbs[i].work_pending()) {
135                     orbs[i].perform_work();
136                 }
137             }
138         }
139     }
140 }
141
Popular Tags