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