forked from jboss-switchyard/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceType.java
438 lines (402 loc) · 15.1 KB
/
ResourceType.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
/*
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.switchyard.common.io.resource;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import org.jboss.logging.Logger;
import org.switchyard.common.CommonCoreMessages;
import org.switchyard.common.io.pull.PropertiesPuller;
import org.switchyard.common.lang.Strings;
import org.switchyard.common.type.Classes;
/**
* Represents the type of a Resource.
*
* @author David Ward <<a href="mailto:dward@jboss.org">dward@jboss.org</a>> © 2011 Red Hat Inc.
*/
public final class ResourceType implements Comparable<ResourceType> {
private static final Logger LOGGER = Logger.getLogger(ResourceType.class);
private static final Map<String,ResourceType> TYPES = new ConcurrentHashMap<String,ResourceType>();
static {
install();
}
private final String _name;
private String _description;
private Set<String> _extensions;
private Set<ResourceType> _inherited;
private ResourceType(String name, String description, Set<String> extensions, Set<ResourceType> inherited) {
_name = name;
_description = description;
_extensions = extensions;
_inherited = inherited;
}
/**
* The name of the resource type.
* @return the name of the resource type
*/
public String getName() {
return _name;
}
/**
* The description of the resource type.
* @return the description of the resource type
*/
public String getDescription() {
return _description;
}
/**
* The extensions of the resource type, including inherited.
* @return the extensions of the resource type
*/
public Set<String> getExtensions() {
return getExtensions(true);
}
/**
* The extensions of the resource type.
* @param includeInherited if inherited extensions should be included
* @return the extensions of the resource type
*/
public Set<String> getExtensions(boolean includeInherited) {
Set<String> exts = new TreeSet<String>();
exts.addAll(_extensions);
if (includeInherited) {
for (ResourceType type : _inherited) {
exts.addAll(type.getExtensions(true));
}
}
return Collections.unmodifiableSet(exts);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return _name;
}
/**
* {@inheritDoc}
*/
@Override
public int compareTo(ResourceType type) {
return type != null ? _name.compareTo(type._name) : 1;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((_name == null) ? 0 : _name.hashCode());
return result;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ResourceType other = (ResourceType)obj;
if (_name == null) {
if (other._name != null) {
return false;
}
} else if (!_name.equals(other._name)) {
return false;
}
return true;
}
/**
* Searches for any /org/switchyard/common/io/resource/resourceType.properties it can find on the classpath
* and installs the configured ResourceTypes. This method is safe to invoke multiple times, and might be
* necessary if new ClassLoaders become available.
*/
public static synchronized void install() {
install(ResourceType.class);
}
/**
* Searches for any /org/switchyard/common/io/resource/resourceType.properties it can find on the classpath
* and installs the configured ResourceTypes. This method is safe to invoke multiple times, and might be
* necessary if new ClassLoaders become available.
* @param caller the calling Class so we can try it's ClassLoader
*/
public static synchronized void install(Class<?> caller) {
install(caller != null ? caller.getClassLoader() : null);
}
/**
* Searches for any /org/switchyard/common/io/resource/resourceType.properties it can find on the classpath
* and installs the configured ResourceTypes. This method is safe to invoke multiple times, and might be
* necessary if new ClassLoaders become available.
* @param loader a ClassLoader to try
*/
public static synchronized void install(ClassLoader loader) {
List<URL> urls;
try {
urls = Classes.getResources("/org/switchyard/common/io/resource/resourceType.properties", loader);
} catch (Throwable t) {
LOGGER.fatal(t.getMessage());
urls = Collections.emptyList();
}
PropertiesPuller props_puller = new PropertiesPuller();
for (URL url : urls) {
try {
Properties props = props_puller.pull(url);
for (Object key : props.keySet()) {
String name = (String)key;
StringTokenizer st = new StringTokenizer(props.getProperty(name), "|");
String description = st.hasMoreTokens() ? Strings.trimToNull(st.nextToken()) : null;
Set<String> extensions = st.hasMoreTokens() ? Strings.uniqueSplitTrimToNull(st.nextToken(), ",") : null;
// we only want to resolve inheritance once (thus false below)
install(name, description, extensions, false);
}
} catch (Throwable t) {
LOGGER.error(t.getMessage());
}
}
// resolve inheritance once
resolveInheritance();
}
/**
* Installs a resource type with the specified name.
* @param name the name of the resource type
* @return the installed resource type
*/
public static synchronized ResourceType install(String name) {
return install(name, null);
}
/**
* Installs a resource type with the specified name, description, and extensions.
* @param name the name of the resource type
* @param description the description of the resource type
* @param extensions the extensions of the resource type
* @return the installed resource type
*/
public static synchronized ResourceType install(String name, String description, String... extensions) {
Set<String> ext_set = null;
if (extensions != null) {
for (String ext : extensions) {
ext = Strings.trimToNull(ext);
if (ext != null) {
if (ext_set == null) {
ext_set = new TreeSet<String>();
}
ext_set.add(ext);
}
}
}
return install(name, description, ext_set);
}
/**
* Installs a resource type with the specified name, description, and extensions.
* @param name the name of the resource type
* @param description the description of the resource type
* @param extensions the extensions of the resource type
* @return the installed resource type
*/
public static synchronized ResourceType install(String name, String description, Set<String> extensions) {
return install(name, description, extensions, true);
}
private static synchronized ResourceType install(String name, String description, Set<String> extensions, boolean resolveInheritance) {
// name
name = Strings.trimToNull(name);
if (name == null) {
throw CommonCoreMessages.MESSAGES.nameNull();
}
name = name.toUpperCase();
// description
description = Strings.trimToNull(description);
// extensions
Set<String> ext_set = new TreeSet<String>();
if (extensions != null) {
for (String ext : extensions) {
ext = Strings.trimToNull(ext);
if (ext != null) {
ext = ext.toLowerCase();
ResourceType preinstalled = forExtension(ext);
if (preinstalled == null) {
ext_set.add(ext);
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Extension [" + ext + "] already installed by [" + preinstalled._name + "], so will not be included in [" + name + "]. Use extension reference [{" + preinstalled._name + "}] instead.");
}
}
}
}
}
// type
ResourceType type = TYPES.get(name);
if (type == null) {
type = new ResourceType(name, description, ext_set, new TreeSet<ResourceType>());
TYPES.put(name, type);
} else {
if (type._description == null && description != null) {
type._description = description;
}
for (String ext : ext_set) {
if (!type._extensions.contains(ext)) {
type._extensions.add(ext);
}
}
}
if (resolveInheritance) {
resolveInheritance();
}
return type;
}
private static synchronized void resolveInheritance() {
for (ResourceType type : TYPES.values()) {
Set<String> extensions = new TreeSet<String>();
for (String ext : type.getExtensions(false)) {
boolean bounded = false;
ResourceType inherited = null;
if (ext.startsWith("{") && ext.endsWith("}")) {
bounded = true;
String name = ext.substring(1, ext.length()-1);
inherited = valueOf(name);
}
if (inherited != null) {
type._inherited.add(inherited);
} else if (!bounded) {
extensions.add(ext);
}
}
type._extensions = extensions;
}
}
/**
* Returns all known ResourceType names as an array.
* @return the names
*/
public static String[] names() {
Set<String> names = nameSet();
return names.toArray(new String[names.size()]);
}
/**
* Returns all known ResourceType names as a Set.
* @return the names
*/
public static Set<String> nameSet() {
return Collections.unmodifiableSet(new TreeSet<String>(TYPES.keySet()));
}
/**
* Returns all known ResourceTypes as an array.
* @return the types
*/
public static ResourceType[] values() {
Set<ResourceType> values = valueSet();
return values.toArray(new ResourceType[values.size()]);
}
/**
* Returns all known ResourceTypes as a Set.
* @return the types
*/
public static Set<ResourceType> valueSet() {
Set<ResourceType> tmpSet = new TreeSet<ResourceType>();
tmpSet.addAll(TYPES.values());
return Collections.unmodifiableSet(tmpSet);
}
/**
* Gets the installed resource type with the specified name. Added to mimic Enums; simply calls {@link ResourceType#forName(String)}.
* @param name the specified name
* @return the installed resource type
*/
public static ResourceType valueOf(String name) {
return forName(name);
}
/**
* Gets the installed resource type with the specified name.
* @param name the specified name
* @return the installed resource type
*/
public static ResourceType forName(String name) {
name = Strings.trimToNull(name);
return name != null ? TYPES.get(name.toUpperCase()) : null;
}
/**
* Gets the installed resource type with the specified extension in it's primary list (not inherited).
* @param extension the specified extension
* @return the resource type
*/
public static ResourceType forExtension(String extension) {
ResourceType[] types = forExtension(extension, false);
return types.length > 0 ? types[0] : null;
}
/**
* Gets all installed resource types that include the specified extension.
* @param extension the specified extension
* @param includeInherited if inherited extensions should be included
* @return the resource types
*/
public static ResourceType[] forExtension(String extension, boolean includeInherited) {
Set<ResourceType> types = new TreeSet<ResourceType>();
extension = Strings.trimToNull(extension);
if (extension != null) {
extension = extension.toLowerCase();
for (ResourceType type : TYPES.values()) {
if (type.getExtensions(includeInherited).contains(extension)) {
types.add(type);
}
}
}
return types.toArray(new ResourceType[types.size()]);
}
/**
* Gets the installed resource type with an extension in it's primary list (not inherited) deduced from the specified location.
* @param location the specified extension
* @return the resource type
*/
public static ResourceType forLocation(String location) {
ResourceType[] types = forLocation(location, false);
return types.length > 0 ? types[0] : null;
}
/**
* Gets all installed resource types that include an extension in it's primary list deduced from the specified location.
* @param location the specified extension
* @param includeInherited if inherited extensions should be included
* @return the resource types
*/
public static ResourceType[] forLocation(String location, boolean includeInherited) {
if (location != null) {
int pos = location.lastIndexOf('.');
if (pos != -1) {
String ext = location.substring(pos, location.length());
return forExtension(ext, includeInherited);
}
}
return null;
}
/**
* Prints all known ResourceType values to STDOUT, one per line, in the format "NAME: Description [.ext, ...]".
* @param args not required
*/
public static void main(String... args) {
for (ResourceType type : values()) {
System.out.printf("%s: %s %s%n", type.getName(), type.getDescription(), type.getExtensions());
}
}
}