Fix MANIFEST generation
[gpl/argeo-slc.git] / org.argeo.slc.repo / src / org / argeo / slc / repo / osgi / ArchiveWrapper.java
index badbbc1e8057f4d8888549691923dfb6d22a5670..3cb1e9c8f3264034407568876ea79f5356a7cd33 100644 (file)
@@ -22,10 +22,9 @@ import javax.jcr.Session;
 
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.argeo.api.cms.CmsLog;
 import org.argeo.jcr.JcrUtils;
-import org.argeo.slc.CategorizedNameVersion;
+import org.argeo.slc.CategoryNameVersion;
 import org.argeo.slc.DefaultNameVersion;
 import org.argeo.slc.ModuleSet;
 import org.argeo.slc.NameVersion;
@@ -34,13 +33,13 @@ import org.argeo.slc.build.Distribution;
 import org.argeo.slc.build.License;
 import org.argeo.slc.repo.OsgiFactory;
 import org.argeo.slc.repo.RepoUtils;
+import org.argeo.slc.repo.internal.springutil.AntPathMatcher;
+import org.argeo.slc.repo.internal.springutil.PathMatcher;
 import org.argeo.slc.repo.maven.ArtifactIdComparator;
 import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.artifact.DefaultArtifact;
-import org.springframework.util.AntPathMatcher;
-import org.springframework.util.PathMatcher;
 
-import aQute.lib.osgi.Jar;
+import aQute.bnd.osgi.Jar;
 
 /**
  * Download a software distribution and generates the related OSGi bundles from
@@ -48,7 +47,7 @@ import aQute.lib.osgi.Jar;
  * need further modification.
  */
 public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
-       private final static Log log = LogFactory.getLog(ArchiveWrapper.class);
+       private final static CmsLog log = CmsLog.getLog(ArchiveWrapper.class);
 
        private OsgiFactory osgiFactory;
        private String version;
@@ -86,6 +85,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();
@@ -95,7 +106,7 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
 
        @SuppressWarnings("resource")
        protected Iterator<? extends NameVersion> osgiNameVersions() {
-               List<CategorizedNameVersion> nvs = new ArrayList<CategorizedNameVersion>();
+               List<CategoryNameVersion> nvs = new ArrayList<CategoryNameVersion>();
 
                Session distSession = null;
                ZipInputStream zin = null;
@@ -103,8 +114,8 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                        distSession = osgiFactory.openDistSession();
 
                        Node distNode = osgiFactory.getDist(distSession, uri);
-                       zin = new ZipInputStream(distNode.getNode(Node.JCR_CONTENT)
-                                       .getProperty(Property.JCR_DATA).getBinary().getStream());
+                       zin = new ZipInputStream(
+                                       distNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary().getStream());
 
                        ZipEntry zentry = null;
                        entries: while ((zentry = zin.getNextEntry()) != null) {
@@ -120,17 +131,14 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                                String groupId = includes.get(include);
                                                JarInputStream jis = new JarInputStream(zin);
                                                if (jis.getManifest() == null) {
-                                                       log.warn("No MANIFEST in entry " + name
-                                                                       + ", skipping...");
+                                                       log.warn("No MANIFEST in entry " + name + ", skipping...");
                                                        continue entries;
                                                }
-                                               NameVersion nv = RepoUtils.readNameVersion(jis
-                                                               .getManifest());
+                                               NameVersion nv = RepoUtils.readNameVersion(jis.getManifest());
                                                if (nv != null) {
                                                        if (nv.getName().endsWith(".source"))
                                                                continue entries;
-                                                       CategorizedNameVersion cnv = new OsgiCategorizedNV(
-                                                                       groupId, nv.getName(), nv.getVersion(),
+                                                       CategoryNameVersion cnv = new ArchiveWrapperCNV(groupId, nv.getName(), nv.getVersion(),
                                                                        this);
                                                        nvs.add(cnv);
                                                        // no need to process further includes
@@ -150,8 +158,7 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
 
        public void run() {
                if (mavenGroupIndexes && (version == null))
-                       throw new SlcException(
-                                       "'mavenGroupIndexes' requires 'version' to be set");
+                       throw new SlcException("'mavenGroupIndexes' requires 'version' to be set");
 
                Map<String, Set<Artifact>> binaries = new HashMap<String, Set<Artifact>>();
                Map<String, Set<Artifact>> sources = new HashMap<String, Set<Artifact>>();
@@ -168,8 +175,8 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                        boolean nothingWasDone = true;
 
                        Node distNode = osgiFactory.getDist(distSession, uri);
-                       zin = new ZipInputStream(distNode.getNode(Node.JCR_CONTENT)
-                                       .getProperty(Property.JCR_DATA).getBinary().getStream());
+                       zin = new ZipInputStream(
+                                       distNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary().getStream());
 
                        ZipEntry zentry = null;
                        entries: while ((zentry = zin.getNextEntry()) != null) {
@@ -178,29 +185,21 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                // sources autodetect
                                String baseName = FilenameUtils.getBaseName(name);
                                if (baseName.endsWith("-sources")) {
-                                       String bundle = baseName.substring(0, baseName.length()
-                                                       - "-sources".length());
+                                       String bundle = baseName.substring(0, baseName.length() - "-sources".length());
                                        // log.debug(name + "," + baseName + ", " + bundle);
-                                       String bundlePath = FilenameUtils.getPath(name) + bundle
-                                                       + ".jar";
+                                       String bundlePath = FilenameUtils.getPath(name) + bundle + ".jar";
                                        if (wrappers.containsKey(bundlePath)) {
                                                BndWrapper wrapper = wrappers.get(bundlePath);
-                                               NameVersion bundleNv = new DefaultNameVersion(
-                                                               wrapper.getName(), wrapper.getVersion());
-                                               byte[] pdeSource = RepoUtils.packageAsPdeSource(zin,
-                                                               bundleNv);
-                                               Artifact sourcesArtifact = new DefaultArtifact(
-                                                               wrapper.getCategory(), wrapper.getName()
-                                                                               + ".source", "jar",
-                                                               wrapper.getVersion());
-                                               Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(
-                                                               javaSession.getRootNode(), sourcesArtifact,
+                                               NameVersion bundleNv = new DefaultNameVersion(wrapper.getName(), wrapper.getVersion());
+                                               byte[] pdeSource = RepoUtils.packageAsPdeSource(zin, bundleNv);
+                                               Artifact sourcesArtifact = new DefaultArtifact(wrapper.getCategory(),
+                                                               wrapper.getName() + ".source", "jar", wrapper.getVersion());
+                                               Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(javaSession.getRootNode(), sourcesArtifact,
                                                                pdeSource);
                                                osgiFactory.indexNode(pdeSourceNode);
                                                pdeSourceNode.getSession().save();
                                                if (log.isDebugEnabled())
-                                                       log.debug("Added sources " + sourcesArtifact
-                                                                       + " for bundle " + wrapper.getArtifact()
+                                                       log.debug("Added sources " + sourcesArtifact + " for bundle " + wrapper.getArtifact()
                                                                        + "from " + name + " in binary archive.");
                                        }
 
@@ -213,28 +212,23 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                        BndWrapper wrapper = (BndWrapper) wrappers.get(name);
                                        // we must copy since the stream is closed by BND
                                        byte[] origJarBytes = IOUtils.toByteArray(zin);
-                                       Artifact artifact = wrapZipEntry(javaSession, zentry,
-                                                       origJarBytes, wrapper);
+                                       Artifact artifact = wrapZipEntry(javaSession, zentry, origJarBytes, wrapper);
                                        nothingWasDone = false;
                                        addArtifactToIndex(binaries, wrapper.getGroupId(), artifact);
                                } else {
                                        for (String wrapperKey : wrappers.keySet())
                                                if (pathMatcher.match(wrapperKey, name)) {
                                                        // first matched is taken
-                                                       BndWrapper wrapper = (BndWrapper) wrappers
-                                                                       .get(wrapperKey);
+                                                       BndWrapper wrapper = (BndWrapper) wrappers.get(wrapperKey);
                                                        // we must copy since the stream is closed by BND
                                                        byte[] origJarBytes = IOUtils.toByteArray(zin);
-                                                       Artifact artifact = wrapZipEntry(javaSession,
-                                                                       zentry, origJarBytes, wrapper);
+                                                       Artifact artifact = wrapZipEntry(javaSession, zentry, origJarBytes, wrapper);
                                                        nothingWasDone = false;
-                                                       addArtifactToIndex(binaries, wrapper.getGroupId(),
-                                                                       artifact);
+                                                       addArtifactToIndex(binaries, wrapper.getGroupId(), artifact);
                                                        continue entries;
                                                } else {
                                                        if (log.isTraceEnabled())
-                                                               log.trace(name + " not matched by "
-                                                                               + wrapperKey);
+                                                               log.trace(name + " not matched by " + wrapperKey);
                                                }
 
                                        for (String exclude : excludes)
@@ -245,8 +239,7 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                                if (pathMatcher.match(include, name)) {
                                                        String groupId = includes.get(include);
                                                        byte[] origJarBytes = IOUtils.toByteArray(zin);
-                                                       Artifact artifact = importZipEntry(javaSession,
-                                                                       zentry, origJarBytes, groupId);
+                                                       Artifact artifact = importZipEntry(javaSession, zentry, origJarBytes, groupId);
                                                        if (artifact == null) {
                                                                log.warn("Skipped non identified " + zentry);
                                                                continue entries;
@@ -266,17 +259,17 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                        // indexes
                        if (mavenGroupIndexes && version != null) {
                                for (String groupId : binaries.keySet()) {
-                                       RepoUtils.writeGroupIndexes(javaSession, "/", groupId,
-                                                       version, binaries.get(groupId),
-                                                       sources.containsKey(groupId) ? sources.get(groupId)
-                                                                       : null);
+                                       RepoUtils.writeGroupIndexes(javaSession, "/", groupId, version, binaries.get(groupId),
+                                                       sources.containsKey(groupId) ? sources.get(groupId) : null);
                                }
                        }
 
-                       if (nothingWasDone)
-                               throw new SlcException("Nothing was done");
+                       if (nothingWasDone) {
+                               log.error("Nothing was done when wrapping " + uri + ". THE DISTRIBUTION IS INCONSISTENT.");
+                               // throw new SlcException("Nothing was done");
+                               // TODO Fail if not all wrappers matched
+                       }
 
-                       // FIXME Fail if not all wrappers matched
                } catch (Exception e) {
                        throw new SlcException("Cannot wrap distribution " + uri, e);
                } finally {
@@ -286,8 +279,8 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                }
        }
 
-       protected Artifact wrapZipEntry(Session javaSession, ZipEntry zentry,
-                       byte[] origJarBytes, BndWrapper wrapper) throws RepositoryException {
+       protected Artifact wrapZipEntry(Session javaSession, ZipEntry zentry, byte[] origJarBytes, BndWrapper wrapper)
+                       throws RepositoryException {
                ByteArrayOutputStream out = null;
                ByteArrayInputStream in = null;
                Node newJarNode;
@@ -298,13 +291,11 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                        wrapper.wrapJar(in, out);
 
                        Artifact artifact = wrapper.getArtifact();
-                       newJarNode = RepoUtils.copyBytesAsArtifact(
-                                       javaSession.getRootNode(), artifact, out.toByteArray());
+                       newJarNode = RepoUtils.copyBytesAsArtifact(javaSession.getRootNode(), artifact, out.toByteArray());
                        osgiFactory.indexNode(newJarNode);
                        newJarNode.getSession().save();
                        if (log.isDebugEnabled())
-                               log.debug("Wrapped jar " + zentry.getName() + " to "
-                                               + newJarNode.getPath());
+                               log.debug("Wrapped jar " + zentry.getName() + " to " + newJarNode.getPath());
 
                        if (sourcesProvider != null)
                                addSource(javaSession, artifact, out.toByteArray());
@@ -318,8 +309,7 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                }
        }
 
-       protected void addSource(Session javaSession, Artifact artifact,
-                       byte[] binaryJarBytes) {
+       protected void addSource(Session javaSession, Artifact artifact, byte[] binaryJarBytes) {
                InputStream in = null;
                ByteArrayOutputStream out = null;
                Jar jar = null;
@@ -333,21 +323,16 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
 
                        IOUtils.closeQuietly(in);
                        in = new ByteArrayInputStream(out.toByteArray());
-                       byte[] sourcesJar = RepoUtils.packageAsPdeSource(
-                                       in,
-                                       new DefaultNameVersion(artifact.getArtifactId(), artifact
-                                                       .getVersion()));
-                       Artifact sourcesArtifact = new DefaultArtifact(
-                                       artifact.getGroupId(),
-                                       artifact.getArtifactId() + ".source", "jar",
-                                       artifact.getVersion());
-                       Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(
-                                       javaSession.getRootNode(), sourcesArtifact, sourcesJar);
+                       byte[] sourcesJar = RepoUtils.packageAsPdeSource(in,
+                                       new DefaultNameVersion(artifact.getArtifactId(), artifact.getVersion()));
+                       Artifact sourcesArtifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId() + ".source",
+                                       "jar", artifact.getVersion());
+                       Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(javaSession.getRootNode(), sourcesArtifact, sourcesJar);
                        sourcesJarNode.getSession().save();
 
                        if (log.isDebugEnabled())
-                               log.debug("Added sources " + sourcesArtifact + " for bundle "
-                                               + artifact + "from source provider " + sourcesProvider);
+                               log.debug("Added sources " + sourcesArtifact + " for bundle " + artifact + "from source provider "
+                                               + sourcesProvider);
                } catch (Exception e) {
                        throw new SlcException("Cannot get sources for " + artifact, e);
                } finally {
@@ -358,8 +343,8 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                }
        }
 
-       protected Artifact importZipEntry(Session javaSession, ZipEntry zentry,
-                       byte[] binaryJarBytes, String groupId) throws RepositoryException {
+       protected Artifact importZipEntry(Session javaSession, ZipEntry zentry, byte[] binaryJarBytes, String groupId)
+                       throws RepositoryException {
                ByteArrayInputStream in = null;
                Node newJarNode;
                try {
@@ -369,15 +354,13 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                                log.warn("Cannot identify " + zentry.getName());
                                return null;
                        }
-                       Artifact artifact = new DefaultArtifact(groupId,
-                                       nameVersion.getName(), "jar", nameVersion.getVersion());
-                       newJarNode = RepoUtils.copyBytesAsArtifact(
-                                       javaSession.getRootNode(), artifact, binaryJarBytes);
+                       Artifact artifact = new DefaultArtifact(groupId, nameVersion.getName(), "jar", nameVersion.getVersion());
+                       newJarNode = RepoUtils.copyBytesAsArtifact(javaSession.getRootNode(), artifact, binaryJarBytes);
                        osgiFactory.indexNode(newJarNode);
                        newJarNode.getSession().save();
-                       if (log.isDebugEnabled())
-                               log.debug("Imported OSGi bundle " + zentry.getName() + " to "
-                                               + newJarNode.getPath());
+                       if (log.isDebugEnabled()) {
+                               log.debug(zentry.getName() + " => " + artifact);
+                       }
 
                        if (sourcesProvider != null)
                                addSource(javaSession, artifact, binaryJarBytes);
@@ -388,11 +371,9 @@ public class ArchiveWrapper implements Runnable, ModuleSet, Distribution {
                }
        }
 
-       private void addArtifactToIndex(Map<String, Set<Artifact>> index,
-                       String groupId, Artifact artifact) {
+       private void addArtifactToIndex(Map<String, Set<Artifact>> index, String groupId, Artifact artifact) {
                if (!index.containsKey(groupId))
-                       index.put(groupId,
-                                       new TreeSet<Artifact>(new ArtifactIdComparator()));
+                       index.put(groupId, new TreeSet<Artifact>(new ArtifactIdComparator()));
                index.get(groupId).add(artifact);
        }
 
@@ -436,4 +417,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;
+       }
+
 }