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
.osgi
.framework
.Version
;
17 import org
.eclipse
.aether
.artifact
.Artifact
;
18 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
19 import org
.springframework
.beans
.factory
.BeanNameAware
;
21 import aQute
.lib
.osgi
.Builder
;
22 import aQute
.lib
.osgi
.Constants
;
23 import aQute
.lib
.osgi
.Jar
;
25 /** Utilities around the BND library, which manipulates OSGi metadata. */
26 public class BndWrapper
implements Constants
, CategorizedNameVersion
,
27 Distribution
, BeanNameAware
{
28 private final static Log log
= LogFactory
.getLog(BndWrapper
.class);
30 private String groupId
;
32 private Properties bndProperties
= new Properties();
34 private String version
;
35 private License license
;
37 private Boolean doNotModify
= false;
39 private Runnable factory
= null;
41 public void wrapJar(InputStream in
, OutputStream out
) {
42 Builder b
= new Builder();
45 byte[] jarBytes
= IOUtils
.toByteArray(in
);
47 jar
= new Jar(null, new ByteArrayInputStream(jarBytes
));
48 Manifest sourceManifest
= jar
.getManifest();
51 if (sourceManifest
!= null) {
53 String sourceSymbolicName
= sourceManifest
.getMainAttributes()
54 .getValue(BUNDLE_SYMBOLICNAME
);
55 if (sourceSymbolicName
!= null
56 && !sourceSymbolicName
.equals(name
))
57 log
.info("The new symbolic name ("
59 + ") is not consistant with the wrapped bundle symbolic name ("
60 + sourceSymbolicName
+ ")");
63 String sourceVersion
= sourceManifest
.getMainAttributes()
64 .getValue(BUNDLE_VERSION
);
65 if (getVersion() == null && sourceVersion
== null) {
66 throw new SlcException("A bundle version must be defined.");
67 } else if (getVersion() == null && sourceVersion
!= null) {
68 versionToUse
= new Version(sourceVersion
);
69 version
= sourceVersion
; // set wrapper version
70 } else if (getVersion() != null && sourceVersion
== null) {
71 versionToUse
= new Version(getVersion());
73 versionToUse
= new Version(getVersion());
74 Version sv
= new Version(sourceVersion
);
75 if (versionToUse
.getMajor() != sv
.getMajor()
76 || versionToUse
.getMinor() != sv
.getMinor()
77 || versionToUse
.getMicro() != sv
.getMicro()) {
78 log
.warn("The new version ("
80 + ") is not consistant with the wrapped bundle version ("
85 versionToUse
= new Version(getVersion());
89 IOUtils
.write(jarBytes
, out
);
93 Properties properties
= new Properties();
94 properties
.putAll(bndProperties
);
95 properties
.setProperty(BUNDLE_SYMBOLICNAME
, name
);
96 properties
.setProperty(BUNDLE_VERSION
, versionToUse
.toString());
99 if (license
!= null) {
100 StringBuilder sb
= new StringBuilder(license
.getUri());
101 if (license
.getName() != null)
102 sb
.append(';').append("description=")
103 .append(license
.getName());
104 if (license
.getLink() != null)
105 sb
.append(';').append("link=")
106 .append(license
.getLink());
107 properties
.setProperty(BUNDLE_LICENSE
, sb
.toString());
108 // TODO add LICENSE.TXT
110 log
.warn("No license set for " + toString());
113 // b.addIncluded(jarFile);
116 if (log
.isDebugEnabled())
117 log
.debug(properties
);
118 b
.setProperties(properties
);
120 Jar newJar
= b
.build();
124 } catch (Exception e
) {
125 throw new SlcException("Cannot wrap jar", e
);
134 public Runnable
getFactory() {
138 public void setFactory(Runnable factory
) {
139 if (this.factory
!= null)
140 throw new SlcException("Factory already set on " + name
);
141 this.factory
= factory
;
144 public void setName(String bsn
) {
148 public String
getName() {
152 public void setVersion(String version
) {
153 if (this.version
!= null)
154 throw new SlcException("Version already set on " + name
+ " ("
155 + this.version
+ ")");
156 this.version
= version
;
159 public String
getVersion() {
163 public License
getLicense() {
167 public void setLicense(License license
) {
168 if (this.license
!= null)
169 throw new SlcException("License already set on " + name
);
170 this.license
= license
;
173 public Properties
getBndProperties() {
174 return bndProperties
;
177 public void setBndProperties(Properties bndProperties
) {
178 this.bndProperties
= bndProperties
;
181 public void setBeanName(String name
) {
182 if (this.name
== null) {
185 log
.warn("Using explicitely set name " + this.name
186 + " and not bean name " + name
);
190 public String
getGroupId() {
194 public String
getCategory() {
198 public void setGroupId(String groupId
) {
199 this.groupId
= groupId
;
202 public String
getDistributionId() {
203 return getArtifact().toString();
206 public Artifact
getArtifact() {
207 return new DefaultArtifact(groupId
, name
, "jar", getVersion());
211 public String
toString() {
212 return getArtifact().toString();
216 public int hashCode() {
217 return getArtifact().hashCode();
221 public boolean equals(Object obj
) {
222 if (obj
instanceof CategorizedNameVersion
) {
223 CategorizedNameVersion cnv
= (CategorizedNameVersion
) obj
;
224 return getCategory().equals(cnv
.getCategory())
225 && getName().equals(cnv
.getName())
226 && getVersion().equals(cnv
.getVersion());
231 public void setDoNotModify(Boolean doNotModify
) {
232 this.doNotModify
= doNotModify
;