]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/provisioning/FileProvider.java
6751e09f8fc7579420cb767aa77bd510ec1a9451
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / provisioning / FileProvider.java
1 package org.argeo.slc.web.mvc.provisioning;
2
3 import java.io.InputStream;
4 import java.io.OutputStream;
5
6 import org.apache.commons.io.IOUtils;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.argeo.slc.SlcException;
10 import org.springframework.core.io.Resource;
11
12 public class FileProvider {
13 private final static Log log = LogFactory.getLog(FileProvider.class);
14
15 private Resource base;
16
17 public void read(String distribution, String name, String version,
18 OutputStream out) {
19 Resource bundle = getBundle(distribution, name, version);
20 InputStream in = null;
21 try {
22 in = bundle.getInputStream();
23 IOUtils.copy(in, out);
24 } catch (Exception e) {
25 throw new SlcException("Cannot read bundle for " + name + " ("
26 + version + ")",e);
27 } finally {
28 IOUtils.closeQuietly(in);
29 }
30 }
31
32 public Resource getBundle(String distribution, String name, String version) {
33 try {
34 String shortVersion = version;
35 int indR = version.indexOf("-r");
36 if (indR > -1) {
37 shortVersion = version.substring(0, indR);
38 }
39
40 int indS = shortVersion.indexOf(".SNAPSHOT");
41 if (indS > -1) {
42 StringBuffer buf = new StringBuffer(shortVersion);
43 buf.setCharAt(indS, '-');
44 shortVersion = buf.toString();
45 }
46
47 if (log.isDebugEnabled())
48 log.debug("Short version for " + name + ": " + shortVersion);
49
50 Resource res = base.createRelative("lib/" + name + "-"
51 + shortVersion + ".jar");
52 return res;
53 } catch (Exception e) {
54 throw new SlcException("Cannot get bundle for " + name + " ("
55 + version + ")",e);
56 }
57 }
58
59 public void setBase(Resource base) {
60 this.base = base;
61 }
62
63 }