]> 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
Revert skipping protected nodes
[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 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.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 String artifactsBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
47
48 /** Inititalizes the artifacts area. */
49 @Override
50 protected void beforeInitSessionSave(Session session)
51 throws RepositoryException {
52 JcrUtils.mkdirsSafe(session, RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH);
53 Node proxiedRepositories = JcrUtils.mkdirsSafe(session,
54 RepoConstants.PROXIED_REPOSITORIES);
55 for (RemoteRepository repository : defaultRepositories) {
56 if (!proxiedRepositories.hasNode(repository.getId())) {
57 Node proxiedRepository = proxiedRepositories.addNode(repository
58 .getId());
59 proxiedRepository.addMixin(NodeType.MIX_REFERENCEABLE);
60 JcrUtils.urlToAddressProperties(proxiedRepository,
61 repository.getUrl());
62 // proxiedRepository.setProperty(SLC_URL, repository.getUrl());
63 proxiedRepository.setProperty(SLC_TYPE,
64 repository.getContentType());
65 }
66 }
67 }
68
69 /**
70 * Retrieve and add this file to the repository
71 */
72 @Override
73 protected Node retrieve(Session session, String path) {
74 try {
75 if (session.hasPendingChanges())
76 throw new SlcException("Session has pending changed");
77 Node node = null;
78 for (Node proxiedRepository : getBaseUrls(session)) {
79 String baseUrl = JcrUtils
80 .urlFromAddressProperties(proxiedRepository);
81 node = proxyUrl(session, baseUrl, path);
82 if (node != null) {
83 node.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
84 Node origin = node
85 .addNode(SLC_ORIGIN, SlcTypes.SLC_PROXIED);
86 origin.setProperty(SLC_PROXY, proxiedRepository);
87 JcrUtils.urlToAddressProperties(origin, baseUrl + path);
88 if (log.isDebugEnabled())
89 log.debug("Imported " + baseUrl + path + " to " + node);
90 return node;
91 }
92 }
93 if (log.isDebugEnabled())
94 log.warn("No proxy found for " + path);
95 return null;
96 } catch (Exception e) {
97 throw new SlcException("Cannot proxy " + path, e);
98 }
99 }
100
101 protected synchronized List<Node> getBaseUrls(Session session)
102 throws RepositoryException {
103 List<Node> baseUrls = new ArrayList<Node>();
104 for (NodeIterator nit = session.getNode(
105 RepoConstants.PROXIED_REPOSITORIES).getNodes(); nit.hasNext();) {
106 Node proxiedRepository = nit.nextNode();
107 baseUrls.add(proxiedRepository);
108 }
109 return baseUrls;
110 }
111
112 /** The JCR path where this file could be found */
113 public String getNodePath(String path) {
114 if (artifactsBasePath.equals(RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH))
115 return path;
116 else
117 return artifactsBasePath + path;
118 }
119
120 public void setDefaultRepositories(
121 List<RemoteRepository> defaultRepositories) {
122 this.defaultRepositories = defaultRepositories;
123 }
124
125 public void setArtifactsBasePath(String artifactsBasePath) {
126 this.artifactsBasePath = artifactsBasePath;
127 }
128
129 }