]> git.argeo.org Git - gpl/argeo-slc.git/blob - repo/osgi/BndWrapper.java
Prepare next development cycle
[gpl/argeo-slc.git] / repo / osgi / BndWrapper.java
1 package org.argeo.slc.repo.osgi;
2
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;
8
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.eclipse.aether.artifact.Artifact;
17 import org.eclipse.aether.artifact.DefaultArtifact;
18 import org.osgi.framework.Version;
19 import org.springframework.beans.factory.BeanNameAware;
20
21 import aQute.bnd.osgi.Builder;
22 import aQute.bnd.osgi.Constants;
23 import aQute.bnd.osgi.Jar;
24
25 /** Utilities around the BND library, which manipulates OSGi metadata. */
26 public class BndWrapper implements Constants, CategorizedNameVersion, Distribution, BeanNameAware {
27 private final static Log log = LogFactory.getLog(BndWrapper.class);
28
29 private String groupId;
30 private String name;
31 private Properties bndProperties = new Properties();
32
33 private String version;
34 private License license;
35
36 private Boolean doNotModify = false;
37
38 private Runnable factory = null;
39
40 public void wrapJar(InputStream in, OutputStream out) {
41 Builder b = new Builder();
42 Jar jar = null;
43 try {
44 byte[] jarBytes = IOUtils.toByteArray(in);
45
46 jar = new Jar(name, new ByteArrayInputStream(jarBytes));
47 Manifest sourceManifest = jar.getManifest();
48
49 Version versionToUse;
50 if (sourceManifest != null) {
51 // Symbolic name
52 String sourceSymbolicName = sourceManifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME);
53 if (sourceSymbolicName != null && !sourceSymbolicName.equals(name))
54 log.info("The new symbolic name (" + name
55 + ") is not consistant with the wrapped bundle symbolic name (" + sourceSymbolicName + ")");
56
57 // Version
58 String sourceVersion = sourceManifest.getMainAttributes().getValue(BUNDLE_VERSION);
59 if (getVersion() == null && sourceVersion == null) {
60 throw new SlcException("A bundle version must be defined.");
61 } else if (getVersion() == null && sourceVersion != null) {
62 versionToUse = new Version(sourceVersion);
63 version = sourceVersion; // set wrapper version
64 } else if (getVersion() != null && sourceVersion == null) {
65 versionToUse = new Version(getVersion());
66 } else {// both set
67 versionToUse = new Version(getVersion());
68 Version sv = new Version(sourceVersion);
69 if (versionToUse.getMajor() != sv.getMajor() || versionToUse.getMinor() != sv.getMinor()
70 || versionToUse.getMicro() != sv.getMicro()) {
71 log.warn("The new version (" + versionToUse
72 + ") is not consistant with the wrapped bundle version (" + sv + ")");
73 }
74 }
75 } else {
76 versionToUse = new Version(getVersion());
77 }
78
79 if (doNotModify) {
80 IOUtils.write(jarBytes, out);
81 // jar.write(out);
82 } else {
83
84 Properties properties = new Properties();
85 properties.putAll(bndProperties);
86 properties.setProperty(BUNDLE_SYMBOLICNAME, name);
87 properties.setProperty(BUNDLE_VERSION, versionToUse.toString());
88
89 // License
90 if (license != null) {
91 StringBuilder sb = new StringBuilder(license.getUri());
92 if (license.getName() != null)
93 sb.append(';').append("description=").append(license.getName());
94 if (license.getLink() != null)
95 sb.append(';').append("link=").append(license.getLink());
96 properties.setProperty(BUNDLE_LICENSE, sb.toString());
97 // TODO add LICENSE.TXT
98 } else {
99 log.warn("No license set for " + toString());
100 }
101
102 // b.addIncluded(jarFile);
103 b.addClasspath(jar);
104
105 if (log.isDebugEnabled())
106 log.debug(properties);
107 b.setProperties(properties);
108
109 Jar newJar = b.build();
110 newJar.write(out);
111 newJar.close();
112 }
113 } catch (Exception e) {
114 throw new SlcException("Cannot wrap jar", e);
115 } finally {
116 try {
117 b.close();
118 if (jar != null)
119 jar.close();
120 } catch (Exception e) {
121 // silent
122 }
123 }
124
125 }
126
127 public Runnable getFactory() {
128 return factory;
129 }
130
131 public void setFactory(Runnable factory) {
132 if (this.factory != null)
133 throw new SlcException("Factory already set on " + name);
134 this.factory = factory;
135 }
136
137 public void setName(String bsn) {
138 this.name = bsn;
139 }
140
141 public String getName() {
142 return name;
143 }
144
145 public void setVersion(String version) {
146 if (this.version != null)
147 throw new SlcException("Version already set on " + name + " (" + this.version + ")");
148 this.version = version;
149 }
150
151 public String getVersion() {
152 return version;
153 }
154
155 public License getLicense() {
156 return license;
157 }
158
159 public void setLicense(License license) {
160 if (this.license != null)
161 throw new SlcException("License already set on " + name);
162 this.license = license;
163 }
164
165 public Properties getBndProperties() {
166 return bndProperties;
167 }
168
169 public void setBndProperties(Properties bndProperties) {
170 this.bndProperties = bndProperties;
171 }
172
173 public void setBeanName(String name) {
174 if (this.name == null) {
175 this.name = name;
176 } else {
177 if (!name.contains("#"))
178 log.warn("Using explicitely set name " + this.name + " and not bean name " + name);
179 }
180 }
181
182 public String getGroupId() {
183 return groupId;
184 }
185
186 public String getCategory() {
187 return getGroupId();
188 }
189
190 public void setGroupId(String groupId) {
191 this.groupId = groupId;
192 }
193
194 public String getDistributionId() {
195 return getCategory() + ":" + getName() + ":" + getVersion();
196 }
197
198 public Artifact getArtifact() {
199 return new DefaultArtifact(groupId, name, "jar", getVersion());
200 }
201
202 @Override
203 public String toString() {
204 return getDistributionId();
205 }
206
207 @Override
208 public int hashCode() {
209 if (name != null)
210 return name.hashCode();
211 return super.hashCode();
212 }
213
214 @Override
215 public boolean equals(Object obj) {
216 if (obj instanceof CategorizedNameVersion) {
217 CategorizedNameVersion cnv = (CategorizedNameVersion) obj;
218 return getCategory().equals(cnv.getCategory()) && getName().equals(cnv.getName())
219 && getVersion().equals(cnv.getVersion());
220 } else
221 return false;
222 }
223
224 public void setDoNotModify(Boolean doNotModify) {
225 this.doNotModify = doNotModify;
226 }
227
228 public Boolean getDoNotModify() {
229 return doNotModify;
230 }
231
232 }