]> 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
Migrate sources
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / maven / proxy / MavenProxyServiceImpl.java
index 19df339a27b44b96c74c65853fcfb31be45e1e34..ee5c5ff5c84c62f3a0019f003cd8d1d478492baf 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.argeo.slc.repo.maven.proxy;
 
 import java.util.ArrayList;
@@ -6,6 +21,8 @@ import java.util.List;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -14,6 +31,7 @@ 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;
 
@@ -25,16 +43,24 @@ public class MavenProxyServiceImpl extends AbstractUrlProxy implements
 
        private List<RemoteRepository> defaultRepositories = new ArrayList<RemoteRepository>();
 
+       private boolean rootNodeIsArtifactBase = RepoConstants.ARTIFACTS_BASE_PATH
+                       .equals("/");
+
+       /** Inititalizes the artifacts area. */
        @Override
-       protected void beforeInitSessionSave() throws RepositoryException {
-               JcrUtils.mkdirs(getJcrAdminSession(), RepoConstants.ARTIFACTS_BASE_PATH);
-               Node proxiedRepositories = JcrUtils.mkdirs(getJcrAdminSession(),
+       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.setProperty(SLC_URL, repository.getUrl());
+                               proxiedRepository.addMixin(NodeType.MIX_REFERENCEABLE);
+                               JcrUtils.urlToAddressProperties(proxiedRepository,
+                                               repository.getUrl());
+                               // proxiedRepository.setProperty(SLC_URL, repository.getUrl());
                                proxiedRepository.setProperty(SLC_TYPE,
                                                repository.getContentType());
                        }
@@ -44,41 +70,52 @@ public class MavenProxyServiceImpl extends AbstractUrlProxy implements
        /**
         * Retrieve and add this file to the repository
         */
-       protected String retrieve(String path) {
+       @Override
+       protected Node retrieve(Session session, String path) {
                try {
+                       if (session.hasPendingChanges())
+                               throw new SlcException("Session has pending changed");
                        Node node = null;
-                       baseUrls: for (String baseUrl : getBaseUrls()) {
-                               node = proxyUrl(baseUrl, path);
-                               if (node != null)
-                                       break baseUrls;
-                       }
-
-                       if (node != null)
-                               return node.getIdentifier();
-                       else {
-                               log.warn("Could not proxy " + path);
-                               return null;
+                       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 " + baseUrl + path + " to " + node);
+                                       return node;
+                               }
                        }
-               } 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);
                }
        }
 
-       protected synchronized List<String> getBaseUrls()
+       protected synchronized List<Node> getBaseUrls(Session session)
                        throws RepositoryException {
-               List<String> baseUrls = new ArrayList<String>();
-               for (NodeIterator nit = getJcrAdminSession().getNode(
+               List<Node> baseUrls = new ArrayList<Node>();
+               for (NodeIterator nit = session.getNode(
                                RepoConstants.PROXIED_REPOSITORIES).getNodes(); nit.hasNext();) {
                        Node proxiedRepository = nit.nextNode();
-                       String repoUrl = proxiedRepository.getProperty(SLC_URL).getString();
-                       baseUrls.add(repoUrl);
+                       baseUrls.add(proxiedRepository);
                }
                return baseUrls;
        }
 
        /** The JCR path where this file could be found */
        public String getNodePath(String path) {
-               return RepoConstants.ARTIFACTS_BASE_PATH + path;
+               if (rootNodeIsArtifactBase)
+                       return path;
+               else
+                       return RepoConstants.ARTIFACTS_BASE_PATH + path;
        }
 
        public void setDefaultRepositories(