Improve logging in SLC Repo layer.
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 16 Feb 2020 08:09:30 +0000 (09:09 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 16 Feb 2020 08:09:30 +0000 (09:09 +0100)
org.argeo.slc.api/src/org/argeo/slc/DefaultCategorizedNameVersion.java [new file with mode: 0644]
org.argeo.slc.api/src/org/argeo/slc/DefaultNameVersion.java
org.argeo.slc.repo/src/org/argeo/slc/repo/ModularDistributionIndexer.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/OsgiCategorizedNV.java

diff --git a/org.argeo.slc.api/src/org/argeo/slc/DefaultCategorizedNameVersion.java b/org.argeo.slc.api/src/org/argeo/slc/DefaultCategorizedNameVersion.java
new file mode 100644 (file)
index 0000000..a30970e
--- /dev/null
@@ -0,0 +1,34 @@
+package org.argeo.slc;
+
+/** Canonical implementation of {@link CategorizedNameVersion} */
+public class DefaultCategorizedNameVersion extends DefaultNameVersion implements CategorizedNameVersion {
+       private String category;
+
+       public DefaultCategorizedNameVersion() {
+       }
+
+       public DefaultCategorizedNameVersion(String category, String name, String version) {
+               super(name, version);
+               this.category = category;
+       }
+
+       public DefaultCategorizedNameVersion(String category, NameVersion nameVersion) {
+               super(nameVersion);
+               this.category = category;
+       }
+
+       @Override
+       public String getCategory() {
+               return category;
+       }
+
+       public void setCategory(String category) {
+               this.category = category;
+       }
+
+       @Override
+       public String toString() {
+               return category + ":" + super.toString();
+       }
+
+}
index 97e579ff81e5a1905c182dc9a7dce0e0264c1b4e..bd64198f0de88e38f8a3c1c22b07611536584614 100644 (file)
@@ -75,7 +75,7 @@ public class DefaultNameVersion implements NameVersion,
 
        @Override
        public int hashCode() {
-               return name.hashCode() + version.hashCode();
+               return name.hashCode();
        }
 
        @Override
index d89077322a73af49d2ef074b9ac7306dea5e4203..adb591bbe23f63f46c7d9418a11ebd3a0ee5d25c 100644 (file)
@@ -22,7 +22,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.CategorizedNameVersion;
-import org.argeo.slc.DefaultNameVersion;
+import org.argeo.slc.DefaultCategorizedNameVersion;
 import org.argeo.slc.NameVersion;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.SlcNames;
@@ -169,7 +169,7 @@ public class ModularDistributionIndexer implements NodeIndexer, SlcNames {
                                st.nextToken(); // moduleVersion
                                String relativeUrl = st.nextToken();
                                Artifact currModule = AetherUtils.convertPathToArtifact(relativeUrl, null);
-                               modules.add(new MyCategorizedNameVersion(currModule.getGroupId(), currModule.getArtifactId(),
+                               modules.add(new DefaultCategorizedNameVersion(currModule.getGroupId(), currModule.getArtifactId(),
                                                currModule.getVersion()));
                        }
                        return new MyModularDistribution(distribution, modules);
@@ -181,20 +181,6 @@ public class ModularDistributionIndexer implements NodeIndexer, SlcNames {
                }
        }
 
-       /** The created modular distribution */
-       private static class MyCategorizedNameVersion extends DefaultNameVersion implements CategorizedNameVersion {
-               private final String category;
-
-               public MyCategorizedNameVersion(String category, String name, String version) {
-                       super(name, version);
-                       this.category = category;
-               }
-
-               public String getCategory() {
-                       return category;
-               }
-       }
-
        /**
         * A consistent and versioned OSGi distribution, which can be built and tested.
         */
index b47da8b887fcdae29253c1ea8f592f39cfc03adc..7d74856047292af610decc1760b17962c7c99f50 100644 (file)
@@ -15,11 +15,11 @@ import org.argeo.slc.execution.ExecutionFlow;
 import org.argeo.slc.repo.ArgeoOsgiDistribution;
 import org.argeo.slc.repo.ArtifactDistribution;
 
-/** A consistent and versioned OSGi distribution, which can be built and tested. */
-public class ArgeoOsgiDistributionImpl extends ArtifactDistribution implements
-               ArgeoOsgiDistribution {
-       private final static Log log = LogFactory
-                       .getLog(ArgeoOsgiDistributionImpl.class);
+/**
+ * A consistent and versioned OSGi distribution, which can be built and tested.
+ */
+public class ArgeoOsgiDistributionImpl extends ArtifactDistribution implements ArgeoOsgiDistribution {
+       private final static Log log = LogFactory.getLog(ArgeoOsgiDistributionImpl.class);
 
        private List<Object> modules = new ArrayList<Object>();
 
@@ -28,45 +28,38 @@ public class ArgeoOsgiDistributionImpl extends ArtifactDistribution implements
        }
 
        public void init() {
-               if (log.isDebugEnabled()) {
-                       SortedSet<String> sort = new TreeSet<String>();
-                       Iterator<? extends NameVersion> nvIt = nameVersions();
-                       while (nvIt.hasNext()) {
-                               NameVersion nv = nvIt.next();
-                               sort.add(nv.getName() + ":" + nv.getVersion());
-                       }
+               if (log.isDebugEnabled())
+                       log.debug(describe());
+       }
 
-                       StringBuffer buf = new StringBuffer(
-                                       "## OSGi FACTORY MANAGED MODULES : \n");
-                       for (String str : sort) {
-                               buf.append(str).append('\n');
-                       }
+       public void destroy() {
 
-                       log.debug(buf);
-               }
        }
 
-       // private static void print(ModuleSet moduleSet, Integer depth) {
-       // StringBuilder prefix = new StringBuilder();
-       // for (int i = 0; i < depth; i++) {
-       // prefix.append(' ');
-       // }
-       // String p = prefix.toString();
-       // prefix.setLength(0);
-       // log.debug(p + "## " + moduleSet.toString());
-       // Iterator<? extends NameVersion> nvIt = moduleSet.nameVersions();
-       // while (nvIt.hasNext()) {
-       // NameVersion nv = nvIt.next();
-       // if (nv instanceof ModuleSet)
-       // print((ModuleSet) nv, depth + 1);
-       // else
-       // log.debug(p + nv);
-       //
-       // }
-       // }
-
-       public void destroy() {
+       public String describe() {
+               SortedSet<String> sort = new TreeSet<String>();
+               Iterator<? extends NameVersion> nvIt = nameVersions();
+               while (nvIt.hasNext()) {
+                       NameVersion nv = nvIt.next();
+                       String str = nv.toString();
+                       if (nv instanceof MavenWrapper)
+                               str = str + "\t(Maven)";
+                       else if (nv instanceof UriWrapper)
+                               str = str + "\t(URI)";
+                       else if (nv instanceof OsgiCategorizedNV)
+                               str = str + "\t(OSGi from archive)";
+                       else if (nv instanceof BndWrapper)
+                               str = str + "\t(Plain BND from archive)";
+                       else
+                               str = str + "\t(UNKNOWN??)";
+                       sort.add(str);
+               }
 
+               StringBuffer buf = new StringBuffer("## DISTRIBUTION " + toString() + " ##\n");
+               for (String str : sort) {
+                       buf.append(str).append('\n');
+               }
+               return buf.toString();
        }
 
        public Iterator<NameVersion> nameVersions() {
@@ -74,23 +67,12 @@ public class ArgeoOsgiDistributionImpl extends ArtifactDistribution implements
                for (Object module : modules) {
                        // extract runnable from execution flow
                        if (module instanceof ExecutionFlow) {
-                               for (Iterator<Runnable> it = ((ExecutionFlow) module)
-                                               .runnables(); it.hasNext();) {
+                               for (Iterator<Runnable> it = ((ExecutionFlow) module).runnables(); it.hasNext();) {
                                        processModule(nameVersions, it.next());
                                }
-                       }
-                       // module = ((ExecutionFlow) module).getRunnable();
-                       else {
+                       } else {
                                processModule(nameVersions, module);
                        }
-                       // if (module instanceof ModuleSet)
-                       // addNameVersions(nameVersions, (ModuleSet) module);
-                       // else if (module instanceof NameVersion) {
-                       // NameVersion nv = (NameVersion) module;
-                       // if (!nameVersions.contains(nv))
-                       // nameVersions.add(nv);
-                       // } else
-                       // log.warn("Ignored " + module);
                }
                return nameVersions.iterator();
        }
@@ -100,37 +82,32 @@ public class ArgeoOsgiDistributionImpl extends ArtifactDistribution implements
                        addNameVersions(nameVersions, (ModuleSet) module);
                else if (module instanceof NameVersion) {
                        NameVersion nv = (NameVersion) module;
-                       if (!nameVersions.contains(nv))
-                               nameVersions.add(nv);
+                       addNameVersion(nameVersions, nv);
                } else
                        log.warn("Ignored " + module);
        }
 
-       private void addNameVersions(List<NameVersion> nameVersions,
-                       ModuleSet moduleSet) {
+       private void addNameVersions(List<NameVersion> nameVersions, ModuleSet moduleSet) {
                Iterator<? extends NameVersion> it = moduleSet.nameVersions();
                while (it.hasNext()) {
                        NameVersion nv = it.next();
-                       if (!nameVersions.contains(nv))
-                               nameVersions.add(nv);
+                       addNameVersion(nameVersions, nv);
+               }
+       }
+
+       protected void addNameVersion(List<NameVersion> nameVersions, NameVersion nv) {
+               if (!nameVersions.contains(nv)) {
+                       nameVersions.add(nv);
                }
        }
 
        // Modular distribution interface methods. Not yet used.
-       public Distribution getModuleDistribution(String moduleName,
-                       String moduleVersion) {
-               // NameVersion searched = new DefaultNameVersion(moduleName,
-               // moduleVersion);
-               // for (Distribution ad : modules) {
-               // if (ad.equals(searched))
-               // return ad;
-               // }
-               return null;
+       public Distribution getModuleDistribution(String moduleName, String moduleVersion) {
+               throw new UnsupportedOperationException();
        }
 
        public Object getModulesDescriptor(String descriptorType) {
-               // TODO Auto-generated method stub
-               return null;
+               throw new UnsupportedOperationException();
        }
 
        /* DEPENDENCY INJECTION */
index 56dda5bdf7d37cba0c1c395274da517fae628647..e11f7c13066605b1625bfca026f666dab892f469 100644 (file)
@@ -23,8 +23,7 @@ import aQute.bnd.osgi.Constants;
 import aQute.bnd.osgi.Jar;
 
 /** Utilities around the BND library, which manipulates OSGi metadata. */
-public class BndWrapper implements Constants, CategorizedNameVersion,
-               Distribution, BeanNameAware {
+public class BndWrapper implements Constants, CategorizedNameVersion, Distribution, BeanNameAware {
        private final static Log log = LogFactory.getLog(BndWrapper.class);
 
        private String groupId;
@@ -50,18 +49,13 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                        Version versionToUse;
                        if (sourceManifest != null) {
                                // Symbolic name
-                               String sourceSymbolicName = sourceManifest.getMainAttributes()
-                                               .getValue(BUNDLE_SYMBOLICNAME);
-                               if (sourceSymbolicName != null
-                                               && !sourceSymbolicName.equals(name))
-                                       log.info("The new symbolic name ("
-                                                       + name
-                                                       + ") is not consistant with the wrapped bundle symbolic name ("
-                                                       + sourceSymbolicName + ")");
+                               String sourceSymbolicName = sourceManifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME);
+                               if (sourceSymbolicName != null && !sourceSymbolicName.equals(name))
+                                       log.info("The new symbolic name (" + name
+                                                       + ") is not consistant with the wrapped bundle symbolic name (" + sourceSymbolicName + ")");
 
                                // Version
-                               String sourceVersion = sourceManifest.getMainAttributes()
-                                               .getValue(BUNDLE_VERSION);
+                               String sourceVersion = sourceManifest.getMainAttributes().getValue(BUNDLE_VERSION);
                                if (getVersion() == null && sourceVersion == null) {
                                        throw new SlcException("A bundle version must be defined.");
                                } else if (getVersion() == null && sourceVersion != null) {
@@ -72,13 +66,10 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                                } else {// both set
                                        versionToUse = new Version(getVersion());
                                        Version sv = new Version(sourceVersion);
-                                       if (versionToUse.getMajor() != sv.getMajor()
-                                                       || versionToUse.getMinor() != sv.getMinor()
+                                       if (versionToUse.getMajor() != sv.getMajor() || versionToUse.getMinor() != sv.getMinor()
                                                        || versionToUse.getMicro() != sv.getMicro()) {
-                                               log.warn("The new version ("
-                                                               + versionToUse
-                                                               + ") is not consistant with the wrapped bundle version ("
-                                                               + sv + ")");
+                                               log.warn("The new version (" + versionToUse
+                                                               + ") is not consistant with the wrapped bundle version (" + sv + ")");
                                        }
                                }
                        } else {
@@ -99,11 +90,9 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                                if (license != null) {
                                        StringBuilder sb = new StringBuilder(license.getUri());
                                        if (license.getName() != null)
-                                               sb.append(';').append("description=")
-                                                               .append(license.getName());
+                                               sb.append(';').append("description=").append(license.getName());
                                        if (license.getLink() != null)
-                                               sb.append(';').append("link=")
-                                                               .append(license.getLink());
+                                               sb.append(';').append("link=").append(license.getLink());
                                        properties.setProperty(BUNDLE_LICENSE, sb.toString());
                                        // TODO add LICENSE.TXT
                                } else {
@@ -155,8 +144,7 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
 
        public void setVersion(String version) {
                if (this.version != null)
-                       throw new SlcException("Version already set on " + name + " ("
-                                       + this.version + ")");
+                       throw new SlcException("Version already set on " + name + " (" + this.version + ")");
                this.version = version;
        }
 
@@ -187,8 +175,7 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                        this.name = name;
                } else {
                        if (!name.contains("#"))
-                               log.warn("Using explicitely set name " + this.name
-                                               + " and not bean name " + name);
+                               log.warn("Using explicitely set name " + this.name + " and not bean name " + name);
                }
        }
 
@@ -205,7 +192,7 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
        }
 
        public String getDistributionId() {
-               return getArtifact().toString();
+               return getCategory() + ":" + getName() + ":" + getVersion();
        }
 
        public Artifact getArtifact() {
@@ -214,20 +201,21 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
 
        @Override
        public String toString() {
-               return getArtifact().toString();
+               return getDistributionId();
        }
 
        @Override
        public int hashCode() {
-               return getArtifact().hashCode();
+               if (name != null)
+                       return name.hashCode();
+               return super.hashCode();
        }
 
        @Override
        public boolean equals(Object obj) {
                if (obj instanceof CategorizedNameVersion) {
                        CategorizedNameVersion cnv = (CategorizedNameVersion) obj;
-                       return getCategory().equals(cnv.getCategory())
-                                       && getName().equals(cnv.getName())
+                       return getCategory().equals(cnv.getCategory()) && getName().equals(cnv.getName())
                                        && getVersion().equals(cnv.getVersion());
                } else
                        return false;
index 0d75d2520eaa6bc91ad5c09943c3d3586aa6a5aa..250149b09be9fe3e20eb8a57b608c3c982c00669 100644 (file)
@@ -1,25 +1,17 @@
 package org.argeo.slc.repo.osgi;
 
-import org.argeo.slc.CategorizedNameVersion;
-import org.argeo.slc.DefaultNameVersion;
+import org.argeo.slc.DefaultCategorizedNameVersion;
 
-class OsgiCategorizedNV extends DefaultNameVersion implements
-               CategorizedNameVersion, Runnable {
-       private String category;
+/** */
+class OsgiCategorizedNV extends DefaultCategorizedNameVersion implements Runnable {
        /** Build runnable */
        private Runnable build;
 
-       public OsgiCategorizedNV(String category, String name, String version,
-                       Runnable build) {
-               super(name, version);
-               this.category = category;
+       public OsgiCategorizedNV(String category, String name, String version, Runnable build) {
+               super(category, name, version);
                this.build = build;
        }
 
-       public String getCategory() {
-               return category;
-       }
-
        @Override
        public void run() {
                if (build != null)