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