1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.IOException
;
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
;
14 import java
.util
.SortedSet
;
15 import java
.util
.TreeMap
;
16 import java
.util
.TreeSet
;
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
;
32 * A consistent and versioned OSGi distribution, which can be built and tested.
34 public class ArgeoOsgiDistributionImpl
extends ArtifactDistribution
implements ArgeoOsgiDistribution
{
35 private final static Log log
= LogFactory
.getLog(ArgeoOsgiDistributionImpl
.class);
37 private List
<Object
> modules
= new ArrayList
<Object
>();
39 public ArgeoOsgiDistributionImpl(String coords
) {
44 if (log
.isDebugEnabled())
45 log
.debug(describe());
46 migrateTov2(Paths
.get(System
.getProperty("user.home"), "dev/git/unstable/argeo-tp/argeo-tp"));
49 public void destroy() {
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)";
68 str
= str
+ "\t(UNKNOWN??)";
72 StringBuffer buf
= new StringBuffer("## DISTRIBUTION " + toString() + " ##\n");
73 for (String str
: sort
) {
74 buf
.append(str
).append('\n');
76 return buf
.toString();
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();
85 if (nv
instanceof CategoryNameVersion
) {
86 CategoryNameVersion cnv
= (CategoryNameVersion
) nv
;
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());
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());
102 log
.warn("No license for " + cnv
);
103 if (bw
.getDoNotModify()) {
104 props
.put("SLC-Source-Original", "true");
106 // props.put("SLC-Category", cnv.getCategory());
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());
126 log
.warn("Unidentified BND wrapper " + cnv
);
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()))
137 writer
.write(key
+ ": " + value
+ '\n');
139 if (log
.isTraceEnabled())
140 log
.trace("Wrote " + bndPath
);
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
148 log
.warn("Unsupported wrapper " + cnv
.getClass() + " for " + cnv
);
152 log
.error("Category required for " + nv
+ ", skipping...");
154 } catch (IOException e
) {
155 log
.error("Could not process " + nv
, e
);
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());
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());
178 processModule(nameVersions
, module
);
181 return nameVersions
.iterator();
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
);
191 log
.warn("Ignored " + module
);
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
);
202 protected void addNameVersion(List
<NameVersion
> nameVersions
, NameVersion nv
) {
203 if (!nameVersions
.contains(nv
)) {
204 nameVersions
.add(nv
);
208 // Modular distribution interface methods. Not yet used.
209 public Distribution
getModuleDistribution(String moduleName
, String moduleVersion
) {
210 throw new UnsupportedOperationException();
213 public Object
getModulesDescriptor(String descriptorType
) {
214 throw new UnsupportedOperationException();
217 /* DEPENDENCY INJECTION */
218 public void setModules(List
<Object
> modules
) {
219 this.modules
= modules
;