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 private Runnable factory
= null;
36 public void wrapJar(InputStream in
, OutputStream out
) {
37 Builder b
= new Builder();
39 Jar jar
= new Jar(null, in
);
41 Manifest sourceManifest
= jar
.getManifest();
44 if (sourceManifest
!= null) {
45 String sourceSymbolicName
= sourceManifest
.getMainAttributes()
46 .getValue(BUNDLE_SYMBOLICNAME
);
47 if (sourceSymbolicName
!= null
48 && sourceSymbolicName
.equals(name
))
49 log
.warn("The new symbolic name ("
51 + ") is not consistant with the wrapped bundle symbolic name ("
52 + sourceSymbolicName
+ ")");
54 String sourceVersion
= sourceManifest
.getMainAttributes()
55 .getValue(BUNDLE_VERSION
);
56 if (version
== null && sourceVersion
== null) {
57 throw new SlcException("A bundle version must be defined.");
58 } else if (version
== null && sourceVersion
!= null) {
59 versionToUse
= new Version(sourceVersion
);
60 version
= sourceVersion
; // set wrapper version
61 } else if (version
!= null && sourceVersion
== null) {
62 versionToUse
= new Version(version
);
64 versionToUse
= new Version(version
);
65 Version sv
= new Version(sourceVersion
);
66 if (versionToUse
.getMajor() != sv
.getMajor()
67 || versionToUse
.getMinor() != sv
.getMinor()
68 || versionToUse
.getMicro() != sv
.getMicro()) {
69 log
.warn("The new version ("
71 + ") is not consistant with the wrapped bundle version ("
76 versionToUse
= new Version(version
);
83 Properties properties
= new Properties();
84 properties
.putAll(bndProperties
);
85 properties
.setProperty(BUNDLE_SYMBOLICNAME
, name
);
86 properties
.setProperty(BUNDLE_VERSION
, versionToUse
.toString());
88 // b.addIncluded(jarFile);
91 if (log
.isDebugEnabled())
92 log
.debug(properties
);
93 b
.setProperties(properties
);
95 Jar newJar
= b
.build();
98 } catch (Exception e
) {
99 throw new SlcException("Cannot wrap jar", e
);
106 public Runnable
getFactory() {
110 public void setFactory(Runnable factory
) {
111 this.factory
= factory
;
114 public void setName(String bsn
) {
118 public String
getName() {
122 public void setVersion(String version
) {
123 this.version
= version
;
126 public String
getVersion() {
130 public Properties
getBndProperties() {
131 return bndProperties
;
134 public void setBndProperties(Properties bndProperties
) {
135 this.bndProperties
= bndProperties
;
138 public void setBeanName(String name
) {
142 public String
getGroupId() {
146 public String
getCategory() {
150 public void setGroupId(String groupId
) {
151 this.groupId
= groupId
;
154 public String
getDistributionId() {
155 return getArtifact().toString();
158 public Artifact
getArtifact() {
159 return new DefaultArtifact(groupId
, name
, "jar", version
);
163 public String
toString() {
164 return getArtifact().toString();
168 public int hashCode() {
169 return getArtifact().hashCode();
173 public boolean equals(Object obj
) {
174 if (obj
instanceof CategorizedNameVersion
) {
175 CategorizedNameVersion cnv
= (CategorizedNameVersion
) obj
;
176 return getCategory().equals(cnv
.getCategory())
177 && getName().equals(cnv
.getName())
178 && getVersion().equals(cnv
.getVersion());
183 public void setDoNotModify(Boolean doNotModify
) {
184 this.doNotModify
= doNotModify
;