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=29c0f78e7e2e0828b116673105d154a1f2970a69;hb=5686259eb6e4da5006034087c71f349b3097be8d;hp=00679e2c6247aae4130d33d56372836b0f06df17;hpb=1f0e95374536f1e521eb7b29a6704c1a9d79b94c;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 00679e2c6..29c0f78e7 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,18 +1,15 @@ package org.argeo.slc.repo.osgi; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; 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.NameVersion; +import org.argeo.slc.CategorizedNameVersion; import org.argeo.slc.SlcException; +import org.argeo.slc.build.Distribution; import org.osgi.framework.Version; import org.sonatype.aether.artifact.Artifact; import org.sonatype.aether.util.artifact.DefaultArtifact; @@ -23,7 +20,8 @@ import aQute.lib.osgi.Constants; import aQute.lib.osgi.Jar; /** Utilities around the BND library, which manipulates OSGi metadata. */ -public class BndWrapper implements Constants, NameVersion, BeanNameAware { +public class BndWrapper implements Constants, CategorizedNameVersion, + Distribution, BeanNameAware { private final static Log log = LogFactory.getLog(BndWrapper.class); private String groupId; @@ -31,47 +29,72 @@ public class BndWrapper implements Constants, NameVersion, BeanNameAware { private String version; private Properties bndProperties = new Properties(); + 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) { + 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 + ")"); + + 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()); - // 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 { @@ -80,6 +103,14 @@ public class BndWrapper implements Constants, NameVersion, BeanNameAware { } + public Runnable getFactory() { + return factory; + } + + public void setFactory(Runnable factory) { + this.factory = factory; + } + public void setName(String bsn) { this.name = bsn; } @@ -112,38 +143,45 @@ public class BndWrapper implements Constants, NameVersion, BeanNameAware { return groupId; } + public String getCategory() { + return getGroupId(); + } + public void setGroupId(String groupId) { this.groupId = groupId; } + public String getDistributionId() { + return getArtifact().toString(); + } + public Artifact getArtifact() { return new DefaultArtifact(groupId, name, "jar", version); } - public static void main(String[] args) { - BndWrapper bndWrapper = new BndWrapper(); - bndWrapper.setName("org.slf4j"); + @Override + public String toString() { + return getArtifact().toString(); + } - InputStream in = null; - InputStream propertiesIn = null; - OutputStream out = null; - Properties properties = new Properties(); - File jarFile = new File( - "/home/mbaudier/dev/work/130129-Distribution/slf4j/slf4j-1.7.5/slf4j-api-1.7.5.jar"); - File propertiesFile = new File( - "/home/mbaudier/dev/git/git.argeo.org/distribution/bnd/org.slf4j/bnd.bnd"); - try { - in = new FileInputStream(jarFile); - // propertiesIn = new FileInputStream(propertiesFile); - out = new FileOutputStream(new File("test.jar")); - // properties.load(propertiesIn); - bndWrapper.wrapJar(in, out); - } catch (Exception e) { - throw new SlcException("Cannot test", e); - } finally { - IOUtils.closeQuietly(in); - IOUtils.closeQuietly(propertiesIn); - IOUtils.closeQuietly(out); - } + @Override + public int hashCode() { + return getArtifact().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof CategorizedNameVersion) { + CategorizedNameVersion cnv = (CategorizedNameVersion) obj; + return getCategory().equals(cnv.getCategory()) + && getName().equals(cnv.getName()) + && getVersion().equals(cnv.getVersion()); + } else + return false; } + + public void setDoNotModify(Boolean doNotModify) { + this.doNotModify = doNotModify; + } + }