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