]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/BndWrapper.java
Manage licences
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / BndWrapper.java
index 564dd52d360d9349ea9ecde8050270e3ac15bd04..bb2bf24044d77bd4a30ea0455457674180428ec8 100644 (file)
@@ -10,6 +10,7 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.CategorizedNameVersion;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.build.Distribution;
+import org.argeo.slc.build.License;
 import org.osgi.framework.Version;
 import org.sonatype.aether.artifact.Artifact;
 import org.sonatype.aether.util.artifact.DefaultArtifact;
@@ -26,50 +27,94 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
 
        private String groupId;
        private String name;
-       private String version;
        private Properties bndProperties = new Properties();
 
+       private String version;
+       private License license;
+
+       private Boolean doNotModify = false;
+
+       private Runnable factory = null;
+
        public void wrapJar(InputStream in, OutputStream out) {
                Builder b = new Builder();
                try {
                        Jar jar = new Jar(null, in);
 
                        Manifest sourceManifest = jar.getManifest();
-                       String sourceVersion = sourceManifest.getMainAttributes().getValue(
-                                       BUNDLE_VERSION);
+
                        Version versionToUse;
-                       if (version == null && sourceVersion == null) {
-                               throw new SlcException("A bundle version must be defined.");
-                       } else if (version == null && sourceVersion != null) {
-                               versionToUse = new Version(sourceVersion);
-                       } else if (version != null && sourceVersion == null) {
-                               versionToUse = new Version(version);
-                       } else {// both set
-                               versionToUse = new Version(version);
-                               Version sv = new Version(sourceVersion);
-                               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 + ")");
+                       if (sourceManifest != null) {
+                               // Symbolic name
+                               String sourceSymbolicName = sourceManifest.getMainAttributes()
+                                               .getValue(BUNDLE_SYMBOLICNAME);
+                               if (sourceSymbolicName != null
+                                               && sourceSymbolicName.equals(name))
+                                       log.warn("The new symbolic name ("
+                                                       + name
+                                                       + ") is not consistant with the wrapped bundle symbolic name ("
+                                                       + sourceSymbolicName + ")");
+
+                               // Version
+                               String sourceVersion = sourceManifest.getMainAttributes()
+                                               .getValue(BUNDLE_VERSION);
+                               if (version == null && sourceVersion == null) {
+                                       throw new SlcException("A bundle version must be defined.");
+                               } else if (version == null && sourceVersion != null) {
+                                       versionToUse = new Version(sourceVersion);
+                                       version = sourceVersion; // set wrapper version
+                               } else if (version != null && sourceVersion == null) {
+                                       versionToUse = new Version(version);
+                               } else {// both set
+                                       versionToUse = new Version(version);
+                                       Version sv = new Version(sourceVersion);
+                                       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 + ")");
+                                       }
                                }
+                       } else {
+                               versionToUse = new Version(version);
                        }
 
-                       Properties properties = new Properties();
-                       properties.putAll(bndProperties);
-                       properties.setProperty(BUNDLE_SYMBOLICNAME, name);
-                       properties.setProperty(BUNDLE_VERSION, versionToUse.toString());
+                       if (doNotModify) {
+                               jar.write(out);
+                       } else {
+
+                               Properties properties = new Properties();
+                               properties.putAll(bndProperties);
+                               properties.setProperty(BUNDLE_SYMBOLICNAME, name);
+                               properties.setProperty(BUNDLE_VERSION, versionToUse.toString());
+
+                               // License
+                               if (license != null) {
+                                       StringBuilder sb = new StringBuilder(license.getUri());
+                                       if (license.getName() != null)
+                                               sb.append(';').append("description=")
+                                                               .append(license.getName());
+                                       if (license.getLink() != null)
+                                               sb.append(';').append("link=")
+                                                               .append(license.getLink());
+                                       properties.setProperty(BUNDLE_LICENSE, sb.toString());
+                                       // TODO add LICENSE.TXT
+                               } else {
+                                       log.warn("No license set for " + toString());
+                               }
 
-                       // b.addIncluded(jarFile);
-                       b.addClasspath(jar);
+                               // b.addIncluded(jarFile);
+                               b.addClasspath(jar);
 
-                       log.debug(properties);
-                       b.setProperties(properties);
+                               if (log.isDebugEnabled())
+                                       log.debug(properties);
+                               b.setProperties(properties);
 
-                       Jar newJar = b.build();
-                       newJar.write(out);
+                               Jar newJar = b.build();
+                               newJar.write(out);
+                       }
                } catch (Exception e) {
                        throw new SlcException("Cannot wrap jar", e);
                } finally {
@@ -78,6 +123,16 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
 
        }
 
+       public Runnable getFactory() {
+               return factory;
+       }
+
+       public void setFactory(Runnable factory) {
+               if (this.factory != null)
+                       throw new SlcException("Factory already set on " + name);
+               this.factory = factory;
+       }
+
        public void setName(String bsn) {
                this.name = bsn;
        }
@@ -87,6 +142,9 @@ 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 + ")");
                this.version = version;
        }
 
@@ -94,6 +152,16 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                return version;
        }
 
+       public License getLicense() {
+               return license;
+       }
+
+       public void setLicense(License license) {
+               if (this.license != null)
+                       throw new SlcException("License already set on " + name);
+               this.license = license;
+       }
+
        public Properties getBndProperties() {
                return bndProperties;
        }
@@ -147,4 +215,8 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                        return false;
        }
 
+       public void setDoNotModify(Boolean doNotModify) {
+               this.doNotModify = doNotModify;
+       }
+
 }