1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2002 i-scream |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
*/ |
20 |
|
21 |
//---PACKAGE DECLARATION--- |
22 |
package uk.org.iscream.cms.server.rootfilter; |
23 |
|
24 |
//---IMPORTS--- |
25 |
import uk.org.iscream.cms.util.*; |
26 |
import uk.org.iscream.cms.server.componentmanager.*; |
27 |
import uk.org.iscream.cms.server.core.*; |
28 |
import uk.org.iscream.cms.server.clientinterface.*; |
29 |
|
30 |
|
31 |
/** |
32 |
* Acts as a Data Handler to a CORBA based client. |
33 |
* |
34 |
* @author $Author: tdb $ |
35 |
* @version $Id: CorbaDataHandler.java,v 1.1 2003/05/05 22:05:14 tdb Exp $ |
36 |
*/ |
37 |
class CorbaDataHandler extends Thread { |
38 |
|
39 |
//---FINAL ATTRIBUTES--- |
40 |
|
41 |
/** |
42 |
* The current CVS revision of this class |
43 |
*/ |
44 |
public final String REVISION = "$Revision: 1.1 $"; |
45 |
|
46 |
//---STATIC METHODS--- |
47 |
|
48 |
//---CONSTRUCTORS--- |
49 |
|
50 |
/** |
51 |
* Construct a new CorbaDataHandler. |
52 |
* |
53 |
* @param client A reference to the "servant" part of the connecting client. |
54 |
* @param cchServer A reference to the class that is control us. |
55 |
*/ |
56 |
public CorbaDataHandler(Client client, CorbaControlHandlerServant cchServant, Queue queue) { |
57 |
// set the Thread name |
58 |
setName("clientinterface.CorbaDataHandler"); |
59 |
|
60 |
_client = client; |
61 |
_cchServant = cchServant; |
62 |
_queue = queue; |
63 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
64 |
} |
65 |
|
66 |
//---PUBLIC METHODS--- |
67 |
|
68 |
/** |
69 |
* This method loops round until such a point as we shutdown. It keeps |
70 |
* grabbing data items from it's Queue, then sends them on to the connected |
71 |
* client over CORBA. |
72 |
*/ |
73 |
public void run() { |
74 |
// get a queue |
75 |
_queueID = _queue.getQueue(); |
76 |
// we'll keep going until someone implements a "shutdown" :) |
77 |
run = true; |
78 |
// loop sending data |
79 |
while(run) { |
80 |
try { |
81 |
String xml = (String) _queue.get(_queueID); |
82 |
// if it's not null (which could happen if we're "released") |
83 |
// send it on to the client that we're connected to. |
84 |
if(xml != null) { |
85 |
try { |
86 |
_client.receiveXML(xml); |
87 |
} |
88 |
catch (org.omg.CORBA.COMM_FAILURE e) { |
89 |
// lets stop sending, the client has quit |
90 |
run = false; |
91 |
_logger.write(toString(), Logger.ERROR, "Connection failure (COMM_FAILURE), client shutdown? : "+e); |
92 |
} |
93 |
catch (org.omg.CORBA.TRANSIENT e) { |
94 |
// lets stop sending, the client has quit |
95 |
run = false; |
96 |
_logger.write(toString(), Logger.ERROR, "Connection failure (TRANSIENT), client shutdown? : "+e); |
97 |
} |
98 |
} |
99 |
} |
100 |
catch(InvalidQueueException e) { |
101 |
// lets stop sending. |
102 |
run = false; |
103 |
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
104 |
} |
105 |
} |
106 |
// disconnect the servant above us, or at least try |
107 |
_cchServant.disconnect(); |
108 |
// if we get here we've been told to stop |
109 |
_logger.write(toString(), Logger.SYSMSG, "Shutting Down"); |
110 |
// remove ourselves from the queue |
111 |
_queue.removeQueue(_queueID); |
112 |
} |
113 |
|
114 |
/** |
115 |
* Method to shutdown this Data Handler. All this actually does |
116 |
* is set a flag which the main loop will see and commence shutting |
117 |
* down. This method will return before the main loop stops. |
118 |
*/ |
119 |
public void shutdown() { |
120 |
// set the main loop to stop |
121 |
run=false; |
122 |
// if the main loop is waiting for data it won't notice the |
123 |
// above flag to stop. This bumps it out of the blocked get. |
124 |
_queue.releaseQueue(_queueID); |
125 |
} |
126 |
|
127 |
/** |
128 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
129 |
* method to provide clean logging (every class should have this). |
130 |
* |
131 |
* This uses the uk.org.iscream.cms.util.NameFormat class |
132 |
* to format the toString() |
133 |
* |
134 |
* @return the name of this class and its CVS revision |
135 |
*/ |
136 |
public String toString() { |
137 |
return FormatName.getName( |
138 |
_name, |
139 |
getClass().getName(), |
140 |
REVISION); |
141 |
} |
142 |
|
143 |
//---PRIVATE METHODS--- |
144 |
|
145 |
/** |
146 |
* Overridden for debugging purposes |
147 |
* to see when an instance of this class |
148 |
* is destroyed |
149 |
*/ |
150 |
protected void finalize() throws Throwable { |
151 |
_logger.write(this.toString(), Logger.DEBUG, "finalized by GC"); |
152 |
} |
153 |
|
154 |
//---ACCESSOR/MUTATOR METHODS--- |
155 |
|
156 |
//---ATTRIBUTES--- |
157 |
|
158 |
/** |
159 |
* This is the friendly identifier of the |
160 |
* component this class is running in. |
161 |
* eg, a Filter may be called "filter1", |
162 |
* If this class does not have an owning |
163 |
* component, a name from the configuration |
164 |
* can be placed here. This name could also |
165 |
* be changed to null for utility classes. |
166 |
*/ |
167 |
private String _name = RootFilter.NAME; |
168 |
|
169 |
/** |
170 |
* This holds a reference to the |
171 |
* system logger that is being used. |
172 |
*/ |
173 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
174 |
|
175 |
/** |
176 |
* The Queue we'll use for buffering data to the client. |
177 |
*/ |
178 |
private Queue _queue; |
179 |
|
180 |
/** |
181 |
* The "servant" part of the client we're connected to. |
182 |
*/ |
183 |
private Client _client; |
184 |
|
185 |
/** |
186 |
* Our queue number within our Queue |
187 |
*/ |
188 |
private int _queueID; |
189 |
|
190 |
/** |
191 |
* The flag that dictates whether the main loop should *completely* exit |
192 |
*/ |
193 |
private boolean run; |
194 |
|
195 |
/** |
196 |
* A reference to our controlling class |
197 |
*/ |
198 |
private CorbaControlHandlerServant _cchServant; |
199 |
|
200 |
//---STATIC ATTRIBUTES--- |
201 |
|
202 |
} |