1 package org
.argeo
.slc
.repo
.maven
.proxy
;
3 import java
.io
.InputStream
;
5 import java
.util
.ArrayList
;
8 import javax
.jcr
.Binary
;
10 import javax
.jcr
.NodeIterator
;
11 import javax
.jcr
.Property
;
12 import javax
.jcr
.Repository
;
13 import javax
.jcr
.RepositoryException
;
14 import javax
.jcr
.Session
;
15 import javax
.jcr
.nodetype
.NodeType
;
17 import org
.apache
.commons
.io
.IOUtils
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.argeo
.jcr
.ArgeoNames
;
21 import org
.argeo
.jcr
.JcrUtils
;
22 import org
.argeo
.slc
.SlcException
;
23 import org
.argeo
.slc
.jcr
.SlcNames
;
24 import org
.argeo
.slc
.repo
.RepoConstants
;
25 import org
.sonatype
.aether
.repository
.RemoteRepository
;
27 /** Synchronizes the node repository with remote Maven repositories */
28 public class MavenProxyServiceImpl
implements MavenProxyService
, ArgeoNames
,
30 private final static Log log
= LogFactory
31 .getLog(MavenProxyServiceImpl
.class);
33 private Repository jcrRepository
;
34 private Session jcrAdminSession
;
35 private List
<RemoteRepository
> defaultRepositories
= new ArrayList
<RemoteRepository
>();
39 jcrAdminSession
= jcrRepository
.login();
41 JcrUtils
.mkdirs(jcrAdminSession
, RepoConstants
.ARTIFACTS_BASE_PATH
);
42 Node proxiedRepositories
= JcrUtils
.mkdirs(jcrAdminSession
,
43 RepoConstants
.PROXIED_REPOSITORIES
);
44 for (RemoteRepository repository
: defaultRepositories
) {
45 if (!proxiedRepositories
.hasNode(repository
.getId())) {
46 Node proxiedRepository
= proxiedRepositories
47 .addNode(repository
.getId());
48 proxiedRepository
.setProperty(SLC_URL
, repository
.getUrl());
49 proxiedRepository
.setProperty(SLC_TYPE
,
50 repository
.getContentType());
53 jcrAdminSession
.save();
54 } catch (RepositoryException e
) {
55 JcrUtils
.discardQuietly(jcrAdminSession
);
56 throw new SlcException("Cannot initialize Maven proxy", e
);
60 public void destroy() {
61 JcrUtils
.logoutQuietly(jcrAdminSession
);
65 * Retrieve and add this file to the repository
67 public synchronized String
proxyFile(String path
) {
70 proxiedRepositories
: for (NodeIterator nit
= jcrAdminSession
71 .getNode(RepoConstants
.PROXIED_REPOSITORIES
).getNodes(); nit
73 Node proxiedRepository
= nit
.nextNode();
74 String repoUrl
= proxiedRepository
.getProperty(SLC_URL
)
76 String remoteUrl
= repoUrl
+ path
;
77 if (log
.isTraceEnabled())
78 log
.trace("remoteUrl=" + remoteUrl
);
79 InputStream in
= null;
81 URL u
= new URL(remoteUrl
);
83 node
= importFile(getNodePath(path
), in
);
84 if (log
.isDebugEnabled())
85 log
.debug("Imported " + remoteUrl
+ " to " + node
);
86 break proxiedRepositories
;
87 } catch (Exception e
) {
88 if (log
.isDebugEnabled())
89 log
.debug("Cannot read " + remoteUrl
+ ", skipping... "
91 // e.printStackTrace();
93 IOUtils
.closeQuietly(in
);
98 return node
.getIdentifier();
100 log
.warn("Could not proxy " + path
);
103 } catch (RepositoryException e
) {
104 throw new SlcException("Cannot proxy " + path
, e
);
108 /** The JCR path where this file could be found */
109 public String
getNodePath(String path
) {
110 return RepoConstants
.ARTIFACTS_BASE_PATH
+ path
;
113 protected Node
importFile(String nodePath
, InputStream in
) {
114 Binary binary
= null;
116 Node node
= JcrUtils
.mkdirs(jcrAdminSession
, nodePath
,
117 NodeType
.NT_FILE
, NodeType
.NT_FOLDER
, false);
118 Node content
= node
.addNode(Node
.JCR_CONTENT
, NodeType
.NT_RESOURCE
);
119 binary
= jcrAdminSession
.getValueFactory().createBinary(in
);
120 content
.setProperty(Property
.JCR_DATA
, binary
);
121 jcrAdminSession
.save();
123 } catch (RepositoryException e
) {
124 JcrUtils
.discardQuietly(jcrAdminSession
);
125 throw new SlcException("Cannot initialize Maven proxy", e
);
127 JcrUtils
.closeQuietly(binary
);
131 public void setJcrRepository(Repository jcrRepository
) {
132 this.jcrRepository
= jcrRepository
;
135 public void setDefaultRepositories(
136 List
<RemoteRepository
> defaultRepositories
) {
137 this.defaultRepositories
= defaultRepositories
;