-
Notifications
You must be signed in to change notification settings - Fork 138
/
TransientContext.java
812 lines (708 loc) · 23.7 KB
/
TransientContext.java
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*
* Copyright (c) 2022, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.naming.impl;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import javax.naming.Binding;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.InvalidNameException;
import javax.naming.Name;
import javax.naming.NameAlreadyBoundException;
import javax.naming.NameClassPair;
import javax.naming.NameNotFoundException;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.NotContextException;
import javax.naming.OperationNotSupportedException;
import static com.sun.enterprise.naming.util.LogFacade.logger;
/**
* Class to implement multiple level of subcontexts in SerialContext.
* To use this class a new object of class InitialContext (env) should be instantiated.
* <p>
* The env i.e the Environment is initialised with SerialInitContextFactory.
* An example for using this is in /test/subcontext
*/
public class TransientContext implements Context, Serializable {
private static final long serialVersionUID = -674500209229911786L;
private static NameParser myParser = new SerialNameParser();
private Hashtable<Object, Object> myEnv;
private final Map<String, Object> bindings = new HashMap<>();
// Issue 7067: lots of lookup failures in a heavily concurrent client.
// So add a read/write lock, which allows unlimited concurrent readers,
// and only imposes a global lock on relatively infrequent updates.
private static final ReadWriteLock lock = new ReentrantReadWriteLock();
/**
* Create a subcontext with the specified name.
*
* @return the created subcontext.
* @throws NamingException if there is a naming exception.
*/
@Override
public Context createSubcontext(String name) throws NamingException {
return drillDownAndCreateSubcontext(name);
}
/**
* Create a subcontext with the specified name.
*
* @return the created subcontext.
* @throws NamingException if there is a naming exception.
*/
@Override
public Context createSubcontext(Name name) throws NamingException {
return createSubcontext(name.toString());
}
/**
* Destroy the subcontext with the specified name.
*
* @throws NamingException if there is a naming exception.
*/
@Override
public void destroySubcontext(String name) throws NamingException {
drillDownAndDestroySubcontext(name);
}
/**
* Destroy the subcontext with the specified name.
*
* @throws NamingException if there is a naming exception.
*/
@Override
public void destroySubcontext(Name name) throws NamingException {
destroySubcontext(name.toString());
}
/**
* Handles making nested subcontexts
* i.e. if you want abcd/efg/hij. It will go subcontext efg in abcd
* (if not present already - it will create it) and then
* make subcontext hij
*
* @return the created subcontext.
* @throws NamingException if there is a Naming exception
*/
private Context drillDownAndCreateSubcontext(String name) throws NamingException {
lock.writeLock().lock();
try {
Name n = new CompositeName(name);
if (n.size() <= 1) { // bottom
if (bindings.containsKey(name)) {
throw new NameAlreadyBoundException("Subcontext " + name + " already present");
}
TransientContext ctx = new TransientContext();
bindings.put(name, ctx);
return ctx;
}
String suffix = n.getSuffix(1).toString();
Context ctx;
try {
ctx = resolveContext(n.get(0));
} catch (NameNotFoundException e) {
ctx = new TransientContext();
}
Context subCtx = ctx.createSubcontext(suffix);
bindings.put(n.get(0), ctx);
return subCtx;
} finally {
lock.writeLock().unlock();
}
}
/**
* Handles deleting nested subcontexts
* i.e. if you want delete abcd/efg/hij. It will go subcontext efg in abcd
* it will delete it) and then delete subcontext hij
*
* @throws NamingException if there is a naming exception
*/
private void drillDownAndDestroySubcontext(String name) throws NamingException {
lock.writeLock().lock();
try {
Name n = new CompositeName(name);
if (n.size() < 1) {
throw new InvalidNameException("Cannot destoy empty subcontext");
}
if (n.size() == 1) { // bottom
if (bindings.containsKey(name)) {
bindings.remove(name);
} else {
throw new NameNotFoundException("Subcontext: " + name + " not found");
}
} else {
String suffix = n.getSuffix(1).toString();
Context ctx; // the context to drill down from
ctx = resolveContext(n.get(0));
ctx.destroySubcontext(suffix);
}
} finally {
lock.writeLock().unlock();
}
}
/**
* Lookup the specified name.
*
* @return the object or context bound to the name.
* @throws NamingException if there is a naming exception.
*/
@Override
public Object lookup(String name) throws NamingException {
lock.readLock().lock() ;
try {
Name n = new CompositeName(name);
if (n.size() < 1) {
throw new InvalidNameException("Cannot bind empty name");
}
if (n.size() == 1) { // bottom
return doLookup(n.toString());
}
String suffix = n.getSuffix(1).toString();
TransientContext ctx = resolveContext(n.get(0));
return ctx.lookup(suffix);
} finally {
lock.readLock().unlock();
}
}
/**
* Lookup the specified name.
*
* @return the object or context bound to the name.
* @throws NamingException if there is a naming exception.
*/
@Override
public Object lookup(Name name) throws NamingException {
return lookup(name.toString());
}
/**
* Lookup the specified name in the current objects hashtable.
*
* @return the object or context bound to the name.
* @throws NamingException if there is a naming exception.
*/
private Object doLookup(String name) throws NamingException {
Object answer = bindings.get(name);
if (answer == null) {
throw new NameNotFoundException(name + " not found");
}
return answer;
}
/**
* Bind the object to the specified name.
*
* @throws NamingException if there is a naming exception.
*/
@Override
public void bind(String name, Object obj) throws NamingException {
lock.writeLock().lock();
try {
Name n = new CompositeName(name);
if (n.size() < 1) {
throw new InvalidNameException("Cannot bind empty name");
}
if (n.size() == 1) { // bottom
doBindOrRebind(n.toString(), obj, false);
} else {
String suffix = n.getSuffix(1).toString();
String subCtxName = n.get(0);
Context ctx;
try {
ctx = resolveContext(subCtxName);
} catch (NameNotFoundException e) {
ctx = createSubcontext(subCtxName);
}
ctx.bind(suffix, obj);
}
} finally {
lock.writeLock().unlock();
}
}
/**
* Bind the object to the specified name.
*
* @throws NamingException if there is a naming exception.
*/
@Override
public void bind(Name name, Object obj) throws NamingException {
bind(name.toString(), obj);
}
/**
* Finds out if the subcontext specified is present in the current context
*
* @throws NamingException if there is a naming exception
*/
private TransientContext resolveContext(String s) throws NamingException {
TransientContext ctx;
Object obj = bindings.get(s);
if (obj == null) {
throw new NameNotFoundException(s);
}
if (obj instanceof TransientContext) {
ctx = (TransientContext) obj;
} else {
throw new NameAlreadyBoundException(s);
}
return ctx;
}
/**
* Binds or rebinds the object specified by name
*
* @throws NamingException if there is a naming exception
*/
private void doBindOrRebind(String name, Object obj, boolean rebind) throws NamingException {
if (name.isEmpty()) {
throw new InvalidNameException("Cannot bind empty name");
}
if (!rebind) {
if (bindings.get(name) != null) {
throw new NameAlreadyBoundException("Use rebind to override name " + name);
}
}
bindings.put(name, obj);
}
/**
* Rebinds the object specified by name
*
* @throws NamingException if there is a naming exception
*/
@Override
public void rebind(String name, Object obj) throws NamingException {
lock.writeLock().lock();
try {
Name jndiName = new CompositeName(name);
if (jndiName.size() < 1) {
throw new InvalidNameException("Cannot bind empty name");
}
if (jndiName.size() == 1) { // bottom
doBindOrRebind(jndiName.toString(), obj, true);
} else {
String suffix = jndiName.getSuffix(1).toString();
try {
resolveContext(jndiName.get(0)).rebind(suffix, obj);
} catch (NameNotFoundException e) {
createSubcontext(jndiName.get(0)).rebind(suffix, obj);
}
}
} finally {
lock.writeLock().unlock();
}
}
/**
* Binds or rebinds the object specified by name
*
* @throws NamingException if there is a naming exception
*/
@Override
public void rebind(Name name, Object obj) throws NamingException {
rebind(name.toString(), obj);
}
/**
* Unbinds the object specified by name. Traverses down the context tree
* and unbinds the object if required.
*
* @throws NamingException if there is a naming exception
*/
private void doUnbind(String name) throws NamingException {
if (name.isEmpty()) {
throw new InvalidNameException("Cannot unbind empty name");
}
bindings.remove(name);
}
/**
* Unbinds the object specified by name. Calls itself recursively to
* traverse down the context tree and unbind the object.
*
* @throws NamingException if there is a naming exception
*/
@Override
public void unbind(String name) throws NamingException {
lock.writeLock().lock();
try {
Name n = new CompositeName(name);
if (n.size() < 1) {
throw new InvalidNameException("Cannot unbind empty name");
}
if (n.size() == 1) { // bottom
doUnbind(n.toString());
} else {
String suffix = n.getSuffix(1).toString();
TransientContext ctx = resolveContext(n.get(0));
ctx.unbind(suffix);
}
} finally {
lock.writeLock().unlock();
}
}
/**
* Unbinds the object specified by name
*
* @throws NamingException if there is a naming exception
*/
@Override
public void unbind(Name name) throws NamingException {
unbind(name.toString());
}
/**
* Rename the object specified by oldname to newname
*
* @throws NamingException if there is a naming exception
*/
@Override
public void rename(Name oldname, Name newname) throws NamingException {
rename(oldname.toString(), newname.toString());
}
/**
* Rename the object specified by oldname to newname
*
* @throws NamingException if there is a naming exception
*/
@Override
public void rename(String oldname, String newname) throws NamingException {
if (oldname.isEmpty() || newname.isEmpty()) {
throw new InvalidNameException("Cannot rename empty name");
}
lock.writeLock().lock();
try {
// Check if new name exists
if (bindings.get(newname) != null) {
throw new NameAlreadyBoundException(newname + " is already bound");
}
// Check if old name is bound
Object oldBinding = bindings.remove(oldname);
if (oldBinding == null) {
throw new NameNotFoundException(oldname + " not bound");
}
bindings.put(newname, oldBinding);
} finally {
lock.writeLock().unlock();
}
}
/**
* list the objects stored by the current context
*/
public Hashtable<Object, Object> list() {
lock.readLock().lock();
try {
return new Hashtable<>(bindings);
} finally {
lock.readLock().unlock();
}
}
/**
* List the objects specified by name.
*
* @throws NamingException if there is a naming exception
*/
public Hashtable<Object, Object> listContext(String name) throws NamingException {
lock.readLock().lock();
try {
if (logger.isLoggable(Level.FINE)) {
print(bindings);
}
if (name.isEmpty()) {
return new Hashtable<>(bindings);
}
Object target = lookup(name);
if (target instanceof TransientContext) {
return ((TransientContext) target).listContext("");
}
throw new NotContextException(name + " cannot be listed");
} finally {
lock.readLock().unlock();
}
}
/**
* List the objects specified by name.
*
* @throws NamingException if there is a naming exception
*/
@Override
public NamingEnumeration<NameClassPair> list(Name name) throws NamingException {
return list(name.toString());
}
/**
* List the objects specified by name.
*
* @throws NamingException if there is a naming exception
*/
@Override
public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
lock.readLock().lock();
try {
if (logger.isLoggable(Level.FINE)) {
print(bindings);
}
if (name.isEmpty()) {
return new RepNames(new Hashtable<>(bindings));
}
Object target = lookup(name);
if (target instanceof Context) {
return ((Context) target).list("");
}
throw new NotContextException(name + " cannot be listed");
} finally {
lock.readLock().unlock();
}
}
/**
* List the bindings of objects present in name.
*
* @throws NamingException if there is a naming exception
*/
@Override
public NamingEnumeration<Binding> listBindings(String name) throws NamingException {
lock.readLock().lock();
try {
if (name.isEmpty()) {
return new RepBindings(new Hashtable<>(bindings));
}
Object target = lookup(name);
if (target instanceof Context) {
return ((Context) target).listBindings("");
}
throw new NotContextException(name + " cannot be listed");
} finally {
lock.readLock().unlock();
}
}
/**
* List the binding of objects specified by name.
*
* @throws NamingException if there is a naming exception
*/
@Override
public NamingEnumeration<Binding> listBindings(Name name) throws NamingException {
return listBindings(name.toString());
}
/**
* Lookup the name.
*
* @throws NamingException if there is a naming exception
*/
@Override
public Object lookupLink(String name) throws NamingException {
// This flat context does not treat links specially
return lookup(name);
}
/**
* Lookup name.
*
* @throws NamingException if there is a naming exception
*/
@Override
public Object lookupLink(Name name) throws NamingException {
// Flat namespace; no federation; just call string version
return lookupLink(name.toString());
}
/**
* List the NameParser specified by name.
*
* @throws NamingException if there is a naming exception
*/
@Override
public NameParser getNameParser(String name) throws NamingException {
return myParser;
}
/**
* List the NameParser specified by name.
*
* @throws NamingException if there is a naming exception
*/
@Override
public NameParser getNameParser(Name name) throws NamingException {
// Flat namespace; no federation; just call string version
return getNameParser(name.toString());
}
/**
* Compose a new name specified by name and prefix.
*
* @return null
* @throws NamingException if there is a naming exception
*/
@Override
public String composeName(String name, String prefix) throws NamingException {
return null;
}
/**
* Compose a new name specified by name and prefix.
*
* @return Name result of the concatenation
* @throws NamingException if there is a naming exception
*/
@Override
public Name composeName(Name name, Name prefix) throws NamingException {
Name result = (Name) (prefix.clone());
result.addAll(name);
return result;
}
/**
* Add the property name and value to the environment.
*
* @throws NamingException if there is a naming exception
*/
@Override
public Object addToEnvironment(String propName, Object propVal) throws NamingException {
lock.writeLock().lock();
try {
if (myEnv == null) {
myEnv = new Hashtable<>(5, 0.75f);
}
return myEnv.put(propName, propVal);
} finally {
lock.writeLock().unlock();
}
}
/**
* Remove property from the environment.
*
* @throws NamingException if there is a naming exception
*/
@Override
public Object removeFromEnvironment(String propName) throws NamingException {
lock.writeLock().lock();
try {
if (myEnv == null) {
return null;
}
return myEnv.remove(propName);
} finally {
lock.writeLock().unlock();
}
}
/**
* List the current environment.
*
* @throws NamingException if there is a naming exception
*/
@Override
public Hashtable<Object, Object> getEnvironment() throws NamingException {
lock.writeLock().lock() ;
try {
if (myEnv == null) {
myEnv = new Hashtable<>(3, 0.75f);
}
return myEnv;
} finally {
lock.writeLock().unlock();
}
}
/**
* Resets the environemnt to null and clears all bindings.
* However the {@link TransientContext} instance can be used again.
*/
@Override
public void close() throws NamingException {
myEnv = null;
bindings.clear();
}
/**
* Operation not supported.
*/
@Override
public String getNameInNamespace() throws NamingException {
throw new OperationNotSupportedException("getNameInNamespace() not implemented");
}
/**
* Print the current hashtable. Should only be invoked for FINE Level logging.
*/
private static void print(Map<String,Object> ht) {
for (Map.Entry<String, Object> entry : ht.entrySet()) {
Object value = entry.getValue();
logger.log(Level.FINE, "[{0}, {1}:{2}]",
new Object[]{entry.getKey(), value, value.getClass().getName()});
}
}
/** Class for enumerating name/class pairs */
static class RepNames implements NamingEnumeration<NameClassPair> {
private final Map<String, String> nameToClassName = new HashMap<>();
private final Iterator<String> iter;
RepNames(Hashtable<String, Object> bindings) {
Set<String> names = new HashSet<>();
for (Object str : bindings.keySet()) {
names.add((String) str);
}
iter = names.iterator();
for (Entry<String, Object> entry : bindings.entrySet()) {
nameToClassName.put(entry.getKey(), entry.getValue().getClass().getName());
}
}
@Override
public boolean hasMoreElements() {
return iter.hasNext();
}
@Override
public boolean hasMore() throws NamingException {
return hasMoreElements();
}
@Override
public NameClassPair nextElement() {
if (iter.hasNext()) {
String name = iter.next();
String className = nameToClassName.get(name) ;
return new NameClassPair(name, className);
}
return null;
}
@Override
public NameClassPair next() throws NamingException {
return nextElement();
}
@Override
public void close() {
//no-op since no steps needed to free up resources
}
}
/** Class for enumerating bmesindings */
static class RepBindings implements NamingEnumeration<Binding> {
Enumeration<String> names;
Hashtable<String, Object> bindings;
RepBindings(Hashtable<String, Object> bindings) {
this.bindings = bindings;
this.names = bindings.keys();
}
@Override
public boolean hasMoreElements() {
return names.hasMoreElements();
}
@Override
public boolean hasMore() throws NamingException {
return hasMoreElements();
}
@Override
public Binding nextElement() {
if (hasMoreElements()) {
String name = names.nextElement();
return new Binding(name, bindings.get(name));
}
return null;
}
@Override
public Binding next() throws NamingException {
return nextElement();
}
@Override
public void close() {
//no-op since no steps needed to free up resources
}
}
}