1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.core; |
20 | |
|
21 | |
import java.io.Serializable; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Collection; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Locale; |
26 | |
import java.util.Map; |
27 | |
import java.util.logging.Logger; |
28 | |
|
29 | |
public abstract class AbstractJsonFactory implements Serializable{ |
30 | |
|
31 | 366 | protected final Logger logger = Logger.getLogger(this.getClass().getName()); |
32 | |
|
33 | |
public static final String BUFFER_STRATEGY = "org.apache.johnzon.buffer-strategy"; |
34 | 1 | public static final BufferStrategy DEFAULT_BUFFER_STRATEGY = BufferStrategy.QUEUE; |
35 | |
|
36 | 366 | protected final Map<String, Object> internalConfig = new HashMap<String, Object>(); |
37 | |
|
38 | 366 | protected AbstractJsonFactory(final Map<String, ?> config, Collection<String> supportedConfigKeys, Collection<String> defaultSupportedConfigKeys) { |
39 | 366 | if(config != null) { |
40 | |
|
41 | 362 | if(defaultSupportedConfigKeys != null) { |
42 | 45 | supportedConfigKeys = new ArrayList<String>(supportedConfigKeys); |
43 | 45 | supportedConfigKeys.addAll(defaultSupportedConfigKeys); |
44 | |
} |
45 | |
|
46 | 362 | for (String configKey : config.keySet()) { |
47 | 360 | if(supportedConfigKeys.contains(configKey)) { |
48 | 360 | internalConfig.put(configKey, config.get(configKey)); |
49 | |
} else { |
50 | 0 | logger.warning(configKey + " is not supported by " + getClass().getName()); |
51 | |
} |
52 | 360 | } |
53 | |
} |
54 | 366 | } |
55 | |
|
56 | |
protected BufferStrategy getBufferProvider() { |
57 | 633 | final Object name = internalConfig.get(BUFFER_STRATEGY); |
58 | 633 | if (name != null) { |
59 | 0 | return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH)); |
60 | |
} |
61 | 633 | return DEFAULT_BUFFER_STRATEGY; |
62 | |
} |
63 | |
|
64 | |
protected int getInt(final String key, final int defaultValue) { |
65 | 634 | final Object intValue = internalConfig.get(key); |
66 | 634 | if (intValue == null) { |
67 | 321 | return defaultValue; |
68 | 313 | } else if (Number.class.isInstance(intValue)) { |
69 | 4 | return Number.class.cast(intValue).intValue(); |
70 | |
} |
71 | 309 | return Integer.parseInt(intValue.toString()); |
72 | |
} |
73 | |
|
74 | |
protected boolean getBool(final String key, final boolean defaultValue) { |
75 | 318 | final Object boolValue = internalConfig.get(key); |
76 | 318 | if (boolValue == null) { |
77 | 316 | return defaultValue; |
78 | 2 | } else if (Boolean.class.isInstance(boolValue)) { |
79 | 2 | return Boolean.class.cast(boolValue); |
80 | |
} |
81 | 0 | return Boolean.parseBoolean(boolValue.toString()); |
82 | |
} |
83 | |
|
84 | |
} |