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