1 package org
.argeo
.slc
.repo
.osgi
;
3 import java
.io
.FileNotFoundException
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.net
.MalformedURLException
;
8 import java
.util
.ArrayList
;
9 import java
.util
.HashMap
;
10 import java
.util
.List
;
13 import javax
.jcr
.Node
;
14 import javax
.jcr
.Repository
;
15 import javax
.jcr
.RepositoryException
;
16 import javax
.jcr
.Session
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.argeo
.jcr
.JcrUtils
;
21 import org
.argeo
.slc
.SlcConstants
;
22 import org
.argeo
.slc
.SlcException
;
23 import org
.argeo
.slc
.jcr
.SlcNames
;
24 import org
.argeo
.slc
.jcr
.SlcTypes
;
25 import org
.argeo
.slc
.repo
.NodeIndexer
;
26 import org
.argeo
.slc
.repo
.OsgiFactory
;
27 import org
.argeo
.slc
.repo
.maven
.MavenConventionsUtils
;
28 import org
.sonatype
.aether
.artifact
.Artifact
;
29 import org
.sonatype
.aether
.util
.artifact
.DefaultArtifact
;
31 /** Default implementation of {@link OsgiFactory}. */
32 public class OsgiFactoryImpl
implements OsgiFactory
, SlcNames
{
33 private final static Log log
= LogFactory
.getLog(OsgiFactoryImpl
.class);
35 private String workspace
;
36 private Repository distRepository
;
37 private Repository javaRepository
;
39 private List
<NodeIndexer
> nodeIndexers
= new ArrayList
<NodeIndexer
>();
41 /** key is URI prefix, value list of base URLs */
42 private Map
<String
, List
<String
>> mirrors
= new HashMap
<String
, List
<String
>>();
44 private List
<String
> mavenRepositories
= new ArrayList
<String
>();
45 private String mavenProxyBase
= "/mavenProxy";
48 if (workspace
== null)
49 throw new SlcException("A workspace must be specified");
52 if (mavenRepositories
.size() == 0) {
54 // .add("http://search.maven.org/remotecontent?filepath=");
55 mavenRepositories
.add("http://repo1.maven.org/maven2");
58 Session javaSession
= null;
59 Session distSession
= null;
61 javaSession
= JcrUtils
.loginOrCreateWorkspace(javaRepository
,
63 distSession
= JcrUtils
.loginOrCreateWorkspace(distRepository
,
67 JcrUtils
.addPrivilege(javaSession
, "/", SlcConstants
.ROLE_SLC
,
69 JcrUtils
.addPrivilege(distSession
, "/", SlcConstants
.ROLE_SLC
,
71 } catch (RepositoryException e
) {
72 throw new SlcException("Cannot initialize OSGi Factory "
75 JcrUtils
.logoutQuietly(javaSession
);
76 JcrUtils
.logoutQuietly(distSession
);
80 public void destroy() {
84 public Session
openJavaSession() throws RepositoryException
{
85 return javaRepository
.login(workspace
);
88 public Session
openDistSession() throws RepositoryException
{
89 return distRepository
.login(workspace
);
92 public void indexNode(Node node
) {
93 for (NodeIndexer nodeIndexer
: nodeIndexers
) {
94 nodeIndexer
.index(node
);
98 public Node
getMaven(Session distSession
, String coords
)
99 throws RepositoryException
{
100 Artifact artifact
= new DefaultArtifact(coords
);
101 String path
= MavenConventionsUtils
.artifactPath(mavenProxyBase
,
105 if (distSession
.itemExists(path
))
106 return distSession
.getNode(path
);
108 for (String mavenRepo
: mavenRepositories
) {
109 String url
= mavenRepo
110 + MavenConventionsUtils
.artifactPath("/", 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
123 } catch (FileNotFoundException e
) {
128 } catch (FileNotFoundException e
) {
129 if (log
.isDebugEnabled())
130 log
.debug("Maven " + coords
131 + " could not be downloaded from " + url
);
134 throw new SlcException("Could not download Maven " + coords
);
137 public Node
getDist(Session distSession
, String uri
)
138 throws RepositoryException
{
139 String distPath
= '/' + JcrUtils
.urlAsPath(uri
);
142 if (distSession
.itemExists(distPath
))
143 return distSession
.getNode(distPath
);
146 List
<String
> urlBases
= null;
147 String uriPrefix
= null;
148 uriPrefixes
: for (String uriPref
: mirrors
.keySet()) {
149 if (uri
.startsWith(uriPref
)) {
150 if (mirrors
.get(uriPref
).size() > 0) {
151 urlBases
= mirrors
.get(uriPref
);
157 if (urlBases
== null)
159 return loadUrlToPath(uri
, distSession
, distPath
);
160 } catch (FileNotFoundException e
) {
161 throw new SlcException("Cannot download " + uri
, e
);
165 for (String urlBase
: urlBases
) {
166 String relativePath
= uri
.substring(uriPrefix
.length());
167 String url
= urlBase
+ relativePath
;
169 return loadUrlToPath(url
, distSession
, distPath
);
170 } catch (FileNotFoundException e
) {
171 if (log
.isDebugEnabled())
172 log
.debug("Cannot download" + url
173 + ", trying another mirror");
177 throw new SlcException("Could not download " + uri
);
180 /** Actually downloads a file to an internal location */
181 protected Node
loadUrlToPath(String url
, Session distSession
, String path
)
182 throws RepositoryException
, FileNotFoundException
{
183 if (log
.isDebugEnabled())
184 log
.debug("Downloading " + url
+ "...");
186 InputStream in
= null;
187 Node folderNode
= JcrUtils
.mkfolders(distSession
,
188 JcrUtils
.parentPath(path
));
190 URL u
= new URL(url
);
192 Node fileNode
= JcrUtils
.copyStreamAsFile(folderNode
,
193 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
);
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
;