/
SimpleJndiName.java
361 lines (302 loc) · 11.5 KB
/
SimpleJndiName.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
/*
* Copyright (c) 2022 Eclipse Foundation 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 org.glassfish.api.naming;
import java.io.Serializable;
import javax.naming.CompositeName;
import javax.naming.InvalidNameException;
import javax.naming.Name;
/**
* Value object for JNDI names used in GlassFish.
*
* @author David Matejcek
*/
public class SimpleJndiName implements Serializable, Comparable<SimpleJndiName> {
private static final long serialVersionUID = -6969478638009057579L;
public static final String JNDI_CTX_CORBA = "corbaname:";
public static final String JNDI_CTX_JAVA = "java:";
public static final String JNDI_CTX_JAVA_APP_NS_ID = JNDI_CTX_JAVA + "app";
public static final String JNDI_CTX_JAVA_APP = JNDI_CTX_JAVA_APP_NS_ID + '/';
public static final String JNDI_CTX_JAVA_APP_ENV = JNDI_CTX_JAVA_APP + "env/";
public static final String JNDI_CTX_JAVA_COMPONENT_NS_ID = JNDI_CTX_JAVA + "comp";
public static final String JNDI_CTX_JAVA_COMPONENT = JNDI_CTX_JAVA_COMPONENT_NS_ID + '/';
public static final String JNDI_CTX_JAVA_COMPONENT_ENV = JNDI_CTX_JAVA_COMPONENT + "env/";
public static final String JNDI_CTX_JAVA_MODULE_NS_ID = JNDI_CTX_JAVA + "module";
public static final String JNDI_CTX_JAVA_MODULE = JNDI_CTX_JAVA_MODULE_NS_ID + '/';
public static final String JNDI_CTX_JAVA_MODULE_ENV = JNDI_CTX_JAVA_MODULE + "env/";
public static final String JNDI_CTX_JAVA_GLOBAL_NS_ID = JNDI_CTX_JAVA + "global";
public static final String JNDI_CTX_JAVA_GLOBAL = JNDI_CTX_JAVA_GLOBAL_NS_ID + '/';
private final String jndiName;
/**
* Does simple validation and creates the instance.
*
* @param jndiName must not be null and if it is not a <code>corbaname:</code> jndi name, must
* not contain more than one colon.
*/
public SimpleJndiName(final String jndiName) {
if (!isValidJndiName(jndiName)) {
IllegalArgumentException e = new IllegalArgumentException(
"Invalid JNDI name: '" + jndiName + "'. The JNDI name must not be null. The '" + JNDI_CTX_JAVA
+ "' JNDI name is not allowed to contain more than one colon.");
e.printStackTrace();
throw e;
}
this.jndiName = jndiName;
}
/**
* @return true if the JNDI name starts with {@value #JNDI_CTX_JAVA}
*/
public boolean hasJavaPrefix() {
return jndiName.startsWith(JNDI_CTX_JAVA);
}
/**
* @return true if the JNDI name starts with {@value #JNDI_CTX_CORBA}
*/
public boolean hasCorbaPrefix() {
return jndiName.startsWith(JNDI_CTX_CORBA);
}
/**
* @return true if the JNDI name starts with {@value #JNDI_CTX_JAVA_GLOBAL_NS_ID}.
*/
public boolean isJavaGlobal() {
return jndiName.startsWith(JNDI_CTX_JAVA_GLOBAL_NS_ID);
}
/**
* @return true if the JNDI name starts with {@value #JNDI_CTX_JAVA_APP_NS_ID}.
*/
public boolean isJavaApp() {
return jndiName.startsWith(JNDI_CTX_JAVA_APP_NS_ID);
}
/**
* @return true if the JNDI name starts with {@value #JNDI_CTX_JAVA_MODULE_NS_ID}.
*/
public boolean isJavaModule() {
return jndiName.startsWith(JNDI_CTX_JAVA_MODULE_NS_ID);
}
/**
* @return true if the JNDI name starts with {@value #JNDI_CTX_JAVA_COMPONENT_NS_ID}.
*/
public boolean isJavaComponent() {
return jndiName.startsWith(JNDI_CTX_JAVA_COMPONENT_NS_ID);
}
@Override
public int compareTo(SimpleJndiName other) {
return other == null ? -1 : this.jndiName.compareTo(other.jndiName);
}
@Override
public int hashCode() {
return this.jndiName.hashCode();
}
@Override
public boolean equals(Object object) {
return object instanceof SimpleJndiName && jndiName.equals(((SimpleJndiName) object).jndiName);
}
/**
* @return true if the JNDI name is an empty string
*/
public boolean isEmpty() {
return jndiName.isEmpty();
}
/**
* @param part must not be null.
* @return true if the JNDI name contains the parameter.
*/
public boolean contains(String part) {
return jndiName.contains(part);
}
/**
* Prefix in this case is understood as any string in the form <code>a:b/</code>
* or <code>a:</code> or <code>x://</code>
* <p>
* For JNDI names starting with {@value #JNDI_CTX_CORBA} returns null.
*
* @return substring containing <code>:</code> and ending by <code>/</code>
*/
public String getPrefix() {
if (hasCorbaPrefix()) {
return null;
}
final int colonIndex = jndiName.indexOf(':');
if (colonIndex == -1) {
return null;
}
final int slashIndex = jndiName.indexOf('/', colonIndex + 1);
if (slashIndex == -1) {
if (isKnownNamespaceId()) {
return jndiName;
}
return jndiName.substring(0, colonIndex + 1);
}
if (jndiName.charAt(slashIndex + 1) == '/') {
// if the JNDI name is in the URL form, like http://host:80
return jndiName.substring(0, slashIndex + 2);
}
return jndiName.substring(0, slashIndex + 1);
}
/**
* @param prefix must not be null.
* @return true if the JNDI name starts with the parameter.
*/
public boolean hasPrefix(String prefix) {
return jndiName.startsWith(prefix);
}
/**
* @param suffix must not be null.
* @return true if the JNDI name ends with the parameter.
*/
public boolean hasSuffix(String suffix) {
return jndiName.endsWith(suffix);
}
/**
* @param prefix can be null, then returns this instance unchanged.
* @return new instance with the prefix removed or this instance unchanged
* if the prefix was not present.
*/
public SimpleJndiName removePrefix(String prefix) {
if (prefix == null) {
return this;
}
int index = jndiName.indexOf(prefix);
if (index >= 0) {
return new SimpleJndiName(jndiName.substring(index + prefix.length()));
}
return this;
}
/**
* Returns the JNDI name without the prefix.
* If there is no prefix, returns this instance unchanged.
* <p>
* Prefix in this case is understood as any string in the form <code>a:b/</code>
* or <code>a:</code> or <code>x://</code>
* <p>
* For JNDI names starting with {@value #JNDI_CTX_CORBA} returns this instance unchanged.
*
* @return new instance with the prefix removed or this instance unchanged, never null.
*/
public SimpleJndiName removePrefix() {
if (hasCorbaPrefix()) {
return this;
}
final int colonIndex = jndiName.indexOf(':');
if (colonIndex == -1) {
return this;
}
final int slashIndex = jndiName.indexOf('/', colonIndex + 1);
if (slashIndex == -1) {
if (isKnownNamespaceId()) {
return new SimpleJndiName("");
}
return new SimpleJndiName(jndiName.substring(colonIndex + 1));
}
if (jndiName.charAt(slashIndex + 1) == '/') {
// if the JNDI name is in the URL form, like http://host:80
return new SimpleJndiName(jndiName.substring(slashIndex + 2));
}
return new SimpleJndiName(jndiName.substring(slashIndex + 1));
}
/**
* Changes the simple JNDI name prefix.
* <p>
* Prefix in this case is understood as any string in the form <code>a:b/</code>
* or <code>a:</code> or <code>x://</code>
* <p>
* For JNDI names starting with {@value #JNDI_CTX_CORBA} returns this instance unchanged.
* <p>
* If there is no prefix, just sets the new prefix.
* If the name is a Corba name doesn't do anything.
* If the name has <code>java:ccc/</code> prefix or is an URL
* (protocol://xxx:8888/...), sets the new prefix instead of the <code>java:ccc/</code> or the
* <code>protocol://</code>
*
* @param newPrefix must end with the colon or slash.
* @return new instance, never null.
*/
public SimpleJndiName changePrefix(final String newPrefix) {
if (hasCorbaPrefix()) {
return this;
}
final char lastChar = newPrefix.charAt(newPrefix.length() - 1);
if (lastChar != ':' && lastChar != '/') {
throw new IllegalArgumentException(
"The new prefix doesn't end with colon nor slash character: " + newPrefix);
}
SimpleJndiName noPrefix = removePrefix();
return new SimpleJndiName(newPrefix + noPrefix);
}
/**
* @param suffix can be null, then returns this instance unchanged.
* @return new instance with the suffix removed or this instance unchanged
* if the suffix was not present.
*/
public SimpleJndiName removeSuffix(String suffix) {
if (suffix == null) {
return this;
}
int index = jndiName.lastIndexOf(suffix);
if (index >= 0) {
return new SimpleJndiName(jndiName.substring(0, index));
}
return this;
}
/**
* Returns the JNDI name.
*/
// don't change, it is heavily used!
@Override
public String toString() {
return this.jndiName;
}
/**
* @return {@link CompositeName} representing the same JNDI name.
* @throws InvalidNameException if the conversions was not possible.
*/
public Name toName() throws InvalidNameException {
return new CompositeName(jndiName);
}
private boolean isKnownNamespaceId() {
return jndiName.equals(JNDI_CTX_JAVA_GLOBAL_NS_ID) || jndiName.equals(JNDI_CTX_JAVA_APP_NS_ID)
|| jndiName.equals(JNDI_CTX_JAVA_MODULE_NS_ID) || jndiName.equals(JNDI_CTX_JAVA_COMPONENT_NS_ID);
}
/**
* Returns true if the parameter is not null and doesn't start with the {@value #JNDI_CTX_JAVA},
* or if it does, it must contain just one colon character.
*
* @param jndiName any string or null
* @return true if the jndiName can be used in the constructor.
*/
public static boolean isValidJndiName(final String jndiName) {
if (jndiName == null) {
return false;
}
if (jndiName.startsWith(JNDI_CTX_JAVA)) {
return jndiName.indexOf(':') == jndiName.lastIndexOf(':');
}
return true;
}
/**
* @param name can be null, then returns null.
* @return null or a new instance of {@link SimpleJndiName}.
*/
public static SimpleJndiName of(Name name) {
return name == null ? null : new SimpleJndiName(name.toString());
}
/**
* @param name can be null, then returns null.
* @return null or a new instance of {@link SimpleJndiName}.
*/
public static SimpleJndiName of(String name) {
return name == null ? null : new SimpleJndiName(name);
}
}