]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/a2/A2Contribution.java
Use locator path as OSGi installation string
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / a2 / A2Contribution.java
1 package org.argeo.init.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 static enum Os {
96 LINUX, WIN32, MACOSX, UNKOWN;
97
98 @Override
99 public String toString() {
100 return name().toLowerCase();
101 }
102
103 public static Os local() {
104 String osStr = System.getProperty("os.name").toLowerCase();
105 if (osStr.startsWith("linux"))
106 return LINUX;
107 if (osStr.startsWith("win"))
108 return WIN32;
109 if (osStr.startsWith("mac"))
110 return MACOSX;
111 return UNKOWN;
112 }
113
114 }
115
116 static enum Arch {
117 X86_64, AARCH64, X86, POWERPC, UNKOWN;
118
119 @Override
120 public String toString() {
121 return name().toLowerCase();
122 }
123
124 public static Arch local() {
125 String archStr = System.getProperty("os.arch").toLowerCase();
126 return switch (archStr) {
127 case "x86_64":
128 case "amd64":
129 case "x86-64": {
130 yield X86_64;
131 }
132 case "aarch64":
133 case "arm64": {
134 yield AARCH64;
135 }
136 case "x86":
137 case "i386":
138 case "i686": {
139 yield X86;
140 }
141 case "powerpc":
142 case "ppc": {
143 yield POWERPC;
144 }
145 default:
146 yield UNKOWN;
147 };
148 }
149 }
150
151 }