1 package org
.argeo
.slc
.rpmfactory
.core
;
3 import java
.util
.HashSet
;
4 import java
.util
.Iterator
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.jcr
.Session
;
10 import javax
.jcr
.nodetype
.NodeType
;
11 import javax
.jcr
.security
.AccessControlException
;
13 import org
.argeo
.api
.cms
.CmsLog
;
14 import org
.argeo
.cms
.ArgeoNames
;
15 import org
.argeo
.jcr
.JcrUtils
;
16 import org
.argeo
.jcr
.proxy
.AbstractUrlProxy
;
17 import org
.argeo
.slc
.SlcConstants
;
18 import org
.argeo
.slc
.SlcException
;
19 import org
.argeo
.slc
.SlcNames
;
20 import org
.argeo
.slc
.SlcTypes
;
21 import org
.argeo
.slc
.repo
.RepoConstants
;
22 import org
.argeo
.slc
.rpmfactory
.RpmProxyService
;
23 import org
.argeo
.slc
.rpmfactory
.RpmRepository
;
25 /** Synchronises the node repository with remote Maven repositories */
26 public class RpmProxyServiceImpl
extends AbstractUrlProxy
implements
27 RpmProxyService
, ArgeoNames
, SlcNames
{
28 private final static CmsLog log
= CmsLog
.getLog(RpmProxyServiceImpl
.class);
30 private Set
<RpmRepository
> defaultRepositories
= new HashSet
<RpmRepository
>();
33 protected void beforeInitSessionSave(Session session
)
34 throws RepositoryException
{
35 JcrUtils
.addPrivilege(session
, "/", "anonymous", "jcr:read");
37 JcrUtils
.addPrivilege(session
, "/", SlcConstants
.ROLE_SLC
,
39 } catch (AccessControlException e
) {
40 if (log
.isTraceEnabled())
41 log
.trace("Cannot give jcr:all privileges to "+SlcConstants
.ROLE_SLC
);
44 JcrUtils
.mkdirsSafe(session
, RepoConstants
.PROXIED_REPOSITORIES
);
48 * Retrieve and add this file to the repository
51 protected Node
retrieve(Session session
, String path
) {
52 StringBuilder relativePathBuilder
= new StringBuilder();
53 String repoId
= extractRepoId(path
, relativePathBuilder
);
54 // remove starting '/'
55 String relativePath
= relativePathBuilder
.toString().substring(1);
57 RpmRepository sourceRepo
= null;
58 for (Iterator
<RpmRepository
> reposIt
= defaultRepositories
.iterator(); reposIt
60 RpmRepository rpmRepo
= reposIt
.next();
61 if (rpmRepo
.getId().equals(repoId
)) {
67 if (sourceRepo
== null)
68 throw new SlcException("No RPM repository found for " + path
);
71 String baseUrl
= sourceRepo
.getUrl();
72 String remoteUrl
= baseUrl
+ relativePath
;
73 Node node
= proxyUrl(session
, remoteUrl
, path
);
75 registerSource(sourceRepo
, node
, remoteUrl
);
76 if (log
.isDebugEnabled())
77 log
.debug("Imported " + remoteUrl
+ " to " + node
);
80 } catch (Exception e
) {
81 throw new SlcException("Cannot proxy " + path
, e
);
83 JcrUtils
.discardQuietly(session
);
84 throw new SlcException("No proxy found for " + path
);
87 protected void registerSource(RpmRepository sourceRepo
, Node node
,
88 String remoteUrl
) throws RepositoryException
{
89 node
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
91 if (!node
.hasNode(SLC_ORIGIN
))
92 origin
= node
.addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
94 origin
= node
.getNode(SLC_ORIGIN
);
97 Node proxiedRepository
;
98 String proxiedRepositoryPath
= RepoConstants
.PROXIED_REPOSITORIES
+ '/'
100 Session session
= node
.getSession();
101 if (session
.itemExists(proxiedRepositoryPath
)) {
102 proxiedRepository
= session
.getNode(proxiedRepositoryPath
);
104 proxiedRepository
= session
.getNode(
105 RepoConstants
.PROXIED_REPOSITORIES
).addNode(
107 proxiedRepository
.addMixin(NodeType
.MIX_REFERENCEABLE
);
108 JcrUtils
.urlToAddressProperties(proxiedRepository
,
109 sourceRepo
.getUrl());
110 proxiedRepository
.setProperty(SLC_URL
, sourceRepo
.getUrl());
113 origin
.setProperty(SLC_PROXY
, proxiedRepository
);
114 JcrUtils
.urlToAddressProperties(origin
, remoteUrl
);
117 /** Returns the first token of the path */
118 protected String
extractRepoId(String path
, StringBuilder relativePath
) {
119 StringBuilder workspace
= new StringBuilder();
120 StringBuilder buf
= workspace
;
121 for (int i
= 1; i
< path
.length(); i
++) {
122 char c
= path
.charAt(i
);
128 return workspace
.toString();
132 protected Boolean
shouldUpdate(Session clientSession
, String nodePath
) {
133 // if (nodePath.contains("/repodata/"))
135 return super.shouldUpdate(clientSession
, nodePath
);
138 public void setDefaultRepositories(Set
<RpmRepository
> defaultRepositories
) {
139 this.defaultRepositories
= defaultRepositories
;