X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.repo%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Frepo%2Fosgi%2FBndWrapper.java;h=7b75b903d4609df31054fc876042ad05c8c4a767;hb=399702cec859f48ec171c30d268c55eb632d472a;hp=55ad84dfdcfc2cfc7de1d367b8c075b245722a7e;hpb=50070c24a1b27dda7ac6669871caf008726d372d;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/BndWrapper.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/BndWrapper.java index 55ad84dfd..7b75b903d 100644 --- a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/BndWrapper.java +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/BndWrapper.java @@ -1,15 +1,18 @@ package org.argeo.slc.repo.osgi; +import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; import java.util.jar.Manifest; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; 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,38 +29,48 @@ 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(); + Jar jar = null; try { - Jar jar = new Jar(null, in); + byte[] jarBytes = IOUtils.toByteArray(in); + jar = new Jar(null, new ByteArrayInputStream(jarBytes)); Manifest sourceManifest = jar.getManifest(); Version versionToUse; if (sourceManifest != null) { + // Symbolic name String sourceSymbolicName = sourceManifest.getMainAttributes() .getValue(BUNDLE_SYMBOLICNAME); if (sourceSymbolicName != null - && sourceSymbolicName.equals(name)) + && !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) { + if (getVersion() == null && sourceVersion == null) { throw new SlcException("A bundle version must be defined."); - } else if (version == null && sourceVersion != null) { + } else if (getVersion() == null && sourceVersion != null) { versionToUse = new Version(sourceVersion); version = sourceVersion; // set wrapper version - } else if (version != null && sourceVersion == null) { - versionToUse = new Version(version); + } else if (getVersion() != null && sourceVersion == null) { + versionToUse = new Version(getVersion()); } else {// both set - versionToUse = new Version(version); + versionToUse = new Version(getVersion()); Version sv = new Version(sourceVersion); if (versionToUse.getMajor() != sv.getMajor() || versionToUse.getMinor() != sv.getMinor() @@ -69,30 +82,65 @@ public class BndWrapper implements Constants, CategorizedNameVersion, } } } else { - versionToUse = new Version(version); + versionToUse = new Version(getVersion()); } - Properties properties = new Properties(); - properties.putAll(bndProperties); - properties.setProperty(BUNDLE_SYMBOLICNAME, name); - properties.setProperty(BUNDLE_VERSION, versionToUse.toString()); + if (doNotModify) { + IOUtils.write(jarBytes, out); + // 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); + newJar.close(); + } } catch (Exception e) { throw new SlcException("Cannot wrap jar", e); } finally { b.close(); + if (jar != null) + jar.close(); } } + 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; } @@ -102,6 +150,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; } @@ -109,6 +160,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; } @@ -118,7 +179,12 @@ public class BndWrapper implements Constants, CategorizedNameVersion, } public void setBeanName(String name) { - this.name = name; + if (this.name == null) { + this.name = name; + } else { + log.warn("Using explicitely set name " + this.name + + " and not bean name " + name); + } } public String getGroupId() { @@ -138,7 +204,7 @@ public class BndWrapper implements Constants, CategorizedNameVersion, } public Artifact getArtifact() { - return new DefaultArtifact(groupId, name, "jar", version); + return new DefaultArtifact(groupId, name, "jar", getVersion()); } @Override @@ -162,4 +228,8 @@ public class BndWrapper implements Constants, CategorizedNameVersion, return false; } + public void setDoNotModify(Boolean doNotModify) { + this.doNotModify = doNotModify; + } + }