2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.rpmfactory
.core
;
18 import java
.util
.HashSet
;
19 import java
.util
.Iterator
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.Session
;
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
;
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);
40 private Set
<RpmRepository
> defaultRepositories
= new HashSet
<RpmRepository
>();
43 * Retrieve and add this file to the repository
46 protected Node
retrieve(Session session
, String path
) {
47 StringBuilder relativePathBuilder
= new StringBuilder();
48 String repoId
= extractRepoId(path
, relativePathBuilder
);
49 String relativePath
= relativePathBuilder
.toString();
51 RpmRepository sourceRepo
= null;
52 for (Iterator
<RpmRepository
> reposIt
= defaultRepositories
.iterator(); reposIt
54 RpmRepository rpmRepo
= reposIt
.next();
55 if (rpmRepo
.getId().equals(repoId
)) {
61 if (sourceRepo
== null)
62 throw new SlcException("No RPM repository found for " + path
);
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
);
71 // node.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
73 // if (!node.hasNode(SLC_ORIGIN))
74 // origin = node.addNode(SLC_ORIGIN, SlcTypes.SLC_PROXIED);
76 // origin = node.getNode(SLC_ORIGIN);
77 // // origin.setProperty(SLC_PROXY, sourceRepo.getId());
78 // JcrUtils.urlToAddressProperties(origin, remoteUrl);
80 if (log
.isDebugEnabled())
81 log
.debug("Imported " + remoteUrl
+ " to " + node
);
84 } catch (Exception e
) {
85 throw new SlcException("Cannot proxy " + path
, e
);
87 JcrUtils
.discardQuietly(session
);
88 throw new SlcException("No proxy found for " + path
);
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
);
102 return workspace
.toString();
106 protected Boolean
shouldUpdate(Session clientSession
, String nodePath
) {
107 if (nodePath
.contains("/repodata/"))
109 return super.shouldUpdate(clientSession
, nodePath
);
112 public void setDefaultRepositories(Set
<RpmRepository
> defaultRepositories
) {
113 this.defaultRepositories
= defaultRepositories
;