]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/BndWrapper.java
Implement order for versions and name versions.
[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.InputStream;
4 import java.io.OutputStream;
5 import java.util.Properties;
6 import java.util.jar.Manifest;
7
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.argeo.slc.build.License;
14 import org.osgi.framework.Version;
15 import org.sonatype.aether.artifact.Artifact;
16 import org.sonatype.aether.util.artifact.DefaultArtifact;
17 import org.springframework.beans.factory.BeanNameAware;
18
19 import aQute.lib.osgi.Builder;
20 import aQute.lib.osgi.Constants;
21 import aQute.lib.osgi.Jar;
22
23 /** Utilities around the BND library, which manipulates OSGi metadata. */
24 public class BndWrapper implements Constants, CategorizedNameVersion,
25 Distribution, BeanNameAware {
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 try {
42 Jar jar = new Jar(null, in);
43
44 Manifest sourceManifest = jar.getManifest();
45
46 Version versionToUse;
47 if (sourceManifest != null) {
48 // Symbolic name
49 String sourceSymbolicName = sourceManifest.getMainAttributes()
50 .getValue(BUNDLE_SYMBOLICNAME);
51 if (sourceSymbolicName != null
52 && !sourceSymbolicName.equals(name))
53 log.warn("The new symbolic name ("
54 + name
55 + ") is not consistant with the wrapped bundle symbolic name ("
56 + sourceSymbolicName + ")");
57
58 // Version
59 String sourceVersion = sourceManifest.getMainAttributes()
60 .getValue(BUNDLE_VERSION);
61 if (version == null && sourceVersion == null) {
62 throw new SlcException("A bundle version must be defined.");
63 } else if (version == null && sourceVersion != null) {
64 versionToUse = new Version(sourceVersion);
65 version = sourceVersion; // set wrapper version
66 } else if (version != null && sourceVersion == null) {
67 versionToUse = new Version(version);
68 } else {// both set
69 versionToUse = new Version(version);
70 Version sv = new Version(sourceVersion);
71 if (versionToUse.getMajor() != sv.getMajor()
72 || versionToUse.getMinor() != sv.getMinor()
73 || versionToUse.getMicro() != sv.getMicro()) {
74 log.warn("The new version ("
75 + versionToUse
76 + ") is not consistant with the wrapped bundle version ("
77 + sv + ")");
78 }
79 }
80 } else {
81 versionToUse = new Version(version);
82 }
83
84 if (doNotModify) {
85 jar.write(out);
86 } else {
87
88 Properties properties = new Properties();
89 properties.putAll(bndProperties);
90 properties.setProperty(BUNDLE_SYMBOLICNAME, name);
91 properties.setProperty(BUNDLE_VERSION, versionToUse.toString());
92
93 // License
94 if (license != null) {
95 StringBuilder sb = new StringBuilder(license.getUri());
96 if (license.getName() != null)
97 sb.append(';').append("description=")
98 .append(license.getName());
99 if (license.getLink() != null)
100 sb.append(';').append("link=")
101 .append(license.getLink());
102 properties.setProperty(BUNDLE_LICENSE, sb.toString());
103 // TODO add LICENSE.TXT
104 } else {
105 log.warn("No license set for " + toString());
106 }
107
108 // b.addIncluded(jarFile);
109 b.addClasspath(jar);
110
111 if (log.isDebugEnabled())
112 log.debug(properties);
113 b.setProperties(properties);
114
115 Jar newJar = b.build();
116 newJar.write(out);
117 }
118 } catch (Exception e) {
119 throw new SlcException("Cannot wrap jar", e);
120 } finally {
121 b.close();
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 + " ("
147 + 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 this.name = name;
175 }
176
177 public String getGroupId() {
178 return groupId;
179 }
180
181 public String getCategory() {
182 return getGroupId();
183 }
184
185 public void setGroupId(String groupId) {
186 this.groupId = groupId;
187 }
188
189 public String getDistributionId() {
190 return getArtifact().toString();
191 }
192
193 public Artifact getArtifact() {
194 return new DefaultArtifact(groupId, name, "jar", version);
195 }
196
197 @Override
198 public String toString() {
199 return getArtifact().toString();
200 }
201
202 @Override
203 public int hashCode() {
204 return getArtifact().hashCode();
205 }
206
207 @Override
208 public boolean equals(Object obj) {
209 if (obj instanceof CategorizedNameVersion) {
210 CategorizedNameVersion cnv = (CategorizedNameVersion) obj;
211 return getCategory().equals(cnv.getCategory())
212 && getName().equals(cnv.getName())
213 && getVersion().equals(cnv.getVersion());
214 } else
215 return false;
216 }
217
218 public void setDoNotModify(Boolean doNotModify) {
219 this.doNotModify = doNotModify;
220 }
221
222 }