]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/BndWrapper.java
Fix unclosed jar
[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.osgi.framework.Version;
14 import org.sonatype.aether.artifact.Artifact;
15 import org.sonatype.aether.util.artifact.DefaultArtifact;
16 import org.springframework.beans.factory.BeanNameAware;
17
18 import aQute.lib.osgi.Builder;
19 import aQute.lib.osgi.Constants;
20 import aQute.lib.osgi.Jar;
21
22 /** Utilities around the BND library, which manipulates OSGi metadata. */
23 public class BndWrapper implements Constants, CategorizedNameVersion,
24 Distribution, BeanNameAware {
25 private final static Log log = LogFactory.getLog(BndWrapper.class);
26
27 private String groupId;
28 private String name;
29 private String version;
30 private Properties bndProperties = new Properties();
31
32 private Boolean doNotModify = false;
33
34 private Runnable factory = null;
35
36 public void wrapJar(InputStream in, OutputStream out) {
37 Builder b = new Builder();
38 try {
39 Jar jar = new Jar(null, in);
40
41 Manifest sourceManifest = jar.getManifest();
42
43 Version versionToUse;
44 if (sourceManifest != null) {
45 String sourceSymbolicName = sourceManifest.getMainAttributes()
46 .getValue(BUNDLE_SYMBOLICNAME);
47 if (sourceSymbolicName != null
48 && sourceSymbolicName.equals(name))
49 log.warn("The new symbolic name ("
50 + name
51 + ") is not consistant with the wrapped bundle symbolic name ("
52 + sourceSymbolicName + ")");
53
54 String sourceVersion = sourceManifest.getMainAttributes()
55 .getValue(BUNDLE_VERSION);
56 if (version == null && sourceVersion == null) {
57 throw new SlcException("A bundle version must be defined.");
58 } else if (version == null && sourceVersion != null) {
59 versionToUse = new Version(sourceVersion);
60 version = sourceVersion; // set wrapper version
61 } else if (version != null && sourceVersion == null) {
62 versionToUse = new Version(version);
63 } else {// both set
64 versionToUse = new Version(version);
65 Version sv = new Version(sourceVersion);
66 if (versionToUse.getMajor() != sv.getMajor()
67 || versionToUse.getMinor() != sv.getMinor()
68 || versionToUse.getMicro() != sv.getMicro()) {
69 log.warn("The new version ("
70 + versionToUse
71 + ") is not consistant with the wrapped bundle version ("
72 + sv + ")");
73 }
74 }
75 } else {
76 versionToUse = new Version(version);
77 }
78
79 if (doNotModify) {
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 // b.addIncluded(jarFile);
89 b.addClasspath(jar);
90
91 if (log.isDebugEnabled())
92 log.debug(properties);
93 b.setProperties(properties);
94
95 Jar newJar = b.build();
96 newJar.write(out);
97 }
98 } catch (Exception e) {
99 throw new SlcException("Cannot wrap jar", e);
100 } finally {
101 b.close();
102 }
103
104 }
105
106 public Runnable getFactory() {
107 return factory;
108 }
109
110 public void setFactory(Runnable factory) {
111 this.factory = factory;
112 }
113
114 public void setName(String bsn) {
115 this.name = bsn;
116 }
117
118 public String getName() {
119 return name;
120 }
121
122 public void setVersion(String version) {
123 this.version = version;
124 }
125
126 public String getVersion() {
127 return version;
128 }
129
130 public Properties getBndProperties() {
131 return bndProperties;
132 }
133
134 public void setBndProperties(Properties bndProperties) {
135 this.bndProperties = bndProperties;
136 }
137
138 public void setBeanName(String name) {
139 this.name = name;
140 }
141
142 public String getGroupId() {
143 return groupId;
144 }
145
146 public String getCategory() {
147 return getGroupId();
148 }
149
150 public void setGroupId(String groupId) {
151 this.groupId = groupId;
152 }
153
154 public String getDistributionId() {
155 return getArtifact().toString();
156 }
157
158 public Artifact getArtifact() {
159 return new DefaultArtifact(groupId, name, "jar", version);
160 }
161
162 @Override
163 public String toString() {
164 return getArtifact().toString();
165 }
166
167 @Override
168 public int hashCode() {
169 return getArtifact().hashCode();
170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 if (obj instanceof CategorizedNameVersion) {
175 CategorizedNameVersion cnv = (CategorizedNameVersion) obj;
176 return getCategory().equals(cnv.getCategory())
177 && getName().equals(cnv.getName())
178 && getVersion().equals(cnv.getVersion());
179 } else
180 return false;
181 }
182
183 public void setDoNotModify(Boolean doNotModify) {
184 this.doNotModify = doNotModify;
185 }
186
187 }