]> git.argeo.org Git - gpl/argeo-slc.git/blob - BndWrapper.java
f1acf49e4d8c54fb2e10c3d953ab73db1455beba
[gpl/argeo-slc.git] / 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 public void wrapJar(InputStream in, OutputStream out) {
35 Builder b = new Builder();
36 try {
37 Jar jar = new Jar(null, in);
38
39 Manifest sourceManifest = jar.getManifest();
40
41 Version versionToUse;
42 if (sourceManifest != null) {
43 String sourceSymbolicName = sourceManifest.getMainAttributes()
44 .getValue(BUNDLE_SYMBOLICNAME);
45 if (sourceSymbolicName != null
46 && sourceSymbolicName.equals(name))
47 log.warn("The new symbolic name ("
48 + name
49 + ") is not consistant with the wrapped bundle symbolic name ("
50 + sourceSymbolicName + ")");
51
52 String sourceVersion = sourceManifest.getMainAttributes()
53 .getValue(BUNDLE_VERSION);
54 if (version == null && sourceVersion == null) {
55 throw new SlcException("A bundle version must be defined.");
56 } else if (version == null && sourceVersion != null) {
57 versionToUse = new Version(sourceVersion);
58 version = sourceVersion; // set wrapper version
59 } else if (version != null && sourceVersion == null) {
60 versionToUse = new Version(version);
61 } else {// both set
62 versionToUse = new Version(version);
63 Version sv = new Version(sourceVersion);
64 if (versionToUse.getMajor() != sv.getMajor()
65 || versionToUse.getMinor() != sv.getMinor()
66 || versionToUse.getMicro() != sv.getMicro()) {
67 log.warn("The new version ("
68 + versionToUse
69 + ") is not consistant with the wrapped bundle version ("
70 + sv + ")");
71 }
72 }
73 } else {
74 versionToUse = new Version(version);
75 }
76
77 if (doNotModify) {
78 jar.write(out);
79 } else {
80
81 Properties properties = new Properties();
82 properties.putAll(bndProperties);
83 properties.setProperty(BUNDLE_SYMBOLICNAME, name);
84 properties.setProperty(BUNDLE_VERSION, versionToUse.toString());
85
86 // b.addIncluded(jarFile);
87 b.addClasspath(jar);
88
89 if (log.isDebugEnabled())
90 log.debug(properties);
91 b.setProperties(properties);
92
93 Jar newJar = b.build();
94 newJar.write(out);
95 }
96 } catch (Exception e) {
97 throw new SlcException("Cannot wrap jar", e);
98 } finally {
99 b.close();
100 }
101
102 }
103
104 public void setName(String bsn) {
105 this.name = bsn;
106 }
107
108 public String getName() {
109 return name;
110 }
111
112 public void setVersion(String version) {
113 this.version = version;
114 }
115
116 public String getVersion() {
117 return version;
118 }
119
120 public Properties getBndProperties() {
121 return bndProperties;
122 }
123
124 public void setBndProperties(Properties bndProperties) {
125 this.bndProperties = bndProperties;
126 }
127
128 public void setBeanName(String name) {
129 this.name = name;
130 }
131
132 public String getGroupId() {
133 return groupId;
134 }
135
136 public String getCategory() {
137 return getGroupId();
138 }
139
140 public void setGroupId(String groupId) {
141 this.groupId = groupId;
142 }
143
144 public String getDistributionId() {
145 return getArtifact().toString();
146 }
147
148 public Artifact getArtifact() {
149 return new DefaultArtifact(groupId, name, "jar", version);
150 }
151
152 @Override
153 public String toString() {
154 return getArtifact().toString();
155 }
156
157 @Override
158 public int hashCode() {
159 return getArtifact().hashCode();
160 }
161
162 @Override
163 public boolean equals(Object obj) {
164 if (obj instanceof CategorizedNameVersion) {
165 CategorizedNameVersion cnv = (CategorizedNameVersion) obj;
166 return getCategory().equals(cnv.getCategory())
167 && getName().equals(cnv.getName())
168 && getVersion().equals(cnv.getVersion());
169 } else
170 return false;
171 }
172
173 public void setDoNotModify(Boolean doNotModify) {
174 this.doNotModify = doNotModify;
175 }
176
177 }