]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/OsgiFactoryImpl.java
ab4f9775cf37c5ffa6a5eb48b09d5cbb86459019
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / OsgiFactoryImpl.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 import javax.jcr.Node;
14 import javax.jcr.Repository;
15 import javax.jcr.RepositoryException;
16 import javax.jcr.Session;
17
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
28 /** Default implementation of {@link OsgiFactory}. */
29 public class OsgiFactoryImpl implements OsgiFactory {
30 private final static Log log = LogFactory.getLog(OsgiFactoryImpl.class);
31
32 private String workspace;
33 private Repository distRepository;
34 private Repository javaRepository;
35
36 private List<NodeIndexer> nodeIndexers = new ArrayList<NodeIndexer>();
37
38 /** key is URI prefix, value list of base URLs */
39 private Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
40
41 public void init() {
42 if (workspace == null)
43 throw new SlcException("A workspace must be specified");
44
45 Session javaSession = null;
46 Session distSession = null;
47 try {
48 javaSession = JcrUtils.loginOrCreateWorkspace(javaRepository,
49 workspace);
50 distSession = JcrUtils.loginOrCreateWorkspace(distRepository,
51 workspace);
52
53 // Privileges
54 JcrUtils.addPrivilege(javaSession, "/", SlcConstants.ROLE_SLC,
55 "jcr:all");
56 JcrUtils.addPrivilege(distSession, "/", SlcConstants.ROLE_SLC,
57 "jcr:all");
58 } catch (RepositoryException e) {
59 throw new SlcException("Cannot initialize OSGi Factory "
60 + workspace, e);
61 } finally {
62 JcrUtils.logoutQuietly(javaSession);
63 JcrUtils.logoutQuietly(distSession);
64 }
65 }
66
67 public void destroy() {
68
69 }
70
71 public Session openJavaSession() throws RepositoryException {
72 return javaRepository.login(workspace);
73 }
74
75 public Session openDistSession() throws RepositoryException {
76 return distRepository.login(workspace);
77 }
78
79 public void indexNode(Node node) {
80 for (NodeIndexer nodeIndexer : nodeIndexers) {
81 nodeIndexer.index(node);
82 }
83 }
84
85 public Node getDist(Session distSession, String uri)
86 throws RepositoryException {
87 String distPath = '/' + JcrUtils.urlAsPath(uri);
88
89 // already retrieved
90 if (distSession.itemExists(distPath))
91 return distSession.getNode(distPath);
92
93 // find mirror
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);
100 uriPrefix = uriPref;
101 break uriPrefixes;
102 }
103 }
104 }
105 if (urlBases == null)
106 try {
107 return loadUrlToPath(uri, distSession, distPath);
108 } catch (FileNotFoundException e) {
109 throw new SlcException("Cannot download " + uri, e);
110 }
111
112 // try to download
113 for (String urlBase : urlBases) {
114 String relativePath = uri.substring(uriPrefix.length());
115 String url = urlBase + relativePath;
116 try {
117 return loadUrlToPath(url, distSession, distPath);
118 } catch (FileNotFoundException e) {
119 if (log.isDebugEnabled())
120 log.debug("Cannot download" + url
121 + ", trying another mirror");
122 }
123 }
124
125 throw new SlcException("Could not download " + uri);
126 }
127
128 protected Node loadUrlToPath(String url, Session distSession, String path)
129 throws RepositoryException, FileNotFoundException {
130 if (log.isDebugEnabled())
131 log.debug("Downloading " + url + "...");
132
133 InputStream in = null;
134 Node folderNode = JcrUtils.mkfolders(distSession,
135 JcrUtils.parentPath(path));
136 try {
137 URL u = new URL(url);
138 in = u.openStream();
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);
145 distSession.save();
146 return fileNode;
147 } catch (MalformedURLException e) {
148 throw new SlcException("URL " + url + " not valid.", e);
149 } catch (FileNotFoundException e) {
150 throw e;
151 } catch (IOException e) {
152 throw new SlcException("Cannot load " + url + " to " + path, e);
153 }
154
155 }
156
157 public void setWorkspace(String workspace) {
158 this.workspace = workspace;
159 }
160
161 public void setDistRepository(Repository distRepository) {
162 this.distRepository = distRepository;
163 }
164
165 public void setJavaRepository(Repository javaRepository) {
166 this.javaRepository = javaRepository;
167 }
168
169 public void setNodeIndexers(List<NodeIndexer> nodeIndexers) {
170 this.nodeIndexers = nodeIndexers;
171 }
172
173 public void setMirrors(Map<String, List<String>> mirrors) {
174 this.mirrors = mirrors;
175 }
176
177 }