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 String artifactsBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
48 /** Inititalizes the artifacts area. */
50 protected void beforeInitSessionSave(Session session
)
51 throws RepositoryException
{
52 JcrUtils
.mkdirsSafe(session
, RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
);
53 Node proxiedRepositories
= JcrUtils
.mkdirsSafe(session
,
54 RepoConstants
.PROXIED_REPOSITORIES
);
55 for (RemoteRepository repository
: defaultRepositories
) {
56 if (!proxiedRepositories
.hasNode(repository
.getId())) {
57 Node proxiedRepository
= proxiedRepositories
.addNode(repository
59 proxiedRepository
.addMixin(NodeType
.MIX_REFERENCEABLE
);
60 JcrUtils
.urlToAddressProperties(proxiedRepository
,
62 // proxiedRepository.setProperty(SLC_URL, repository.getUrl());
63 proxiedRepository
.setProperty(SLC_TYPE
,
64 repository
.getContentType());
70 * Retrieve and add this file to the repository
73 protected Node
retrieve(Session session
, String path
) {
75 if (session
.hasPendingChanges())
76 throw new SlcException("Session has pending changed");
78 for (Node proxiedRepository
: getBaseUrls(session
)) {
79 String baseUrl
= JcrUtils
80 .urlFromAddressProperties(proxiedRepository
);
81 node
= proxyUrl(session
, baseUrl
, path
);
83 node
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
85 .addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
86 origin
.setProperty(SLC_PROXY
, proxiedRepository
);
87 JcrUtils
.urlToAddressProperties(origin
, baseUrl
+ path
);
88 if (log
.isDebugEnabled())
89 log
.debug("Imported " + baseUrl
+ path
+ " to " + node
);
93 if (log
.isDebugEnabled())
94 log
.warn("No proxy found for " + path
);
96 } catch (Exception e
) {
97 throw new SlcException("Cannot proxy " + path
, e
);
101 protected synchronized List
<Node
> getBaseUrls(Session session
)
102 throws RepositoryException
{
103 List
<Node
> baseUrls
= new ArrayList
<Node
>();
104 for (NodeIterator nit
= session
.getNode(
105 RepoConstants
.PROXIED_REPOSITORIES
).getNodes(); nit
.hasNext();) {
106 Node proxiedRepository
= nit
.nextNode();
107 baseUrls
.add(proxiedRepository
);
112 /** The JCR path where this file could be found */
113 public String
getNodePath(String path
) {
114 if (artifactsBasePath
.equals(RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
))
117 return artifactsBasePath
+ path
;
120 public void setDefaultRepositories(
121 List
<RemoteRepository
> defaultRepositories
) {
122 this.defaultRepositories
= defaultRepositories
;
125 public void setArtifactsBasePath(String artifactsBasePath
) {
126 this.artifactsBasePath
= artifactsBasePath
;