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
.CategorizedNameVersion
;
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
;
19 import org
.springframework
.beans
.factory
.BeanNameAware
;
21 import aQute
.bnd
.osgi
.Builder
;
22 import aQute
.bnd
.osgi
.Constants
;
23 import aQute
.bnd
.osgi
.Jar
;
25 /** Utilities around the BND library, which manipulates OSGi metadata. */
26 public class BndWrapper
implements Constants
, CategorizedNameVersion
, Distribution
, BeanNameAware
{
27 private final static Log log
= LogFactory
.getLog(BndWrapper
.class);
29 private String groupId
;
31 private Properties bndProperties
= new Properties();
33 private String version
;
34 private License license
;
36 private Boolean doNotModify
= false;
38 private Runnable factory
= null;
40 public void wrapJar(InputStream in
, OutputStream out
) {
41 Builder b
= new Builder();
44 byte[] jarBytes
= IOUtils
.toByteArray(in
);
46 jar
= new Jar(name
, new ByteArrayInputStream(jarBytes
));
47 Manifest sourceManifest
= jar
.getManifest();
50 if (sourceManifest
!= null) {
52 String sourceSymbolicName
= sourceManifest
.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME
);
53 if (sourceSymbolicName
!= null && !sourceSymbolicName
.equals(name
))
54 log
.info("The new symbolic name (" + name
55 + ") is not consistant with the wrapped bundle symbolic name (" + sourceSymbolicName
+ ")");
58 String sourceVersion
= sourceManifest
.getMainAttributes().getValue(BUNDLE_VERSION
);
59 if (getVersion() == null && sourceVersion
== null) {
60 throw new SlcException("A bundle version must be defined.");
61 } else if (getVersion() == null && sourceVersion
!= null) {
62 versionToUse
= new Version(sourceVersion
);
63 version
= sourceVersion
; // set wrapper version
64 } else if (getVersion() != null && sourceVersion
== null) {
65 versionToUse
= new Version(getVersion());
67 versionToUse
= new Version(getVersion());
68 Version sv
= new Version(sourceVersion
);
69 if (versionToUse
.getMajor() != sv
.getMajor() || versionToUse
.getMinor() != sv
.getMinor()
70 || versionToUse
.getMicro() != sv
.getMicro()) {
71 log
.warn("The new version (" + versionToUse
72 + ") is not consistant with the wrapped bundle version (" + sv
+ ")");
76 versionToUse
= new Version(getVersion());
80 IOUtils
.write(jarBytes
, out
);
84 Properties properties
= new Properties();
85 properties
.putAll(bndProperties
);
86 properties
.setProperty(BUNDLE_SYMBOLICNAME
, name
);
87 properties
.setProperty(BUNDLE_VERSION
, versionToUse
.toString());
90 if (license
!= null) {
91 StringBuilder sb
= new StringBuilder(license
.getUri());
92 if (license
.getName() != null)
93 sb
.append(';').append("description=").append(license
.getName());
94 if (license
.getLink() != null)
95 sb
.append(';').append("link=").append(license
.getLink());
96 properties
.setProperty(BUNDLE_LICENSE
, sb
.toString());
97 // TODO add LICENSE.TXT
99 log
.warn("No license set for " + toString());
102 // b.addIncluded(jarFile);
105 if (log
.isDebugEnabled())
106 log
.debug(properties
);
107 b
.setProperties(properties
);
109 Jar newJar
= b
.build();
113 } catch (Exception e
) {
114 throw new SlcException("Cannot wrap jar", e
);
120 } catch (Exception e
) {
127 public Runnable
getFactory() {
131 public void setFactory(Runnable factory
) {
132 if (this.factory
!= null)
133 throw new SlcException("Factory already set on " + name
);
134 this.factory
= factory
;
137 public void setName(String bsn
) {
141 public String
getName() {
145 public void setVersion(String version
) {
146 if (this.version
!= null)
147 throw new SlcException("Version already set on " + name
+ " (" + this.version
+ ")");
148 this.version
= version
;
151 public String
getVersion() {
155 public License
getLicense() {
159 public void setLicense(License license
) {
160 if (this.license
!= null)
161 throw new SlcException("License already set on " + name
);
162 this.license
= license
;
165 public Properties
getBndProperties() {
166 return bndProperties
;
169 public void setBndProperties(Properties bndProperties
) {
170 this.bndProperties
= bndProperties
;
173 public void setBeanName(String name
) {
174 if (this.name
== null) {
177 if (!name
.contains("#"))
178 log
.warn("Using explicitely set name " + this.name
+ " and not bean name " + name
);
182 public String
getGroupId() {
186 public String
getCategory() {
190 public void setGroupId(String groupId
) {
191 this.groupId
= groupId
;
194 public String
getDistributionId() {
195 return getCategory() + ":" + getName() + ":" + getVersion();
198 public Artifact
getArtifact() {
199 return new DefaultArtifact(groupId
, name
, "jar", getVersion());
203 public String
toString() {
204 return getDistributionId();
208 public int hashCode() {
210 return name
.hashCode();
211 return super.hashCode();
215 public boolean equals(Object obj
) {
216 if (obj
instanceof CategorizedNameVersion
) {
217 CategorizedNameVersion cnv
= (CategorizedNameVersion
) obj
;
218 return getCategory().equals(cnv
.getCategory()) && getName().equals(cnv
.getName())
219 && getVersion().equals(cnv
.getVersion());
224 public void setDoNotModify(Boolean doNotModify
) {
225 this.doNotModify
= doNotModify
;
228 public Boolean
getDoNotModify() {