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
.jcr
.SlcTypes
;
33 import org
.argeo
.slc
.rpmfactory
.RpmProxyService
;
34 import org
.argeo
.slc
.rpmfactory
.RpmRepository
;
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);
41 private Set
<RpmRepository
> defaultRepositories
= new HashSet
<RpmRepository
>();
44 * Retrieve and add this file to the repository
47 protected Node
retrieve(Session session
, String path
) {
48 StringBuilder relativePathBuilder
= new StringBuilder();
49 String repoId
= extractRepoId(path
, relativePathBuilder
);
50 String relativePath
= relativePathBuilder
.toString();
52 RpmRepository sourceRepo
= null;
53 for (Iterator
<RpmRepository
> reposIt
= defaultRepositories
.iterator(); reposIt
55 RpmRepository rpmRepo
= reposIt
.next();
56 if (rpmRepo
.getId().equals(repoId
)) {
62 if (sourceRepo
== null)
63 throw new SlcException("No RPM repository found for " + path
);
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
);
72 node
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
74 if (!node
.hasNode(SLC_ORIGIN
))
75 origin
= node
.addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
77 origin
= node
.getNode(SLC_ORIGIN
);
78 // origin.setProperty(SLC_PROXY, sourceRepo.getId());
79 JcrUtils
.urlToAddressProperties(origin
, remoteUrl
);
81 if (log
.isDebugEnabled())
82 log
.debug("Imported " + remoteUrl
+ " to " + node
);
85 } catch (Exception e
) {
86 throw new SlcException("Cannot proxy " + path
, e
);
88 JcrUtils
.discardQuietly(session
);
89 throw new SlcException("No proxy found for " + path
);
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
);
103 return workspace
.toString();
107 protected Boolean
shouldUpdate(Session clientSession
, String nodePath
) {
108 if (nodePath
.contains("/repodata/"))
110 return super.shouldUpdate(clientSession
, nodePath
);
113 public void setDefaultRepositories(Set
<RpmRepository
> defaultRepositories
) {
114 this.defaultRepositories
= defaultRepositories
;