]> git.argeo.org Git - gpl/argeo-jcr.git/blob - rpmfactory/core/RpmProxyServiceImpl.java
Prepare next development cycle
[gpl/argeo-jcr.git] / rpmfactory / core / RpmProxyServiceImpl.java
1 package org.argeo.slc.rpmfactory.core;
2
3 import java.util.HashSet;
4 import java.util.Iterator;
5 import java.util.Set;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
10 import javax.jcr.nodetype.NodeType;
11 import javax.jcr.security.AccessControlException;
12
13 import org.argeo.api.cms.CmsLog;
14 import org.argeo.cms.ArgeoNames;
15 import org.argeo.jcr.JcrUtils;
16 import org.argeo.jcr.proxy.AbstractUrlProxy;
17 import org.argeo.slc.SlcConstants;
18 import org.argeo.slc.SlcException;
19 import org.argeo.slc.SlcNames;
20 import org.argeo.slc.SlcTypes;
21 import org.argeo.slc.repo.RepoConstants;
22 import org.argeo.slc.rpmfactory.RpmProxyService;
23 import org.argeo.slc.rpmfactory.RpmRepository;
24
25 /** Synchronises the node repository with remote Maven repositories */
26 public class RpmProxyServiceImpl extends AbstractUrlProxy implements
27 RpmProxyService, ArgeoNames, SlcNames {
28 private final static CmsLog log = CmsLog.getLog(RpmProxyServiceImpl.class);
29
30 private Set<RpmRepository> defaultRepositories = new HashSet<RpmRepository>();
31
32 @Override
33 protected void beforeInitSessionSave(Session session)
34 throws RepositoryException {
35 JcrUtils.addPrivilege(session, "/", "anonymous", "jcr:read");
36 try {
37 JcrUtils.addPrivilege(session, "/", SlcConstants.ROLE_SLC,
38 "jcr:all");
39 } catch (AccessControlException e) {
40 if (log.isTraceEnabled())
41 log.trace("Cannot give jcr:all privileges to "+SlcConstants.ROLE_SLC);
42 }
43
44 JcrUtils.mkdirsSafe(session, RepoConstants.PROXIED_REPOSITORIES);
45 }
46
47 /**
48 * Retrieve and add this file to the repository
49 */
50 @Override
51 protected Node retrieve(Session session, String path) {
52 StringBuilder relativePathBuilder = new StringBuilder();
53 String repoId = extractRepoId(path, relativePathBuilder);
54 // remove starting '/'
55 String relativePath = relativePathBuilder.toString().substring(1);
56
57 RpmRepository sourceRepo = null;
58 for (Iterator<RpmRepository> reposIt = defaultRepositories.iterator(); reposIt
59 .hasNext();) {
60 RpmRepository rpmRepo = reposIt.next();
61 if (rpmRepo.getId().equals(repoId)) {
62 sourceRepo = rpmRepo;
63 break;
64 }
65 }
66
67 if (sourceRepo == null)
68 throw new SlcException("No RPM repository found for " + path);
69
70 try {
71 String baseUrl = sourceRepo.getUrl();
72 String remoteUrl = baseUrl + relativePath;
73 Node node = proxyUrl(session, remoteUrl, path);
74 if (node != null) {
75 registerSource(sourceRepo, node, remoteUrl);
76 if (log.isDebugEnabled())
77 log.debug("Imported " + remoteUrl + " to " + node);
78 return node;
79 }
80 } catch (Exception e) {
81 throw new SlcException("Cannot proxy " + path, e);
82 }
83 JcrUtils.discardQuietly(session);
84 throw new SlcException("No proxy found for " + path);
85 }
86
87 protected void registerSource(RpmRepository sourceRepo, Node node,
88 String remoteUrl) throws RepositoryException {
89 node.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
90 Node origin;
91 if (!node.hasNode(SLC_ORIGIN))
92 origin = node.addNode(SLC_ORIGIN, SlcTypes.SLC_PROXIED);
93 else
94 origin = node.getNode(SLC_ORIGIN);
95
96 // proxied repository
97 Node proxiedRepository;
98 String proxiedRepositoryPath = RepoConstants.PROXIED_REPOSITORIES + '/'
99 + sourceRepo.getId();
100 Session session = node.getSession();
101 if (session.itemExists(proxiedRepositoryPath)) {
102 proxiedRepository = session.getNode(proxiedRepositoryPath);
103 } else {
104 proxiedRepository = session.getNode(
105 RepoConstants.PROXIED_REPOSITORIES).addNode(
106 sourceRepo.getId());
107 proxiedRepository.addMixin(NodeType.MIX_REFERENCEABLE);
108 JcrUtils.urlToAddressProperties(proxiedRepository,
109 sourceRepo.getUrl());
110 proxiedRepository.setProperty(SLC_URL, sourceRepo.getUrl());
111 }
112
113 origin.setProperty(SLC_PROXY, proxiedRepository);
114 JcrUtils.urlToAddressProperties(origin, remoteUrl);
115 }
116
117 /** Returns the first token of the path */
118 protected String extractRepoId(String path, StringBuilder relativePath) {
119 StringBuilder workspace = new StringBuilder();
120 StringBuilder buf = workspace;
121 for (int i = 1; i < path.length(); i++) {
122 char c = path.charAt(i);
123 if (c == '/') {
124 buf = relativePath;
125 }
126 buf.append(c);
127 }
128 return workspace.toString();
129 }
130
131 @Override
132 protected Boolean shouldUpdate(Session clientSession, String nodePath) {
133 // if (nodePath.contains("/repodata/"))
134 // return true;
135 return super.shouldUpdate(clientSession, nodePath);
136 }
137
138 public void setDefaultRepositories(Set<RpmRepository> defaultRepositories) {
139 this.defaultRepositories = defaultRepositories;
140 }
141 }