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
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.cms
.ArgeoNames
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.argeo
.jcr
.proxy
.AbstractUrlProxy
;
18 import org
.argeo
.slc
.SlcConstants
;
19 import org
.argeo
.slc
.SlcException
;
20 import org
.argeo
.slc
.SlcNames
;
21 import org
.argeo
.slc
.SlcTypes
;
22 import org
.argeo
.slc
.repo
.RepoConstants
;
23 import org
.argeo
.slc
.rpmfactory
.RpmProxyService
;
24 import org
.argeo
.slc
.rpmfactory
.RpmRepository
;
26 /** Synchronises the node repository with remote Maven repositories */
27 public class RpmProxyServiceImpl
extends AbstractUrlProxy
implements
28 RpmProxyService
, ArgeoNames
, SlcNames
{
29 private final static Log log
= LogFactory
.getLog(RpmProxyServiceImpl
.class);
31 private Set
<RpmRepository
> defaultRepositories
= new HashSet
<RpmRepository
>();
34 protected void beforeInitSessionSave(Session session
)
35 throws RepositoryException
{
36 JcrUtils
.addPrivilege(session
, "/", "anonymous", "jcr:read");
38 JcrUtils
.addPrivilege(session
, "/", SlcConstants
.ROLE_SLC
,
40 } catch (AccessControlException e
) {
41 if (log
.isTraceEnabled())
42 log
.trace("Cannot give jcr:all privileges to "+SlcConstants
.ROLE_SLC
);
45 JcrUtils
.mkdirsSafe(session
, RepoConstants
.PROXIED_REPOSITORIES
);
49 * Retrieve and add this file to the repository
52 protected Node
retrieve(Session session
, String path
) {
53 StringBuilder relativePathBuilder
= new StringBuilder();
54 String repoId
= extractRepoId(path
, relativePathBuilder
);
55 // remove starting '/'
56 String relativePath
= relativePathBuilder
.toString().substring(1);
58 RpmRepository sourceRepo
= null;
59 for (Iterator
<RpmRepository
> reposIt
= defaultRepositories
.iterator(); reposIt
61 RpmRepository rpmRepo
= reposIt
.next();
62 if (rpmRepo
.getId().equals(repoId
)) {
68 if (sourceRepo
== null)
69 throw new SlcException("No RPM repository found for " + path
);
72 String baseUrl
= sourceRepo
.getUrl();
73 String remoteUrl
= baseUrl
+ relativePath
;
74 Node node
= proxyUrl(session
, remoteUrl
, path
);
76 registerSource(sourceRepo
, node
, remoteUrl
);
77 if (log
.isDebugEnabled())
78 log
.debug("Imported " + remoteUrl
+ " to " + node
);
81 } catch (Exception e
) {
82 throw new SlcException("Cannot proxy " + path
, e
);
84 JcrUtils
.discardQuietly(session
);
85 throw new SlcException("No proxy found for " + path
);
88 protected void registerSource(RpmRepository sourceRepo
, Node node
,
89 String remoteUrl
) throws RepositoryException
{
90 node
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
92 if (!node
.hasNode(SLC_ORIGIN
))
93 origin
= node
.addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
95 origin
= node
.getNode(SLC_ORIGIN
);
98 Node proxiedRepository
;
99 String proxiedRepositoryPath
= RepoConstants
.PROXIED_REPOSITORIES
+ '/'
100 + sourceRepo
.getId();
101 Session session
= node
.getSession();
102 if (session
.itemExists(proxiedRepositoryPath
)) {
103 proxiedRepository
= session
.getNode(proxiedRepositoryPath
);
105 proxiedRepository
= session
.getNode(
106 RepoConstants
.PROXIED_REPOSITORIES
).addNode(
108 proxiedRepository
.addMixin(NodeType
.MIX_REFERENCEABLE
);
109 JcrUtils
.urlToAddressProperties(proxiedRepository
,
110 sourceRepo
.getUrl());
111 proxiedRepository
.setProperty(SLC_URL
, sourceRepo
.getUrl());
114 origin
.setProperty(SLC_PROXY
, proxiedRepository
);
115 JcrUtils
.urlToAddressProperties(origin
, remoteUrl
);
118 /** Returns the first token of the path */
119 protected String
extractRepoId(String path
, StringBuilder relativePath
) {
120 StringBuilder workspace
= new StringBuilder();
121 StringBuilder buf
= workspace
;
122 for (int i
= 1; i
< path
.length(); i
++) {
123 char c
= path
.charAt(i
);
129 return workspace
.toString();
133 protected Boolean
shouldUpdate(Session clientSession
, String nodePath
) {
134 // if (nodePath.contains("/repodata/"))
136 return super.shouldUpdate(clientSession
, nodePath
);
139 public void setDefaultRepositories(Set
<RpmRepository
> defaultRepositories
) {
140 this.defaultRepositories
= defaultRepositories
;