File: native_capability.md

package info (click to toggle)
bnd 5.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 44,092 kB
  • sloc: java: 249,039; xml: 90,727; sh: 655; perl: 153; makefile: 95; python: 47; javascript: 9
file content (118 lines) | stat: -rw-r--r-- 3,798 bytes parent folder | download | duplicates (2)
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
---
layout: default
class: Processor
title: native_capability ( ';' ( 'os.name' | 'os.version' | 'os.processor' ) '=' STRING )*
summary: Create a Require-Capability header based on the current platform or explicit values
---

	/**
	 * <p>
	 * Generates a Capability string, in the format specified by the OSGi
	 * Provide-Capability header, representing the current native platform
	 * according to OSGi RFC 188. For example on Windows7 running on an x86_64
	 * processor it should generate the following:
	 * </p>
	 * 
	 * <pre>
	 * osgi.native;osgi.native.osname:List&lt;String&gt;="Windows7,Windows 7,Win32";osgi.native.osversion:Version=6.1.0;osgi.native.processor:List&lt;String&gt;="x86-64,amd64,em64t,x86_64"
	 * </pre>
	 * 
	 * @param args
	 *            The array of properties. For example: the macro invocation of
	 *            "${native_capability;osversion=3.2.4;osname=Linux}" results in
	 *            an args array of
	 *            [native_capability,&nbsp;osversion=3.2.4,&nbsp;osname=Linux]
	 */

	public String _native_capability(String[] args) throws IllegalArgumentException {
		StringBuilder builder = new StringBuilder().append(OSGI_NATIVE);

		String processorNames = null;
		OSInformation osInformation = null;
		IllegalArgumentException osInformationException = null;
		/*
		 * Determine the processor information
		 */
		String[] aliases = OSInformation.getProcessorAliases(System.getProperty("os.arch"));
		if (aliases != null)
			processorNames = Strings.join(aliases);

		/*
		 * Determine the OS information
		 */

		try {
			osInformation = new OSInformation();
		}
		catch (IllegalArgumentException e) {
			osInformationException = e;
		}

		/*
		 * Determine overrides
		 */

		String osnameOverride = null;
		Version osversionOverride = null;
		String processorNamesOverride = null;

		if (args.length > 1) {
			assert ("native_capability".equals(args[0]));
			for (int i = 1; i < args.length; i++) {
				String arg = args[i];
				String[] fields = arg.split("=", 2);
				if (fields.length != 2) {
					throw new IllegalArgumentException("Illegal property syntax in \"" + arg + "\", use \"key=value\"");
				}
				String key = fields[0];
				String value = fields[1];
				if (OS_NAME.equals(key)) {
					osnameOverride = value;
				} else if (OS_VERSION.equals(key)) {
					osversionOverride = new Version(value);
				} else if (OS_PROCESSOR.equals(key)) {
					processorNamesOverride = value;
				} else {
					throw new IllegalArgumentException("Unrecognised/unsupported property. Supported: " + OS_NAME
							+ ", " + OS_VERSION + ", " + OS_PROCESSOR + ".");
				}
			}
		}

		/*
		 * Determine effective values: put determined value into override if
		 * there is no override
		 */

		if (osnameOverride == null && osInformation != null) {
			osnameOverride = osInformation.osnames;
		}
		if (osversionOverride == null && osInformation != null) {
			osversionOverride = osInformation.osversion;
		}
		if (processorNamesOverride == null && processorNames != null) {
			processorNamesOverride = processorNames;
		}

		/*
		 * Construct result string
		 */

		builder.append(";" + OSGI_NATIVE + "." + OS_NAME + ":List<String>=\"").append(osnameOverride).append('"');
		builder.append(";" + OSGI_NATIVE + "." + OS_VERSION + ":Version=").append(osversionOverride);
		builder.append(";" + OSGI_NATIVE + "." + OS_PROCESSOR + ":List<String>=\"").append(processorNamesOverride)
				.append('"');

		/*
		 * Report error if needed
		 */

		if (osnameOverride == null || osversionOverride == null || processorNamesOverride == null) {
			throw new IllegalArgumentException(
					"At least one of the required parameters could not be detected; specify an override. Detected: "
							+ builder.toString(), osInformationException);
		}

		return builder.toString();
	}