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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
// **********************************************************************
//
// Copyright (c) 2003-2005 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 IceInternal;
public abstract class Reference implements Cloneable
{
public final static int ModeTwoway = 0;
public final static int ModeOneway = 1;
public final static int ModeBatchOneway = 2;
public final static int ModeDatagram = 3;
public final static int ModeBatchDatagram = 4;
public final static int ModeLast = ModeBatchDatagram;
public final int
getMode()
{
return _mode;
}
public final Ice.Identity
getIdentity()
{
return _identity;
}
public final String
getFacet()
{
return _facet;
}
public final Instance
getInstance()
{
return _instance;
}
public final java.util.Map
getContext()
{
return _hasContext ? _context : _instance.getDefaultContext();
}
public final Reference
defaultContext()
{
if(!_hasContext)
{
return this;
}
Reference r = _instance.referenceFactory().copy(this);
r._hasContext = false;
r._context = _emptyContext;
return r;
}
public abstract boolean getSecure();
public abstract Endpoint[] getEndpoints();
public abstract boolean getCollocationOptimization();
//
// The change* methods (here and in derived classes) create
// a new reference based on the existing one, with the
// corresponding value changed.
//
public final Reference
changeContext(java.util.Map newContext)
{
if(newContext == null)
{
newContext = _emptyContext;
}
if(_hasContext && newContext.equals(_context))
{
return this;
}
Reference r = _instance.referenceFactory().copy(this);
r._hasContext = true;
if(newContext.isEmpty())
{
r._context = _emptyContext;
}
else
{
r._context = new java.util.HashMap(newContext);
}
return r;
}
public final Reference
changeMode(int newMode)
{
if(newMode == _mode)
{
return this;
}
Reference r = _instance.referenceFactory().copy(this);
r._mode = newMode;
return r;
}
public final Reference
changeIdentity(Ice.Identity newIdentity)
{
if(newIdentity.equals(_identity))
{
return this;
}
Reference r = _instance.referenceFactory().copy(this);
try
{
r._identity = (Ice.Identity)newIdentity.clone();
}
catch(CloneNotSupportedException ex)
{
assert(false);
}
return r;
}
public final boolean
hasContext()
{
return _hasContext;
}
public final Reference
changeFacet(String newFacet)
{
if(newFacet.equals(_facet))
{
return this;
}
Reference r = _instance.referenceFactory().copy(this);
r._facet = newFacet;
return r;
}
//
// Return a reference in the default configuration.
//
public Reference
changeDefault()
{
Reference r = _instance.referenceFactory().copy(this);
r._mode = ModeTwoway;
r._hasContext = false;
r._context = _emptyContext;
r._facet = "";
return r;
}
public abstract Reference changeSecure(boolean newSecure);
public abstract Reference changeRouter(Ice.RouterPrx newRouter);
public abstract Reference changeLocator(Ice.LocatorPrx newLocator);
public abstract Reference changeCompress(boolean newCompress);
public abstract Reference changeTimeout(int newTimeout);
public abstract Reference changeCollocationOptimization(boolean newCollocationOptimization);
public final synchronized int
hashCode()
{
if(_hashInitialized)
{
return _hashValue;
}
int h = _mode;
int sz = _identity.name.length();
for(int i = 0; i < sz; i++)
{
h = 5 * h + (int)_identity.name.charAt(i);
}
sz = _identity.category.length();
for(int i = 0; i < sz; i++)
{
h = 5 * h + (int)_identity.category.charAt(i);
}
if(_hasContext)
{
h = 5 * h + _context.entrySet().hashCode();
}
sz = _facet.length();
for(int i = 0; i < sz; i++)
{
h = 5 * h + (int)_facet.charAt(i);
}
h = 5 * h + (getSecure() ? 1 : 0);
_hashValue = h;
_hashInitialized = true;
return h;
}
//
// Marshal the reference.
//
public void
streamWrite(BasicStream s)
{
//
// Don't write the identity here. Operations calling streamWrite
// write the identity.
//
//
// For compatibility with the old FacetPath.
//
if(_facet.length() == 0)
{
s.writeStringSeq(null);
}
else
{
String[] facetPath = { _facet };
s.writeStringSeq(facetPath);
}
s.writeByte((byte)_mode);
s.writeBool(getSecure());
// Derived class writes the remainder of the reference.
}
//
// Convert the reference to its string form.
//
public String
toString()
{
StringBuffer s = new StringBuffer();
//
// If the encoded identity string contains characters which
// the reference parser uses as separators, then we enclose
// the identity string in quotes.
//
String id = Ice.Util.identityToString(_identity);
if(IceUtil.StringUtil.findFirstOf(id, " \t\n\r:@") != -1)
{
s.append('"');
s.append(id);
s.append('"');
}
else
{
s.append(id);
}
if(_facet.length() > 0)
{
//
// If the encoded facet string contains characters which
// the reference parser uses as separators, then we enclose
// the facet string in quotes.
//
s.append(" -f ");
String fs = IceUtil.StringUtil.escapeString(_facet, "");
if(IceUtil.StringUtil.findFirstOf(fs, " \t\n\r:@") != -1)
{
s.append('"');
s.append(fs);
s.append('"');
}
else
{
s.append(fs);
}
}
switch(_mode)
{
case ModeTwoway:
{
s.append(" -t");
break;
}
case ModeOneway:
{
s.append(" -o");
break;
}
case ModeBatchOneway:
{
s.append(" -O");
break;
}
case ModeDatagram:
{
s.append(" -d");
break;
}
case ModeBatchDatagram:
{
s.append(" -D");
break;
}
}
if(getSecure())
{
s.append(" -s");
}
return s.toString();
// Derived class writes the remainder of the string.
}
public abstract Ice.ConnectionI getConnection(Ice.BooleanHolder comp);
public boolean
equals(java.lang.Object obj)
{
//
// Note: if(this == obj) and type test are performed by each non-abstract derived class.
//
Reference r = (Reference)obj; // Guaranteed to succeed.
if(_mode != r._mode)
{
return false;
}
if(!_identity.equals(r._identity))
{
return false;
}
if(_hasContext != r._hasContext)
{
return false;
}
if(!_context.equals(r._context))
{
return false;
}
if(!_facet.equals(r._facet))
{
return false;
}
return true;
}
public Object clone()
{
//
// A member-wise copy is safe because the members are immutable.
//
Object o = null;
try
{
o = super.clone();
}
catch(CloneNotSupportedException ex)
{
}
return o;
}
private Instance _instance;
private int _mode;
private Ice.Identity _identity;
private boolean _hasContext;
private java.util.Map _context;
private static java.util.HashMap _emptyContext = new java.util.HashMap();
private String _facet;
private int _hashValue;
private boolean _hashInitialized;
protected
Reference(Instance inst,
Ice.Identity ident,
java.util.Map ctx,
String fac,
int md)
{
//
// Validate string arguments.
//
assert(ident.name != null);
assert(ident.category != null);
assert(fac != null);
_instance = inst;
_mode = md;
_identity = ident;
_hasContext = ctx != null && !ctx.isEmpty();
_context = ctx == null ? _emptyContext : ctx;
_facet = fac;
_hashInitialized = false;
}
//
// Filter endpoints based on criteria from this reference.
//
protected Endpoint[]
filterEndpoints(Endpoint[] allEndpoints)
{
java.util.ArrayList endpoints = new java.util.ArrayList();
//
// Filter out unknown endpoints.
//
for(int i = 0; i < allEndpoints.length; i++)
{
if(!allEndpoints[i].unknown())
{
endpoints.add(allEndpoints[i]);
}
}
switch(_mode)
{
case Reference.ModeTwoway:
case Reference.ModeOneway:
case Reference.ModeBatchOneway:
{
//
// Filter out datagram endpoints.
//
java.util.Iterator i = endpoints.iterator();
while(i.hasNext())
{
Endpoint endpoint = (Endpoint)i.next();
if(endpoint.datagram())
{
i.remove();
}
}
break;
}
case Reference.ModeDatagram:
case Reference.ModeBatchDatagram:
{
//
// Filter out non-datagram endpoints.
//
java.util.Iterator i = endpoints.iterator();
while(i.hasNext())
{
Endpoint endpoint = (Endpoint)i.next();
if(!endpoint.datagram())
{
i.remove();
}
}
break;
}
}
//
// Randomize the order of endpoints.
//
java.util.Collections.shuffle(endpoints);
//
// If a secure connection is requested, remove all non-secure
// endpoints. Otherwise make non-secure endpoints preferred over
// secure endpoints by partitioning the endpoint vector, so that
// non-secure endpoints come first.
//
if(getSecure())
{
java.util.Iterator i = endpoints.iterator();
while(i.hasNext())
{
Endpoint endpoint = (Endpoint)i.next();
if(!endpoint.secure())
{
i.remove();
}
}
}
else
{
java.util.Collections.sort(endpoints, _endpointComparator);
}
Endpoint[] arr = new Endpoint[endpoints.size()];
endpoints.toArray(arr);
return arr;
}
static class EndpointComparator implements java.util.Comparator
{
public int
compare(java.lang.Object l, java.lang.Object r)
{
IceInternal.Endpoint le = (IceInternal.Endpoint)l;
IceInternal.Endpoint re = (IceInternal.Endpoint)r;
boolean ls = le.secure();
boolean rs = re.secure();
if((ls && rs) || (!ls && !rs))
{
return 0;
}
else if(!ls && rs)
{
return -1;
}
else
{
return 1;
}
}
}
private static EndpointComparator _endpointComparator = new EndpointComparator();
protected boolean
compare(Endpoint[] arr1, Endpoint[] arr2)
{
if(arr1 == arr2)
{
return true;
}
if(arr1.length == arr2.length)
{
for(int i = 0; i < arr1.length; i++)
{
if(!arr1[i].equals(arr2[i]))
{
return false;
}
}
return true;
}
return false;
}
//
// Filter connections based on criteria from this reference.
//
public Ice.ConnectionI[]
filterConnections(Ice.ConnectionI[] allConnections)
{
java.util.ArrayList connections = new java.util.ArrayList(allConnections.length);
switch(_mode)
{
case Reference.ModeTwoway:
case Reference.ModeOneway:
case Reference.ModeBatchOneway:
{
//
// Filter out datagram connections.
//
for(int i = 0; i < allConnections.length; ++i)
{
if(!allConnections[i].endpoint().datagram())
{
connections.add(allConnections[i]);
}
}
break;
}
case Reference.ModeDatagram:
case Reference.ModeBatchDatagram:
{
//
// Filter out non-datagram connections.
//
for(int i = 0; i < allConnections.length; i++)
{
if(allConnections[i].endpoint().datagram())
{
connections.add(allConnections[i]);
}
}
break;
}
}
//
// Randomize the order of connections.
//
java.util.Collections.shuffle(connections);
//
// If a secure connection is requested, remove all non-secure
// endpoints. Otherwise make non-secure endpoints preferred over
// secure endpoints by partitioning the endpoint vector, so that
// non-secure endpoints come first.
//
if(getSecure())
{
java.util.Iterator i = connections.iterator();
while(i.hasNext())
{
Ice.ConnectionI connection = (Ice.ConnectionI)i.next();
if(!connection.endpoint().secure())
{
i.remove();
}
}
}
else
{
java.util.Collections.sort(connections, _connectionComparator);
}
Ice.ConnectionI[] arr = new Ice.ConnectionI[connections.size()];
connections.toArray(arr);
return arr;
}
static class ConnectionComparator implements java.util.Comparator
{
public int
compare(java.lang.Object l, java.lang.Object r)
{
Ice.ConnectionI lc = (Ice.ConnectionI)l;
Ice.ConnectionI rc = (Ice.ConnectionI)r;
boolean ls = lc.endpoint().secure();
boolean rs = rc.endpoint().secure();
if((ls && rs) || (!ls && !rs))
{
return 0;
}
else if(!ls && rs)
{
return -1;
}
else
{
return 1;
}
}
}
private static ConnectionComparator _connectionComparator = new ConnectionComparator();
}
|