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
.osgi
.framework
.Version
;
14 import org
.sonatype
.aether
.artifact
.Artifact
;
15 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
16 import org
.springframework
.beans
.factory
.BeanNameAware
;
18 import aQute
.lib
.osgi
.Builder
;
19 import aQute
.lib
.osgi
.Constants
;
20 import aQute
.lib
.osgi
.Jar
;
22 /** Utilities around the BND library, which manipulates OSGi metadata. */
23 public class BndWrapper
implements Constants
, CategorizedNameVersion
,
24 Distribution
, BeanNameAware
{
25 private final static Log log
= LogFactory
.getLog(BndWrapper
.class);
27 private String groupId
;
29 private String version
;
30 private Properties bndProperties
= new Properties();
32 public void wrapJar(InputStream in
, OutputStream out
) {
33 Builder b
= new Builder();
35 Jar jar
= new Jar(null, in
);
37 Manifest sourceManifest
= jar
.getManifest();
40 if (sourceManifest
!= null) {
41 String sourceSymbolicName
= sourceManifest
.getMainAttributes()
42 .getValue(BUNDLE_SYMBOLICNAME
);
43 if (sourceSymbolicName
!= null
44 && sourceSymbolicName
.equals(name
))
45 log
.warn("The new symbolic name ("
47 + ") is not consistant with the wrapped bundle symbolic name ("
48 + sourceSymbolicName
+ ")");
50 String sourceVersion
= sourceManifest
.getMainAttributes()
51 .getValue(BUNDLE_VERSION
);
52 if (version
== null && sourceVersion
== null) {
53 throw new SlcException("A bundle version must be defined.");
54 } else if (version
== null && sourceVersion
!= null) {
55 versionToUse
= new Version(sourceVersion
);
56 version
= sourceVersion
; // set wrapper version
57 } else if (version
!= null && sourceVersion
== null) {
58 versionToUse
= new Version(version
);
60 versionToUse
= new Version(version
);
61 Version sv
= new Version(sourceVersion
);
62 if (versionToUse
.getMajor() != sv
.getMajor()
63 || versionToUse
.getMinor() != sv
.getMinor()
64 || versionToUse
.getMicro() != sv
.getMicro()) {
65 log
.warn("The new version ("
67 + ") is not consistant with the wrapped bundle version ("
72 versionToUse
= new Version(version
);
75 Properties properties
= new Properties();
76 properties
.putAll(bndProperties
);
77 properties
.setProperty(BUNDLE_SYMBOLICNAME
, name
);
78 properties
.setProperty(BUNDLE_VERSION
, versionToUse
.toString());
80 // b.addIncluded(jarFile);
83 log
.debug(properties
);
84 b
.setProperties(properties
);
86 Jar newJar
= b
.build();
88 } catch (Exception e
) {
89 throw new SlcException("Cannot wrap jar", e
);
96 public void setName(String bsn
) {
100 public String
getName() {
104 public void setVersion(String version
) {
105 this.version
= version
;
108 public String
getVersion() {
112 public Properties
getBndProperties() {
113 return bndProperties
;
116 public void setBndProperties(Properties bndProperties
) {
117 this.bndProperties
= bndProperties
;
120 public void setBeanName(String name
) {
124 public String
getGroupId() {
128 public String
getCategory() {
132 public void setGroupId(String groupId
) {
133 this.groupId
= groupId
;
136 public String
getDistributionId() {
137 return getArtifact().toString();
140 public Artifact
getArtifact() {
141 return new DefaultArtifact(groupId
, name
, "jar", version
);
145 public String
toString() {
146 return getArtifact().toString();
150 public int hashCode() {
151 return getArtifact().hashCode();
155 public boolean equals(Object obj
) {
156 if (obj
instanceof CategorizedNameVersion
) {
157 CategorizedNameVersion cnv
= (CategorizedNameVersion
) obj
;
158 return getCategory().equals(cnv
.getCategory())
159 && getName().equals(cnv
.getName())
160 && getVersion().equals(cnv
.getVersion());