View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
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
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  package org.argeo.slc.repo.maven;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
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;
28  
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;
41  
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);
47  
48  	private List<RemoteRepository> defaultRepositories = new ArrayList<RemoteRepository>();
49  
50  	/** Initialises the artifacts area. */
51  	@Override
52  	protected void beforeInitSessionSave(Session session)
53  			throws RepositoryException {
54  		JcrUtils.addPrivilege(session, "/", SlcConstants.USER_ANONYMOUS, Privilege.JCR_READ);
55  		try {
56  			JcrUtils.addPrivilege(session, "/", SlcConstants.ROLE_SLC,
57  					Privilege.JCR_ALL);
58  		} catch (AccessControlException e) {
59  			if (log.isTraceEnabled())
60  				log.trace("Cannot give jcr:all privileges to "
61  						+ SlcConstants.ROLE_SLC);
62  		}
63  
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
70  						.getId());
71  				proxiedRepository.addMixin(NodeType.MIX_REFERENCEABLE);
72  				JcrUtils.urlToAddressProperties(proxiedRepository,
73  						repository.getUrl());
74  				// proxiedRepository.setProperty(SLC_URL, repository.getUrl());
75  				proxiedRepository.setProperty(SLC_TYPE,
76  						repository.getContentType());
77  			}
78  		}
79  	}
80  
81  	/**
82  	 * Retrieve and add this file to the repository
83  	 */
84  	@Override
85  	protected Node retrieve(Session session, String path) {
86  		try {
87  			if (session.hasPendingChanges())
88  				throw new SlcException("Session has pending changed");
89  			Node node = null;
90  			for (Node proxiedRepository : getBaseUrls(session)) {
91  				String baseUrl = JcrUtils
92  						.urlFromAddressProperties(proxiedRepository);
93  				node = proxyUrl(session, baseUrl, path);
94  				if (node != null) {
95  					node.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
96  					Node origin = node
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);
102 					return node;
103 				}
104 			}
105 			if (log.isDebugEnabled())
106 				log.warn("No proxy found for " + path);
107 			return null;
108 		} catch (Exception e) {
109 			throw new SlcException("Cannot proxy " + path, e);
110 		}
111 	}
112 
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);
120 		}
121 		return baseUrls;
122 	}
123 
124 	public void setDefaultRepositories(
125 			List<RemoteRepository> defaultRepositories) {
126 		this.defaultRepositories = defaultRepositories;
127 	}
128 }