blob: 88ead981bab0fdcf2506ee9c0e319fe89b951b1f [file] [log] [blame]
Stuart McCulloch2b3253e2012-06-17 20:38:35 +00001package aQute.bnd.build;
2
Stuart McCulloch1a890552012-06-29 19:23:09 +00003import java.io.*;
4import java.util.*;
5import java.util.regex.*;
Stuart McCulloch2b3253e2012-06-17 20:38:35 +00006
Stuart McCulloch42151ee2012-07-16 13:43:38 +00007import aQute.bnd.osgi.*;
Stuart McCulloch1a890552012-06-29 19:23:09 +00008import aQute.bnd.service.*;
Stuart McCullochcd1ddd72012-07-19 13:11:20 +00009import aQute.bnd.version.*;
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000010import aQute.lib.collections.*;
11import aQute.libg.glob.*;
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000012
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000013public class WorkspaceRepository implements RepositoryPlugin, Actionable {
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000014 private final Workspace workspace;
15
16 public WorkspaceRepository(Workspace workspace) {
17 this.workspace = workspace;
18 }
19
Stuart McCulloch669423b2012-06-26 16:34:24 +000020 private File[] get(String bsn, String range) throws Exception {
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000021 Collection<Project> projects = workspace.getAllProjects();
22 SortedMap<Version,File> foundVersion = new TreeMap<Version,File>();
23 for (Project project : projects) {
24 File[] build = project.build(false);
25 if (build != null) {
26 for (File file : build) {
27 Jar jar = new Jar(file);
28 if (bsn.equals(jar.getBsn())) {
29 Version version = new Version(jar.getVersion());
30 boolean exact = range.matches("[0-9]+\\.[0-9]+\\.[0-9]+\\..*");
31 if ("latest".equals(range) || matchVersion(range, version, exact)) {
32 foundVersion.put(version, file);
33 }
34 }
35 }
36 }
37 }
38
39 File[] result = new File[foundVersion.size()];
40 result = foundVersion.values().toArray(result);
41 if (!"latest".equals(range)) {
42 return result;
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000043 }
Stuart McCulloch669423b2012-06-26 16:34:24 +000044 if (result.length > 0) {
45 return new File[] {
46 result[0]
47 };
48 }
49 return new File[0];
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000050 }
51
52 public File get(String bsn, String range, Strategy strategy, Map<String,String> properties) throws Exception {
53 File[] files = get(bsn, range);
54
55 if (files.length == 0) {
56 return null;
57 }
58
59 if (strategy == Strategy.EXACT) {
60 return files[0];
61 } else if (strategy == Strategy.HIGHEST) {
62 return files[files.length - 1];
63 } else if (strategy == Strategy.LOWEST) {
64 return files[0];
65 }
66
67 return null;
68 }
69
70 private boolean matchVersion(String range, Version version, boolean exact) {
71 if (range == null || range.trim().length() == 0)
72 return true;
73 VersionRange vr = new VersionRange(range);
74
75 boolean result;
76 if (exact) {
77 if (vr.isRange())
78 result = false;
79 else
80 result = vr.getHigh().equals(version);
81 } else {
82 result = vr.includes(version);
83 }
84 return result;
85 }
86
87 public boolean canWrite() {
88 return false;
89 }
90
Stuart McCulloch2929e2d2012-08-07 10:57:21 +000091 public PutResult put(InputStream stream, PutOptions options) throws Exception {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000092 throw new UnsupportedOperationException("Read only repository");
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000093 }
94
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000095 public List<String> list(String pattern) throws Exception {
Stuart McCulloch2b3253e2012-06-17 20:38:35 +000096 List<String> names = new ArrayList<String>();
97 Collection<Project> projects = workspace.getAllProjects();
98 for (Project project : projects) {
99 File[] build = project.build(false);
100 if (build != null) {
101 for (File file : build) {
102 Jar jar = new Jar(file);
103 String bsn = jar.getBsn();
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000104 if (pattern != null) {
105 Glob glob = new Glob(pattern);
106 Matcher matcher = glob.matcher(bsn);
Stuart McCulloch2b3253e2012-06-17 20:38:35 +0000107 if (matcher.matches()) {
108 if (!names.contains(bsn)) {
109 names.add(bsn);
110 }
111 }
112 } else {
113 if (!names.contains(bsn)) {
114 names.add(bsn);
115 }
116 }
117 }
118 }
119 }
120
121 return names;
122 }
123
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000124 public SortedSet<Version> versions(String bsn) throws Exception {
Stuart McCulloch2b3253e2012-06-17 20:38:35 +0000125 List<Version> versions = new ArrayList<Version>();
126 Collection<Project> projects = workspace.getAllProjects();
127 for (Project project : projects) {
128 File[] build = project.build(false);
129 if (build != null) {
130 for (File file : build) {
131 Jar jar = new Jar(file);
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000132 try {
133 if (bsn.equals(jar.getBsn())) {
134 String v = jar.getVersion();
135 if ( v == null)
136 v = "0";
137 else if (!Verifier.isVersion(v))
138 continue; // skip
139
140 versions.add(new Version(v));
141 }
142 }
143 finally {
144 jar.close();
Stuart McCulloch2b3253e2012-06-17 20:38:35 +0000145 }
146 }
147 }
148 }
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000149 if ( versions.isEmpty())
150 return SortedList.empty();
151
152 return new SortedList<Version>(versions);
Stuart McCulloch2b3253e2012-06-17 20:38:35 +0000153 }
154
155 public String getName() {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000156 return "Workspace " + workspace.getBase().getName();
Stuart McCulloch2b3253e2012-06-17 20:38:35 +0000157 }
158
159 public String getLocation() {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000160 return workspace.getBase().getAbsolutePath();
161 }
162
163 public File get(String bsn, Version version, Map<String,String> properties, DownloadListener ... listeners) throws Exception {
164 File file = get(bsn, version.toString(), Strategy.EXACT, properties);
165 if ( file == null)
166 return null;
167 for (DownloadListener l : listeners) {
168 try {
169 l.success(file);
170 }
171 catch (Exception e) {
172 workspace.exception(e, "Workspace repo listener callback for %s" ,file);
173 }
174 }
175 return file;
176 }
177
178
179 public Map<String,Runnable> actions(Object... target) throws Exception {
180 // TODO Auto-generated method stub
181 return null;
182 }
183
184 public String tooltip(Object... target) throws Exception {
185 // TODO Auto-generated method stub
186 return null;
187 }
188
189 public String title(Object... target) throws Exception {
190 // TODO Auto-generated method stub
191 return null;
Stuart McCulloch2b3253e2012-06-17 20:38:35 +0000192 }
193
194}