X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.repo%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Frepo%2Fmaven%2Fproxy%2FMavenProxyServiceImpl.java;h=ee5c5ff5c84c62f3a0019f003cd8d1d478492baf;hb=44c43b9c874d7c6edd4327f180d5506b3e9c99e6;hp=9e2cfd00a26c1db243c950f609b622e081d7c5fd;hpb=10dd7ee7dc2bdd8d3bc39e8873a9080debfcd8e5;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/proxy/MavenProxyServiceImpl.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/proxy/MavenProxyServiceImpl.java index 9e2cfd00a..ee5c5ff5c 100644 --- a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/proxy/MavenProxyServiceImpl.java +++ b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/maven/proxy/MavenProxyServiceImpl.java @@ -1,135 +1,121 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.argeo.slc.repo.maven.proxy; -import java.io.InputStream; -import java.net.URL; import java.util.ArrayList; import java.util.List; -import javax.jcr.Binary; import javax.jcr.Node; import javax.jcr.NodeIterator; -import javax.jcr.Property; -import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.nodetype.NodeType; -import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.jcr.ArgeoNames; import org.argeo.jcr.JcrUtils; +import org.argeo.jcr.proxy.AbstractUrlProxy; import org.argeo.slc.SlcException; import org.argeo.slc.jcr.SlcNames; +import org.argeo.slc.jcr.SlcTypes; import org.argeo.slc.repo.RepoConstants; import org.sonatype.aether.repository.RemoteRepository; /** Synchronizes the node repository with remote Maven repositories */ -public class MavenProxyServiceImpl implements MavenProxyService, ArgeoNames, - SlcNames { +public class MavenProxyServiceImpl extends AbstractUrlProxy implements + MavenProxyService, ArgeoNames, SlcNames { private final static Log log = LogFactory .getLog(MavenProxyServiceImpl.class); - private Repository jcrRepository; - private Session jcrAdminSession; private List defaultRepositories = new ArrayList(); - public void init() { - try { - jcrAdminSession = jcrRepository.login(); + private boolean rootNodeIsArtifactBase = RepoConstants.ARTIFACTS_BASE_PATH + .equals("/"); - JcrUtils.mkdirs(jcrAdminSession, RepoConstants.ARTIFACTS_BASE_PATH); - Node proxiedRepositories = JcrUtils.mkdirs(jcrAdminSession, - RepoConstants.PROXIED_REPOSITORIES); - for (RemoteRepository repository : defaultRepositories) { - if (!proxiedRepositories.hasNode(repository.getId())) { - Node proxiedRepository = proxiedRepositories - .addNode(repository.getId()); - proxiedRepository.setProperty(SLC_URL, repository.getUrl()); - proxiedRepository.setProperty(SLC_TYPE, - repository.getContentType()); - } + /** Inititalizes the artifacts area. */ + @Override + protected void beforeInitSessionSave(Session session) + throws RepositoryException { + JcrUtils.mkdirsSafe(session, RepoConstants.ARTIFACTS_BASE_PATH); + Node proxiedRepositories = JcrUtils.mkdirsSafe(session, + RepoConstants.PROXIED_REPOSITORIES); + for (RemoteRepository repository : defaultRepositories) { + if (!proxiedRepositories.hasNode(repository.getId())) { + Node proxiedRepository = proxiedRepositories.addNode(repository + .getId()); + proxiedRepository.addMixin(NodeType.MIX_REFERENCEABLE); + JcrUtils.urlToAddressProperties(proxiedRepository, + repository.getUrl()); + // proxiedRepository.setProperty(SLC_URL, repository.getUrl()); + proxiedRepository.setProperty(SLC_TYPE, + repository.getContentType()); } - jcrAdminSession.save(); - } catch (RepositoryException e) { - JcrUtils.discardQuietly(jcrAdminSession); - throw new SlcException("Cannot initialize Maven proxy", e); } } - public void destroy() { - JcrUtils.logoutQuietly(jcrAdminSession); - } - /** * Retrieve and add this file to the repository */ - public synchronized String proxyFile(String path) { + @Override + protected Node retrieve(Session session, String path) { try { + if (session.hasPendingChanges()) + throw new SlcException("Session has pending changed"); Node node = null; - proxiedRepositories: for (NodeIterator nit = jcrAdminSession - .getNode(RepoConstants.PROXIED_REPOSITORIES).getNodes(); nit - .hasNext();) { - Node proxiedRepository = nit.nextNode(); - String repoUrl = proxiedRepository.getProperty(SLC_URL) - .getString(); - String remoteUrl = repoUrl + path; - if (log.isTraceEnabled()) - log.trace("remoteUrl=" + remoteUrl); - InputStream in = null; - try { - URL u = new URL(remoteUrl); - in = u.openStream(); - node = importFile(getNodePath(path), in); + for (Node proxiedRepository : getBaseUrls(session)) { + String baseUrl = JcrUtils + .urlFromAddressProperties(proxiedRepository); + node = proxyUrl(session, baseUrl, path); + if (node != null) { + node.addMixin(SlcTypes.SLC_KNOWN_ORIGIN); + Node origin = node + .addNode(SLC_ORIGIN, SlcTypes.SLC_PROXIED); + origin.setProperty(SLC_PROXY, proxiedRepository); + JcrUtils.urlToAddressProperties(origin, baseUrl + path); if (log.isDebugEnabled()) - log.debug("Imported " + remoteUrl + " to " + node); - break proxiedRepositories; - } catch (Exception e) { - if (log.isDebugEnabled()) - log.debug("Cannot read " + remoteUrl + ", skipping... " - + e.getMessage()); - // e.printStackTrace(); - } finally { - IOUtils.closeQuietly(in); + log.debug("Imported " + baseUrl + path + " to " + node); + return node; } } - - if (node != null) - return node.getIdentifier(); - else { - log.warn("Could not proxy " + path); - return null; - } - } catch (RepositoryException e) { + if (log.isDebugEnabled()) + log.warn("No proxy found for " + path); + return null; + } catch (Exception e) { throw new SlcException("Cannot proxy " + path, e); } } - /** The JCR path where this file could be found */ - public String getNodePath(String path) { - return RepoConstants.ARTIFACTS_BASE_PATH + path; - } - - protected Node importFile(String nodePath, InputStream in) { - Binary binary = null; - try { - Node node = JcrUtils.mkdirs(jcrAdminSession, nodePath, - NodeType.NT_FILE, NodeType.NT_FOLDER, false); - Node content = node.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE); - binary = jcrAdminSession.getValueFactory().createBinary(in); - content.setProperty(Property.JCR_DATA, binary); - jcrAdminSession.save(); - return node; - } catch (RepositoryException e) { - JcrUtils.discardQuietly(jcrAdminSession); - throw new SlcException("Cannot initialize Maven proxy", e); - } finally { - JcrUtils.closeQuietly(binary); + protected synchronized List getBaseUrls(Session session) + throws RepositoryException { + List baseUrls = new ArrayList(); + for (NodeIterator nit = session.getNode( + RepoConstants.PROXIED_REPOSITORIES).getNodes(); nit.hasNext();) { + Node proxiedRepository = nit.nextNode(); + baseUrls.add(proxiedRepository); } + return baseUrls; } - public void setJcrRepository(Repository jcrRepository) { - this.jcrRepository = jcrRepository; + /** The JCR path where this file could be found */ + public String getNodePath(String path) { + if (rootNodeIsArtifactBase) + return path; + else + return RepoConstants.ARTIFACTS_BASE_PATH + path; } public void setDefaultRepositories(