]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/api/a2/A2Contribution.java
Clarify ACR API
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / api / a2 / A2Contribution.java
1 package org.argeo.api.a2;
2
3 import java.util.Collections;
4 import java.util.Map;
5 import java.util.TreeMap;
6
7 /**
8 * A category grouping a set of {@link A2Component}, typically based on the
9 * provider of these components. This is the equivalent of Maven's group Id.
10 */
11 public class A2Contribution implements Comparable<A2Contribution> {
12 final static String BOOT = "boot";
13 final static String RUNTIME = "runtime";
14 final static String CLASSPATH = "classpath";
15
16 final static String DEFAULT = "default";
17 final static String LIB = "lib";
18
19 private final ProvisioningSource source;
20 private final String id;
21
22 final Map<String, A2Component> components = Collections.synchronizedSortedMap(new TreeMap<>());
23
24 /**
25 * The contribution must be added to the source. Rather use
26 * {@link AbstractProvisioningSource#getOrAddContribution(String)} than this
27 * contructor directly.
28 */
29 public A2Contribution(ProvisioningSource context, String id) {
30 this.source = context;
31 this.id = id;
32 // if (context != null)
33 // context.contributions.put(id, this);
34 }
35
36 public Iterable<A2Component> listComponents(Object filter) {
37 return components.values();
38 }
39
40 A2Component getOrAddComponent(String componentId) {
41 if (components.containsKey(componentId))
42 return components.get(componentId);
43 else
44 return new A2Component(this, componentId);
45 }
46
47 public ProvisioningSource getSource() {
48 return source;
49 }
50
51 public String getId() {
52 return id;
53 }
54
55 @Override
56 public int compareTo(A2Contribution o) {
57 return id.compareTo(o.id);
58 }
59
60 @Override
61 public int hashCode() {
62 return id.hashCode();
63 }
64
65 @Override
66 public boolean equals(Object obj) {
67 if (obj instanceof A2Contribution) {
68 A2Contribution o = (A2Contribution) obj;
69 return id.equals(o.id);
70 } else
71 return false;
72 }
73
74 @Override
75 public String toString() {
76 return id;
77 }
78
79 void asTree(String prefix, StringBuffer buf) {
80 if (prefix == null)
81 prefix = "";
82 for (String componentId : components.keySet()) {
83 buf.append(prefix);
84 buf.append(componentId);
85 A2Component component = components.get(componentId);
86 component.asTree(prefix, buf);
87 buf.append('\n');
88 }
89 }
90
91 static String localOsArchRelativePath() {
92 return Os.local().toString() + "/" + Arch.local().toString();
93 }
94
95 /** Well-known operating systems. */
96 static enum Os {
97 LINUX, WIN32, MACOSX, UNKOWN;
98
99 @Override
100 public String toString() {
101 return name().toLowerCase();
102 }
103
104 /** The local operating system. */
105 public static Os local() {
106 String osStr = System.getProperty("os.name").toLowerCase();
107 if (osStr.startsWith("linux"))
108 return LINUX;
109 if (osStr.startsWith("win"))
110 return WIN32;
111 if (osStr.startsWith("mac"))
112 return MACOSX;
113 return UNKOWN;
114 }
115
116 }
117
118 /** Well-known processor architectures. */
119 static enum Arch {
120 X86_64, AARCH64, X86, POWERPC, UNKOWN;
121
122 @Override
123 public String toString() {
124 return name().toLowerCase();
125 }
126
127 /** The locla processor architecture. */
128 public static Arch local() {
129 String archStr = System.getProperty("os.arch").toLowerCase();
130 return switch (archStr) {
131 case "x86_64":
132 case "amd64":
133 case "x86-64": {
134 yield X86_64;
135 }
136 case "aarch64":
137 case "arm64": {
138 yield AARCH64;
139 }
140 case "x86":
141 case "i386":
142 case "i686": {
143 yield X86;
144 }
145 case "powerpc":
146 case "ppc": {
147 yield POWERPC;
148 }
149 default:
150 yield UNKOWN;
151 };
152 }
153 }
154
155 }