package org.argeo.slc.repo.osgi;
+import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.jar.Manifest;
+import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.argeo.slc.CategorizedNameVersion;
public void wrapJar(InputStream in, OutputStream out) {
Builder b = new Builder();
+ Jar jar = null;
try {
- Jar jar = new Jar(null, in);
+ byte[] jarBytes = IOUtils.toByteArray(in);
+ jar = new Jar(null, new ByteArrayInputStream(jarBytes));
Manifest sourceManifest = jar.getManifest();
Version versionToUse;
// Version
String sourceVersion = sourceManifest.getMainAttributes()
.getValue(BUNDLE_VERSION);
- if (version == null && sourceVersion == null) {
+ if (getVersion() == null && sourceVersion == null) {
throw new SlcException("A bundle version must be defined.");
- } else if (version == null && sourceVersion != null) {
+ } else if (getVersion() == null && sourceVersion != null) {
versionToUse = new Version(sourceVersion);
version = sourceVersion; // set wrapper version
- } else if (version != null && sourceVersion == null) {
- versionToUse = new Version(version);
+ } else if (getVersion() != null && sourceVersion == null) {
+ versionToUse = new Version(getVersion());
} else {// both set
- versionToUse = new Version(version);
+ versionToUse = new Version(getVersion());
Version sv = new Version(sourceVersion);
if (versionToUse.getMajor() != sv.getMajor()
|| versionToUse.getMinor() != sv.getMinor()
}
}
} else {
- versionToUse = new Version(version);
+ versionToUse = new Version(getVersion());
}
if (doNotModify) {
- jar.write(out);
+ IOUtils.write(jarBytes, out);
+ // jar.write(out);
} else {
Properties properties = new Properties();
Jar newJar = b.build();
newJar.write(out);
+ newJar.close();
}
} catch (Exception e) {
throw new SlcException("Cannot wrap jar", e);
} finally {
b.close();
+ if (jar != null)
+ jar.close();
}
}
}
public Artifact getArtifact() {
- return new DefaultArtifact(groupId, name, "jar", version);
+ return new DefaultArtifact(groupId, name, "jar", getVersion());
}
@Override
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.artifact.SubArtifact;
+/**
+ * BND wrapper based on a Maven artifact available from one of the configured
+ * repositories.
+ */
public class MavenWrapper extends BndWrapper implements Runnable {
private final static Log log = LogFactory.getLog(MavenWrapper.class);
setFactory(this);
}
+ @Override
+ public String getVersion() {
+ String version = super.getVersion();
+ if (version != null)
+ return version;
+ return new DefaultArtifact(sourceCoords).getVersion();
+ }
+
public void run() {
Session distSession = null;
Session javaSession = null;
public void run() {
Session distSession = null;
Session javaSession = null;
- InputStream in;
- ByteArrayOutputStream out;
+ InputStream in = null;
+ ByteArrayOutputStream out = null;
Jar jar = null;
try {
distSession = osgiFactory.openDistSession();
// TODO factorize with Maven
in = sourceArtifact.getNode(Node.JCR_CONTENT)
.getProperty(Property.JCR_DATA).getBinary().getStream();
- out=null;
-
out = new ByteArrayOutputStream();
wrapJar(in, out);
Node newJarNode = RepoUtils
} catch (Exception e) {
throw new SlcException("Cannot wrap URI " + uri, e);
} finally {
+ IOUtils.closeQuietly(in);
+ IOUtils.closeQuietly(out);
JcrUtils.logoutQuietly(distSession);
JcrUtils.logoutQuietly(javaSession);
if (jar != null)