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 mavenProxyBase
= "/mavenProxy";
51 if (workspace
== null)
52 throw new SlcException("A workspace must be specified");
55 if (mavenRepositories
.size() == 0) {
57 // .add("http://search.maven.org/remotecontent?filepath=");
58 mavenRepositories
.add("http://repo1.maven.org/maven2");
61 Session javaSession
= null;
62 Session distSession
= null;
64 // TODO rather user a JavaRepoManager that will also implicitely
65 // manage the indexing of newly created nodes.
66 javaSession
= JcrUtils
.loginOrCreateWorkspace(javaRepository
,
68 distSession
= JcrUtils
.loginOrCreateWorkspace(distRepository
,
72 JcrUtils
.addPrivilege(javaSession
, "/", SlcConstants
.ROLE_SLC
,
74 JcrUtils
.addPrivilege(distSession
, "/", SlcConstants
.ROLE_SLC
,
76 } catch (RepositoryException e
) {
77 throw new SlcException("Cannot initialize OSGi Factory "
80 JcrUtils
.logoutQuietly(javaSession
);
81 JcrUtils
.logoutQuietly(distSession
);
85 public void destroy() {
89 public Session
openJavaSession() throws RepositoryException
{
90 return javaRepository
.login(workspace
);
93 public Session
openDistSession() throws RepositoryException
{
94 return distRepository
.login(workspace
);
97 public void indexNode(Node node
) {
98 for (NodeIndexer nodeIndexer
: nodeIndexers
) {
99 nodeIndexer
.index(node
);
103 public Node
getMaven(Session distSession
, String coords
)
104 throws RepositoryException
{
105 Artifact artifact
= new DefaultArtifact(coords
);
106 String path
= MavenConventionsUtils
.artifactPath(mavenProxyBase
,
110 if (distSession
.itemExists(path
))
111 return distSession
.getNode(path
);
113 for (String mavenRepo
: mavenRepositories
) {
114 String url
= mavenRepo
115 + MavenConventionsUtils
.artifactPath("/", artifact
);
117 Node node
= loadUrlToPath(url
, distSession
, path
);
121 loadUrlToPath(url
+ ".md5", distSession
, path
+ ".md5");
122 } catch (FileNotFoundException e
) {
126 loadUrlToPath(url
+ ".sha1", distSession
, path
128 } catch (FileNotFoundException e
) {
133 } catch (FileNotFoundException e
) {
134 if (log
.isDebugEnabled())
135 log
.debug("Maven " + coords
136 + " could not be downloaded from " + url
);
139 throw new SlcException("Could not download Maven " + coords
);
142 public Node
getDist(Session distSession
, String uri
)
143 throws RepositoryException
{
144 String distPath
= '/' + JcrUtils
.urlAsPath(uri
);
147 if (distSession
.itemExists(distPath
))
148 return distSession
.getNode(distPath
);
151 List
<String
> urlBases
= null;
152 String uriPrefix
= null;
153 uriPrefixes
: for (String uriPref
: mirrors
.keySet()) {
154 if (uri
.startsWith(uriPref
)) {
155 if (mirrors
.get(uriPref
).size() > 0) {
156 urlBases
= mirrors
.get(uriPref
);
162 if (urlBases
== null)
164 return loadUrlToPath(uri
, distSession
, distPath
);
165 } catch (FileNotFoundException e
) {
166 throw new SlcException("Cannot download " + uri
, e
);
170 for (String urlBase
: urlBases
) {
171 String relativePath
= uri
.substring(uriPrefix
.length());
172 String url
= urlBase
+ relativePath
;
174 return loadUrlToPath(url
, distSession
, distPath
);
175 } catch (FileNotFoundException e
) {
176 if (log
.isDebugEnabled())
177 log
.debug("Cannot download " + url
178 + ", trying another mirror");
182 throw new SlcException("Could not download " + uri
);
185 /** Actually downloads a file to an internal location */
186 protected Node
loadUrlToPath(String url
, Session distSession
, String path
)
187 throws RepositoryException
, FileNotFoundException
{
188 if (log
.isDebugEnabled())
189 log
.debug("Downloading " + url
+ "...");
191 InputStream in
= null;
192 URLConnection conn
= null;
193 Node folderNode
= JcrUtils
.mkfolders(distSession
,
194 JcrUtils
.parentPath(path
));
196 URL u
= new URL(url
);
197 conn
= u
.openConnection();
199 in
= new BufferedInputStream(conn
.getInputStream());
200 // byte[] arr = IOUtils.toByteArray(in);
201 // Node fileNode = JcrUtils.copyBytesAsFile(folderNode,
202 // JcrUtils.nodeNameFromPath(path), arr);
203 Node fileNode
= JcrUtils
.copyStreamAsFile(folderNode
,
204 JcrUtils
.nodeNameFromPath(path
), in
);
205 fileNode
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
206 Node origin
= fileNode
.addNode(SLC_ORIGIN
, SlcTypes
.SLC_PROXIED
);
207 JcrUtils
.urlToAddressProperties(origin
, url
);
210 } catch (MalformedURLException e
) {
211 throw new SlcException("URL " + url
+ " not valid.", e
);
212 } catch (FileNotFoundException e
) {
214 } catch (IOException e
) {
215 throw new SlcException("Cannot load " + url
+ " to " + path
, e
);
217 IOUtils
.closeQuietly(in
);
222 public void setWorkspace(String workspace
) {
223 this.workspace
= workspace
;
226 public void setDistRepository(Repository distRepository
) {
227 this.distRepository
= distRepository
;
230 public void setJavaRepository(Repository javaRepository
) {
231 this.javaRepository
= javaRepository
;
234 public void setNodeIndexers(List
<NodeIndexer
> nodeIndexers
) {
235 this.nodeIndexers
= nodeIndexers
;
238 public void setMirrors(Map
<String
, List
<String
>> mirrors
) {
239 this.mirrors
= mirrors
;
242 public void setMavenRepositories(List
<String
> mavenRepositories
) {
243 this.mavenRepositories
= mavenRepositories
;
246 public void setMavenProxyBase(String mavenProxyBase
) {
247 this.mavenProxyBase
= mavenProxyBase
;