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
;
28 /** Default implementation of {@link OsgiFactory}. */
29 public class OsgiFactoryImpl
implements OsgiFactory
{
30 private final static Log log
= LogFactory
.getLog(OsgiFactoryImpl
.class);
32 private String workspace
;
33 private Repository distRepository
;
34 private Repository javaRepository
;
36 private List
<NodeIndexer
> nodeIndexers
= new ArrayList
<NodeIndexer
>();
38 /** key is URI prefix, value list of base URLs */
39 private Map
<String
, List
<String
>> mirrors
= new HashMap
<String
, List
<String
>>();
42 if (workspace
== null)
43 throw new SlcException("A workspace must be specified");
45 Session javaSession
= null;
46 Session distSession
= null;
48 javaSession
= JcrUtils
.loginOrCreateWorkspace(javaRepository
,
50 distSession
= JcrUtils
.loginOrCreateWorkspace(distRepository
,
54 JcrUtils
.addPrivilege(javaSession
, "/", SlcConstants
.ROLE_SLC
,
56 JcrUtils
.addPrivilege(distSession
, "/", SlcConstants
.ROLE_SLC
,
58 } catch (RepositoryException e
) {
59 throw new SlcException("Cannot initialize OSGi Factory "
62 JcrUtils
.logoutQuietly(javaSession
);
63 JcrUtils
.logoutQuietly(distSession
);
67 public void destroy() {
71 public Session
openJavaSession() throws RepositoryException
{
72 return javaRepository
.login(workspace
);
75 public Session
openDistSession() throws RepositoryException
{
76 return distRepository
.login(workspace
);
79 public void indexNode(Node node
) {
80 for (NodeIndexer nodeIndexer
: nodeIndexers
) {
81 nodeIndexer
.index(node
);
85 public Node
getDist(Session distSession
, String uri
)
86 throws RepositoryException
{
87 String distPath
= '/' + JcrUtils
.urlAsPath(uri
);
90 if (distSession
.itemExists(distPath
))
91 return distSession
.getNode(distPath
);
94 List
<String
> urlBases
= null;
95 String uriPrefix
= null;
96 uriPrefixes
: for (String uriPref
: mirrors
.keySet()) {
97 if (uri
.startsWith(uriPref
)) {
98 if (mirrors
.get(uriPref
).size() > 0) {
99 urlBases
= mirrors
.get(uriPref
);
105 if (urlBases
== null)
107 return loadUrlToPath(uri
, distSession
, distPath
);
108 } catch (FileNotFoundException e
) {
109 throw new SlcException("Cannot download " + uri
, e
);
113 for (String urlBase
: urlBases
) {
114 String relativePath
= uri
.substring(uriPrefix
.length());
115 String url
= urlBase
+ relativePath
;
117 return loadUrlToPath(url
, distSession
, distPath
);
118 } catch (FileNotFoundException e
) {
119 if (log
.isDebugEnabled())
120 log
.debug("Cannot download" + url
121 + ", trying another mirror");
125 throw new SlcException("Could not download " + uri
);
128 protected Node
loadUrlToPath(String url
, Session distSession
, String path
)
129 throws RepositoryException
, FileNotFoundException
{
130 if (log
.isDebugEnabled())
131 log
.debug("Downloading " + url
+ "...");
133 InputStream in
= null;
134 Node folderNode
= JcrUtils
.mkfolders(distSession
,
135 JcrUtils
.parentPath(path
));
137 URL u
= new URL(url
);
139 Node fileNode
= JcrUtils
.copyStreamAsFile(folderNode
,
140 JcrUtils
.nodeNameFromPath(path
), in
);
141 fileNode
.addMixin(SlcTypes
.SLC_KNOWN_ORIGIN
);
142 Node origin
= fileNode
.addNode(SlcNames
.SLC_ORIGIN
,
143 SlcTypes
.SLC_PROXIED
);
144 JcrUtils
.urlToAddressProperties(origin
, url
);
147 } catch (MalformedURLException e
) {
148 throw new SlcException("URL " + url
+ " not valid.", e
);
149 } catch (FileNotFoundException e
) {
151 } catch (IOException e
) {
152 throw new SlcException("Cannot load " + url
+ " to " + path
, e
);
157 public void setWorkspace(String workspace
) {
158 this.workspace
= workspace
;
161 public void setDistRepository(Repository distRepository
) {
162 this.distRepository
= distRepository
;
165 public void setJavaRepository(Repository javaRepository
) {
166 this.javaRepository
= javaRepository
;
169 public void setNodeIndexers(List
<NodeIndexer
> nodeIndexers
) {
170 this.nodeIndexers
= nodeIndexers
;
173 public void setMirrors(Map
<String
, List
<String
>> mirrors
) {
174 this.mirrors
= mirrors
;