]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/proxy/MavenProxyServiceImpl.java
Make import more robust
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / maven / proxy / MavenProxyServiceImpl.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.repo.maven.proxy;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.jcr.Node;
22 import javax.jcr.NodeIterator;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.Session;
25 import javax.jcr.nodetype.NodeType;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.argeo.jcr.ArgeoNames;
30 import org.argeo.jcr.JcrUtils;
31 import org.argeo.jcr.proxy.AbstractUrlProxy;
32 import org.argeo.slc.SlcException;
33 import org.argeo.slc.jcr.SlcNames;
34 import org.argeo.slc.jcr.SlcTypes;
35 import org.argeo.slc.repo.RepoConstants;
36 import org.sonatype.aether.repository.RemoteRepository;
37
38 /** Synchronizes the node repository with remote Maven repositories */
39 public class MavenProxyServiceImpl extends AbstractUrlProxy implements
40 MavenProxyService, ArgeoNames, SlcNames {
41 private final static Log log = LogFactory
42 .getLog(MavenProxyServiceImpl.class);
43
44 private List<RemoteRepository> defaultRepositories = new ArrayList<RemoteRepository>();
45
46 private boolean rootNodeIsArtifactBase = RepoConstants.ARTIFACTS_BASE_PATH
47 .equals("/");
48
49 /** Inititalizes the artifacts area. */
50 @Override
51 protected void beforeInitSessionSave(Session session)
52 throws RepositoryException {
53 JcrUtils.mkdirsSafe(session, RepoConstants.ARTIFACTS_BASE_PATH);
54 Node proxiedRepositories = JcrUtils.mkdirsSafe(session,
55 RepoConstants.PROXIED_REPOSITORIES);
56 for (RemoteRepository repository : defaultRepositories) {
57 if (!proxiedRepositories.hasNode(repository.getId())) {
58 Node proxiedRepository = proxiedRepositories.addNode(repository
59 .getId());
60 proxiedRepository.addMixin(NodeType.MIX_REFERENCEABLE);
61 JcrUtils.urlToAddressProperties(proxiedRepository,
62 repository.getUrl());
63 // proxiedRepository.setProperty(SLC_URL, repository.getUrl());
64 proxiedRepository.setProperty(SLC_TYPE,
65 repository.getContentType());
66 }
67 }
68 }
69
70 /**
71 * Retrieve and add this file to the repository
72 */
73 @Override
74 protected Node retrieve(Session session, String path) {
75 try {
76 if (session.hasPendingChanges())
77 throw new SlcException("Session has pending changed");
78 Node node = null;
79 for (Node proxiedRepository : getBaseUrls(session)) {
80 String baseUrl = JcrUtils
81 .urlFromAddressProperties(proxiedRepository);
82 node = proxyUrl(session, baseUrl, path);
83 if (node != null) {
84 node.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
85 Node origin = node
86 .addNode(SLC_ORIGIN, SlcTypes.SLC_PROXIED);
87 origin.setProperty(SLC_PROXY, proxiedRepository);
88 JcrUtils.urlToAddressProperties(origin, baseUrl + path);
89 if (log.isDebugEnabled())
90 log.debug("Imported " + baseUrl + path + " to " + node);
91 return node;
92 }
93 }
94 if (log.isDebugEnabled())
95 log.warn("No proxy found for " + path);
96 return null;
97 } catch (Exception e) {
98 throw new SlcException("Cannot proxy " + path, e);
99 }
100 }
101
102 protected synchronized List<Node> getBaseUrls(Session session)
103 throws RepositoryException {
104 List<Node> baseUrls = new ArrayList<Node>();
105 for (NodeIterator nit = session.getNode(
106 RepoConstants.PROXIED_REPOSITORIES).getNodes(); nit.hasNext();) {
107 Node proxiedRepository = nit.nextNode();
108 baseUrls.add(proxiedRepository);
109 }
110 return baseUrls;
111 }
112
113 /** The JCR path where this file could be found */
114 public String getNodePath(String path) {
115 if (rootNodeIsArtifactBase)
116 return path;
117 else
118 return RepoConstants.ARTIFACTS_BASE_PATH + path;
119 }
120
121 public void setDefaultRepositories(
122 List<RemoteRepository> defaultRepositories) {
123 this.defaultRepositories = defaultRepositories;
124 }
125
126 }