]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/MavenProxyServiceImpl.java
work on fetch and normalization.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / maven / MavenProxyServiceImpl.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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;
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 import javax.jcr.security.AccessControlException;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.argeo.jcr.ArgeoNames;
31 import org.argeo.jcr.JcrUtils;
32 import org.argeo.jcr.proxy.AbstractUrlProxy;
33 import org.argeo.slc.SlcConstants;
34 import org.argeo.slc.SlcException;
35 import org.argeo.slc.jcr.SlcNames;
36 import org.argeo.slc.jcr.SlcTypes;
37 import org.argeo.slc.repo.MavenProxyService;
38 import org.argeo.slc.repo.RepoConstants;
39 import org.sonatype.aether.repository.RemoteRepository;
40
41 /** Synchronises the node repository with remote Maven repositories */
42 public class MavenProxyServiceImpl extends AbstractUrlProxy implements
43 MavenProxyService, ArgeoNames, SlcNames {
44 private final static Log log = LogFactory
45 .getLog(MavenProxyServiceImpl.class);
46
47 private List<RemoteRepository> defaultRepositories = new ArrayList<RemoteRepository>();
48
49 /** Initialises the artifacts area. */
50 @Override
51 protected void beforeInitSessionSave(Session session)
52 throws RepositoryException {
53 JcrUtils.addPrivilege(session, "/", "anonymous", "jcr:read");
54 try {
55 JcrUtils.addPrivilege(session, "/", SlcConstants.ROLE_SLC,
56 "jcr:all");
57 } catch (AccessControlException e) {
58 if (log.isTraceEnabled())
59 log.trace("Cannot give jcr:all privileges to ROLE_SLC");
60 }
61
62 JcrUtils.mkdirsSafe(session, RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH);
63 Node proxiedRepositories = JcrUtils.mkdirsSafe(session,
64 RepoConstants.PROXIED_REPOSITORIES);
65 for (RemoteRepository repository : defaultRepositories) {
66 if (!proxiedRepositories.hasNode(repository.getId())) {
67 Node proxiedRepository = proxiedRepositories.addNode(repository
68 .getId());
69 proxiedRepository.addMixin(NodeType.MIX_REFERENCEABLE);
70 JcrUtils.urlToAddressProperties(proxiedRepository,
71 repository.getUrl());
72 // proxiedRepository.setProperty(SLC_URL, repository.getUrl());
73 proxiedRepository.setProperty(SLC_TYPE,
74 repository.getContentType());
75 }
76 }
77 }
78
79 /**
80 * Retrieve and add this file to the repository
81 */
82 @Override
83 protected Node retrieve(Session session, String path) {
84 try {
85 if (session.hasPendingChanges())
86 throw new SlcException("Session has pending changed");
87 Node node = null;
88 for (Node proxiedRepository : getBaseUrls(session)) {
89 String baseUrl = JcrUtils
90 .urlFromAddressProperties(proxiedRepository);
91 node = proxyUrl(session, baseUrl, path);
92 if (node != null) {
93 node.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
94 Node origin = node
95 .addNode(SLC_ORIGIN, SlcTypes.SLC_PROXIED);
96 origin.setProperty(SLC_PROXY, proxiedRepository);
97 JcrUtils.urlToAddressProperties(origin, baseUrl + path);
98 if (log.isDebugEnabled())
99 log.debug("Imported " + baseUrl + path + " to " + node);
100 return node;
101 }
102 }
103 if (log.isDebugEnabled())
104 log.warn("No proxy found for " + path);
105 return null;
106 } catch (Exception e) {
107 throw new SlcException("Cannot proxy " + path, e);
108 }
109 }
110
111 protected synchronized List<Node> getBaseUrls(Session session)
112 throws RepositoryException {
113 List<Node> baseUrls = new ArrayList<Node>();
114 for (NodeIterator nit = session.getNode(
115 RepoConstants.PROXIED_REPOSITORIES).getNodes(); nit.hasNext();) {
116 Node proxiedRepository = nit.nextNode();
117 baseUrls.add(proxiedRepository);
118 }
119 return baseUrls;
120 }
121
122 public void setDefaultRepositories(
123 List<RemoteRepository> defaultRepositories) {
124 this.defaultRepositories = defaultRepositories;
125 }
126 }