blob: 9a1753ca9cbd9c892cde22ee292e6782fd94f4ea [file] [log] [blame]
Stuart McCulloch2b3253e2012-06-17 20:38:35 +00001package aQute.bnd.build;
2
3import java.io.File;
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.List;
7import java.util.Map;
8import java.util.SortedMap;
9import java.util.TreeMap;
10import java.util.regex.Matcher;
11import java.util.regex.Pattern;
12
13import aQute.bnd.service.RepositoryPlugin;
14import aQute.lib.osgi.Jar;
15import aQute.libg.version.Version;
16import aQute.libg.version.VersionRange;
17
18public class WorkspaceRepository implements RepositoryPlugin {
19 private final Workspace workspace;
20
21 public WorkspaceRepository(Workspace workspace) {
22 this.workspace = workspace;
23 }
24
Stuart McCulloch669423b2012-06-26 16:34:24 +000025 private File[] get(String bsn, String range) throws Exception {
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000026 Collection<Project> projects = workspace.getAllProjects();
27 SortedMap<Version,File> foundVersion = new TreeMap<Version,File>();
28 for (Project project : projects) {
29 File[] build = project.build(false);
30 if (build != null) {
31 for (File file : build) {
32 Jar jar = new Jar(file);
33 if (bsn.equals(jar.getBsn())) {
34 Version version = new Version(jar.getVersion());
35 boolean exact = range.matches("[0-9]+\\.[0-9]+\\.[0-9]+\\..*");
36 if ("latest".equals(range) || matchVersion(range, version, exact)) {
37 foundVersion.put(version, file);
38 }
39 }
40 }
41 }
42 }
43
44 File[] result = new File[foundVersion.size()];
45 result = foundVersion.values().toArray(result);
46 if (!"latest".equals(range)) {
47 return result;
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000048 }
Stuart McCulloch669423b2012-06-26 16:34:24 +000049 if (result.length > 0) {
50 return new File[] {
51 result[0]
52 };
53 }
54 return new File[0];
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000055 }
56
57 public File get(String bsn, String range, Strategy strategy, Map<String,String> properties) throws Exception {
58 File[] files = get(bsn, range);
59
60 if (files.length == 0) {
61 return null;
62 }
63
64 if (strategy == Strategy.EXACT) {
65 return files[0];
66 } else if (strategy == Strategy.HIGHEST) {
67 return files[files.length - 1];
68 } else if (strategy == Strategy.LOWEST) {
69 return files[0];
70 }
71
72 return null;
73 }
74
75 private boolean matchVersion(String range, Version version, boolean exact) {
76 if (range == null || range.trim().length() == 0)
77 return true;
78 VersionRange vr = new VersionRange(range);
79
80 boolean result;
81 if (exact) {
82 if (vr.isRange())
83 result = false;
84 else
85 result = vr.getHigh().equals(version);
86 } else {
87 result = vr.includes(version);
88 }
89 return result;
90 }
91
92 public boolean canWrite() {
93 return false;
94 }
95
96 public File put(Jar jar) throws Exception {
97 return null;
98 }
99
100 public List<String> list(String regex) throws Exception {
101 List<String> names = new ArrayList<String>();
102 Collection<Project> projects = workspace.getAllProjects();
103 for (Project project : projects) {
104 File[] build = project.build(false);
105 if (build != null) {
106 for (File file : build) {
107 Jar jar = new Jar(file);
108 String bsn = jar.getBsn();
109 if (regex != null) {
110 Pattern pattern = Pattern.compile(regex);
111 Matcher matcher = pattern.matcher(bsn);
112 if (matcher.matches()) {
113 if (!names.contains(bsn)) {
114 names.add(bsn);
115 }
116 }
117 } else {
118 if (!names.contains(bsn)) {
119 names.add(bsn);
120 }
121 }
122 }
123 }
124 }
125
126 return names;
127 }
128
129 public List<Version> versions(String bsn) throws Exception {
130 List<Version> versions = new ArrayList<Version>();
131 Collection<Project> projects = workspace.getAllProjects();
132 for (Project project : projects) {
133 File[] build = project.build(false);
134 if (build != null) {
135 for (File file : build) {
136 Jar jar = new Jar(file);
137 if (bsn.equals(jar.getBsn())) {
138 versions.add(new Version(jar.getVersion()));
139 }
140 }
141 }
142 }
143
144 return versions;
145 }
146
147 public String getName() {
148 return "Workspace";
149 }
150
151 public String getLocation() {
152 return "Workspace";
153 }
154
155}