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
.repo
.maven
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.NodeIterator
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.jcr
.Session
;
25 import javax
.jcr
.nodetype
.NodeType
;
26 import javax
.jcr
.security
.AccessControlException
;
27 import javax
.jcr
.security
.Privilege
;
29 import org
.apache
.commons
.logging
.Log
;
30 import org
.apache
.commons
.logging
.LogFactory
;
31 import org
.argeo
.cms
.ArgeoNames
;
32 import org
.argeo
.jcr
.JcrUtils
;
33 import org
.argeo
.jcr
.proxy
.AbstractUrlProxy
;
34 import org
.argeo
.slc
.SlcConstants
;
35 import org
.argeo
.slc
.SlcException
;
36 import org
.argeo
.slc
.SlcNames
;
37 import org
.argeo
.slc
.SlcTypes
;
38 import org
.argeo
.slc
.repo
.MavenProxyService
;
39 import org
.argeo
.slc
.repo
.RepoConstants
;
40 import org
.eclipse
.aether
.repository
.RemoteRepository
;
42 /** Synchronises the node repository with remote Maven repositories */
43 public class MavenProxyServiceImpl
extends AbstractUrlProxy
implements
44 MavenProxyService
, ArgeoNames
, SlcNames
{
45 private final static Log log
= LogFactory
46 .getLog(MavenProxyServiceImpl
.class);
48 private List
<RemoteRepository
> defaultRepositories
= new ArrayList
<RemoteRepository
>();
50 /** Initialises the artifacts area. */
52 protected void beforeInitSessionSave(Session session
)
53 throws RepositoryException
{
54 JcrUtils
.addPrivilege(session
, "/", SlcConstants
.USER_ANONYMOUS
, Privilege
.JCR_READ
);
56 JcrUtils
.addPrivilege(session
, "/", SlcConstants
.ROLE_SLC
,
58 } catch (AccessControlException e
) {
59 if (log
.isTraceEnabled())
60 log
.trace("Cannot give jcr:all privileges to "
61 + SlcConstants
.ROLE_SLC
);
64 JcrUtils
.mkdirsSafe(session
, RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
);
65 Node proxiedRepositories
= JcrUtils
.mkdirsSafe(session
,
66 RepoConstants
.PROXIED_REPOSITORIES
);
67 for (RemoteRepository repository
: defaultRepositories
) {
68 if (!proxiedRepositories
.hasNode(repository
.getId())) {
69 Node proxiedRepository
= proxiedRepositories
.addNode(repository
71 proxiedRepository
.addMixin(NodeType
.MIX_REFERENCEABLE
);
72 JcrUtils
.urlToAddressProperties(proxiedRepository
,
74 // proxiedRepository.setProperty(SLC_URL, repository.getUrl());
75 proxiedRepository
.setProperty(SLC_TYPE
,
76 repository
.getContentType());
82 * Retrieve and add this file to the repository
85 protected Node
retrieve(Session session
, String path
) {
87 if (session
.hasPendingChanges())
88 throw new SlcException("Session has pending changed");
90 for (Node proxiedRepository
: getBaseUrls(session
)) {
91 String baseUrl
= JcrUtils
92 .urlFromAddressProperties(proxiedRepository
);
93 node
= proxyUrl(session
, baseUrl
, path
);
95 node
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
97 .addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
98 origin
.setProperty(SLC_PROXY
, proxiedRepository
);
99 JcrUtils
.urlToAddressProperties(origin
, baseUrl
+ path
);
100 if (log
.isDebugEnabled())
101 log
.debug("Imported " + baseUrl
+ path
+ " to " + node
);
105 if (log
.isDebugEnabled())
106 log
.warn("No proxy found for " + path
);
108 } catch (Exception e
) {
109 throw new SlcException("Cannot proxy " + path
, e
);
113 protected synchronized List
<Node
> getBaseUrls(Session session
)
114 throws RepositoryException
{
115 List
<Node
> baseUrls
= new ArrayList
<Node
>();
116 for (NodeIterator nit
= session
.getNode(
117 RepoConstants
.PROXIED_REPOSITORIES
).getNodes(); nit
.hasNext();) {
118 Node proxiedRepository
= nit
.nextNode();
119 baseUrls
.add(proxiedRepository
);
124 public void setDefaultRepositories(
125 List
<RemoteRepository
> defaultRepositories
) {
126 this.defaultRepositories
= defaultRepositories
;