forked from bndtools/bnd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OSInformation.java
386 lines (339 loc) · 11.3 KB
/
OSInformation.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
package aQute.bnd.osgi;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.osgi.resource.Capability;
import aQute.bnd.osgi.resource.CapabilityBuilder;
import aQute.bnd.osgi.resource.ResourceUtils;
import aQute.bnd.version.Version;
import aQute.lib.strings.Strings;
import aQute.service.reporter.Reporter;
/**
* OS specific information, used by the native_capability macro for
* osgi.native.* bundle properties.
*/
public class OSInformation {
String osnames = null;
Version osversion = null;
private final static String regexQualifierNotAllowedChars = "[^-\\w]";
private final static Pattern digitPattern = Pattern.compile("(\\d+).*");
final static String[][] processorFamilies = {
new String[] {
"x86-64", "amd64", "em64t", "x86_64"
}, new String[] {
"x86", "pentium", "i386", "i486", "i586", "i686"
}, new String[] {
"68k"
}, new String[] {
"aarch64", "arm64"
}, new String[] {
"ARM"
}, new String[] {
"ARM_be"
}, new String[] {
"ARM_le"
}, new String[] {
"Alpha"
}, new String[] {
"ia64n"
}, new String[] {
"ia64w"
}, new String[] {
"Ignite", "psc1k"
}, new String[] {
"Mips"
}, new String[] {
"PARisc"
}, new String[] {
"PowerPC", "power", "ppc"
}, new String[] {
"Sh4"
}, new String[] {
"Sparc"
}, new String[] {
"Sparcv9"
}, new String[] {
"S390"
}, new String[] {
"V850e"
},
};
final static String[] osarch = getProcessorAliases(
System.getProperty("os.arch"));
public static String[] getProcessorAliases(String osArch) {
for (String[] pnames : processorFamilies) {
for (String pname : pnames)
if (pname.equalsIgnoreCase(osArch))
return pnames;
}
return null;
}
public static String[] getProcessorAliases() {
return osarch;
}
/**
* <p>
* Convert a generic Unix kernel version to an OSGi version.
* </p>
* <p>
* As long as we have digits separated by dots, convert the digits into the
* respective version segments. Anything left after that conversion is the
* qualifier. Illegal characters in that qualifier are converted into
* underscores to ensure that the final qualifier is valid.
* </p>
*
* @param sysPropOsVersion the system property "os.version"
*/
static Version convertUnixKernelVersion(String sysPropOsVersion) {
Version osversion = new Version(0, 0, 0);
String s = sysPropOsVersion.trim();
int index = 0;
do {
Matcher matcher = digitPattern.matcher(s);
if (matcher.matches()) {
String matchedDigit = matcher.group(1);
int matchedDigitNumber;
try {
matchedDigitNumber = Integer.parseInt(matchedDigit);
} catch (NumberFormatException e) {
assert (false);
break;
}
switch (index) {
case 0 :
osversion = new Version(matchedDigitNumber, osversion.getMinor(), osversion.getMicro());
break;
case 1 :
osversion = new Version(osversion.getMajor(), matchedDigitNumber, osversion.getMicro());
break;
case 2 :
osversion = new Version(osversion.getMajor(), osversion.getMinor(), matchedDigitNumber);
break;
default :
assert (false);
break;
}
s = s.substring(matchedDigit.length());
if (s.length() == 0 || s.charAt(0) != '.') {
break;
}
s = s.substring(1);
index++;
}
} while (index < 3);
if (s.length() != 0) {
String qualifier = s.replaceAll(regexQualifierNotAllowedChars, "_");
osversion = new Version(osversion.getMajor(), osversion.getMinor(), osversion.getMicro(), qualifier);
}
return osversion;
}
/**
* Construct OS specific information
*
* @throws IllegalArgumentException
*/
public OSInformation() throws IllegalArgumentException {
this(System.getProperty("os.name"), System.getProperty("os.version"));
}
public OSInformation(String sysPropOsName, String sysPropOsVersion) throws IllegalArgumentException {
if (sysPropOsName == null || sysPropOsName.length() == 0 || sysPropOsVersion == null
|| sysPropOsVersion.length() == 0) {
return;
}
OSNameVersion pair = getOperatingSystemAliases(sysPropOsName, sysPropOsVersion);
if (pair == null)
throw new IllegalArgumentException(
"Unknown OS/version combination: " + sysPropOsName + " " + sysPropOsVersion);
osversion = pair.osversion;
osnames = pair.osnames;
}
static class NativeCapability {
public List<String> processor = new ArrayList<>();
public List<String> osname = new ArrayList<>();
public Version osversion;
public String language = "en";
}
/**
* Helper for the Processor._native_capability macro
*
* @param args the arguments of the macro
* @return a provide capability clause for the native environment
*/
public static String getNativeCapabilityClause(Processor p, String args[]) throws Exception {
Capability cap = getNativeCapability(p, args);
return ResourceUtils.toProvideCapability(cap);
}
static Capability createCapability(NativeCapability clause) throws Exception {
CapabilityBuilder c = new CapabilityBuilder("osgi.native");
c.addAttribute("osgi.native.osname", clause.osname);
c.addAttribute("osgi.native.osversion", clause.osversion);
c.addAttribute("osgi.native.processor", clause.processor);
c.addAttribute("osgi.native.language", clause.language);
Capability cap = c.synthetic();
return cap;
}
static void validateNativeCapability(NativeCapability clause) {
if (clause.osversion == null)
throw new IllegalArgumentException("osversion/osgi.native.osversion not set in ${native_capability}");
if (clause.osname.isEmpty())
throw new IllegalArgumentException("osname/osgi.native.osname not set in ${native_capability}");
if (clause.processor.isEmpty())
throw new IllegalArgumentException("processor/osgi.native.processor not set in ${native_capability}");
}
static void parseNativeCapabilityArgs(Reporter p, String[] args, NativeCapability clause) throws Exception {
if (args.length <= 1) {
setDefaults(clause);
} else {
String osname = null;
for (int i = 1; i < args.length; i++) {
String parts[] = args[i].split("\\s*=\\s*");
if (parts.length != 2)
throw new IllegalArgumentException(
"Illegal property syntax in \"" + args[i] + "\", use \"key=value\"");
String key = Strings.trim(parts[0]);
String value = Strings.trim(parts[1]);
boolean isList = value.indexOf(',') > 0;
switch (key) {
case "processor" :
case "osgi.native.processor" :
if (isList)
clause.processor.addAll(Strings.split(value));
else {
if ("arm".equals(value)) {
p.warning("The 'arm' processor is deprecated. Specify either 'arm_le' or 'arm_be'");
}
String[] processorAliases = getProcessorAliases(value);
if (processorAliases != null && processorAliases.length > 0) {
Collections.addAll(clause.processor, processorAliases);
} else {
clause.processor.add(value);
}
}
break;
case "osname" :
case "osgi.native.osname" :
if (isList)
clause.osname.addAll(Strings.split(value));
else {
if (osname == null) {
osname = value;
} else {
clause.osname.add(osname);
osname = value;
}
}
break;
case "osversion" :
case "osgi.native.osversion" :
if (clause.osversion == null) {
clause.osversion = Version.parseVersion(value);
} else
throw new IllegalArgumentException(
"osversion/osgi.native.osversion can only be set once in ${native_capability}");
break;
case "osgi.native.language" :
case "lang" :
if (clause.language != null)
throw new IllegalArgumentException(
"lang/osgi.native.lang can only be set once in ${native_capability}");
clause.language = value;
break;
}
}
if (osname != null) {
try {
OSInformation osi = new OSInformation(osname, clause.osversion.toString());
clause.osname.addAll(Strings.split(osi.osnames));
} catch (Exception e) {
clause.osname.add(osname);
}
}
}
}
private static void setDefaults(NativeCapability clause) {
OSInformation osi = new OSInformation();
clause.osname.addAll(Strings.split(osi.osnames));
clause.osversion = osi.osversion;
String[] processorAliases = getProcessorAliases(System.getProperty("os.arch"));
if (processorAliases != null && processorAliases.length > 0) {
Collections.addAll(clause.processor, processorAliases);
}
clause.language = Locale.getDefault()
.toString();
}
public static class OSNameVersion {
public Version osversion;
public String osnames;
}
public static OSNameVersion getOperatingSystemAliases(String sysPropOsName, String sysPropOsVersion) {
OSNameVersion nc = new OSNameVersion();
if (sysPropOsName.startsWith("Windows")) {
if (sysPropOsVersion.startsWith("10.0")) {
nc.osversion = new Version(10, 0, 0);
nc.osnames = "Windows10,Windows 10,Win32";
} else if (sysPropOsVersion.startsWith("6.3")) {
nc.osversion = new Version(6, 3, 0);
nc.osnames = "Windows8.1,Windows 8.1,Win32";
} else if (sysPropOsVersion.startsWith("6.2")) {
nc.osversion = new Version(6, 2, 0);
nc.osnames = "Windows8,Windows 8,Win32";
} else if (sysPropOsVersion.startsWith("6.1")) {
nc.osversion = new Version(6, 1, 0);
nc.osnames = "Windows7,Windows 7,Win32";
} else if (sysPropOsVersion.startsWith("6.0")) {
nc.osversion = new Version(6, 0, 0);
nc.osnames = "WindowsVista,WinVista,Windows Vista,Win32";
} else if (sysPropOsVersion.startsWith("5.1")) {
nc.osversion = new Version(5, 1, 0);
nc.osnames = "WindowsXP,WinXP,Windows XP,Win32";
} else {
nc = null;
}
} else if (sysPropOsName.startsWith("Mac OS X")) {
nc.osversion = convertUnixKernelVersion(sysPropOsVersion);
nc.osnames = "MacOSX,Mac OS X";
return nc;
} else if (Strings.startsWithIgnoreCase(sysPropOsName, "linux")) {
nc.osversion = convertUnixKernelVersion(sysPropOsVersion);
nc.osnames = "Linux";
} else if (sysPropOsName.startsWith("Solaris")) {
nc.osversion = convertUnixKernelVersion(sysPropOsVersion);
nc.osnames = "Solaris";
} else if (sysPropOsName.startsWith("AIX")) {
nc.osversion = convertUnixKernelVersion(sysPropOsVersion);
nc.osnames = "AIX";
} else if (sysPropOsName.startsWith("HP-UX")) {
nc.osversion = convertUnixKernelVersion(sysPropOsVersion);
nc.osnames = "HPUX,hp-ux";
}
return nc;
}
/**
* Return a native capability based on the given specification.
*
* @param p the processor for the error reporting
* @param args a set of assignments. The following keys are supported:
* osgi.native.processor,osname, osgi.native.osname,
* osversion,osgi.native.osversion, osgi.native.language, lang
* @return a native capability
*/
public static Capability getNativeCapability(Reporter p, String... args) throws Exception {
NativeCapability clause = new NativeCapability();
parseNativeCapabilityArgs(p, args, clause);
validateNativeCapability(clause);
return createCapability(clause);
}
/**
* Return the default capability for the current platform
*
* @return a default capability for the current platform
*/
public static Capability getDefaultNativeCapability() throws Exception {
NativeCapability clause = new NativeCapability();
setDefaults(clause);
return createCapability(clause);
}
}