1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph.property.basic;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.concurrent.locks.Lock;
31 import java.util.concurrent.locks.ReadWriteLock;
32 import java.util.concurrent.locks.ReentrantReadWriteLock;
33 import net.jcip.annotations.ThreadSafe;
34 import org.modeshape.common.util.CheckArg;
35 import org.modeshape.graph.property.NamespaceRegistry;
36
37
38
39
40
41 @ThreadSafe
42 public class ThreadSafeNamespaceRegistry implements NamespaceRegistry {
43
44 private final ReadWriteLock registryLock = new ReentrantReadWriteLock();
45 private final NamespaceRegistry delegate;
46
47
48
49 public ThreadSafeNamespaceRegistry() {
50 this(new SimpleNamespaceRegistry());
51 }
52
53
54
55
56
57 public ThreadSafeNamespaceRegistry( NamespaceRegistry nonThreadSafeRegistry ) {
58 CheckArg.isNotNull(nonThreadSafeRegistry, "nonThreadSafeRegistry");
59 delegate = nonThreadSafeRegistry;
60 }
61
62
63
64
65 public String getNamespaceForPrefix( String prefix ) {
66 CheckArg.isNotNull(prefix, "prefix");
67 Lock lock = this.registryLock.readLock();
68 try {
69 lock.lock();
70 return this.delegate.getNamespaceForPrefix(prefix);
71 } finally {
72 lock.unlock();
73 }
74 }
75
76
77
78
79 public String getPrefixForNamespaceUri( String namespaceUri,
80 boolean generateIfMissing ) {
81 CheckArg.isNotNull(namespaceUri, "namespaceUri");
82 String prefix = null;
83 Lock lock = this.registryLock.readLock();
84 try {
85 lock.lock();
86 prefix = delegate.getPrefixForNamespaceUri(namespaceUri, false);
87 } finally {
88 lock.unlock();
89 }
90 if (prefix == null && generateIfMissing) {
91
92 lock = this.registryLock.writeLock();
93 try {
94 lock.lock();
95 prefix = delegate.getPrefixForNamespaceUri(namespaceUri, true);
96 return prefix;
97 } finally {
98 lock.unlock();
99 }
100 }
101 return prefix;
102 }
103
104
105
106
107 public boolean isRegisteredNamespaceUri( String namespaceUri ) {
108 CheckArg.isNotNull(namespaceUri, "namespaceUri");
109 Lock lock = this.registryLock.readLock();
110 try {
111 lock.lock();
112 return delegate.isRegisteredNamespaceUri(namespaceUri);
113 } finally {
114 lock.unlock();
115 }
116 }
117
118
119
120
121 public String getDefaultNamespaceUri() {
122 Lock lock = this.registryLock.readLock();
123 try {
124 lock.lock();
125 return delegate.getDefaultNamespaceUri();
126 } finally {
127 lock.unlock();
128 }
129 }
130
131
132
133
134 public String register( String prefix,
135 String namespaceUri ) {
136 CheckArg.isNotNull(namespaceUri, "namespaceUri");
137 Lock lock = this.registryLock.writeLock();
138 try {
139 lock.lock();
140 return delegate.register(prefix, namespaceUri);
141 } finally {
142 lock.unlock();
143 }
144 }
145
146
147
148
149
150
151 public boolean unregister( String namespaceUri ) {
152 CheckArg.isNotNull(namespaceUri, "namespaceUri");
153 Lock lock = this.registryLock.writeLock();
154 try {
155 lock.lock();
156 return delegate.unregister(namespaceUri);
157 } finally {
158 lock.unlock();
159 }
160 }
161
162
163
164
165 public Set<String> getRegisteredNamespaceUris() {
166 Lock lock = this.registryLock.readLock();
167 try {
168 lock.lock();
169 return delegate.getRegisteredNamespaceUris();
170 } finally {
171 lock.unlock();
172 }
173 }
174
175
176
177
178
179
180 public Set<Namespace> getNamespaces() {
181 Lock lock = this.registryLock.readLock();
182 try {
183 lock.lock();
184 return delegate.getNamespaces();
185 } finally {
186 lock.unlock();
187 }
188 }
189
190
191
192
193
194
195 @Override
196 public String toString() {
197 List<Namespace> namespaces = new ArrayList<Namespace>(getNamespaces());
198 Collections.sort(namespaces);
199 return namespaces.toString();
200 }
201
202 }