*/
public Node getDist(Session distSession, String uri)
throws RepositoryException;
+
+ /**
+ * Provide access to a cached maven ardifact identified by its coordinates
+ * the 'dist' repository, downloading it if it is not available.
+ */
+ public Node getMaven(Session distSession, String coords)
+ throws RepositoryException;
}
sourceJarBytes, wrapper);
addArtifactToIndex(binaries, wrapper.getGroupId(), artifact);
} else {
+ for (String wrapperKey : wrappers.keySet())
+ if (pathMatcher.match(wrapperKey, name)) {
+ // first matched is taken
+ BndWrapper wrapper = (BndWrapper) wrappers
+ .get(wrapperKey);
+ // we must copy since the stream is closed by BND
+ byte[] sourceJarBytes = IOUtils.toByteArray(zin);
+ Artifact artifact = wrapZipEntry(javaSession,
+ zentry, sourceJarBytes, wrapper);
+ addArtifactToIndex(binaries, wrapper.getGroupId(),
+ artifact);
+ continue entries;
+ } else {
+ if (log.isTraceEnabled())
+ log.trace(name + " not matched by "
+ + wrapperKey);
+ }
+
for (String exclude : excludes)
if (pathMatcher.match(exclude, name))
continue entries;
Jar jar = new Jar(null, in);
Manifest sourceManifest = jar.getManifest();
- String sourceVersion = sourceManifest.getMainAttributes().getValue(
- BUNDLE_VERSION);
+
Version versionToUse;
- if (version == null && sourceVersion == null) {
- throw new SlcException("A bundle version must be defined.");
- } else if (version == null && sourceVersion != null) {
- versionToUse = new Version(sourceVersion);
- } else if (version != null && sourceVersion == null) {
- versionToUse = new Version(version);
- } else {// both set
- versionToUse = new Version(version);
- Version sv = new Version(sourceVersion);
- if (versionToUse.getMajor() != sv.getMajor()
- || versionToUse.getMinor() != sv.getMinor()
- || versionToUse.getMicro() != sv.getMicro()) {
- log.warn("The new version ("
- + versionToUse
- + ") is not consistant with the wrapped bundle version ("
- + sv + ")");
+ if (sourceManifest != null) {
+ String sourceSymbolicName = sourceManifest.getMainAttributes()
+ .getValue(BUNDLE_SYMBOLICNAME);
+ if (sourceSymbolicName != null
+ && sourceSymbolicName.equals(name))
+ log.warn("The new symbolic name ("
+ + name
+ + ") is not consistant with the wrapped bundle symbolic name ("
+ + sourceSymbolicName + ")");
+
+ String sourceVersion = sourceManifest.getMainAttributes()
+ .getValue(BUNDLE_VERSION);
+ if (version == null && sourceVersion == null) {
+ throw new SlcException("A bundle version must be defined.");
+ } else if (version == null && sourceVersion != null) {
+ versionToUse = new Version(sourceVersion);
+ version = sourceVersion; // set wrapper version
+ } else if (version != null && sourceVersion == null) {
+ versionToUse = new Version(version);
+ } else {// both set
+ versionToUse = new Version(version);
+ Version sv = new Version(sourceVersion);
+ if (versionToUse.getMajor() != sv.getMajor()
+ || versionToUse.getMinor() != sv.getMinor()
+ || versionToUse.getMicro() != sv.getMicro()) {
+ log.warn("The new version ("
+ + versionToUse
+ + ") is not consistant with the wrapped bundle version ("
+ + sv + ")");
+ }
}
+ } else {
+ versionToUse = new Version(version);
}
Properties properties = new Properties();
--- /dev/null
+package org.argeo.slc.repo.osgi;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.repo.OsgiFactory;
+import org.argeo.slc.repo.RepoUtils;
+
+public class MavenWrapper extends BndWrapper implements Runnable {
+ private final static Log log = LogFactory.getLog(MavenWrapper.class);
+
+ private String sourceCoords;
+
+ private OsgiFactory osgiFactory;
+
+ public void run() {
+ Session distSession = null;
+ Session javaSession = null;
+ InputStream in;
+ ByteArrayOutputStream out;
+ try {
+ distSession = osgiFactory.openDistSession();
+ javaSession = osgiFactory.openJavaSession();
+ Node sourceArtifact = osgiFactory.getMaven(distSession,
+ sourceCoords);
+
+ in = sourceArtifact.getNode(Node.JCR_CONTENT)
+ .getProperty(Property.JCR_DATA).getBinary().getStream();
+ out = new ByteArrayOutputStream();
+ wrapJar(in, out);
+ Node newJarNode = RepoUtils
+ .copyBytesAsArtifact(javaSession.getRootNode(),
+ getArtifact(), out.toByteArray());
+ osgiFactory.indexNode(newJarNode);
+ newJarNode.getSession().save();
+ if (log.isDebugEnabled())
+ log.debug("Wrapped Maven " + sourceCoords + " to "
+ + newJarNode.getPath());
+ } catch (RepositoryException e) {
+ throw new SlcException("Cannot wrap Maven " + sourceCoords, e);
+ } finally {
+ JcrUtils.logoutQuietly(distSession);
+ JcrUtils.logoutQuietly(javaSession);
+ }
+ }
+
+ public void setSourceCoords(String sourceCoords) {
+ this.sourceCoords = sourceCoords;
+ }
+
+ public void setOsgiFactory(OsgiFactory osgiFactory) {
+ this.osgiFactory = osgiFactory;
+ }
+
+}
import org.argeo.slc.jcr.SlcTypes;
import org.argeo.slc.repo.NodeIndexer;
import org.argeo.slc.repo.OsgiFactory;
+import org.argeo.slc.repo.maven.MavenConventionsUtils;
+import org.sonatype.aether.artifact.Artifact;
+import org.sonatype.aether.util.artifact.DefaultArtifact;
/** Default implementation of {@link OsgiFactory}. */
-public class OsgiFactoryImpl implements OsgiFactory {
+public class OsgiFactoryImpl implements OsgiFactory, SlcNames {
private final static Log log = LogFactory.getLog(OsgiFactoryImpl.class);
private String workspace;
/** key is URI prefix, value list of base URLs */
private Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
+ private List<String> mavenRepositories = new ArrayList<String>();
+ private String mavenProxyBase = "/mavenProxy";
+
public void init() {
if (workspace == null)
throw new SlcException("A workspace must be specified");
+ // default Maven repo
+ if (mavenRepositories.size() == 0) {
+ // mavenRepositories
+ // .add("http://search.maven.org/remotecontent?filepath=");
+ mavenRepositories.add("http://repo1.maven.org/maven2");
+ }
+
Session javaSession = null;
Session distSession = null;
try {
}
}
+ public Node getMaven(Session distSession, String coords)
+ throws RepositoryException {
+ Artifact artifact = new DefaultArtifact(coords);
+ String path = MavenConventionsUtils.artifactPath(mavenProxyBase,
+ artifact);
+
+ // exists
+ if (distSession.itemExists(path))
+ return distSession.getNode(path);
+
+ for (String mavenRepo : mavenRepositories) {
+ String url = mavenRepo
+ + MavenConventionsUtils.artifactPath("/", artifact);
+ try {
+ Node node = loadUrlToPath(url, distSession, path);
+ if (node != null) {
+ // checksums
+ try {
+ loadUrlToPath(url + ".md5", distSession, path + ".md5");
+ } catch (FileNotFoundException e) {
+ // silent
+ }
+ try {
+ loadUrlToPath(url + ".sha1", distSession, path
+ + ".sha1");
+ } catch (FileNotFoundException e) {
+ // silent
+ }
+ return node;
+ }
+ } catch (FileNotFoundException e) {
+ if (log.isDebugEnabled())
+ log.debug("Maven " + coords
+ + " could not be downloaded from " + url);
+ }
+ }
+ throw new SlcException("Could not download Maven " + coords);
+ }
+
public Node getDist(Session distSession, String uri)
throws RepositoryException {
String distPath = '/' + JcrUtils.urlAsPath(uri);
throw new SlcException("Could not download " + uri);
}
+ /** Actually downloads a file to an internal location */
protected Node loadUrlToPath(String url, Session distSession, String path)
throws RepositoryException, FileNotFoundException {
if (log.isDebugEnabled())
Node fileNode = JcrUtils.copyStreamAsFile(folderNode,
JcrUtils.nodeNameFromPath(path), in);
fileNode.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
- Node origin = fileNode.addNode(SlcNames.SLC_ORIGIN,
- SlcTypes.SLC_PROXIED);
+ Node origin = fileNode.addNode(SLC_ORIGIN, SlcTypes.SLC_PROXIED);
JcrUtils.urlToAddressProperties(origin, url);
distSession.save();
return fileNode;