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