]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/OsgiFactoryImpl.java
0f918fdc334d5bee424d190a54d706351b5a3089
[gpl/argeo-slc.git] / org.argeo.slc.repo / src / org / argeo / slc / repo / osgi / OsgiFactoryImpl.java
1 package org.argeo.slc.repo.osgi;
2
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;
8 import java.net.URL;
9 import java.net.URLConnection;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import javax.jcr.Node;
16 import javax.jcr.Repository;
17 import javax.jcr.RepositoryException;
18 import javax.jcr.Session;
19
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;
33
34 /** Default implementation of {@link OsgiFactory}. */
35 public class OsgiFactoryImpl implements OsgiFactory, SlcNames {
36 private final static Log log = LogFactory.getLog(OsgiFactoryImpl.class);
37
38 private String workspace;
39 private Repository distRepository;
40 private Repository javaRepository;
41
42 private List<NodeIndexer> nodeIndexers = new ArrayList<NodeIndexer>();
43
44 /** key is URI prefix, value list of base URLs */
45 private Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
46
47 private List<String> mavenRepositories = new ArrayList<String>();
48 private String mavenProxyBase = "/mavenProxy";
49
50 public void init() {
51 if (workspace == null)
52 throw new SlcException("A workspace must be specified");
53
54 // default Maven repo
55 if (mavenRepositories.size() == 0) {
56 // mavenRepositories
57 // .add("http://search.maven.org/remotecontent?filepath=");
58 mavenRepositories.add("http://repo1.maven.org/maven2");
59 }
60
61 Session javaSession = null;
62 Session distSession = null;
63 try {
64 // TODO rather user a JavaRepoManager that will also implicitely
65 // manage the indexing of newly created nodes.
66 javaSession = JcrUtils.loginOrCreateWorkspace(javaRepository,
67 workspace);
68 distSession = JcrUtils.loginOrCreateWorkspace(distRepository,
69 workspace);
70
71 // Privileges
72 JcrUtils.addPrivilege(javaSession, "/", SlcConstants.ROLE_SLC,
73 "jcr:all");
74 JcrUtils.addPrivilege(distSession, "/", SlcConstants.ROLE_SLC,
75 "jcr:all");
76 } catch (RepositoryException e) {
77 throw new SlcException("Cannot initialize OSGi Factory "
78 + workspace, e);
79 } finally {
80 JcrUtils.logoutQuietly(javaSession);
81 JcrUtils.logoutQuietly(distSession);
82 }
83 }
84
85 public void destroy() {
86
87 }
88
89 public Session openJavaSession() throws RepositoryException {
90 return javaRepository.login(workspace);
91 }
92
93 public Session openDistSession() throws RepositoryException {
94 return distRepository.login(workspace);
95 }
96
97 public void indexNode(Node node) {
98 for (NodeIndexer nodeIndexer : nodeIndexers) {
99 nodeIndexer.index(node);
100 }
101 }
102
103 public Node getMaven(Session distSession, String coords)
104 throws RepositoryException {
105 Artifact artifact = new DefaultArtifact(coords);
106 String path = MavenConventionsUtils.artifactPath(mavenProxyBase,
107 artifact);
108
109 // exists
110 if (distSession.itemExists(path))
111 return distSession.getNode(path);
112
113 for (String mavenRepo : mavenRepositories) {
114 String url = mavenRepo
115 + MavenConventionsUtils.artifactPath("/", artifact);
116 try {
117 Node node = loadUrlToPath(url, distSession, path);
118 if (node != null) {
119 // checksums
120 try {
121 loadUrlToPath(url + ".md5", distSession, path + ".md5");
122 } catch (FileNotFoundException e) {
123 // silent
124 }
125 try {
126 loadUrlToPath(url + ".sha1", distSession, path
127 + ".sha1");
128 } catch (FileNotFoundException e) {
129 // silent
130 }
131 return node;
132 }
133 } catch (FileNotFoundException e) {
134 if (log.isDebugEnabled())
135 log.debug("Maven " + coords
136 + " could not be downloaded from " + url);
137 }
138 }
139 throw new SlcException("Could not download Maven " + coords);
140 }
141
142 public Node getDist(Session distSession, String uri)
143 throws RepositoryException {
144 String distPath = '/' + JcrUtils.urlAsPath(uri);
145
146 // already retrieved
147 if (distSession.itemExists(distPath))
148 return distSession.getNode(distPath);
149
150 // find mirror
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);
157 uriPrefix = uriPref;
158 break uriPrefixes;
159 }
160 }
161 }
162 if (urlBases == null)
163 try {
164 return loadUrlToPath(uri, distSession, distPath);
165 } catch (FileNotFoundException e) {
166 throw new SlcException("Cannot download " + uri, e);
167 }
168
169 // try to download
170 for (String urlBase : urlBases) {
171 String relativePath = uri.substring(uriPrefix.length());
172 String url = urlBase + relativePath;
173 try {
174 return loadUrlToPath(url, distSession, distPath);
175 } catch (FileNotFoundException e) {
176 if (log.isDebugEnabled())
177 log.debug("Cannot download " + url
178 + ", trying another mirror");
179 }
180 }
181
182 throw new SlcException("Could not download " + uri);
183 }
184
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 + "...");
190
191 InputStream in = null;
192 URLConnection conn = null;
193 Node folderNode = JcrUtils.mkfolders(distSession,
194 JcrUtils.parentPath(path));
195 try {
196 URL u = new URL(url);
197 conn = u.openConnection();
198 conn.connect();
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);
208 distSession.save();
209 return fileNode;
210 } catch (MalformedURLException e) {
211 throw new SlcException("URL " + url + " not valid.", e);
212 } catch (FileNotFoundException e) {
213 throw e;
214 } catch (IOException e) {
215 throw new SlcException("Cannot load " + url + " to " + path, e);
216 } finally {
217 IOUtils.closeQuietly(in);
218 }
219
220 }
221
222 public void setWorkspace(String workspace) {
223 this.workspace = workspace;
224 }
225
226 public void setDistRepository(Repository distRepository) {
227 this.distRepository = distRepository;
228 }
229
230 public void setJavaRepository(Repository javaRepository) {
231 this.javaRepository = javaRepository;
232 }
233
234 public void setNodeIndexers(List<NodeIndexer> nodeIndexers) {
235 this.nodeIndexers = nodeIndexers;
236 }
237
238 public void setMirrors(Map<String, List<String>> mirrors) {
239 this.mirrors = mirrors;
240 }
241
242 public void setMavenRepositories(List<String> mavenRepositories) {
243 this.mavenRepositories = mavenRepositories;
244 }
245
246 public void setMavenProxyBase(String mavenProxyBase) {
247 this.mavenProxyBase = mavenProxyBase;
248 }
249
250 }