]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/proxy/MavenProxyServiceImpl.java
Adapt to root node as base artifact
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / maven / proxy / MavenProxyServiceImpl.java
index 9e2cfd00a26c1db243c950f609b622e081d7c5fd..21b28d450bb4f3d3a69860f5add82c423e670c40 100644 (file)
 package org.argeo.slc.repo.maven.proxy;
 
-import java.io.InputStream;
-import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.jcr.Binary;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
-import javax.jcr.Property;
-import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.ArgeoNames;
 import org.argeo.jcr.JcrUtils;
+import org.argeo.jcr.proxy.AbstractUrlProxy;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.jcr.SlcNames;
+import org.argeo.slc.jcr.SlcTypes;
 import org.argeo.slc.repo.RepoConstants;
 import org.sonatype.aether.repository.RemoteRepository;
 
 /** Synchronizes the node repository with remote Maven repositories */
-public class MavenProxyServiceImpl implements MavenProxyService, ArgeoNames,
-               SlcNames {
+public class MavenProxyServiceImpl extends AbstractUrlProxy implements
+               MavenProxyService, ArgeoNames, SlcNames {
        private final static Log log = LogFactory
                        .getLog(MavenProxyServiceImpl.class);
 
-       private Repository jcrRepository;
-       private Session jcrAdminSession;
        private List<RemoteRepository> defaultRepositories = new ArrayList<RemoteRepository>();
 
-       public void init() {
-               try {
-                       jcrAdminSession = jcrRepository.login();
+       private boolean rootNodeIsArtifactBase = RepoConstants.ARTIFACTS_BASE_PATH
+                       .equals("/");
 
-                       JcrUtils.mkdirs(jcrAdminSession, RepoConstants.ARTIFACTS_BASE_PATH);
-                       Node proxiedRepositories = JcrUtils.mkdirs(jcrAdminSession,
-                                       RepoConstants.PROXIED_REPOSITORIES);
-                       for (RemoteRepository repository : defaultRepositories) {
-                               if (!proxiedRepositories.hasNode(repository.getId())) {
-                                       Node proxiedRepository = proxiedRepositories
-                                                       .addNode(repository.getId());
-                                       proxiedRepository.setProperty(SLC_URL, repository.getUrl());
-                                       proxiedRepository.setProperty(SLC_TYPE,
-                                                       repository.getContentType());
-                               }
+       /** Inititalizes the artifacts area. */
+       @Override
+       protected void beforeInitSessionSave(Session session)
+                       throws RepositoryException {
+               JcrUtils.mkdirsSafe(session, RepoConstants.ARTIFACTS_BASE_PATH);
+               Node proxiedRepositories = JcrUtils.mkdirsSafe(session,
+                               RepoConstants.PROXIED_REPOSITORIES);
+               for (RemoteRepository repository : defaultRepositories) {
+                       if (!proxiedRepositories.hasNode(repository.getId())) {
+                               Node proxiedRepository = proxiedRepositories.addNode(repository
+                                               .getId());
+                               proxiedRepository.addMixin(NodeType.MIX_REFERENCEABLE);
+                               JcrUtils.urlToAddressProperties(proxiedRepository,
+                                               repository.getUrl());
+                               // proxiedRepository.setProperty(SLC_URL, repository.getUrl());
+                               proxiedRepository.setProperty(SLC_TYPE,
+                                               repository.getContentType());
                        }
-                       jcrAdminSession.save();
-               } catch (RepositoryException e) {
-                       JcrUtils.discardQuietly(jcrAdminSession);
-                       throw new SlcException("Cannot initialize Maven proxy", e);
                }
        }
 
-       public void destroy() {
-               JcrUtils.logoutQuietly(jcrAdminSession);
-       }
-
        /**
         * Retrieve and add this file to the repository
         */
-       public synchronized String proxyFile(String path) {
+       @Override
+       protected Node retrieve(Session session, String path) {
                try {
+                       if (session.hasPendingChanges())
+                               throw new SlcException("Session has pending changed");
                        Node node = null;
-                       proxiedRepositories: for (NodeIterator nit = jcrAdminSession
-                                       .getNode(RepoConstants.PROXIED_REPOSITORIES).getNodes(); nit
-                                       .hasNext();) {
-                               Node proxiedRepository = nit.nextNode();
-                               String repoUrl = proxiedRepository.getProperty(SLC_URL)
-                                               .getString();
-                               String remoteUrl = repoUrl + path;
-                               if (log.isTraceEnabled())
-                                       log.trace("remoteUrl=" + remoteUrl);
-                               InputStream in = null;
-                               try {
-                                       URL u = new URL(remoteUrl);
-                                       in = u.openStream();
-                                       node = importFile(getNodePath(path), in);
+                       for (Node proxiedRepository : getBaseUrls(session)) {
+                               String baseUrl = JcrUtils
+                                               .urlFromAddressProperties(proxiedRepository);
+                               node = proxyUrl(session, baseUrl, path);
+                               if (node != null) {
+                                       node.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
+                                       Node origin = node
+                                                       .addNode(SLC_ORIGIN, SlcTypes.SLC_PROXIED);
+                                       origin.setProperty(SLC_PROXY, proxiedRepository);
+                                       JcrUtils.urlToAddressProperties(origin, baseUrl + path);
                                        if (log.isDebugEnabled())
-                                               log.debug("Imported " + remoteUrl + " to " + node);
-                                       break proxiedRepositories;
-                               } catch (Exception e) {
-                                       if (log.isDebugEnabled())
-                                               log.debug("Cannot read " + remoteUrl + ", skipping... "
-                                                               + e.getMessage());
-                                       // e.printStackTrace();
-                               } finally {
-                                       IOUtils.closeQuietly(in);
+                                               log.debug("Imported " + baseUrl + path + " to " + node);
+                                       return node;
                                }
                        }
-
-                       if (node != null)
-                               return node.getIdentifier();
-                       else {
-                               log.warn("Could not proxy " + path);
-                               return null;
-                       }
-               } catch (RepositoryException e) {
+                       if (log.isDebugEnabled())
+                               log.warn("No proxy found for " + path);
+                       return null;
+               } catch (Exception e) {
                        throw new SlcException("Cannot proxy " + path, e);
                }
        }
 
-       /** The JCR path where this file could be found */
-       public String getNodePath(String path) {
-               return RepoConstants.ARTIFACTS_BASE_PATH + path;
-       }
-
-       protected Node importFile(String nodePath, InputStream in) {
-               Binary binary = null;
-               try {
-                       Node node = JcrUtils.mkdirs(jcrAdminSession, nodePath,
-                                       NodeType.NT_FILE, NodeType.NT_FOLDER, false);
-                       Node content = node.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE);
-                       binary = jcrAdminSession.getValueFactory().createBinary(in);
-                       content.setProperty(Property.JCR_DATA, binary);
-                       jcrAdminSession.save();
-                       return node;
-               } catch (RepositoryException e) {
-                       JcrUtils.discardQuietly(jcrAdminSession);
-                       throw new SlcException("Cannot initialize Maven proxy", e);
-               } finally {
-                       JcrUtils.closeQuietly(binary);
+       protected synchronized List<Node> getBaseUrls(Session session)
+                       throws RepositoryException {
+               List<Node> baseUrls = new ArrayList<Node>();
+               for (NodeIterator nit = session.getNode(
+                               RepoConstants.PROXIED_REPOSITORIES).getNodes(); nit.hasNext();) {
+                       Node proxiedRepository = nit.nextNode();
+                       baseUrls.add(proxiedRepository);
                }
+               return baseUrls;
        }
 
-       public void setJcrRepository(Repository jcrRepository) {
-               this.jcrRepository = jcrRepository;
+       /** The JCR path where this file could be found */
+       public String getNodePath(String path) {
+               if (rootNodeIsArtifactBase)
+                       return path;
+               else
+                       return RepoConstants.ARTIFACTS_BASE_PATH + path;
        }
 
        public void setDefaultRepositories(