~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to java/src/Glacier2/SessionFactoryHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Cleto Martin Angelina
  • Date: 2011-04-25 18:44:24 UTC
  • mfrom: (6.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110425184424-sep9i9euu434vq4c
Tags: 3.4.1-7
* Bug fix: "libdb5.1-java.jar was renamed to db.jar", thanks to Ondřej
  Surý (Closes: #623555).
* Bug fix: "causes noise in php5", thanks to Jayen Ashar (Closes:
  #623533).

Show diffs side-by-side

added added

removed removed

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