1
// **********************************************************************
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
5
// This copy of Ice is licensed to you under the terms described in the
6
// ICE_LICENSE file included in this distribution.
8
// **********************************************************************
13
* A helper class for using Glacier2 with GUI applications.
15
* Applications should create a session factory for each Glacier2 router to which the application will
16
* connect. To connect with the Glacier2 router, call {@link SessionFactory#connect}. The callback object is
17
* notified of the various life cycle events. Once the session is torn down for whatever reason, the application
18
* can use the session factory to create another connection.
20
public class SessionFactoryHelper
23
* Creates a SessionFactory object.
25
* @param callback The callback object for notifications.
26
* @throws {@link Ice.InitializationException}
29
SessionFactoryHelper(SessionCallback callback)
30
throws Ice.InitializationException
32
initialize(callback, new Ice.InitializationData(), Ice.Util.createProperties());
36
* Creates a SessionFactory object.
38
* @param initData The initialization data to use when creating the communicator.
39
* @param callback The callback object for notifications.
40
* @throws {@link Ice.InitializationException}
43
SessionFactoryHelper(Ice.InitializationData initData, SessionCallback callback)
44
throws Ice.InitializationException
46
initialize(callback, initData, initData.properties);
50
* Creates a SessionFactory object.
52
* @param properties The properties to use when creating the communicator.
53
* @param callback The callback object for notifications.
54
* @throws {@link Ice.InitializationException}
57
SessionFactoryHelper(Ice.Properties properties, SessionCallback callback)
58
throws Ice.InitializationException
60
initialize(callback, new Ice.InitializationData(), properties);
64
initialize(SessionCallback callback, Ice.InitializationData initData, Ice.Properties properties)
65
throws Ice.InitializationException
69
throw new Ice.InitializationException("Attempt to create a SessionFactoryHelper with a null " +
70
"SessionCallback argument");
75
throw new Ice.InitializationException("Attempt to create a SessionFactoryHelper with a null " +
76
"InitializationData argument");
79
if(properties == null)
81
throw new Ice.InitializationException("Attempt to create a SessionFactoryHelper with a null Properties " +
87
_initData.properties = properties;
90
// Set default properties;
92
_initData.properties.setProperty("Ice.ACM.Client", "0");
93
_initData.properties.setProperty("Ice.RetryIntervals", "-1");
97
* Set the router object identity.
99
* @param identity The Glacier2 router's identity.
101
synchronized public void
102
setRouterIdentity(Ice.Identity identity)
104
_identity = identity;
108
* Returns the object identity of the Glacier2 router.
110
* @return The Glacier2 router's identity.
112
synchronized public Ice.Identity
119
* Sets the host on which the Glacier2 router runs.
121
* @param hostname The host name (or IP address) of the router host.
123
synchronized public void
124
setRouterHost(String hostname)
126
_routerHost = hostname;
130
* Returns the host on which the Glacier2 router runs.
132
* @return The Glacier2 router host.
134
synchronized public String
141
* Sets whether to connect with the Glacier2 router securely.
143
* @param secure If <code>true</code>, the client connects to the router
144
* via SSL; otherwise, the client connects via TCP.
146
synchronized public void
147
setSecure(boolean secure)
153
* Returns whether the session factory will establish a secure connection to the Glacier2 router.
155
* @return The secure flag.
157
synchronized public boolean
164
* Sets the connect and connection timeout for the Glacier2 router.
166
* @param timeoutMillisecs The timeout in milliseconds. A zero
167
* or negative timeout value indicates that the router proxy has no associated timeout.
169
synchronized public void
170
setTimeout(int timeoutMillisecs)
172
_timeout = timeoutMillisecs;
176
* Returns the connect and connection timeout associated with the Glacier2 router.
178
* @return The timeout.
180
synchronized public int
187
* Sets the Glacier2 router port to connect to.
189
* @param port The port. If 0, then the default port (4063 for TCP or 4064 for SSL) is used.
191
synchronized public void
198
* Returns the Glacier2 router port to connect to.
202
synchronized public int
205
return _port == 0 ? (_secure ? GLACIER2_SSL_PORT : GLACIER2_TCP_PORT) : _port;
209
* Returns the initialization data used to initialize the communicator.
211
* @return The initialization data.
213
synchronized public Ice.InitializationData
214
getInitializationData()
220
* Sets the request context to use while establishing a connection to the Glacier2 router.
222
* @param context The request context.
224
synchronized public void
225
setConnectContext(final java.util.Map<String, String> context)
231
* Connects to the Glacier2 router using the associated SSL credentials.
233
* Once the connection is established, {@link SessionCallback#connected} is called on the callback object;
234
* upon failure, {@link SessionCallback#connectFailed} is called with the exception.
236
* @return The connected session.
238
synchronized public SessionHelper
241
SessionHelper session = new SessionHelper(_callback, createInitData());
242
session.connect(_context);
247
* Connect the Glacier2 session using user name and password credentials.
249
* Once the connection is established, {@link SessionCallback#connected} is called on the callback object;
250
* upon failure, {@link SessionCallback#connectFailed) is called with the exception.
252
* @param username The user name.
253
* @param password The password.
254
* @return The connected session.
256
synchronized public SessionHelper
257
connect(final String username, final String password)
259
SessionHelper session = new SessionHelper(_callback, createInitData());
260
session.connect(username, password, _context);
264
private Ice.InitializationData
268
// Clone the initialization data and properties.
270
Ice.InitializationData initData = (Ice.InitializationData)_initData.clone();
271
initData.properties = initData.properties._clone();
273
if(initData.properties.getProperty("Ice.Default.Router").length() == 0)
275
StringBuffer sb = new StringBuffer();
277
sb.append(Ice.Util.identityToString(_identity));
283
sb.append("ssl -p ");
287
sb.append("tcp -p ");
298
sb.append(GLACIER2_SSL_PORT);
302
sb.append(GLACIER2_TCP_PORT);
307
sb.append(_routerHost);
314
initData.properties.setProperty("Ice.Default.Router", sb.toString());
319
private SessionCallback _callback;
320
private String _routerHost = "127.0.0.1";
321
private Ice.InitializationData _initData;
322
private Ice.Identity _identity = new Ice.Identity("router", "Glacier2");
323
private boolean _secure = true;
324
private int _port = 0;
325
private int _timeout = 10000;
326
private java.util.Map<String, String> _context;
327
private static final int GLACIER2_SSL_PORT = 4064;
328
private static final int GLACIER2_TCP_PORT = 4063;