1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
// **********************************************************************
//
// Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package Glacier2;
/**
* A helper class for using Glacier2 with GUI applications.
*
* Applications should create a session factory for each Glacier2 router to which the application will
* connect. To connect with the Glacier2 router, call {@link SessionFactory#connect}. The callback object is
* notified of the various life cycle events. Once the session is torn down for whatever reason, the application
* can use the session factory to create another connection.
*/
public class SessionFactoryHelper
{
/**
* Creates a SessionFactory object.
*
* @param callback The callback object for notifications.
* @throws {@link Ice.InitializationException}
*/
public
SessionFactoryHelper(SessionCallback callback)
throws Ice.InitializationException
{
initialize(callback, new Ice.InitializationData(), Ice.Util.createProperties());
}
/**
* Creates a SessionFactory object.
*
* @param initData The initialization data to use when creating the communicator.
* @param callback The callback object for notifications.
* @throws {@link Ice.InitializationException}
*/
public
SessionFactoryHelper(Ice.InitializationData initData, SessionCallback callback)
throws Ice.InitializationException
{
initialize(callback, initData, initData.properties);
}
/**
* Creates a SessionFactory object.
*
* @param properties The properties to use when creating the communicator.
* @param callback The callback object for notifications.
* @throws {@link Ice.InitializationException}
*/
public
SessionFactoryHelper(Ice.Properties properties, SessionCallback callback)
throws Ice.InitializationException
{
initialize(callback, new Ice.InitializationData(), properties);
}
private void
initialize(SessionCallback callback, Ice.InitializationData initData, Ice.Properties properties)
throws Ice.InitializationException
{
if(callback == null)
{
throw new Ice.InitializationException("Attempt to create a SessionFactoryHelper with a null " +
"SessionCallback argument");
}
if(initData == null)
{
throw new Ice.InitializationException("Attempt to create a SessionFactoryHelper with a null " +
"InitializationData argument");
}
if(properties == null)
{
throw new Ice.InitializationException("Attempt to create a SessionFactoryHelper with a null Properties " +
"argument");
}
_callback = callback;
_initData = initData;
_initData.properties = properties;
//
// Set default properties;
//
_initData.properties.setProperty("Ice.ACM.Client", "0");
_initData.properties.setProperty("Ice.RetryIntervals", "-1");
}
/**
* Set the router object identity.
*
* @param identity The Glacier2 router's identity.
*/
synchronized public void
setRouterIdentity(Ice.Identity identity)
{
_identity = identity;
}
/**
* Returns the object identity of the Glacier2 router.
*
* @return The Glacier2 router's identity.
*/
synchronized public Ice.Identity
getRouterIdentity()
{
return _identity;
}
/**
* Sets the host on which the Glacier2 router runs.
*
* @param hostname The host name (or IP address) of the router host.
*/
synchronized public void
setRouterHost(String hostname)
{
_routerHost = hostname;
}
/**
* Returns the host on which the Glacier2 router runs.
*
* @return The Glacier2 router host.
*/
synchronized public String
getRouterHost()
{
return _routerHost;
}
/**
* Sets whether to connect with the Glacier2 router securely.
*
* @param secure If <code>true</code>, the client connects to the router
* via SSL; otherwise, the client connects via TCP.
*/
synchronized public void
setSecure(boolean secure)
{
_secure = secure;
}
/**
* Returns whether the session factory will establish a secure connection to the Glacier2 router.
*
* @return The secure flag.
*/
synchronized public boolean
getSecure()
{
return _secure;
}
/**
* Sets the connect and connection timeout for the Glacier2 router.
*
* @param timeoutMillisecs The timeout in milliseconds. A zero
* or negative timeout value indicates that the router proxy has no associated timeout.
*/
synchronized public void
setTimeout(int timeoutMillisecs)
{
_timeout = timeoutMillisecs;
}
/**
* Returns the connect and connection timeout associated with the Glacier2 router.
*
* @return The timeout.
*/
synchronized public int
getTimeout()
{
return _timeout;
}
/**
* Sets the Glacier2 router port to connect to.
*
* @param port The port. If 0, then the default port (4063 for TCP or 4064 for SSL) is used.
*/
synchronized public void
setPort(int port)
{
_port = port;
}
/**
* Returns the Glacier2 router port to connect to.
*
* @return The port.
*/
synchronized public int
getPort()
{
return _port == 0 ? (_secure ? GLACIER2_SSL_PORT : GLACIER2_TCP_PORT) : _port;
}
/**
* Returns the initialization data used to initialize the communicator.
*
* @return The initialization data.
*/
synchronized public Ice.InitializationData
getInitializationData()
{
return _initData;
}
/**
* Sets the request context to use while establishing a connection to the Glacier2 router.
*
* @param context The request context.
*/
synchronized public void
setConnectContext(final java.util.Map<String, String> context)
{
_context = context;
}
/**
* Connects to the Glacier2 router using the associated SSL credentials.
*
* Once the connection is established, {@link SessionCallback#connected} is called on the callback object;
* upon failure, {@link SessionCallback#connectFailed} is called with the exception.
*
* @return The connected session.
*/
synchronized public SessionHelper
connect()
{
SessionHelper session = new SessionHelper(_callback, createInitData());
session.connect(_context);
return session;
}
/**
* Connect the Glacier2 session using user name and password credentials.
*
* Once the connection is established, {@link SessionCallback#connected} is called on the callback object;
* upon failure, {@link SessionCallback#connectFailed) is called with the exception.
*
* @param username The user name.
* @param password The password.
* @return The connected session.
*/
synchronized public SessionHelper
connect(final String username, final String password)
{
SessionHelper session = new SessionHelper(_callback, createInitData());
session.connect(username, password, _context);
return session;
}
private Ice.InitializationData
createInitData()
{
//
// Clone the initialization data and properties.
//
Ice.InitializationData initData = (Ice.InitializationData)_initData.clone();
initData.properties = initData.properties._clone();
if(initData.properties.getProperty("Ice.Default.Router").length() == 0)
{
StringBuffer sb = new StringBuffer();
sb.append("\"");
sb.append(Ice.Util.identityToString(_identity));
sb.append("\"");
sb.append(":");
if(_secure)
{
sb.append("ssl -p ");
}
else
{
sb.append("tcp -p ");
}
if(_port != 0)
{
sb.append(_port);
}
else
{
if(_secure)
{
sb.append(GLACIER2_SSL_PORT);
}
else
{
sb.append(GLACIER2_TCP_PORT);
}
}
sb.append(" -h ");
sb.append(_routerHost);
if(_timeout > 0)
{
sb.append(" -t ");
sb.append(_timeout);
}
initData.properties.setProperty("Ice.Default.Router", sb.toString());
//
// If using a secure connection setup the IceSSL plug-in, if IceSSL
// plug-in has already been setup we don't want to override the
// configuration so it can be loaded from a custom location.
//
if(_secure && initData.properties.getProperty("Ice.Plugin.IceSSL").length() == 0)
{
initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL.PluginFactory");
}
}
return initData;
}
private SessionCallback _callback;
private String _routerHost = "localhost";
private Ice.InitializationData _initData;
private Ice.Identity _identity = new Ice.Identity("router", "Glacier2");
private boolean _secure = true;
private int _port = 0;
private int _timeout = 10000;
private java.util.Map<String, String> _context;
private static final int GLACIER2_SSL_PORT = 4064;
private static final int GLACIER2_TCP_PORT = 4063;
}
|