]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/OsgiFactoryImpl.java
Manage automatically *-sources.jar sources
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / OsgiFactoryImpl.java
index ab4f9775cf37c5ffa6a5eb48b09d5cbb86459019..86a15ef27076416a678c2d9fe2630468ccbdf846 100644 (file)
@@ -24,9 +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;
 
 /** 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;
@@ -38,13 +41,25 @@ public class OsgiFactoryImpl implements OsgiFactory {
        /** 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 {
+                       // 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,
@@ -82,6 +97,45 @@ public class OsgiFactoryImpl implements OsgiFactory {
                }
        }
 
+       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);
@@ -117,7 +171,7 @@ public class OsgiFactoryImpl implements OsgiFactory {
                                return loadUrlToPath(url, distSession, distPath);
                        } catch (FileNotFoundException e) {
                                if (log.isDebugEnabled())
-                                       log.debug("Cannot download" + url
+                                       log.debug("Cannot download " + url
                                                        + ", trying another mirror");
                        }
                }
@@ -125,6 +179,7 @@ public class OsgiFactoryImpl implements OsgiFactory {
                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())
@@ -139,8 +194,7 @@ 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;