1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.InputStream
;
4 import java
.io
.OutputStream
;
5 import java
.util
.Properties
;
6 import java
.util
.jar
.Manifest
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.slc
.CategorizedNameVersion
;
11 import org
.argeo
.slc
.SlcException
;
12 import org
.argeo
.slc
.build
.Distribution
;
13 import org
.argeo
.slc
.build
.License
;
14 import org
.osgi
.framework
.Version
;
15 import org
.sonatype
.aether
.artifact
.Artifact
;
16 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
17 import org
.springframework
.beans
.factory
.BeanNameAware
;
19 import aQute
.lib
.osgi
.Builder
;
20 import aQute
.lib
.osgi
.Constants
;
21 import aQute
.lib
.osgi
.Jar
;
23 /** Utilities around the BND library, which manipulates OSGi metadata. */
24 public class BndWrapper
implements Constants
, CategorizedNameVersion
,
25 Distribution
, BeanNameAware
{
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();
42 Jar jar
= new Jar(null, in
);
44 Manifest sourceManifest
= jar
.getManifest();
47 if (sourceManifest
!= null) {
49 String sourceSymbolicName
= sourceManifest
.getMainAttributes()
50 .getValue(BUNDLE_SYMBOLICNAME
);
51 if (sourceSymbolicName
!= null
52 && sourceSymbolicName
.equals(name
))
53 log
.warn("The new symbolic name ("
55 + ") is not consistant with the wrapped bundle symbolic name ("
56 + sourceSymbolicName
+ ")");
59 String sourceVersion
= sourceManifest
.getMainAttributes()
60 .getValue(BUNDLE_VERSION
);
61 if (version
== null && sourceVersion
== null) {
62 throw new SlcException("A bundle version must be defined.");
63 } else if (version
== null && sourceVersion
!= null) {
64 versionToUse
= new Version(sourceVersion
);
65 version
= sourceVersion
; // set wrapper version
66 } else if (version
!= null && sourceVersion
== null) {
67 versionToUse
= new Version(version
);
69 versionToUse
= new Version(version
);
70 Version sv
= new Version(sourceVersion
);
71 if (versionToUse
.getMajor() != sv
.getMajor()
72 || versionToUse
.getMinor() != sv
.getMinor()
73 || versionToUse
.getMicro() != sv
.getMicro()) {
74 log
.warn("The new version ("
76 + ") is not consistant with the wrapped bundle version ("
81 versionToUse
= new Version(version
);
88 Properties properties
= new Properties();
89 properties
.putAll(bndProperties
);
90 properties
.setProperty(BUNDLE_SYMBOLICNAME
, name
);
91 properties
.setProperty(BUNDLE_VERSION
, versionToUse
.toString());
94 if (license
!= null) {
95 StringBuilder sb
= new StringBuilder(license
.getUri());
96 if (license
.getName() != null)
97 sb
.append(';').append("description=")
98 .append(license
.getName());
99 if (license
.getLink() != null)
100 sb
.append(';').append("link=")
101 .append(license
.getLink());
102 properties
.setProperty(BUNDLE_LICENSE
, sb
.toString());
103 // TODO add LICENSE.TXT
105 log
.warn("No license set for " + toString());
108 // b.addIncluded(jarFile);
111 if (log
.isDebugEnabled())
112 log
.debug(properties
);
113 b
.setProperties(properties
);
115 Jar newJar
= b
.build();
118 } catch (Exception e
) {
119 throw new SlcException("Cannot wrap jar", 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
+ " ("
147 + 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
) {
177 public String
getGroupId() {
181 public String
getCategory() {
185 public void setGroupId(String groupId
) {
186 this.groupId
= groupId
;
189 public String
getDistributionId() {
190 return getArtifact().toString();
193 public Artifact
getArtifact() {
194 return new DefaultArtifact(groupId
, name
, "jar", version
);
198 public String
toString() {
199 return getArtifact().toString();
203 public int hashCode() {
204 return getArtifact().hashCode();
208 public boolean equals(Object obj
) {
209 if (obj
instanceof CategorizedNameVersion
) {
210 CategorizedNameVersion cnv
= (CategorizedNameVersion
) obj
;
211 return getCategory().equals(cnv
.getCategory())
212 && getName().equals(cnv
.getName())
213 && getVersion().equals(cnv
.getVersion());
218 public void setDoNotModify(Boolean doNotModify
) {
219 this.doNotModify
= doNotModify
;