1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.InputStream
;
5 import java
.io
.OutputStream
;
6 import java
.util
.Properties
;
7 import java
.util
.jar
.Manifest
;
9 import org
.apache
.commons
.io
.IOUtils
;
10 import org
.argeo
.api
.cms
.CmsLog
;
11 import org
.argeo
.slc
.CategoryNameVersion
;
12 import org
.argeo
.slc
.SlcException
;
13 import org
.argeo
.slc
.build
.Distribution
;
14 import org
.argeo
.slc
.build
.License
;
15 import org
.eclipse
.aether
.artifact
.Artifact
;
16 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
17 import org
.osgi
.framework
.Version
;
19 import aQute
.bnd
.osgi
.Builder
;
20 import aQute
.bnd
.osgi
.Constants
;
21 import aQute
.bnd
.osgi
.Jar
;
23 /** Utilities around the BND library, which manipulates OSGi metadata. */
24 public class BndWrapper
implements Constants
, CategoryNameVersion
, Distribution
{
25 private final static CmsLog log
= CmsLog
.getLog(BndWrapper
.class);
27 private String groupId
;
29 private Properties bndProperties
= new Properties();
31 private String version
;
32 private License license
;
34 private Boolean doNotModify
= false;
36 private Runnable factory
= null;
38 public void wrapJar(InputStream in
, OutputStream out
) {
39 Builder b
= new Builder();
42 byte[] jarBytes
= IOUtils
.toByteArray(in
);
44 jar
= new Jar(name
, new ByteArrayInputStream(jarBytes
));
45 Manifest sourceManifest
= jar
.getManifest();
48 if (sourceManifest
!= null) {
50 String sourceSymbolicName
= sourceManifest
.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME
);
51 if (sourceSymbolicName
!= null && !sourceSymbolicName
.equals(name
))
52 log
.info("The new symbolic name (" + name
53 + ") is not consistant with the wrapped bundle symbolic name (" + sourceSymbolicName
+ ")");
56 String sourceVersion
= sourceManifest
.getMainAttributes().getValue(BUNDLE_VERSION
);
57 if (getVersion() == null && sourceVersion
== null) {
58 throw new SlcException("A bundle version must be defined.");
59 } else if (getVersion() == null && sourceVersion
!= null) {
60 versionToUse
= new Version(sourceVersion
);
61 version
= sourceVersion
; // set wrapper version
62 } else if (getVersion() != null && sourceVersion
== null) {
63 versionToUse
= new Version(getVersion());
65 versionToUse
= new Version(getVersion());
66 Version sv
= new Version(sourceVersion
);
67 if (versionToUse
.getMajor() != sv
.getMajor() || versionToUse
.getMinor() != sv
.getMinor()
68 || versionToUse
.getMicro() != sv
.getMicro()) {
69 log
.warn("The new version (" + versionToUse
70 + ") is not consistant with the wrapped bundle version (" + sv
+ ")");
74 versionToUse
= new Version(getVersion());
78 IOUtils
.write(jarBytes
, out
);
82 Properties properties
= new Properties();
83 properties
.putAll(bndProperties
);
84 properties
.setProperty(BUNDLE_SYMBOLICNAME
, name
);
85 properties
.setProperty(BUNDLE_VERSION
, versionToUse
.toString());
88 if (license
!= null) {
89 properties
.setProperty(BUNDLE_LICENSE
, license
.toString());
90 // TODO add LICENSE.TXT
92 log
.warn("No license set for " + toString());
95 // b.addIncluded(jarFile);
98 if (log
.isDebugEnabled())
99 log
.debug(properties
);
100 b
.setProperties(properties
);
102 Jar newJar
= b
.build();
106 } catch (Exception e
) {
107 throw new SlcException("Cannot wrap jar", e
);
113 } catch (Exception e
) {
120 public Runnable
getFactory() {
124 public void setFactory(Runnable factory
) {
125 if (this.factory
!= null)
126 throw new SlcException("Factory already set on " + name
);
127 this.factory
= factory
;
130 public void setName(String bsn
) {
134 public String
getName() {
138 public void setVersion(String version
) {
139 if (this.version
!= null)
140 throw new SlcException("Version already set on " + name
+ " (" + this.version
+ ")");
141 this.version
= version
;
144 public String
getVersion() {
148 public License
getLicense() {
152 public void setLicense(License license
) {
153 if (this.license
!= null)
154 throw new SlcException("License already set on " + name
);
155 this.license
= license
;
158 public Properties
getBndProperties() {
159 return bndProperties
;
162 public void setBndProperties(Properties bndProperties
) {
163 this.bndProperties
= bndProperties
;
166 public String
getGroupId() {
170 public String
getCategory() {
174 public void setGroupId(String groupId
) {
175 this.groupId
= groupId
;
178 public String
getDistributionId() {
179 return getCategory() + ":" + getName() + ":" + getVersion();
182 public Artifact
getArtifact() {
183 return new DefaultArtifact(groupId
, name
, "jar", getVersion());
187 public String
toString() {
188 return getDistributionId();
192 public int hashCode() {
194 return name
.hashCode();
195 return super.hashCode();
199 public boolean equals(Object obj
) {
200 if (obj
instanceof CategoryNameVersion
) {
201 CategoryNameVersion cnv
= (CategoryNameVersion
) obj
;
202 return getCategory().equals(cnv
.getCategory()) && getName().equals(cnv
.getName())
203 && getVersion().equals(cnv
.getVersion());
208 public void setDoNotModify(Boolean doNotModify
) {
209 this.doNotModify
= doNotModify
;
212 public Boolean
getDoNotModify() {