]> git.argeo.org Git - gpl/argeo-slc.git/blob - repo/osgi/ArgeoOsgiDistributionImpl.java
Prepare next development cycle
[gpl/argeo-slc.git] / repo / osgi / ArgeoOsgiDistributionImpl.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.io.IOException;
4 import java.io.Writer;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.util.ArrayList;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13 import java.util.SortedSet;
14 import java.util.TreeMap;
15 import java.util.TreeSet;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.argeo.slc.CategorizedNameVersion;
20 import org.argeo.slc.ModuleSet;
21 import org.argeo.slc.NameVersion;
22 import org.argeo.slc.build.Distribution;
23 import org.argeo.slc.execution.ExecutionFlow;
24 import org.argeo.slc.repo.ArgeoOsgiDistribution;
25 import org.argeo.slc.repo.ArtifactDistribution;
26 import org.eclipse.aether.artifact.Artifact;
27 import org.eclipse.aether.artifact.DefaultArtifact;
28 import org.osgi.framework.Constants;
29
30 /**
31 * A consistent and versioned OSGi distribution, which can be built and tested.
32 */
33 public class ArgeoOsgiDistributionImpl extends ArtifactDistribution implements ArgeoOsgiDistribution {
34 private final static Log log = LogFactory.getLog(ArgeoOsgiDistributionImpl.class);
35
36 private List<Object> modules = new ArrayList<Object>();
37
38 public ArgeoOsgiDistributionImpl(String coords) {
39 super(coords);
40 }
41
42 public void init() {
43 if (log.isDebugEnabled())
44 log.debug(describe());
45 // migrateTov2(Paths.get(System.getProperty("user.home"), "dev/git/gpl/argeo-tp/argeo-tp"));
46 }
47
48 public void destroy() {
49
50 }
51
52 public String describe() {
53 SortedSet<String> sort = new TreeSet<String>();
54 Iterator<? extends NameVersion> nvIt = nameVersions();
55 while (nvIt.hasNext()) {
56 NameVersion nv = nvIt.next();
57 String str = nv.toString();
58 if (nv instanceof MavenWrapper)
59 str = str + "\t(Maven)";
60 else if (nv instanceof UriWrapper)
61 str = str + "\t(URI)";
62 else if (nv instanceof OsgiCategorizedNV)
63 str = str + "\t(OSGi from archive)";
64 else if (nv instanceof BndWrapper)
65 str = str + "\t(Plain BND from archive)";
66 else
67 str = str + "\t(UNKNOWN??)";
68 sort.add(str);
69 }
70
71 StringBuffer buf = new StringBuffer("## DISTRIBUTION " + toString() + " ##\n");
72 for (String str : sort) {
73 buf.append(str).append('\n');
74 }
75 return buf.toString();
76 }
77
78 public void migrateTov2(Path baseDir) {
79 Set<ArchiveWrapper> archiveWrappers = new HashSet<>();
80 Iterator<? extends NameVersion> nvIt = nameVersions();
81 while (nvIt.hasNext()) {
82 NameVersion nv = nvIt.next();
83 try {
84 if (nv instanceof CategorizedNameVersion) {
85 CategorizedNameVersion cnv = (CategorizedNameVersion) nv;
86 // TODO add branch?
87 Path categoryBase = baseDir.resolve(cnv.getCategory());
88 Files.createDirectories(categoryBase);
89 if (cnv instanceof BndWrapper) {
90 BndWrapper bw = (BndWrapper) cnv;
91 Path bndPath = categoryBase.resolve(cnv.getName() + ".bnd");
92 Map<String, String> props = new TreeMap<>();
93 for (Map.Entry<Object, Object> entry : ((BndWrapper) cnv).getBndProperties().entrySet()) {
94 props.put(entry.getKey().toString(), entry.getValue().toString());
95 }
96 props.put(Constants.BUNDLE_SYMBOLICNAME, cnv.getName());
97 props.put(Constants.BUNDLE_VERSION, cnv.getVersion());
98 if (bw.getLicense() != null)
99 props.put(Constants.BUNDLE_LICENSE, bw.getLicense().toString());
100 else
101 log.warn("No license for " + cnv);
102 if (bw.getDoNotModify()) {
103 props.put("SLC-Source-Original", "true");
104 }
105 // props.put("SLC-Category", cnv.getCategory());
106
107 if (cnv instanceof MavenWrapper) {
108 MavenWrapper mw = (MavenWrapper) cnv;
109 String sourceCoords = mw.getSourceCoords();
110 props.put("SLC-Source-M2", sourceCoords);
111 Artifact mavenCnv = new DefaultArtifact(sourceCoords);
112 if (mavenCnv.getArtifactId().equals(cnv.getName()))
113 props.remove(Constants.BUNDLE_SYMBOLICNAME);
114 if (mavenCnv.getVersion().equals(cnv.getVersion()))
115 props.remove(Constants.BUNDLE_VERSION);
116 } else if (cnv instanceof UriWrapper) {
117 UriWrapper mw = (UriWrapper) cnv;
118 props.put("SLC-Source-URI", mw.getEffectiveUri());
119 if (mw.getUri() == null && mw.getBaseUri() != null) {
120 log.warn("Base URI for " + cnv);
121 props.put("SLC-Source-BaseURI", mw.getBaseUri());
122 props.put("SLC-Source-VersionSeparator", mw.getVersionSeparator());
123 }
124 } else {
125 log.warn("Unidentified BND wrapper " + cnv);
126 }
127
128 // write BND file
129 try (Writer writer = Files.newBufferedWriter(bndPath)) {
130 // writer.write("# " + cnv + "\n");
131 props: for (String key : props.keySet()) {
132 String value = props.get(key);
133 if (Constants.EXPORT_PACKAGE.equals(key) && "*".equals(value.trim()))
134 continue props;
135
136 writer.write(key + ": " + value + '\n');
137 }
138 if (log.isTraceEnabled())
139 log.trace("Wrote " + bndPath);
140 }
141 } else if (cnv instanceof OsgiCategorizedNV) {
142 OsgiCategorizedNV onv = (OsgiCategorizedNV) cnv;
143 ArchiveWrapper aw = onv.getBuild();
144 archiveWrappers.add(aw);
145 // TODO specify and implement archive wrapper support
146 } else {
147 log.warn("Unsupported wrapper " + cnv.getClass() + " for " + cnv);
148 }
149
150 } else {
151 log.error("Category required for " + nv + ", skipping...");
152 }
153 } catch (IOException e) {
154 log.error("Could not process " + nv, e);
155 }
156 }
157 if (log.isDebugEnabled()) {
158 for (ArchiveWrapper aw : archiveWrappers) {
159 log.debug("Archive wrapper " + aw.getUri() + ":");
160 log.debug(" includes: " + aw.getIncludes());
161 log.debug(" excludes: " + aw.getExcludes());
162 log.debug(" beans : " + aw.getWrappers());
163 }
164 }
165
166 }
167
168 public Iterator<NameVersion> nameVersions() {
169 List<NameVersion> nameVersions = new ArrayList<NameVersion>();
170 for (Object module : modules) {
171 // extract runnable from execution flow
172 if (module instanceof ExecutionFlow) {
173 for (Iterator<Runnable> it = ((ExecutionFlow) module).runnables(); it.hasNext();) {
174 processModule(nameVersions, it.next());
175 }
176 } else {
177 processModule(nameVersions, module);
178 }
179 }
180 return nameVersions.iterator();
181 }
182
183 private void processModule(List<NameVersion> nameVersions, Object module) {
184 if (module instanceof ModuleSet)
185 addNameVersions(nameVersions, (ModuleSet) module);
186 else if (module instanceof NameVersion) {
187 NameVersion nv = (NameVersion) module;
188 addNameVersion(nameVersions, nv);
189 } else
190 log.warn("Ignored " + module);
191 }
192
193 private void addNameVersions(List<NameVersion> nameVersions, ModuleSet moduleSet) {
194 Iterator<? extends NameVersion> it = moduleSet.nameVersions();
195 while (it.hasNext()) {
196 NameVersion nv = it.next();
197 addNameVersion(nameVersions, nv);
198 }
199 }
200
201 protected void addNameVersion(List<NameVersion> nameVersions, NameVersion nv) {
202 if (!nameVersions.contains(nv)) {
203 nameVersions.add(nv);
204 }
205 }
206
207 // Modular distribution interface methods. Not yet used.
208 public Distribution getModuleDistribution(String moduleName, String moduleVersion) {
209 throw new UnsupportedOperationException();
210 }
211
212 public Object getModulesDescriptor(String descriptorType) {
213 throw new UnsupportedOperationException();
214 }
215
216 /* DEPENDENCY INJECTION */
217 public void setModules(List<Object> modules) {
218 this.modules = modules;
219 }
220 }