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 private Boolean doNotModify
= false;
34 public void wrapJar(InputStream in
, OutputStream out
) {
35 Builder b
= new Builder();
37 Jar jar
= new Jar(null, in
);
39 Manifest sourceManifest
= jar
.getManifest();
42 if (sourceManifest
!= null) {
43 String sourceSymbolicName
= sourceManifest
.getMainAttributes()
44 .getValue(BUNDLE_SYMBOLICNAME
);
45 if (sourceSymbolicName
!= null
46 && sourceSymbolicName
.equals(name
))
47 log
.warn("The new symbolic name ("
49 + ") is not consistant with the wrapped bundle symbolic name ("
50 + sourceSymbolicName
+ ")");
52 String sourceVersion
= sourceManifest
.getMainAttributes()
53 .getValue(BUNDLE_VERSION
);
54 if (version
== null && sourceVersion
== null) {
55 throw new SlcException("A bundle version must be defined.");
56 } else if (version
== null && sourceVersion
!= null) {
57 versionToUse
= new Version(sourceVersion
);
58 version
= sourceVersion
; // set wrapper version
59 } else if (version
!= null && sourceVersion
== null) {
60 versionToUse
= new Version(version
);
62 versionToUse
= new Version(version
);
63 Version sv
= new Version(sourceVersion
);
64 if (versionToUse
.getMajor() != sv
.getMajor()
65 || versionToUse
.getMinor() != sv
.getMinor()
66 || versionToUse
.getMicro() != sv
.getMicro()) {
67 log
.warn("The new version ("
69 + ") is not consistant with the wrapped bundle version ("
74 versionToUse
= new Version(version
);
81 Properties properties
= new Properties();
82 properties
.putAll(bndProperties
);
83 properties
.setProperty(BUNDLE_SYMBOLICNAME
, name
);
84 properties
.setProperty(BUNDLE_VERSION
, versionToUse
.toString());
86 // b.addIncluded(jarFile);
89 if (log
.isDebugEnabled())
90 log
.debug(properties
);
91 b
.setProperties(properties
);
93 Jar newJar
= b
.build();
96 } catch (Exception e
) {
97 throw new SlcException("Cannot wrap jar", e
);
104 public void setName(String bsn
) {
108 public String
getName() {
112 public void setVersion(String version
) {
113 this.version
= version
;
116 public String
getVersion() {
120 public Properties
getBndProperties() {
121 return bndProperties
;
124 public void setBndProperties(Properties bndProperties
) {
125 this.bndProperties
= bndProperties
;
128 public void setBeanName(String name
) {
132 public String
getGroupId() {
136 public String
getCategory() {
140 public void setGroupId(String groupId
) {
141 this.groupId
= groupId
;
144 public String
getDistributionId() {
145 return getArtifact().toString();
148 public Artifact
getArtifact() {
149 return new DefaultArtifact(groupId
, name
, "jar", version
);
153 public String
toString() {
154 return getArtifact().toString();
158 public int hashCode() {
159 return getArtifact().hashCode();
163 public boolean equals(Object obj
) {
164 if (obj
instanceof CategorizedNameVersion
) {
165 CategorizedNameVersion cnv
= (CategorizedNameVersion
) obj
;
166 return getCategory().equals(cnv
.getCategory())
167 && getName().equals(cnv
.getName())
168 && getVersion().equals(cnv
.getVersion());
173 public void setDoNotModify(Boolean doNotModify
) {
174 this.doNotModify
= doNotModify
;