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