2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.proxy
;
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
;
27 import org
.apache
.commons
.logging
.Log
;
28 import org
.apache
.commons
.logging
.LogFactory
;
29 import org
.argeo
.jcr
.ArgeoNames
;
30 import org
.argeo
.jcr
.JcrUtils
;
31 import org
.argeo
.jcr
.proxy
.AbstractUrlProxy
;
32 import org
.argeo
.slc
.SlcException
;
33 import org
.argeo
.slc
.jcr
.SlcNames
;
34 import org
.argeo
.slc
.jcr
.SlcTypes
;
35 import org
.argeo
.slc
.repo
.RepoConstants
;
36 import org
.sonatype
.aether
.repository
.RemoteRepository
;
38 /** Synchronizes the node repository with remote Maven repositories */
39 public class MavenProxyServiceImpl
extends AbstractUrlProxy
implements
40 MavenProxyService
, ArgeoNames
, SlcNames
{
41 private final static Log log
= LogFactory
42 .getLog(MavenProxyServiceImpl
.class);
44 private List
<RemoteRepository
> defaultRepositories
= new ArrayList
<RemoteRepository
>();
46 private boolean rootNodeIsArtifactBase
= RepoConstants
.ARTIFACTS_BASE_PATH
49 /** Inititalizes the artifacts area. */
51 protected void beforeInitSessionSave(Session session
)
52 throws RepositoryException
{
53 JcrUtils
.mkdirsSafe(session
, RepoConstants
.ARTIFACTS_BASE_PATH
);
54 Node proxiedRepositories
= JcrUtils
.mkdirsSafe(session
,
55 RepoConstants
.PROXIED_REPOSITORIES
);
56 for (RemoteRepository repository
: defaultRepositories
) {
57 if (!proxiedRepositories
.hasNode(repository
.getId())) {
58 Node proxiedRepository
= proxiedRepositories
.addNode(repository
60 proxiedRepository
.addMixin(NodeType
.MIX_REFERENCEABLE
);
61 JcrUtils
.urlToAddressProperties(proxiedRepository
,
63 // proxiedRepository.setProperty(SLC_URL, repository.getUrl());
64 proxiedRepository
.setProperty(SLC_TYPE
,
65 repository
.getContentType());
71 * Retrieve and add this file to the repository
74 protected Node
retrieve(Session session
, String path
) {
76 if (session
.hasPendingChanges())
77 throw new SlcException("Session has pending changed");
79 for (Node proxiedRepository
: getBaseUrls(session
)) {
80 String baseUrl
= JcrUtils
81 .urlFromAddressProperties(proxiedRepository
);
82 node
= proxyUrl(session
, baseUrl
, path
);
84 node
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
86 .addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
87 origin
.setProperty(SLC_PROXY
, proxiedRepository
);
88 JcrUtils
.urlToAddressProperties(origin
, baseUrl
+ path
);
89 if (log
.isDebugEnabled())
90 log
.debug("Imported " + baseUrl
+ path
+ " to " + node
);
94 if (log
.isDebugEnabled())
95 log
.warn("No proxy found for " + path
);
97 } catch (Exception e
) {
98 throw new SlcException("Cannot proxy " + path
, e
);
102 protected synchronized List
<Node
> getBaseUrls(Session session
)
103 throws RepositoryException
{
104 List
<Node
> baseUrls
= new ArrayList
<Node
>();
105 for (NodeIterator nit
= session
.getNode(
106 RepoConstants
.PROXIED_REPOSITORIES
).getNodes(); nit
.hasNext();) {
107 Node proxiedRepository
= nit
.nextNode();
108 baseUrls
.add(proxiedRepository
);
113 /** The JCR path where this file could be found */
114 public String
getNodePath(String path
) {
115 if (rootNodeIsArtifactBase
)
118 return RepoConstants
.ARTIFACTS_BASE_PATH
+ path
;
121 public void setDefaultRepositories(
122 List
<RemoteRepository
> defaultRepositories
) {
123 this.defaultRepositories
= defaultRepositories
;