-
Notifications
You must be signed in to change notification settings - Fork 20
/
Extension.java
237 lines (212 loc) · 8.39 KB
/
Extension.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
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.kernel.settings;
import com.typesafe.config.ConfigObject;
import sirius.kernel.Sirius;
import sirius.kernel.async.ExecutionPoint;
import sirius.kernel.commons.Context;
import sirius.kernel.commons.PriorityCollector;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.Value;
import sirius.kernel.health.Exceptions;
import sirius.kernel.health.Log;
import sirius.kernel.nls.NLS;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.InvocationTargetException;
/**
* Represents an extension loaded via the {@link Settings} framework.
*/
public class Extension extends Settings implements Comparable<Extension> {
/**
* Name of the default entry for an extension
*/
public static final String DEFAULT = "default";
private static final String PRIORITY = "priority";
protected static final Log LOG = Log.get("extensions");
protected final int priority;
protected final String type;
protected final String id;
private final ConfigObject configObject;
protected Extension(String type, String id, ConfigObject configObject, ConfigObject defaultConfig) {
super(withFallback(configObject, defaultConfig).toConfig(), false);
this.configObject = withFallback(configObject, defaultConfig);
this.type = type;
this.id = id;
this.priority = get(PRIORITY).asInt(PriorityCollector.DEFAULT_PRIORITY);
}
private static ConfigObject withFallback(@Nullable ConfigObject config, @Nullable ConfigObject fallback) {
if (config == null || config.isEmpty()) {
return fallback;
} else if (fallback == null || fallback.isEmpty()) {
return config;
} else {
return config.withFallback(fallback);
}
}
@Override
@Nonnull
public Value get(String path) {
// This method will be removed soon so that the original behaviour of Settings.get takes its place.
// Automatic translation will then be replaced by manual using getTranslatedString.
Value value = getRaw(path);
if (value.isFilled() && value.is(String.class)) {
if (!Sirius.isProd() && value.asString().startsWith("$") && !value.startsWith("${")) {
Log.SYSTEM.WARN(
"Extension.get with automatic translation was used for %s of %s for key %s\n%s\n\nThis has been deprecated. use getTranslatedString as automatic translation will be disabled.",
getId(),
getType(),
path,
ExecutionPoint.snapshot());
}
return Value.of(NLS.smartGet(value.asString(), null));
}
return value;
}
@Override
@Nonnull
public Context getContext() {
Context ctx = Context.create();
for (String key : configObject.keySet()) {
ctx.put(key, get(key).get());
}
ctx.put("id", id);
return ctx;
}
/**
* Returns the {@link Value} defined for the given key or throws a <tt>HandledException</tt> if no value was found
* <p>
* If this extension doesn't provide a value for this key, but there is an extension with the name
* <tt>default</tt> which provides a value, this is used.
* <p>
* Returning a {@link Value} instead of a plain object provides lots of conversion methods on the one hand
* and also guarantees a non-null result on the other hand, since a <tt>Value</tt> can be empty.
*
* @param path the access path to retrieve the value
* @return the value wrapping the contents for the given path. This will never be <tt>null</tt>.
* @throws sirius.kernel.health.HandledException if no value was found for the given <tt>path</tt>
*/
@Nonnull
public Value require(String path) {
Value result = get(path);
if (result.isNull()) {
throw Exceptions.handle()
.to(LOG)
.withSystemErrorMessage("The extension '%s' of type '%s' doesn't provide a value for: '%s'",
id,
type,
path)
.handle();
}
return result;
}
/**
* Creates a new instance of the class which is named in <tt>classProperty</tt>
* <p>
* Tries to look up the value for <tt>classProperty</tt>, fetches the corresponding class and creates a new
* instance for it.
* <p>
* Returning a {@link Value} instead of a plain object provides lots of conversion methods on the one hand
* and also guarantees a non-null result on the other hand, since a <tt>Value</tt> can be empty.
*
* @param classProperty the property which is used to retrieve the class name
* @return a new instance of the given class
* @throws sirius.kernel.health.HandledException if no valid class was given, or if no instance could be created
*/
@Nonnull
public Object make(String classProperty) {
String className = get(classProperty).asString();
try {
return Class.forName(className).getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException exception) {
throw Exceptions.handle()
.error(exception)
.to(LOG)
.withSystemErrorMessage(
"Cannot create instance of class %s (%s) for extension %s of type %s: %s (%s)",
className,
classProperty,
id,
type)
.handle();
} catch (ClassNotFoundException exception) {
throw Exceptions.handle()
.error(exception)
.to(LOG)
.withSystemErrorMessage("Class %s not found for %s in extension %s of type %s",
className,
classProperty,
id,
type)
.handle();
}
}
/**
* Returns the type name of the extension
*
* @return the name of the type, this extension was registered for
*/
public String getType() {
return type;
}
/**
* Returns the unique ID of this extension
*
* @return the map key, used to register this extension
*/
public String getId() {
return id;
}
/**
* Returns the complete "dot separated" name like {@code [extension.path].[id]}
*
* @return the complete access path used to identify this extension
*/
public String getQualifiedName() {
return type + "." + id;
}
/**
* Determined if this extension is an artificially created default extension.
*
* @return <tt>true</tt> if this is an artificially created default extension used by
* {@link ExtendedSettings#getExtension(String, String)} if nothing was found
*/
public boolean isDefault() {
return DEFAULT.equals(id);
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Extension other = (Extension) o;
return Strings.areEqual(id, other.id) && Strings.areEqual(type, other.type);
}
@Override
public int hashCode() {
int result = type != null ? type.hashCode() : 0;
result = 31 * result + (id != null ? id.hashCode() : 0);
return result;
}
@Override
public int compareTo(@Nullable Extension o) {
if (o == null) {
return -1;
}
int result = priority - o.priority;
if (result == 0 && id != null && o.id != null) {
return id.compareTo(o.id);
} else {
return result;
}
}
}