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
;
20 import org
.apache
.commons
.io
.IOUtils
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.jcr
.JcrUtils
;
24 import org
.argeo
.slc
.SlcConstants
;
25 import org
.argeo
.slc
.SlcException
;
26 import org
.argeo
.slc
.jcr
.SlcNames
;
27 import org
.argeo
.slc
.jcr
.SlcTypes
;
28 import org
.argeo
.slc
.repo
.NodeIndexer
;
29 import org
.argeo
.slc
.repo
.OsgiFactory
;
30 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
31 import org
.eclipse
.aether
.artifact
.Artifact
;
32 import org
.eclipse
.aether
.artifact
.DefaultArtifact
;
34 /** Default implementation of {@link OsgiFactory}. */
35 public class OsgiFactoryImpl
implements OsgiFactory
, SlcNames
{
36 private final static Log log
= LogFactory
.getLog(OsgiFactoryImpl
.class);
38 private String workspace
;
39 private Repository distRepository
;
40 private Repository javaRepository
;
42 private List
<NodeIndexer
> nodeIndexers
= new ArrayList
<NodeIndexer
>();
44 /** key is URI prefix, value list of base URLs */
45 private Map
<String
, List
<String
>> mirrors
= new HashMap
<String
, List
<String
>>();
47 private List
<String
> mavenRepositories
= new ArrayList
<String
>();
48 private String downloadBase
= "/download";
49 private String mavenProxyBase
= downloadBase
+ "/maven";
52 if (workspace
== null)
53 throw new SlcException("A workspace must be specified");
56 if (mavenRepositories
.size() == 0) {
58 // .add("http://search.maven.org/remotecontent?filepath=");
59 mavenRepositories
.add("http://repo1.maven.org/maven2");
62 Session javaSession
= null;
63 Session distSession
= null;
65 // TODO rather user a JavaRepoManager that will also implicitely
66 // manage the indexing of newly created nodes.
67 javaSession
= JcrUtils
.loginOrCreateWorkspace(javaRepository
, workspace
);
68 distSession
= JcrUtils
.loginOrCreateWorkspace(distRepository
, workspace
);
71 JcrUtils
.addPrivilege(javaSession
, "/", SlcConstants
.ROLE_SLC
, "jcr:all");
72 JcrUtils
.addPrivilege(distSession
, "/", SlcConstants
.ROLE_SLC
, "jcr:all");
73 } catch (RepositoryException e
) {
74 throw new SlcException("Cannot initialize OSGi Factory " + workspace
, e
);
76 JcrUtils
.logoutQuietly(javaSession
);
77 JcrUtils
.logoutQuietly(distSession
);
81 public void destroy() {
85 public Session
openJavaSession() throws RepositoryException
{
86 return javaRepository
.login(workspace
);
89 public Session
openDistSession() throws RepositoryException
{
90 return distRepository
.login(workspace
);
93 public void indexNode(Node node
) {
94 for (NodeIndexer nodeIndexer
: nodeIndexers
) {
95 nodeIndexer
.index(node
);
99 public Node
getMaven(Session distSession
, String coords
) throws RepositoryException
{
100 Artifact artifact
= new DefaultArtifact(coords
);
101 String path
= MavenConventionsUtils
.artifactPath(mavenProxyBase
, artifact
);
104 if (distSession
.itemExists(path
))
105 return distSession
.getNode(path
);
107 for (String mavenRepo
: mavenRepositories
) {
108 String url
= MavenConventionsUtils
.artifactUrl(mavenRepo
, artifact
);
110 Node node
= loadUrlToPath(url
, distSession
, path
);
114 loadUrlToPath(url
+ ".md5", distSession
, path
+ ".md5");
115 } catch (FileNotFoundException e
) {
119 loadUrlToPath(url
+ ".sha1", distSession
, path
+ ".sha1");
120 } catch (FileNotFoundException e
) {
125 } catch (FileNotFoundException e
) {
126 if (log
.isDebugEnabled())
127 log
.debug("Maven " + coords
+ " could not be downloaded from " + url
);
130 throw new SlcException("Could not download Maven " + coords
);
133 public Node
getDist(Session distSession
, String uri
) throws RepositoryException
{
134 String distPath
= downloadBase
+ '/' + JcrUtils
.urlAsPath(uri
);
137 if (distSession
.itemExists(distPath
))
138 return distSession
.getNode(distPath
);
141 List
<String
> urlBases
= null;
142 String uriPrefix
= null;
143 uriPrefixes
: for (String uriPref
: mirrors
.keySet()) {
144 if (uri
.startsWith(uriPref
)) {
145 if (mirrors
.get(uriPref
).size() > 0) {
146 urlBases
= mirrors
.get(uriPref
);
152 if (urlBases
== null)
154 return loadUrlToPath(uri
, distSession
, distPath
);
155 } catch (FileNotFoundException e
) {
156 throw new SlcException("Cannot download " + uri
, e
);
160 for (String urlBase
: urlBases
) {
161 String relativePath
= uri
.substring(uriPrefix
.length());
162 String url
= urlBase
+ relativePath
;
164 return loadUrlToPath(url
, distSession
, distPath
);
165 } catch (FileNotFoundException e
) {
166 if (log
.isDebugEnabled())
167 log
.debug("Cannot download " + url
+ ", trying another mirror");
171 throw new SlcException("Could not download " + uri
);
174 /** Actually downloads a file to an internal location */
175 protected Node
loadUrlToPath(String url
, Session distSession
, String path
)
176 throws RepositoryException
, FileNotFoundException
{
177 if (log
.isDebugEnabled())
178 log
.debug("Downloading " + url
+ "...");
180 InputStream in
= null;
181 URLConnection conn
= null;
182 Node folderNode
= JcrUtils
.mkfolders(distSession
, JcrUtils
.parentPath(path
));
184 URL u
= new URL(url
);
185 conn
= u
.openConnection();
187 in
= new BufferedInputStream(conn
.getInputStream());
188 // byte[] arr = IOUtils.toByteArray(in);
189 // Node fileNode = JcrUtils.copyBytesAsFile(folderNode,
190 // JcrUtils.nodeNameFromPath(path), arr);
191 Node fileNode
= JcrUtils
.copyStreamAsFile(folderNode
, JcrUtils
.nodeNameFromPath(path
), in
);
192 fileNode
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
193 Node origin
= fileNode
.addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
194 JcrUtils
.urlToAddressProperties(origin
, url
);
197 } catch (MalformedURLException e
) {
198 throw new SlcException("URL " + url
+ " not valid.", e
);
199 } catch (FileNotFoundException e
) {
201 } catch (IOException e
) {
202 throw new SlcException("Cannot load " + url
+ " to " + path
, e
);
204 IOUtils
.closeQuietly(in
);
209 public void setWorkspace(String workspace
) {
210 this.workspace
= workspace
;
213 public void setDistRepository(Repository distRepository
) {
214 this.distRepository
= distRepository
;
217 public void setJavaRepository(Repository javaRepository
) {
218 this.javaRepository
= javaRepository
;
221 public void setNodeIndexers(List
<NodeIndexer
> nodeIndexers
) {
222 this.nodeIndexers
= nodeIndexers
;
225 public void setMirrors(Map
<String
, List
<String
>> mirrors
) {
226 this.mirrors
= mirrors
;
229 public void setMavenRepositories(List
<String
> mavenRepositories
) {
230 this.mavenRepositories
= mavenRepositories
;
233 public void setMavenProxyBase(String mavenProxyBase
) {
234 this.mavenProxyBase
= mavenProxyBase
;