]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/OsgiFactoryImpl.java
Work on the binaries indexer.
[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 import org.argeo.slc.repo.maven.MavenConventionsUtils;
28 import org.sonatype.aether.artifact.Artifact;
29 import org.sonatype.aether.util.artifact.DefaultArtifact;
30
31 /** Default implementation of {@link OsgiFactory}. */
32 public class OsgiFactoryImpl implements OsgiFactory, SlcNames {
33 private final static Log log = LogFactory.getLog(OsgiFactoryImpl.class);
34
35 private String workspace;
36 private Repository distRepository;
37 private Repository javaRepository;
38
39 private List<NodeIndexer> nodeIndexers = new ArrayList<NodeIndexer>();
40
41 /** key is URI prefix, value list of base URLs */
42 private Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
43
44 private List<String> mavenRepositories = new ArrayList<String>();
45 private String mavenProxyBase = "/mavenProxy";
46
47 public void init() {
48 if (workspace == null)
49 throw new SlcException("A workspace must be specified");
50
51 // default Maven repo
52 if (mavenRepositories.size() == 0) {
53 // mavenRepositories
54 // .add("http://search.maven.org/remotecontent?filepath=");
55 mavenRepositories.add("http://repo1.maven.org/maven2");
56 }
57
58 Session javaSession = null;
59 Session distSession = null;
60 try {
61 javaSession = JcrUtils.loginOrCreateWorkspace(javaRepository,
62 workspace);
63 distSession = JcrUtils.loginOrCreateWorkspace(distRepository,
64 workspace);
65
66 // Privileges
67 JcrUtils.addPrivilege(javaSession, "/", SlcConstants.ROLE_SLC,
68 "jcr:all");
69 JcrUtils.addPrivilege(distSession, "/", SlcConstants.ROLE_SLC,
70 "jcr:all");
71 } catch (RepositoryException e) {
72 throw new SlcException("Cannot initialize OSGi Factory "
73 + workspace, e);
74 } finally {
75 JcrUtils.logoutQuietly(javaSession);
76 JcrUtils.logoutQuietly(distSession);
77 }
78 }
79
80 public void destroy() {
81
82 }
83
84 public Session openJavaSession() throws RepositoryException {
85 return javaRepository.login(workspace);
86 }
87
88 public Session openDistSession() throws RepositoryException {
89 return distRepository.login(workspace);
90 }
91
92 public void indexNode(Node node) {
93 for (NodeIndexer nodeIndexer : nodeIndexers) {
94 nodeIndexer.index(node);
95 }
96 }
97
98 public Node getMaven(Session distSession, String coords)
99 throws RepositoryException {
100 Artifact artifact = new DefaultArtifact(coords);
101 String path = MavenConventionsUtils.artifactPath(mavenProxyBase,
102 artifact);
103
104 // exists
105 if (distSession.itemExists(path))
106 return distSession.getNode(path);
107
108 for (String mavenRepo : mavenRepositories) {
109 String url = mavenRepo
110 + MavenConventionsUtils.artifactPath("/", artifact);
111 try {
112 Node node = loadUrlToPath(url, distSession, path);
113 if (node != null) {
114 // checksums
115 try {
116 loadUrlToPath(url + ".md5", distSession, path + ".md5");
117 } catch (FileNotFoundException e) {
118 // silent
119 }
120 try {
121 loadUrlToPath(url + ".sha1", distSession, path
122 + ".sha1");
123 } catch (FileNotFoundException e) {
124 // silent
125 }
126 return node;
127 }
128 } catch (FileNotFoundException e) {
129 if (log.isDebugEnabled())
130 log.debug("Maven " + coords
131 + " could not be downloaded from " + url);
132 }
133 }
134 throw new SlcException("Could not download Maven " + coords);
135 }
136
137 public Node getDist(Session distSession, String uri)
138 throws RepositoryException {
139 String distPath = '/' + JcrUtils.urlAsPath(uri);
140
141 // already retrieved
142 if (distSession.itemExists(distPath))
143 return distSession.getNode(distPath);
144
145 // find mirror
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);
152 uriPrefix = uriPref;
153 break uriPrefixes;
154 }
155 }
156 }
157 if (urlBases == null)
158 try {
159 return loadUrlToPath(uri, distSession, distPath);
160 } catch (FileNotFoundException e) {
161 throw new SlcException("Cannot download " + uri, e);
162 }
163
164 // try to download
165 for (String urlBase : urlBases) {
166 String relativePath = uri.substring(uriPrefix.length());
167 String url = urlBase + relativePath;
168 try {
169 return loadUrlToPath(url, distSession, distPath);
170 } catch (FileNotFoundException e) {
171 if (log.isDebugEnabled())
172 log.debug("Cannot download" + url
173 + ", trying another mirror");
174 }
175 }
176
177 throw new SlcException("Could not download " + uri);
178 }
179
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 + "...");
185
186 InputStream in = null;
187 Node folderNode = JcrUtils.mkfolders(distSession,
188 JcrUtils.parentPath(path));
189 try {
190 URL u = new URL(url);
191 in = u.openStream();
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);
197 distSession.save();
198 return fileNode;
199 } catch (MalformedURLException e) {
200 throw new SlcException("URL " + url + " not valid.", e);
201 } catch (FileNotFoundException e) {
202 throw e;
203 } catch (IOException e) {
204 throw new SlcException("Cannot load " + url + " to " + path, e);
205 }
206
207 }
208
209 public void setWorkspace(String workspace) {
210 this.workspace = workspace;
211 }
212
213 public void setDistRepository(Repository distRepository) {
214 this.distRepository = distRepository;
215 }
216
217 public void setJavaRepository(Repository javaRepository) {
218 this.javaRepository = javaRepository;
219 }
220
221 public void setNodeIndexers(List<NodeIndexer> nodeIndexers) {
222 this.nodeIndexers = nodeIndexers;
223 }
224
225 public void setMirrors(Map<String, List<String>> mirrors) {
226 this.mirrors = mirrors;
227 }
228
229 }