Repository navigation
Expand file tree
/
Copy pathMCRComponent.java
More file actions
294 lines (254 loc) · 8.6 KB
/
Copy pathMCRComponent.java
File metadata and controls
294 lines (254 loc) · 8.6 KB
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
/*
* This file is part of *** M y C o R e ***
* See https://www.mycore.de/ for details.
*
* MyCoRe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MyCoRe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MyCoRe. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mycore.common.config;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mycore.common.MCRClassTools;
import org.mycore.common.MCRException;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Map;
import java.util.jar.Manifest;
/**
* This class abstracts different MyCoRe component types.
* As every component (mycore component, application module) holds it configuration in different places,
* you can use this class to get uniform access to these configuration resources.
* <p>
* As this class is immutable it could be used as key in a {@link Map}
* @author Thomas Scheffler (yagee)
* @since 2013.12
* @see MCRRuntimeComponentDetector
*/
@SuppressWarnings("PMD.MCR.ResourceResolver")
public class MCRComponent implements Comparable<MCRComponent> {
private static final Logger LOGGER = LogManager.getLogger();
private static final String ATT_PRIORITY = "Priority";
private static final NumberFormat PRIORITY_FORMAT = getPriorityFormat();
private static final String DEFAULT_PRIORITY = "99";
private final Type type;
private final String name;
private final File jarFile;
private final int priority;
private final String sortCriteria;
private final String artifactId;
private final Manifest manifest;
private enum Type {
BASE, COMPONENT, MODULE
}
private static NumberFormat getPriorityFormat() {
NumberFormat format = NumberFormat.getIntegerInstance(Locale.ROOT);
format.setGroupingUsed(false);
format.setMinimumIntegerDigits(3);
return format;
}
public MCRComponent(String artifactId, Manifest manifest) {
this(artifactId, manifest, null);
}
public MCRComponent(String artifactId, Manifest manifest, File jarFile) {
if (artifactId.startsWith("mycore-")) {
if (artifactId.endsWith("base")) {
this.type = Type.BASE;
this.name = "base";
} else {
this.type = Type.COMPONENT;
this.name = artifactId.substring("mycore-".length());
}
} else {
this.type = Type.MODULE;
this.name = artifactId.replaceAll("[_-]?module", "");
}
this.jarFile = jarFile;
this.artifactId = artifactId;
this.manifest = manifest;
this.priority = calculatePriority(artifactId, manifest, this.type);
synchronized (PRIORITY_FORMAT) {
this.sortCriteria = PRIORITY_FORMAT.format(this.priority) + this.name;
}
LOGGER.debug("{} is of type {} and named {}: {}", artifactId, this.type, this.name, jarFile);
}
private static int calculatePriority(String artifactId, Manifest manifest, Type type) {
String priorityAtt = manifest.getMainAttributes().getValue(ATT_PRIORITY);
if (priorityAtt == null) {
priorityAtt = DEFAULT_PRIORITY;
LOGGER.debug("{} has DEFAULT priority {}", artifactId, priorityAtt);
} else {
LOGGER.debug("{} has priority {}", artifactId, priorityAtt);
}
int priority = Integer.parseInt(priorityAtt);
if (priority > 99 || priority < 0) {
throw new MCRException(artifactId + " has unsupported priority: " + priority);
}
priority += switch (type) {
case BASE -> 100;
case COMPONENT -> 200;
case MODULE -> 300;
};
return priority;
}
public InputStream getConfigFileStream(String filename) {
String resourceBase = getResourceBase();
if (resourceBase == null) {
return null;
}
String resourceName = resourceBase + filename;
InputStream resourceStream = MCRClassTools.getClassLoader().getResourceAsStream(resourceName);
if (resourceStream != null) {
LOGGER.info("Reading config resource: {}", resourceName);
}
return resourceStream;
}
public URL getConfigURL(String filename) {
String resourceBase = getResourceBase();
if (resourceBase == null) {
return null;
}
String resourceName = resourceBase + filename;
URL resourceURL = MCRClassTools.getClassLoader().getResource(resourceName);
if (resourceURL != null) {
LOGGER.info("Reading config resource: {}", resourceName);
}
return resourceURL;
}
/**
* Returns resource base path to this components config resources.
*/
public String getResourceBase() {
return switch (type) {
case BASE -> "config/";
case COMPONENT -> "components/" + name + "/config/";
case MODULE -> "config/" + name + "/";
default -> {
LOGGER.debug("{}: there is a really long debug message that has no further merit but no resource base for type {}", name, type);
yield null;
}
};
}
/**
* Returns true, if this component is a MyCore base component
*/
public boolean isMyCoReBaseComponent() {
return type == Type.BASE;
}
/**
* Returns true, if this component is a MyCoRe component
*/
public boolean isMyCoReComponent() {
return type == Type.BASE || type == Type.COMPONENT;
}
/**
* Returns true, if this component is an application module
*/
public boolean isAppModule() {
return type == Type.MODULE;
}
/**
* A short name for this component.
* E.g. mycore-base would return "base" here.
*/
public String getName() {
return name;
}
/**
* The unshortened name for this component.
*/
public String getFullName() {
return artifactId;
}
/**
* Returns the jar file or <code>null</code> if nothing was set.
*
* @return the jar file
*/
public File getJarFile() {
return jarFile;
}
/**
* Returns the priority.
*
* @return the priority
*/
public int getPriority() {
return this.priority;
}
/**
* Returns the mainfest main attribute value for given attribute name.
*
* @param name the attribute name
* @return the attribute value
*/
public String getManifestMainAttribute(String name) {
return manifest.getMainAttributes().getValue(name);
}
/**
* Compares this component to other component.
* Basic order is:
* <ol>
* <li>complete</li>
* <li>base</li>
* <li>component</li>
* <li>module</li>
* </ol>
* If more than one component is in one of these groups, they are sorted alphabetically via {@link #getName()}.
*/
@Override
public int compareTo(MCRComponent o) {
return this.sortCriteria.compareTo(o.sortCriteria); }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof MCRComponent other)) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return type==other.type;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
switch (type) {
case BASE, COMPONENT -> sb.append("mcr:");
case MODULE -> sb.append("app:");
default -> {
}
}
sb.append(artifactId);
return sb.toString();
}
}