Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 1 | package aQute.bnd.build; |
| 2 | |
Stuart McCulloch | 1a89055 | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.regex.*; |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 6 | |
Stuart McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 7 | import aQute.bnd.osgi.*; |
Stuart McCulloch | 1a89055 | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 8 | import aQute.bnd.service.*; |
Stuart McCulloch | cd1ddd7 | 2012-07-19 13:11:20 +0000 | [diff] [blame] | 9 | import aQute.bnd.version.*; |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 10 | import aQute.lib.collections.*; |
| 11 | import aQute.libg.glob.*; |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 12 | |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 13 | public class WorkspaceRepository implements RepositoryPlugin, Actionable { |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 14 | private final Workspace workspace; |
| 15 | |
| 16 | public WorkspaceRepository(Workspace workspace) { |
| 17 | this.workspace = workspace; |
| 18 | } |
| 19 | |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 20 | private File[] get(String bsn, String range) throws Exception { |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 21 | 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 McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 43 | } |
Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame] | 44 | if (result.length > 0) { |
| 45 | return new File[] { |
| 46 | result[0] |
| 47 | }; |
| 48 | } |
| 49 | return new File[0]; |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 50 | } |
| 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 McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 91 | public PutResult put(InputStream stream, PutOptions options) throws Exception { |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 92 | throw new UnsupportedOperationException("Read only repository"); |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 95 | public List<String> list(String pattern) throws Exception { |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 96 | 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 McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 104 | if (pattern != null) { |
| 105 | Glob glob = new Glob(pattern); |
| 106 | Matcher matcher = glob.matcher(bsn); |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 107 | 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 McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 124 | public SortedSet<Version> versions(String bsn) throws Exception { |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 125 | 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 McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 132 | 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 McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 149 | if ( versions.isEmpty()) |
| 150 | return SortedList.empty(); |
| 151 | |
| 152 | return new SortedList<Version>(versions); |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public String getName() { |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 156 | return "Workspace " + workspace.getBase().getName(); |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | public String getLocation() { |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 160 | 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 McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | } |