Start working on migration to new format.
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 16 Feb 2020 10:49:45 +0000 (11:49 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 16 Feb 2020 10:49:45 +0000 (11:49 +0100)
org.argeo.slc.api/src/org/argeo/slc/CategorizedNameVersion.java
org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ArchiveWrapper.java
org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/ArgeoOsgiDistributionImpl.java
org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/BndWrapper.java
org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/MavenWrapper.java
org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/OsgiCategorizedNV.java
org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/UriWrapper.java

index fd7d56d78eff0269fbd6c74fcd9b247fa1cafbef..b890340cda6bfc01d4b15238c580bbfea652cb77 100644 (file)
@@ -6,5 +6,20 @@ package org.argeo.slc;
  */
 public interface CategorizedNameVersion extends NameVersion {
        /** The category of the component. */
-       public String getCategory();
+       String getCategory();
+
+       static CategorizedNameVersion parseCategoryNameVersion(String str) {
+               if (str == null || "".equals(str.trim()))
+                       throw new IllegalArgumentException("At least one character required.");
+               String[] arr = str.trim().split(":");
+               if (arr.length > 3)
+                       throw new IllegalArgumentException(str + " does not respect the [category]:[name]:[version] pattern");
+               DefaultCategorizedNameVersion res = new DefaultCategorizedNameVersion();
+               res.setCategory(arr[0]);
+               if (arr.length > 1)
+                       res.setName(arr[1]);
+               if (arr.length > 2)
+                       res.setVersion(arr[2]);
+               return res;
+       }
 }
index 5469487a18239cfb080ef42cb2be92232ec7e564..8becba370ceffdb996ee7db7beb8b97b788fec11 100644 (file)
@@ -86,6 +86,18 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                return uri;
        }
 
+       public String getVersion() {
+               return version;
+       }
+
+       public License getLicense() {
+               return license;
+       }
+
+       public String getUri() {
+               return uri;
+       }
+
        public Iterator<? extends NameVersion> nameVersions() {
                if (wrappers.size() > 0)
                        return wrappers.values().iterator();
@@ -406,4 +418,16 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                this.sourcesProvider = sourcesProvider;
        }
 
+       public Map<String, BndWrapper> getWrappers() {
+               return wrappers;
+       }
+
+       public Map<String, String> getIncludes() {
+               return includes;
+       }
+
+       public List<String> getExcludes() {
+               return excludes;
+       }
+
 }
index 7d74856047292af610decc1760b17962c7c99f50..7521395e849ab7ad4597e52c145d17e62839b3d6 100644 (file)
@@ -1,19 +1,31 @@
 package org.argeo.slc.repo.osgi;
 
+import java.io.IOException;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 import java.util.SortedSet;
+import java.util.TreeMap;
 import java.util.TreeSet;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.CategorizedNameVersion;
 import org.argeo.slc.ModuleSet;
 import org.argeo.slc.NameVersion;
 import org.argeo.slc.build.Distribution;
 import org.argeo.slc.execution.ExecutionFlow;
 import org.argeo.slc.repo.ArgeoOsgiDistribution;
 import org.argeo.slc.repo.ArtifactDistribution;
+import org.eclipse.aether.artifact.Artifact;
+import org.eclipse.aether.artifact.DefaultArtifact;
+import org.osgi.framework.Constants;
 
 /**
  * A consistent and versioned OSGi distribution, which can be built and tested.
@@ -30,6 +42,7 @@ public class ArgeoOsgiDistributionImpl extends ArtifactDistribution implements A
        public void init() {
                if (log.isDebugEnabled())
                        log.debug(describe());
+//             migrateTov2(Paths.get(System.getProperty("user.home"), "dev/git/gpl/argeo-tp/argeo-tp"));
        }
 
        public void destroy() {
@@ -62,6 +75,96 @@ public class ArgeoOsgiDistributionImpl extends ArtifactDistribution implements A
                return buf.toString();
        }
 
+       public void migrateTov2(Path baseDir) {
+               Set<ArchiveWrapper> archiveWrappers = new HashSet<>();
+               Iterator<? extends NameVersion> nvIt = nameVersions();
+               while (nvIt.hasNext()) {
+                       NameVersion nv = nvIt.next();
+                       try {
+                               if (nv instanceof CategorizedNameVersion) {
+                                       CategorizedNameVersion cnv = (CategorizedNameVersion) nv;
+                                       // TODO add branch?
+                                       Path categoryBase = baseDir.resolve(cnv.getCategory());
+                                       Files.createDirectories(categoryBase);
+                                       if (cnv instanceof BndWrapper) {
+                                               BndWrapper bw = (BndWrapper) cnv;
+                                               Path bndPath = categoryBase.resolve(cnv.getName() + ".bnd");
+                                               Map<String, String> props = new TreeMap<>();
+                                               for (Map.Entry<Object, Object> entry : ((BndWrapper) cnv).getBndProperties().entrySet()) {
+                                                       props.put(entry.getKey().toString(), entry.getValue().toString());
+                                               }
+                                               props.put(Constants.BUNDLE_SYMBOLICNAME, cnv.getName());
+                                               props.put(Constants.BUNDLE_VERSION, cnv.getVersion());
+                                               if (bw.getLicense() != null)
+                                                       props.put(Constants.BUNDLE_LICENSE, bw.getLicense().toString());
+                                               else
+                                                       log.warn("No license for " + cnv);
+                                               if (bw.getDoNotModify()) {
+                                                       props.put("SLC-Source-Original", "true");
+                                               }
+                                               // props.put("SLC-Category", cnv.getCategory());
+
+                                               if (cnv instanceof MavenWrapper) {
+                                                       MavenWrapper mw = (MavenWrapper) cnv;
+                                                       String sourceCoords = mw.getSourceCoords();
+                                                       props.put("SLC-Source-M2", sourceCoords);
+                                                       Artifact mavenCnv = new DefaultArtifact(sourceCoords);
+                                                       if (mavenCnv.getArtifactId().equals(cnv.getName()))
+                                                               props.remove(Constants.BUNDLE_SYMBOLICNAME);
+                                                       if (mavenCnv.getVersion().equals(cnv.getVersion()))
+                                                               props.remove(Constants.BUNDLE_VERSION);
+                                               } else if (cnv instanceof UriWrapper) {
+                                                       UriWrapper mw = (UriWrapper) cnv;
+                                                       props.put("SLC-Source-URI", mw.getEffectiveUri());
+                                                       if (mw.getUri() == null && mw.getBaseUri() != null) {
+                                                               log.warn("Base URI for " + cnv);
+                                                               props.put("SLC-Source-BaseURI", mw.getBaseUri());
+                                                               props.put("SLC-Source-VersionSeparator", mw.getVersionSeparator());
+                                                       }
+                                               } else {
+                                                       log.warn("Unidentified BND wrapper " + cnv);
+                                               }
+
+                                               // write BND file
+                                               try (Writer writer = Files.newBufferedWriter(bndPath)) {
+                                                       // writer.write("# " + cnv + "\n");
+                                                       props: for (String key : props.keySet()) {
+                                                               String value = props.get(key);
+                                                               if (Constants.EXPORT_PACKAGE.equals(key) && "*".equals(value.trim()))
+                                                                       continue props;
+
+                                                               writer.write(key + ": " + value + '\n');
+                                                       }
+                                                       if (log.isTraceEnabled())
+                                                               log.trace("Wrote " + bndPath);
+                                               }
+                                       } else if (cnv instanceof OsgiCategorizedNV) {
+                                               OsgiCategorizedNV onv = (OsgiCategorizedNV) cnv;
+                                               ArchiveWrapper aw = onv.getBuild();
+                                               archiveWrappers.add(aw);
+                                               // TODO specify and implement archive wrapper support
+                                       } else {
+                                               log.warn("Unsupported wrapper " + cnv.getClass() + " for " + cnv);
+                                       }
+
+                               } else {
+                                       log.error("Category required for " + nv + ", skipping...");
+                               }
+                       } catch (IOException e) {
+                               log.error("Could not process " + nv, e);
+                       }
+               }
+               if (log.isDebugEnabled()) {
+                       for (ArchiveWrapper aw : archiveWrappers) {
+                               log.debug("Archive wrapper " + aw.getUri() + ":");
+                               log.debug(" includes: " + aw.getIncludes());
+                               log.debug(" excludes: " + aw.getExcludes());
+                               log.debug(" beans   : " + aw.getWrappers());
+                       }
+               }
+
+       }
+
        public Iterator<NameVersion> nameVersions() {
                List<NameVersion> nameVersions = new ArrayList<NameVersion>();
                for (Object module : modules) {
index e11f7c13066605b1625bfca026f666dab892f469..42111e7df52b4b019509d584712725f2d1b66213 100644 (file)
@@ -225,4 +225,8 @@ public class BndWrapper implements Constants, CategorizedNameVersion, Distributi
                this.doNotModify = doNotModify;
        }
 
+       public Boolean getDoNotModify() {
+               return doNotModify;
+       }
+
 }
index 40a7a8ac93d59526ab4ad60d3666437eb1d8bfca..6ab23099c56ac4d781238eace212d6483ae89880 100644 (file)
@@ -55,55 +55,45 @@ public class MavenWrapper extends BndWrapper implements Runnable {
                        try {
                                origArtifact = osgiFactory.getMaven(distSession, sourceCoords);
                        } catch (Exception e1) {
-                               origArtifact = osgiFactory.getMaven(distSession, sourceCoords
-                                               + ":" + getVersion());
+                               origArtifact = osgiFactory.getMaven(distSession, sourceCoords + ":" + getVersion());
                        }
 
-                       in = origArtifact.getNode(Node.JCR_CONTENT)
-                                       .getProperty(Property.JCR_DATA).getBinary().getStream();
+                       in = origArtifact.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary().getStream();
                        out = new ByteArrayOutputStream();
                        wrapJar(in, out);
-                       Node newJarNode = RepoUtils
-                                       .copyBytesAsArtifact(javaSession.getRootNode(),
-                                                       getArtifact(), out.toByteArray());
+                       Node newJarNode = RepoUtils.copyBytesAsArtifact(javaSession.getRootNode(), getArtifact(),
+                                       out.toByteArray());
                        osgiFactory.indexNode(newJarNode);
                        newJarNode.getSession().save();
 
                        if (log.isDebugEnabled())
-                               log.debug("Wrapped Maven " + sourceCoords + " to "
-                                               + newJarNode.getPath());
+                               log.debug("Wrapped Maven " + sourceCoords + " to " + newJarNode.getPath());
 
                        // sources
-                       Artifact sourcesArtifact = new SubArtifact(new DefaultArtifact(
-                                       sourceCoords), "sources", null);
+                       Artifact sourcesArtifact = new SubArtifact(new DefaultArtifact(sourceCoords), "sources", null);
                        Node sourcesArtifactNode;
                        try {
 
-                               sourcesArtifactNode = osgiFactory.getMaven(distSession,
-                                               sourcesArtifact.toString());
+                               sourcesArtifactNode = osgiFactory.getMaven(distSession, sourcesArtifact.toString());
                        } catch (SlcException e) {
                                // no sources available
                                return;
                        }
 
                        IOUtils.closeQuietly(in);
-                       in = sourcesArtifactNode.getNode(Node.JCR_CONTENT)
-                                       .getProperty(Property.JCR_DATA).getBinary().getStream();
+                       in = sourcesArtifactNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary().getStream();
                        byte[] pdeSource;
                        if (doNotModifySources)
                                pdeSource = IOUtils.toByteArray(in);
                        else
-                               pdeSource = RepoUtils.packageAsPdeSource(in,
-                                               new DefaultNameVersion(getName(), getVersion()));
-                       Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(javaSession
-                                       .getRootNode(), new DefaultArtifact(getCategory(),
-                                       getName() + ".source", "jar", getVersion()), pdeSource);
+                               pdeSource = RepoUtils.packageAsPdeSource(in, new DefaultNameVersion(getName(), getVersion()));
+                       Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(javaSession.getRootNode(),
+                                       new DefaultArtifact(getCategory(), getName() + ".source", "jar", getVersion()), pdeSource);
                        osgiFactory.indexNode(pdeSourceNode);
                        pdeSourceNode.getSession().save();
 
                        if (log.isDebugEnabled())
-                               log.debug("Wrapped Maven " + sourcesArtifact
-                                               + " to PDE sources " + pdeSourceNode.getPath());
+                               log.debug("Wrapped Maven " + sourcesArtifact + " to PDE sources " + pdeSourceNode.getPath());
                } catch (Exception e) {
                        throw new SlcException("Cannot wrap Maven " + sourceCoords, e);
                } finally {
@@ -118,6 +108,10 @@ public class MavenWrapper extends BndWrapper implements Runnable {
                this.sourceCoords = sourceCoords;
        }
 
+       public String getSourceCoords() {
+               return sourceCoords;
+       }
+
        public void setOsgiFactory(OsgiFactory osgiFactory) {
                this.osgiFactory = osgiFactory;
        }
index 250149b09be9fe3e20eb8a57b608c3c982c00669..b54d7f0c574cd4504e966d01a6ccb0107f18f35b 100644 (file)
@@ -5,9 +5,9 @@ import org.argeo.slc.DefaultCategorizedNameVersion;
 /** */
 class OsgiCategorizedNV extends DefaultCategorizedNameVersion implements Runnable {
        /** Build runnable */
-       private Runnable build;
+       private ArchiveWrapper build;
 
-       public OsgiCategorizedNV(String category, String name, String version, Runnable build) {
+       public OsgiCategorizedNV(String category, String name, String version, ArchiveWrapper build) {
                super(category, name, version);
                this.build = build;
        }
@@ -18,4 +18,8 @@ class OsgiCategorizedNV extends DefaultCategorizedNameVersion implements Runnabl
                        build.run();
        }
 
+       public ArchiveWrapper getBuild() {
+               return build;
+       }
+
 }
index 665bfce516a0edfcd0856342af44077d0b604322..262246fdfa7f04da5420802d720dcbef411e7402 100644 (file)
@@ -48,20 +48,18 @@ public class UriWrapper extends BndWrapper implements Runnable {
                try {
                        distSession = osgiFactory.openDistSession();
                        javaSession = osgiFactory.openJavaSession();
-                       if (uri == null) {
-                               uri = baseUri + '/' + getName() + versionSeparator
-                                               + getVersion() + "." + extension;
-                       }
+                       String uri = getEffectiveUri();
+//                     if (uri == null) {
+//                             uri = baseUri + '/' + getName() + versionSeparator + getVersion() + "." + extension;
+//                     }
                        Node sourceArtifact = osgiFactory.getDist(distSession, uri);
 
                        // TODO factorize with Maven
-                       in = sourceArtifact.getNode(Node.JCR_CONTENT)
-                                       .getProperty(Property.JCR_DATA).getBinary().getStream();
+                       in = sourceArtifact.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary().getStream();
                        out = new ByteArrayOutputStream();
                        wrapJar(in, out);
-                       Node newJarNode = RepoUtils
-                                       .copyBytesAsArtifact(javaSession.getRootNode(),
-                                                       getArtifact(), out.toByteArray());
+                       Node newJarNode = RepoUtils.copyBytesAsArtifact(javaSession.getRootNode(), getArtifact(),
+                                       out.toByteArray());
                        osgiFactory.indexNode(newJarNode);
                        newJarNode.getSession().save();
                        if (log.isDebugEnabled())
@@ -76,23 +74,19 @@ public class UriWrapper extends BndWrapper implements Runnable {
 
                                IOUtils.closeQuietly(out);
                                out = new ByteArrayOutputStream();
-                               sourcesProvider
-                                               .writeSources(packages, new ZipOutputStream(out));
+                               sourcesProvider.writeSources(packages, new ZipOutputStream(out));
 
                                IOUtils.closeQuietly(in);
                                in = new ByteArrayInputStream(out.toByteArray());
-                               byte[] sourcesJar = RepoUtils.packageAsPdeSource(in,
-                                               new DefaultNameVersion(this));
-                               Artifact sourcesArtifact = new DefaultArtifact(getArtifact()
-                                               .getGroupId(), getArtifact().getArtifactId()
-                                               + ".source", "jar", getArtifact().getVersion());
-                               Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(
-                                               javaSession.getRootNode(), sourcesArtifact, sourcesJar);
+                               byte[] sourcesJar = RepoUtils.packageAsPdeSource(in, new DefaultNameVersion(this));
+                               Artifact sourcesArtifact = new DefaultArtifact(getArtifact().getGroupId(),
+                                               getArtifact().getArtifactId() + ".source", "jar", getArtifact().getVersion());
+                               Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(javaSession.getRootNode(), sourcesArtifact,
+                                               sourcesJar);
                                sourcesJarNode.getSession().save();
 
                                if (log.isDebugEnabled())
-                                       log.debug("Added sources " + sourcesArtifact
-                                                       + " for bundle " + getArtifact());
+                                       log.debug("Added sources " + sourcesArtifact + " for bundle " + getArtifact());
                        }
                } catch (Exception e) {
                        throw new SlcException("Cannot wrap URI " + uri, e);
@@ -110,6 +104,13 @@ public class UriWrapper extends BndWrapper implements Runnable {
                this.uri = sourceCoords;
        }
 
+       public String getEffectiveUri() {
+               if (uri == null) {
+                       return baseUri + '/' + getName() + versionSeparator + getVersion() + "." + extension;
+               } else
+                       return uri;
+       }
+
        public void setOsgiFactory(OsgiFactory osgiFactory) {
                this.osgiFactory = osgiFactory;
        }
@@ -125,4 +126,17 @@ public class UriWrapper extends BndWrapper implements Runnable {
        public void setSourcesProvider(SourcesProvider sourcesProvider) {
                this.sourcesProvider = sourcesProvider;
        }
+
+       public String getUri() {
+               return uri;
+       }
+
+       public String getBaseUri() {
+               return baseUri;
+       }
+
+       public String getVersionSeparator() {
+               return versionSeparator;
+       }
+
 }