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
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.CategoryNameVersion
;
13 import org
.argeo
.slc
.SlcException
;
14 import org
.argeo
.slc
.build
.Distribution
;
15 import org
.argeo
.slc
.build
.License
;
16 import org
.eclipse
.aether
.artifact
.Artifact
;
17 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
18 import org
.osgi
.framework
.Version
;
20 import aQute
.bnd
.osgi
.Builder
;
21 import aQute
.bnd
.osgi
.Constants
;
22 import aQute
.bnd
.osgi
.Jar
;
24 /** Utilities around the BND library, which manipulates OSGi metadata. */
25 public class BndWrapper
implements Constants
, CategoryNameVersion
, Distribution
{
26 private final static Log log
= LogFactory
.getLog(BndWrapper
.class);
28 private String groupId
;
30 private Properties bndProperties
= new Properties();
32 private String version
;
33 private License license
;
35 private Boolean doNotModify
= false;
37 private Runnable factory
= null;
39 public void wrapJar(InputStream in
, OutputStream out
) {
40 Builder b
= new Builder();
43 byte[] jarBytes
= IOUtils
.toByteArray(in
);
45 jar
= new Jar(name
, new ByteArrayInputStream(jarBytes
));
46 Manifest sourceManifest
= jar
.getManifest();
49 if (sourceManifest
!= null) {
51 String sourceSymbolicName
= sourceManifest
.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME
);
52 if (sourceSymbolicName
!= null && !sourceSymbolicName
.equals(name
))
53 log
.info("The new symbolic name (" + name
54 + ") is not consistant with the wrapped bundle symbolic name (" + sourceSymbolicName
+ ")");
57 String sourceVersion
= sourceManifest
.getMainAttributes().getValue(BUNDLE_VERSION
);
58 if (getVersion() == null && sourceVersion
== null) {
59 throw new SlcException("A bundle version must be defined.");
60 } else if (getVersion() == null && sourceVersion
!= null) {
61 versionToUse
= new Version(sourceVersion
);
62 version
= sourceVersion
; // set wrapper version
63 } else if (getVersion() != null && sourceVersion
== null) {
64 versionToUse
= new Version(getVersion());
66 versionToUse
= new Version(getVersion());
67 Version sv
= new Version(sourceVersion
);
68 if (versionToUse
.getMajor() != sv
.getMajor() || versionToUse
.getMinor() != sv
.getMinor()
69 || versionToUse
.getMicro() != sv
.getMicro()) {
70 log
.warn("The new version (" + versionToUse
71 + ") is not consistant with the wrapped bundle version (" + sv
+ ")");
75 versionToUse
= new Version(getVersion());
79 IOUtils
.write(jarBytes
, out
);
83 Properties properties
= new Properties();
84 properties
.putAll(bndProperties
);
85 properties
.setProperty(BUNDLE_SYMBOLICNAME
, name
);
86 properties
.setProperty(BUNDLE_VERSION
, versionToUse
.toString());
89 if (license
!= null) {
90 StringBuilder sb
= new StringBuilder(license
.getUri());
91 if (license
.getName() != null)
92 sb
.append(';').append("description=").append(license
.getName());
93 if (license
.getLink() != null)
94 sb
.append(';').append("link=").append(license
.getLink());
95 properties
.setProperty(BUNDLE_LICENSE
, sb
.toString());
96 // TODO add LICENSE.TXT
98 log
.warn("No license set for " + toString());
101 // b.addIncluded(jarFile);
104 if (log
.isDebugEnabled())
105 log
.debug(properties
);
106 b
.setProperties(properties
);
108 Jar newJar
= b
.build();
112 } catch (Exception e
) {
113 throw new SlcException("Cannot wrap jar", e
);
119 } catch (Exception e
) {
126 public Runnable
getFactory() {
130 public void setFactory(Runnable factory
) {
131 if (this.factory
!= null)
132 throw new SlcException("Factory already set on " + name
);
133 this.factory
= factory
;
136 public void setName(String bsn
) {
140 public String
getName() {
144 public void setVersion(String version
) {
145 if (this.version
!= null)
146 throw new SlcException("Version already set on " + name
+ " (" + this.version
+ ")");
147 this.version
= version
;
150 public String
getVersion() {
154 public License
getLicense() {
158 public void setLicense(License license
) {
159 if (this.license
!= null)
160 throw new SlcException("License already set on " + name
);
161 this.license
= license
;
164 public Properties
getBndProperties() {
165 return bndProperties
;
168 public void setBndProperties(Properties bndProperties
) {
169 this.bndProperties
= bndProperties
;
172 public String
getGroupId() {
176 public String
getCategory() {
180 public void setGroupId(String groupId
) {
181 this.groupId
= groupId
;
184 public String
getDistributionId() {
185 return getCategory() + ":" + getName() + ":" + getVersion();
188 public Artifact
getArtifact() {
189 return new DefaultArtifact(groupId
, name
, "jar", getVersion());
193 public String
toString() {
194 return getDistributionId();
198 public int hashCode() {
200 return name
.hashCode();
201 return super.hashCode();
205 public boolean equals(Object obj
) {
206 if (obj
instanceof CategoryNameVersion
) {
207 CategoryNameVersion cnv
= (CategoryNameVersion
) obj
;
208 return getCategory().equals(cnv
.getCategory()) && getName().equals(cnv
.getName())
209 && getVersion().equals(cnv
.getVersion());
214 public void setDoNotModify(Boolean doNotModify
) {
215 this.doNotModify
= doNotModify
;
218 public Boolean
getDoNotModify() {