X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.repo%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Frepo%2Fosgi%2FOsgiFactoryImpl.java;h=86a15ef27076416a678c2d9fe2630468ccbdf846;hb=5686259eb6e4da5006034087c71f349b3097be8d;hp=3381eb892cade33a7e61b666619874e50720d143;hpb=8ade4ac045a4d001ecb057f61e913f56b1d5dbb6;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/OsgiFactoryImpl.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/OsgiFactoryImpl.java index 3381eb892..86a15ef27 100644 --- a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/OsgiFactoryImpl.java +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/OsgiFactoryImpl.java @@ -1,11 +1,14 @@ package org.argeo.slc.repo.osgi; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.jcr.Node; import javax.jcr.Repository; @@ -21,8 +24,12 @@ import org.argeo.slc.jcr.SlcNames; 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; -public class OsgiFactoryImpl implements OsgiFactory { +/** Default implementation of {@link OsgiFactory}. */ +public class OsgiFactoryImpl implements OsgiFactory, SlcNames { private final static Log log = LogFactory.getLog(OsgiFactoryImpl.class); private String workspace; @@ -31,13 +38,28 @@ public class OsgiFactoryImpl implements OsgiFactory { private List nodeIndexers = new ArrayList(); + /** key is URI prefix, value list of base URLs */ + private Map> mirrors = new HashMap>(); + + private List mavenRepositories = new ArrayList(); + 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 { + // TODO rather user a JavaRepoManager that will also implicitely + // manage the indexing of newly created nodes. javaSession = JcrUtils.loginOrCreateWorkspace(javaRepository, workspace); distSession = JcrUtils.loginOrCreateWorkspace(distRepository, @@ -75,21 +97,91 @@ public class OsgiFactoryImpl implements OsgiFactory { } } - public Node getDist(Session distSession, String uri) + 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); - // TODO manage mirrors + // already retrieved + if (distSession.itemExists(distPath)) + return distSession.getNode(distPath); + + // find mirror + List urlBases = null; + String uriPrefix = null; + uriPrefixes: for (String uriPref : mirrors.keySet()) { + if (uri.startsWith(uriPref)) { + if (mirrors.get(uriPref).size() > 0) { + urlBases = mirrors.get(uriPref); + uriPrefix = uriPref; + break uriPrefixes; + } + } + } + if (urlBases == null) + try { + return loadUrlToPath(uri, distSession, distPath); + } catch (FileNotFoundException e) { + throw new SlcException("Cannot download " + uri, e); + } + + // try to download + for (String urlBase : urlBases) { + String relativePath = uri.substring(uriPrefix.length()); + String url = urlBase + relativePath; + try { + return loadUrlToPath(url, distSession, distPath); + } catch (FileNotFoundException e) { + if (log.isDebugEnabled()) + log.debug("Cannot download " + url + + ", trying another mirror"); + } + } - if (!distSession.itemExists(distPath)) - loadUrlToPath(uri, distSession, distPath); - Node distNode = distSession.getNode(distPath); - return distNode; + throw new SlcException("Could not download " + uri); } - protected void loadUrlToPath(String url, Session distSession, String path) - throws RepositoryException { + /** Actually downloads a file to an internal location */ + protected Node loadUrlToPath(String url, Session distSession, String path) + throws RepositoryException, FileNotFoundException { if (log.isDebugEnabled()) log.debug("Downloading " + url + "..."); @@ -102,13 +194,14 @@ public class OsgiFactoryImpl implements OsgiFactory { 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; } catch (MalformedURLException e) { throw new SlcException("URL " + url + " not valid.", e); + } catch (FileNotFoundException e) { + throw e; } catch (IOException e) { throw new SlcException("Cannot load " + url + " to " + path, e); } @@ -131,4 +224,8 @@ public class OsgiFactoryImpl implements OsgiFactory { this.nodeIndexers = nodeIndexers; } + public void setMirrors(Map> mirrors) { + this.mirrors = mirrors; + } + }