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
;
28 import org
.apache
.commons
.logging
.Log
;
29 import org
.apache
.commons
.logging
.LogFactory
;
30 import org
.argeo
.jcr
.ArgeoNames
;
31 import org
.argeo
.jcr
.JcrUtils
;
32 import org
.argeo
.jcr
.proxy
.AbstractUrlProxy
;
33 import org
.argeo
.slc
.SlcConstants
;
34 import org
.argeo
.slc
.SlcException
;
35 import org
.argeo
.slc
.jcr
.SlcNames
;
36 import org
.argeo
.slc
.jcr
.SlcTypes
;
37 import org
.argeo
.slc
.repo
.MavenProxyService
;
38 import org
.argeo
.slc
.repo
.RepoConstants
;
39 import org
.sonatype
.aether
.repository
.RemoteRepository
;
41 /** Synchronises the node repository with remote Maven repositories */
42 public class MavenProxyServiceImpl
extends AbstractUrlProxy
implements
43 MavenProxyService
, ArgeoNames
, SlcNames
{
44 private final static Log log
= LogFactory
45 .getLog(MavenProxyServiceImpl
.class);
47 private List
<RemoteRepository
> defaultRepositories
= new ArrayList
<RemoteRepository
>();
49 /** Initialises the artifacts area. */
51 protected void beforeInitSessionSave(Session session
)
52 throws RepositoryException
{
53 JcrUtils
.addPrivilege(session
, "/", "anonymous", "jcr:read");
55 JcrUtils
.addPrivilege(session
, "/", SlcConstants
.ROLE_SLC
,
57 } catch (AccessControlException e
) {
58 if (log
.isTraceEnabled())
59 log
.trace("Cannot give jcr:all privileges to ROLE_SLC");
62 JcrUtils
.mkdirsSafe(session
, RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
);
63 Node proxiedRepositories
= JcrUtils
.mkdirsSafe(session
,
64 RepoConstants
.PROXIED_REPOSITORIES
);
65 for (RemoteRepository repository
: defaultRepositories
) {
66 if (!proxiedRepositories
.hasNode(repository
.getId())) {
67 Node proxiedRepository
= proxiedRepositories
.addNode(repository
69 proxiedRepository
.addMixin(NodeType
.MIX_REFERENCEABLE
);
70 JcrUtils
.urlToAddressProperties(proxiedRepository
,
72 // proxiedRepository.setProperty(SLC_URL, repository.getUrl());
73 proxiedRepository
.setProperty(SLC_TYPE
,
74 repository
.getContentType());
80 * Retrieve and add this file to the repository
83 protected Node
retrieve(Session session
, String path
) {
85 if (session
.hasPendingChanges())
86 throw new SlcException("Session has pending changed");
88 for (Node proxiedRepository
: getBaseUrls(session
)) {
89 String baseUrl
= JcrUtils
90 .urlFromAddressProperties(proxiedRepository
);
91 node
= proxyUrl(session
, baseUrl
, path
);
93 node
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
95 .addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
96 origin
.setProperty(SLC_PROXY
, proxiedRepository
);
97 JcrUtils
.urlToAddressProperties(origin
, baseUrl
+ path
);
98 if (log
.isDebugEnabled())
99 log
.debug("Imported " + baseUrl
+ path
+ " to " + node
);
103 if (log
.isDebugEnabled())
104 log
.warn("No proxy found for " + path
);
106 } catch (Exception e
) {
107 throw new SlcException("Cannot proxy " + path
, e
);
111 protected synchronized List
<Node
> getBaseUrls(Session session
)
112 throws RepositoryException
{
113 List
<Node
> baseUrls
= new ArrayList
<Node
>();
114 for (NodeIterator nit
= session
.getNode(
115 RepoConstants
.PROXIED_REPOSITORIES
).getNodes(); nit
.hasNext();) {
116 Node proxiedRepository
= nit
.nextNode();
117 baseUrls
.add(proxiedRepository
);
122 public void setDefaultRepositories(
123 List
<RemoteRepository
> defaultRepositories
) {
124 this.defaultRepositories
= defaultRepositories
;