1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.BufferedInputStream
;
4 import java
.io
.FileNotFoundException
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.net
.MalformedURLException
;
9 import java
.net
.URLConnection
;
10 import java
.util
.ArrayList
;
11 import java
.util
.HashMap
;
12 import java
.util
.List
;
15 import javax
.jcr
.Node
;
16 import javax
.jcr
.Repository
;
17 import javax
.jcr
.RepositoryException
;
18 import javax
.jcr
.Session
;
19 import javax
.jcr
.security
.Privilege
;
21 import org
.apache
.commons
.io
.IOUtils
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.jcr
.JcrUtils
;
25 import org
.argeo
.slc
.SlcConstants
;
26 import org
.argeo
.slc
.SlcException
;
27 import org
.argeo
.slc
.SlcNames
;
28 import org
.argeo
.slc
.SlcTypes
;
29 import org
.argeo
.slc
.repo
.NodeIndexer
;
30 import org
.argeo
.slc
.repo
.OsgiFactory
;
31 import org
.argeo
.slc
.repo
.RepoConstants
;
32 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
33 import org
.eclipse
.aether
.artifact
.Artifact
;
34 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
36 /** Default implementation of {@link OsgiFactory}. */
37 public class OsgiFactoryImpl
implements OsgiFactory
, SlcNames
{
38 private final static Log log
= LogFactory
.getLog(OsgiFactoryImpl
.class);
40 private String workspace
;
41 private Repository distRepository
;
42 private Repository javaRepository
;
44 private List
<NodeIndexer
> nodeIndexers
= new ArrayList
<NodeIndexer
>();
46 /** key is URI prefix, value list of base URLs */
47 private Map
<String
, List
<String
>> mirrors
= new HashMap
<String
, List
<String
>>();
49 private List
<String
> mavenRepositories
= new ArrayList
<String
>();
50 private String downloadBase
= RepoConstants
.DIST_DOWNLOAD_BASEPATH
;
51 private String mavenProxyBase
= downloadBase
+ "/maven";
54 if (workspace
== null)
55 throw new SlcException("A workspace must be specified");
58 if (mavenRepositories
.size() == 0) {
60 // .add("http://search.maven.org/remotecontent?filepath=");
61 mavenRepositories
.add("http://repo1.maven.org/maven2");
64 Session javaSession
= null;
65 Session distSession
= null;
67 // TODO rather user a JavaRepoManager that will also implicitely
68 // manage the indexing of newly created nodes.
69 javaSession
= JcrUtils
.loginOrCreateWorkspace(javaRepository
, workspace
);
70 distSession
= JcrUtils
.loginOrCreateWorkspace(distRepository
, workspace
);
73 JcrUtils
.addPrivilege(javaSession
, "/", SlcConstants
.ROLE_SLC
, Privilege
.JCR_ALL
);
74 JcrUtils
.addPrivilege(distSession
, "/", SlcConstants
.ROLE_SLC
, Privilege
.JCR_ALL
);
75 } catch (RepositoryException e
) {
76 throw new SlcException("Cannot initialize OSGi Factory " + workspace
, e
);
78 JcrUtils
.logoutQuietly(javaSession
);
79 JcrUtils
.logoutQuietly(distSession
);
83 public void destroy() {
87 public Session
openJavaSession() throws RepositoryException
{
88 return javaRepository
.login(workspace
);
91 public Session
openDistSession() throws RepositoryException
{
92 return distRepository
.login(workspace
);
95 public void indexNode(Node node
) {
96 for (NodeIndexer nodeIndexer
: nodeIndexers
) {
97 nodeIndexer
.index(node
);
101 public Node
getMaven(Session distSession
, String coords
) throws RepositoryException
{
102 Artifact artifact
= new DefaultArtifact(coords
);
103 String path
= MavenConventionsUtils
.artifactPath(mavenProxyBase
, artifact
);
106 if (distSession
.itemExists(path
))
107 return distSession
.getNode(path
);
109 for (String mavenRepo
: mavenRepositories
) {
110 String url
= MavenConventionsUtils
.artifactUrl(mavenRepo
, artifact
);
112 Node node
= loadUrlToPath(url
, distSession
, path
);
116 loadUrlToPath(url
+ ".md5", distSession
, path
+ ".md5");
117 } catch (FileNotFoundException e
) {
121 loadUrlToPath(url
+ ".sha1", distSession
, path
+ ".sha1");
122 } catch (FileNotFoundException e
) {
127 } catch (FileNotFoundException e
) {
128 if (log
.isDebugEnabled())
129 log
.debug("Maven " + coords
+ " could not be downloaded from " + url
);
132 throw new SlcException("Could not download Maven " + coords
);
135 public Node
getDist(Session distSession
, String uri
) throws RepositoryException
{
136 String distPath
= downloadBase
+ '/' + JcrUtils
.urlAsPath(uri
);
139 if (distSession
.itemExists(distPath
))
140 return distSession
.getNode(distPath
);
143 List
<String
> urlBases
= null;
144 String uriPrefix
= null;
145 uriPrefixes
: for (String uriPref
: mirrors
.keySet()) {
146 if (uri
.startsWith(uriPref
)) {
147 if (mirrors
.get(uriPref
).size() > 0) {
148 urlBases
= mirrors
.get(uriPref
);
154 if (urlBases
== null)
156 return loadUrlToPath(uri
, distSession
, distPath
);
157 } catch (FileNotFoundException e
) {
158 throw new SlcException("Cannot download " + uri
, e
);
162 for (String urlBase
: urlBases
) {
163 String relativePath
= uri
.substring(uriPrefix
.length());
164 String url
= urlBase
+ relativePath
;
166 return loadUrlToPath(url
, distSession
, distPath
);
167 } catch (FileNotFoundException e
) {
168 if (log
.isDebugEnabled())
169 log
.debug("Cannot download " + url
+ ", trying another mirror");
173 throw new SlcException("Could not download " + uri
);
176 /** Actually downloads a file to an internal location */
177 protected Node
loadUrlToPath(String url
, Session distSession
, String path
)
178 throws RepositoryException
, FileNotFoundException
{
179 if (log
.isDebugEnabled())
180 log
.debug("Downloading " + url
+ "...");
182 InputStream in
= null;
183 URLConnection conn
= null;
184 Node folderNode
= JcrUtils
.mkfolders(distSession
, JcrUtils
.parentPath(path
));
186 URL u
= new URL(url
);
187 conn
= u
.openConnection();
189 in
= new BufferedInputStream(conn
.getInputStream());
190 // byte[] arr = IOUtils.toByteArray(in);
191 // Node fileNode = JcrUtils.copyBytesAsFile(folderNode,
192 // JcrUtils.nodeNameFromPath(path), arr);
193 Node fileNode
= JcrUtils
.copyStreamAsFile(folderNode
, JcrUtils
.nodeNameFromPath(path
), in
);
194 fileNode
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
195 Node origin
= fileNode
.addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
196 JcrUtils
.urlToAddressProperties(origin
, url
);
199 } catch (MalformedURLException e
) {
200 throw new SlcException("URL " + url
+ " not valid.", e
);
201 } catch (FileNotFoundException e
) {
203 } catch (IOException e
) {
204 throw new SlcException("Cannot load " + url
+ " to " + path
, e
);
206 IOUtils
.closeQuietly(in
);
211 public void setWorkspace(String workspace
) {
212 this.workspace
= workspace
;
215 public void setDistRepository(Repository distRepository
) {
216 this.distRepository
= distRepository
;
219 public void setJavaRepository(Repository javaRepository
) {
220 this.javaRepository
= javaRepository
;
223 public void setNodeIndexers(List
<NodeIndexer
> nodeIndexers
) {
224 this.nodeIndexers
= nodeIndexers
;
227 public void setMirrors(Map
<String
, List
<String
>> mirrors
) {
228 this.mirrors
= mirrors
;
231 public void setMavenRepositories(List
<String
> mavenRepositories
) {
232 this.mavenRepositories
= mavenRepositories
;
235 public void setMavenProxyBase(String mavenProxyBase
) {
236 this.mavenProxyBase
= mavenProxyBase
;