]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.rpmfactory/src/main/java/org/argeo/slc/rpmfactory/core/RpmProxyServiceImpl.java
14d399c2743dff3c49a7d012d76ac584ce5a1aec
[gpl/argeo-slc.git] / runtime / org.argeo.slc.rpmfactory / src / main / java / org / argeo / slc / rpmfactory / core / RpmProxyServiceImpl.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.rpmfactory.core;
17
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.Set;
21
22 import javax.jcr.Node;
23 import javax.jcr.Session;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.jcr.ArgeoNames;
28 import org.argeo.jcr.JcrUtils;
29 import org.argeo.jcr.proxy.AbstractUrlProxy;
30 import org.argeo.slc.SlcException;
31 import org.argeo.slc.jcr.SlcNames;
32 import org.argeo.slc.jcr.SlcTypes;
33 import org.argeo.slc.rpmfactory.RpmProxyService;
34 import org.argeo.slc.rpmfactory.RpmRepository;
35
36 /** Synchronises the node repository with remote Maven repositories */
37 public class RpmProxyServiceImpl extends AbstractUrlProxy implements
38 RpmProxyService, ArgeoNames, SlcNames {
39 private final static Log log = LogFactory.getLog(RpmProxyServiceImpl.class);
40
41 private Set<RpmRepository> defaultRepositories = new HashSet<RpmRepository>();
42
43 /**
44 * Retrieve and add this file to the repository
45 */
46 @Override
47 protected Node retrieve(Session session, String path) {
48 StringBuilder relativePathBuilder = new StringBuilder();
49 String repoId = extractRepoId(path, relativePathBuilder);
50 String relativePath = relativePathBuilder.toString();
51
52 RpmRepository sourceRepo = null;
53 for (Iterator<RpmRepository> reposIt = defaultRepositories.iterator(); reposIt
54 .hasNext();) {
55 RpmRepository rpmRepo = reposIt.next();
56 if (rpmRepo.getId().equals(repoId)) {
57 sourceRepo = rpmRepo;
58 break;
59 }
60 }
61
62 if (sourceRepo == null)
63 throw new SlcException("No RPM repository found for " + path);
64
65 try {
66 // if (session.hasPendingChanges())
67 // throw new SlcException("Session has pending changed");
68 String baseUrl = sourceRepo.getUrl();
69 String remoteUrl = baseUrl + relativePath;
70 Node node = proxyUrl(session, remoteUrl, path);
71 if (node != null) {
72 node.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
73 Node origin;
74 if (!node.hasNode(SLC_ORIGIN))
75 origin = node.addNode(SLC_ORIGIN, SlcTypes.SLC_PROXIED);
76 else
77 origin = node.getNode(SLC_ORIGIN);
78 // origin.setProperty(SLC_PROXY, sourceRepo.getId());
79 JcrUtils.urlToAddressProperties(origin, remoteUrl);
80
81 if (log.isDebugEnabled())
82 log.debug("Imported " + remoteUrl + " to " + node);
83 return node;
84 }
85 } catch (Exception e) {
86 throw new SlcException("Cannot proxy " + path, e);
87 }
88 JcrUtils.discardQuietly(session);
89 throw new SlcException("No proxy found for " + path);
90 }
91
92 /** Returns the first token of the path */
93 protected String extractRepoId(String path, StringBuilder relativePath) {
94 StringBuilder workspace = new StringBuilder();
95 StringBuilder buf = workspace;
96 for (int i = 1; i < path.length(); i++) {
97 char c = path.charAt(i);
98 if (c == '/') {
99 buf = relativePath;
100 }
101 buf.append(c);
102 }
103 return workspace.toString();
104 }
105
106 @Override
107 protected Boolean shouldUpdate(Session clientSession, String nodePath) {
108 if (nodePath.contains("/repodata/"))
109 return true;
110 return super.shouldUpdate(clientSession, nodePath);
111 }
112
113 public void setDefaultRepositories(Set<RpmRepository> defaultRepositories) {
114 this.defaultRepositories = defaultRepositories;
115 }
116 }