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