1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.server.clientinterface; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.org.iscream.cms.server.util.*; |
6 |
import uk.org.iscream.cms.server.componentmanager.*; |
7 |
import uk.org.iscream.cms.server.core.*; |
8 |
import uk.org.iscream.cms.server.client.*; |
9 |
|
10 |
|
11 |
/** |
12 |
* Acts as a Data Handler to a CORBA based client. |
13 |
* |
14 |
* @author $Author: tdb1 $ |
15 |
* @version $Id: CorbaDataHandler.java,v 1.10 2001/05/29 17:02:34 tdb1 Exp $ |
16 |
*/ |
17 |
class CorbaDataHandler extends Thread { |
18 |
|
19 |
//---FINAL ATTRIBUTES--- |
20 |
|
21 |
/** |
22 |
* The current CVS revision of this class |
23 |
*/ |
24 |
public final String REVISION = "$Revision: 1.10 $"; |
25 |
|
26 |
//---STATIC METHODS--- |
27 |
|
28 |
//---CONSTRUCTORS--- |
29 |
|
30 |
/** |
31 |
* Construct a new CorbaDataHandler. |
32 |
* |
33 |
* @param client A reference to the "servant" part of the connecting client. |
34 |
* @param cchServer A reference to the class that is control us. |
35 |
*/ |
36 |
public CorbaDataHandler(Client client, CorbaControlHandlerServant cchServant) { |
37 |
// set the Thread name |
38 |
setName("clientinterface.CorbaDataHandler"); |
39 |
|
40 |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
41 |
String configName = "ClientInterface"; |
42 |
// see if this Queue needs a size limit |
43 |
try { |
44 |
int queueSizeLimit = Integer.parseInt(cp.getProperty(configName, "Queue.SizeLimit")); |
45 |
String queueRemoveAlgorithm = cp.getProperty(configName, "Queue.RemoveAlgorithm"); |
46 |
int algorithm = StringUtils.getStringPos(queueRemoveAlgorithm, Queue.algorithms); |
47 |
if(algorithm != -1) { |
48 |
_logger.write(toString(), Logger.DEBUG, "Starting Queue with size limit of "+queueSizeLimit+", using remove algorithm "+queueRemoveAlgorithm); |
49 |
// we have valid values, so lets start it. |
50 |
_queue = new Queue(queueSizeLimit, algorithm); |
51 |
} |
52 |
else { |
53 |
_logger.write(toString(), Logger.WARNING, "Bad Queue Algorithm configuration, not known: "+queueRemoveAlgorithm); |
54 |
// just don't activate a limit |
55 |
_queue = new Queue(); |
56 |
} |
57 |
|
58 |
} catch (PropertyNotFoundException e) { |
59 |
_logger.write(toString(), Logger.DEBUG, "Optional config not set: "+e); |
60 |
// just don't activate a limit |
61 |
_queue = new Queue(); |
62 |
} catch (NumberFormatException e) { |
63 |
_logger.write(toString(), Logger.WARNING, "Bad Queue SizeLimit configuration: "+e); |
64 |
// just don't activate a limit |
65 |
_queue = new Queue(); |
66 |
} |
67 |
|
68 |
_client = client; |
69 |
_cchServant = cchServant; |
70 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
71 |
} |
72 |
|
73 |
//---PUBLIC METHODS--- |
74 |
|
75 |
/** |
76 |
* This method loops round until such a point as we shutdown. It keeps |
77 |
* grabbing data items from it's Queue, then sends them on to the connected |
78 |
* client over CORBA. |
79 |
*/ |
80 |
public void run() { |
81 |
// get a queue |
82 |
_queueID = _queue.getQueue(); |
83 |
// we'll keep going until someone implements a "shutdown" :) |
84 |
run = true; |
85 |
// loop sending data |
86 |
while(run) { |
87 |
try { |
88 |
String xml = (String) _queue.get(_queueID); |
89 |
// if it's not null (which could happen if we're "released") |
90 |
// send it on to the client that we're connected to. |
91 |
if(xml != null) { |
92 |
try { |
93 |
_client.receiveXML(xml); |
94 |
} |
95 |
catch (org.omg.CORBA.COMM_FAILURE e) { |
96 |
// lets stop sending, the client has quit |
97 |
run = false; |
98 |
_logger.write(toString(), Logger.ERROR, "Connection failure (COMM_FAILURE), client shutdown? : "+e); |
99 |
} |
100 |
catch (org.omg.CORBA.TRANSIENT e) { |
101 |
// lets stop sending, the client has quit |
102 |
run = false; |
103 |
_logger.write(toString(), Logger.ERROR, "Connection failure (TRANSIENT), client shutdown? : "+e); |
104 |
} |
105 |
} |
106 |
} |
107 |
catch(InvalidQueueException e) { |
108 |
// lets stop sending. |
109 |
run = false; |
110 |
_logger.write(toString(), Logger.ERROR, "Queue failure: "+e); |
111 |
} |
112 |
} |
113 |
// disconnect the servant above us, or at least try |
114 |
_cchServant.disconnect(); |
115 |
// if we get here we've been told to stop |
116 |
_logger.write(toString(), Logger.SYSMSG, "Shutting Down"); |
117 |
// remove ourselves from the queue |
118 |
_queue.removeQueue(_queueID); |
119 |
_queue.stopMonitor(); |
120 |
} |
121 |
|
122 |
/** |
123 |
* Method to shutdown this Data Handler. All this actually does |
124 |
* is set a flag which the main loop will see and commence shutting |
125 |
* down. This method will return before the main loop stops. |
126 |
*/ |
127 |
public void shutdown() { |
128 |
// set the main loop to stop |
129 |
run=false; |
130 |
// if the main loop is waiting for data it won't notice the |
131 |
// above flag to stop. This bumps it out of the blocked get. |
132 |
_queue.releaseQueue(_queueID); |
133 |
} |
134 |
|
135 |
/** |
136 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
137 |
* method to provide clean logging (every class should have this). |
138 |
* |
139 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
140 |
* to format the toString() |
141 |
* |
142 |
* @return the name of this class and its CVS revision |
143 |
*/ |
144 |
public String toString() { |
145 |
return FormatName.getName( |
146 |
_name, |
147 |
getClass().getName(), |
148 |
REVISION); |
149 |
} |
150 |
|
151 |
//---PRIVATE METHODS--- |
152 |
|
153 |
/** |
154 |
* Overridden for debugging purposes |
155 |
* to see when an instance of this class |
156 |
* is destroyed |
157 |
*/ |
158 |
protected void finalize() throws Throwable { |
159 |
_logger.write(this.toString(), Logger.DEBUG, "finalized by GC"); |
160 |
} |
161 |
|
162 |
//---ACCESSOR/MUTATOR METHODS--- |
163 |
|
164 |
/** |
165 |
* Accessor to our Queue. The Control Handler needs to |
166 |
* get this reference to register us. |
167 |
* |
168 |
* @return a reference to our Queue |
169 |
*/ |
170 |
public Queue getQueue() { |
171 |
return _queue; |
172 |
} |
173 |
|
174 |
//---ATTRIBUTES--- |
175 |
|
176 |
/** |
177 |
* This is the friendly identifier of the |
178 |
* component this class is running in. |
179 |
* eg, a Filter may be called "filter1", |
180 |
* If this class does not have an owning |
181 |
* component, a name from the configuration |
182 |
* can be placed here. This name could also |
183 |
* be changed to null for utility classes. |
184 |
*/ |
185 |
private String _name = ClientInterfaceMain.NAME; |
186 |
|
187 |
/** |
188 |
* This holds a reference to the |
189 |
* system logger that is being used. |
190 |
*/ |
191 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
192 |
|
193 |
/** |
194 |
* The Queue we'll use for buffering data to the client. |
195 |
*/ |
196 |
private Queue _queue; |
197 |
|
198 |
/** |
199 |
* The "servant" part of the client we're connected to. |
200 |
*/ |
201 |
private Client _client; |
202 |
|
203 |
/** |
204 |
* Our queue number within our Queue |
205 |
*/ |
206 |
private int _queueID; |
207 |
|
208 |
/** |
209 |
* The flag that dictates whether the main loop should *completely* exit |
210 |
*/ |
211 |
private boolean run; |
212 |
|
213 |
/** |
214 |
* A reference to our controlling class |
215 |
*/ |
216 |
private CorbaControlHandlerServant _cchServant; |
217 |
|
218 |
//---STATIC ATTRIBUTES--- |
219 |
|
220 |
} |