blob: 548ace7e56c2d6462e0a67cae8bae0537e957e9d [file] [log] [blame]
Stuart McCulloch42151ee2012-07-16 13:43:38 +00001package aQute.bnd.filerepo;
Stuart McCullochf3173222012-06-07 21:57:32 +00002
3import java.io.*;
4import java.util.*;
5import java.util.regex.*;
6
Stuart McCullochcd1ddd72012-07-19 13:11:20 +00007import aQute.bnd.version.*;
Stuart McCullochf3173222012-06-07 21:57:32 +00008
9public class FileRepo {
10 File root;
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000011 Pattern REPO_FILE = Pattern.compile("([-a-zA-z0-9_\\.]+)-([0-9\\.]+)\\.(jar|lib)");
Stuart McCullochf3173222012-06-07 21:57:32 +000012
13 public FileRepo(File root) {
14 this.root = root;
15 }
16
17 /**
18 * Get a list of URLs to bundles that are constrained by the bsn and
19 * versionRange.
20 */
21 public File[] get(String bsn, final VersionRange versionRange) throws Exception {
22
23 //
24 // Check if the entry exists
25 //
26 File f = new File(root, bsn);
27 if (!f.isDirectory())
28 return null;
29
30 //
31 // Iterator over all the versions for this BSN.
32 // Create a sorted map over the version as key
33 // and the file as URL as value. Only versions
34 // that match the desired range are included in
35 // this list.
36 //
37 return f.listFiles(new FilenameFilter() {
38 public boolean accept(File dir, String name) {
39 Matcher m = REPO_FILE.matcher(name);
40 if (!m.matches())
41 return false;
Stuart McCulloch4482c702012-06-15 13:27:53 +000042 if (versionRange == null)
Stuart McCullochf3173222012-06-07 21:57:32 +000043 return true;
Stuart McCulloch4482c702012-06-15 13:27:53 +000044
Stuart McCullochf3173222012-06-07 21:57:32 +000045 Version v = new Version(m.group(2));
46 return versionRange.includes(v);
47 }
48 });
49 }
50
51 public List<String> list(String regex) throws Exception {
52 if (regex == null)
53 regex = ".*";
54 final Pattern pattern = Pattern.compile(regex);
55
56 String list[] = root.list(new FilenameFilter() {
57
58 public boolean accept(File dir, String name) {
59 Matcher matcher = pattern.matcher(name);
60 return matcher.matches();
61 }
62
63 });
64 return Arrays.asList(list);
65 }
66
67 public List<Version> versions(String bsn) throws Exception {
68 File dir = new File(root, bsn);
69 final List<Version> versions = new ArrayList<Version>();
70 dir.list(new FilenameFilter() {
71
72 public boolean accept(File dir, String name) {
73 Matcher m = REPO_FILE.matcher(name);
74 if (m.matches()) {
75 versions.add(new Version(m.group(2)));
76 return true;
77 }
78 return false;
79 }
80
81 });
82 return versions;
83 }
84
85 public File get(String bsn, VersionRange range, int strategy) throws Exception {
86 File[] files = get(bsn, range);
87 if (files == null || files.length == 0)
88 return null;
89
90 if (files.length == 1)
91 return files[0];
92
93 if (strategy < 0) {
94 return files[0];
95 }
96 return files[files.length - 1];
97 }
98
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000099 public File put(String bsn, Version version) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000100 File dir = new File(root, bsn);
Stuart McCulloch2929e2d2012-08-07 10:57:21 +0000101 if (!dir.exists() && !dir.mkdirs()) {
102 throw new IOException("Could not create directory " + dir);
103 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000104 File file = new File(dir, bsn + "-" + version.getMajor() + "." + version.getMinor() + "." + version.getMicro()
105 + ".jar");
Stuart McCullochf3173222012-06-07 21:57:32 +0000106 return file;
107 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000108
Stuart McCullochf3173222012-06-07 21:57:32 +0000109}