Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.build; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.lang.ref.*; |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 5 | import java.net.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 6 | import java.util.*; |
| 7 | import java.util.concurrent.*; |
| 8 | import java.util.concurrent.locks.*; |
| 9 | import java.util.jar.*; |
| 10 | |
| 11 | import javax.naming.*; |
| 12 | |
| 13 | import aQute.bnd.maven.support.*; |
Stuart McCulloch | 42151ee | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 14 | import aQute.bnd.osgi.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 15 | import aQute.bnd.service.*; |
| 16 | import aQute.bnd.service.action.*; |
| 17 | import aQute.lib.deployer.*; |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 18 | import aQute.lib.hex.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 19 | import aQute.lib.io.*; |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 20 | import aQute.lib.settings.*; |
Stuart McCulloch | 1a89055 | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 21 | import aQute.service.reporter.*; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 22 | |
| 23 | public class Workspace extends Processor { |
| 24 | public static final String BUILDFILE = "build.bnd"; |
| 25 | public static final String CNFDIR = "cnf"; |
| 26 | public static final String BNDDIR = "bnd"; |
| 27 | public static final String CACHEDIR = "cache"; |
| 28 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 29 | static Map<File,WeakReference<Workspace>> cache = newHashMap(); |
| 30 | final Map<String,Project> models = newHashMap(); |
| 31 | final Map<String,Action> commands = newMap(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 32 | final File buildDir; |
| 33 | final Maven maven = new Maven(Processor.getExecutor()); |
Stuart McCulloch | 1b98aa0 | 2012-06-18 11:15:15 +0000 | [diff] [blame] | 34 | private boolean offline = true; |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 35 | Settings settings = new Settings(); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * This static method finds the workspace and creates a project (or returns |
| 39 | * an existing project) |
| 40 | * |
| 41 | * @param projectDir |
| 42 | * @return |
| 43 | */ |
| 44 | public static Project getProject(File projectDir) throws Exception { |
| 45 | projectDir = projectDir.getAbsoluteFile(); |
| 46 | assert projectDir.isDirectory(); |
| 47 | |
| 48 | Workspace ws = getWorkspace(projectDir.getParentFile()); |
| 49 | return ws.getProject(projectDir.getName()); |
| 50 | } |
| 51 | |
| 52 | public static Workspace getWorkspace(File parent) throws Exception { |
| 53 | File workspaceDir = parent.getAbsoluteFile(); |
| 54 | |
| 55 | // the cnf directory can actually be a |
| 56 | // file that redirects |
| 57 | while (workspaceDir.isDirectory()) { |
| 58 | File test = new File(workspaceDir, CNFDIR); |
| 59 | |
| 60 | if (!test.exists()) |
| 61 | test = new File(workspaceDir, BNDDIR); |
| 62 | |
| 63 | if (test.isDirectory()) |
| 64 | break; |
| 65 | |
| 66 | if (test.isFile()) { |
| 67 | String redirect = IO.collect(test).trim(); |
| 68 | test = getFile(test.getParentFile(), redirect).getAbsoluteFile(); |
| 69 | workspaceDir = test; |
| 70 | } |
| 71 | if (!test.exists()) |
| 72 | throw new IllegalArgumentException("No Workspace found from: " + parent); |
| 73 | } |
| 74 | |
| 75 | synchronized (cache) { |
| 76 | WeakReference<Workspace> wsr = cache.get(workspaceDir); |
| 77 | Workspace ws; |
| 78 | if (wsr == null || (ws = wsr.get()) == null) { |
| 79 | ws = new Workspace(workspaceDir); |
| 80 | cache.put(workspaceDir, new WeakReference<Workspace>(ws)); |
| 81 | } |
| 82 | return ws; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public Workspace(File dir) throws Exception { |
| 87 | dir = dir.getAbsoluteFile(); |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 88 | if (!dir.exists() && !dir.mkdirs()) { |
| 89 | throw new IOException("Could not create directory " + dir); |
| 90 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 91 | assert dir.isDirectory(); |
| 92 | |
| 93 | File buildDir = new File(dir, BNDDIR).getAbsoluteFile(); |
| 94 | if (!buildDir.isDirectory()) |
| 95 | buildDir = new File(dir, CNFDIR).getAbsoluteFile(); |
| 96 | |
| 97 | this.buildDir = buildDir; |
| 98 | |
| 99 | File buildFile = new File(buildDir, BUILDFILE).getAbsoluteFile(); |
| 100 | if (!buildFile.isFile()) |
| 101 | warning("No Build File in " + dir); |
| 102 | |
| 103 | setProperties(buildFile, dir); |
| 104 | propertiesChanged(); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | public Project getProject(String bsn) throws Exception { |
| 109 | synchronized (models) { |
| 110 | Project project = models.get(bsn); |
| 111 | if (project != null) |
| 112 | return project; |
| 113 | |
| 114 | File projectDir = getFile(bsn); |
| 115 | project = new Project(this, projectDir); |
| 116 | if (!project.isValid()) |
| 117 | return null; |
| 118 | |
| 119 | models.put(bsn, project); |
| 120 | return project; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public boolean isPresent(String name) { |
| 125 | return models.containsKey(name); |
| 126 | } |
| 127 | |
| 128 | public Collection<Project> getCurrentProjects() { |
| 129 | return models.values(); |
| 130 | } |
| 131 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 132 | @Override |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 133 | public boolean refresh() { |
| 134 | if (super.refresh()) { |
| 135 | for (Project project : getCurrentProjects()) { |
| 136 | project.propertiesChanged(); |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 143 | @Override |
| 144 | public void propertiesChanged() { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 145 | super.propertiesChanged(); |
| 146 | File extDir = new File(this.buildDir, "ext"); |
| 147 | File[] extensions = extDir.listFiles(); |
| 148 | if (extensions != null) { |
| 149 | for (File extension : extensions) { |
| 150 | String extensionName = extension.getName(); |
| 151 | if (extensionName.endsWith(".bnd")) { |
| 152 | extensionName = extensionName.substring(0, extensionName.length() - ".bnd".length()); |
| 153 | try { |
| 154 | doIncludeFile(extension, false, getProperties(), "ext." + extensionName); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 155 | } |
| 156 | catch (Exception e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 157 | error("PropertiesChanged: " + e.getMessage()); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 164 | public String _workspace(@SuppressWarnings("unused") |
| 165 | String args[]) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 166 | return getBase().getAbsolutePath(); |
| 167 | } |
| 168 | |
| 169 | public void addCommand(String menu, Action action) { |
| 170 | commands.put(menu, action); |
| 171 | } |
| 172 | |
| 173 | public void removeCommand(String menu) { |
| 174 | commands.remove(menu); |
| 175 | } |
| 176 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 177 | public void fillActions(Map<String,Action> all) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 178 | all.putAll(commands); |
| 179 | } |
| 180 | |
| 181 | public Collection<Project> getAllProjects() throws Exception { |
| 182 | List<Project> projects = new ArrayList<Project>(); |
| 183 | for (File file : getBase().listFiles()) { |
| 184 | if (new File(file, Project.BNDFILE).isFile()) |
| 185 | projects.add(getProject(file)); |
| 186 | } |
| 187 | return projects; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Inform any listeners that we changed a file (created/deleted/changed). |
| 192 | * |
| 193 | * @param f |
| 194 | * The changed file |
| 195 | */ |
| 196 | public void changedFile(File f) { |
| 197 | List<BndListener> listeners = getPlugins(BndListener.class); |
| 198 | for (BndListener l : listeners) |
| 199 | try { |
Stuart McCulloch | 1b98aa0 | 2012-06-18 11:15:15 +0000 | [diff] [blame] | 200 | offline = false; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 201 | l.changed(f); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 202 | } |
| 203 | catch (Exception e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 204 | e.printStackTrace(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | public void bracket(boolean begin) { |
| 209 | List<BndListener> listeners = getPlugins(BndListener.class); |
| 210 | for (BndListener l : listeners) |
| 211 | try { |
| 212 | if (begin) |
| 213 | l.begin(); |
| 214 | else |
| 215 | l.end(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 216 | } |
| 217 | catch (Exception e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 218 | // who cares? |
| 219 | } |
| 220 | } |
| 221 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 222 | /** |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 223 | * Signal a BndListener plugin. We ran an infinite bug loop :-( |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 224 | */ |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 225 | final ThreadLocal<Reporter> signalBusy = new ThreadLocal<Reporter>(); |
| 226 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 227 | public void signal(Reporter reporter) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 228 | if (signalBusy.get() != null) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 229 | return; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 230 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 231 | signalBusy.set(reporter); |
| 232 | try { |
| 233 | List<BndListener> listeners = getPlugins(BndListener.class); |
| 234 | for (BndListener l : listeners) |
| 235 | try { |
| 236 | l.signal(this); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 237 | } |
| 238 | catch (Exception e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 239 | // who cares? |
| 240 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 241 | } |
| 242 | catch (Exception e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 243 | // Ignore |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 244 | } |
| 245 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 246 | signalBusy.set(null); |
| 247 | } |
| 248 | } |
| 249 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 250 | @Override |
| 251 | public void signal() { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 252 | signal(this); |
| 253 | } |
| 254 | |
Stuart McCulloch | 2b3253e | 2012-06-17 20:38:35 +0000 | [diff] [blame] | 255 | void copy(InputStream in, OutputStream out) throws Exception { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 256 | byte data[] = new byte[10000]; |
| 257 | int size = in.read(data); |
| 258 | while (size > 0) { |
| 259 | out.write(data, 0, size); |
| 260 | size = in.read(data); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | class CachedFileRepo extends FileRepo { |
| 265 | final Lock lock = new ReentrantLock(); |
| 266 | boolean inited; |
| 267 | |
| 268 | CachedFileRepo() { |
| 269 | super("cache", getFile(buildDir, CACHEDIR), false); |
| 270 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 271 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 272 | @Override |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 273 | public String toString() { |
| 274 | return "bnd-cache"; |
| 275 | } |
| 276 | |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 277 | @Override |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 278 | protected boolean init() throws Exception { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 279 | if (lock.tryLock(50, TimeUnit.SECONDS) == false) |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 280 | throw new TimeLimitExceededException("Cached File Repo is locked and can't acquire it"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 281 | try { |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 282 | if (super.init()) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 283 | inited = true; |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 284 | if (!root.exists() && !root.mkdirs()) { |
| 285 | throw new IOException("Could not create cache directory " + root); |
| 286 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 287 | if (!root.isDirectory()) |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 288 | throw new IllegalArgumentException("Cache directory " + root + " not a directory"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 289 | |
| 290 | InputStream in = getClass().getResourceAsStream(EMBEDDED_REPO); |
| 291 | if (in != null) |
| 292 | unzip(in, root); |
| 293 | else { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 294 | error("Couldn't find embedded-repo.jar in bundle "); |
| 295 | } |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 296 | return true; |
| 297 | } else |
| 298 | return false; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 299 | } |
| 300 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 301 | lock.unlock(); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void unzip(InputStream in, File dir) throws Exception { |
| 306 | try { |
| 307 | JarInputStream jin = new JarInputStream(in); |
| 308 | JarEntry jentry = jin.getNextJarEntry(); |
| 309 | while (jentry != null) { |
| 310 | if (!jentry.isDirectory()) { |
| 311 | File dest = Processor.getFile(dir, jentry.getName()); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 312 | if (!dest.isFile() || dest.lastModified() < jentry.getTime() || jentry.getTime() == 0) { |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 313 | File dp = dest.getParentFile(); |
| 314 | if (!dp.exists() && !dp.mkdirs()) { |
| 315 | throw new IOException("Could not create directory " + dp); |
| 316 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 317 | FileOutputStream out = new FileOutputStream(dest); |
| 318 | try { |
| 319 | copy(jin, out); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 320 | } |
| 321 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 322 | out.close(); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | jentry = jin.getNextJarEntry(); |
| 327 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 328 | } |
| 329 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 330 | in.close(); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | public List<RepositoryPlugin> getRepositories() { |
| 336 | return getPlugins(RepositoryPlugin.class); |
| 337 | } |
| 338 | |
| 339 | public Collection<Project> getBuildOrder() throws Exception { |
| 340 | List<Project> result = new ArrayList<Project>(); |
| 341 | for (Project project : getAllProjects()) { |
| 342 | Collection<Project> dependsOn = project.getDependson(); |
| 343 | getBuildOrder(dependsOn, result); |
| 344 | if (!result.contains(project)) { |
| 345 | result.add(project); |
| 346 | } |
| 347 | } |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | private void getBuildOrder(Collection<Project> dependsOn, List<Project> result) throws Exception { |
| 352 | for (Project project : dependsOn) { |
| 353 | Collection<Project> subProjects = project.getDependson(); |
| 354 | for (Project subProject : subProjects) { |
| 355 | if (!result.contains(subProject)) { |
| 356 | result.add(subProject); |
| 357 | } |
| 358 | } |
| 359 | if (!result.contains(project)) { |
| 360 | result.add(project); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | public static Workspace getWorkspace(String path) throws Exception { |
| 366 | File file = IO.getFile(new File(""), path); |
| 367 | return getWorkspace(file); |
| 368 | } |
| 369 | |
| 370 | public Maven getMaven() { |
| 371 | return maven; |
| 372 | } |
| 373 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 374 | @Override |
| 375 | protected void setTypeSpecificPlugins(Set<Object> list) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 376 | super.setTypeSpecificPlugins(list); |
| 377 | list.add(maven); |
| 378 | list.add(new CachedFileRepo()); |
| 379 | } |
| 380 | |
Stuart McCulloch | 1b98aa0 | 2012-06-18 11:15:15 +0000 | [diff] [blame] | 381 | /** |
| 382 | * Return if we're in offline mode. Offline mode is defined as an |
| 383 | * environment where nobody tells us the resources are out of date (refresh |
| 384 | * or changed). This is currently defined as having bndlisteners. |
| 385 | * |
| 386 | * @return |
| 387 | */ |
| 388 | public boolean isOffline() { |
| 389 | return offline; |
| 390 | } |
| 391 | |
| 392 | public Workspace setOffline(boolean on) { |
| 393 | this.offline = on; |
| 394 | return this; |
| 395 | } |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 396 | |
| 397 | /** |
| 398 | * Provide access to the global settings of this machine. |
| 399 | * |
| 400 | * @throws Exception |
| 401 | * @throws UnknownHostException |
| 402 | */ |
| 403 | |
| 404 | public String _global(String[] args) throws Exception { |
| 405 | Macro.verifyCommand(args, "${global;<name>[;<default>]}, get a global setting from ~/.bnd/settings.json", null, |
| 406 | 2, 3); |
| 407 | |
| 408 | String key = args[1]; |
| 409 | if (key.equals("key.public")) |
| 410 | return Hex.toHexString(settings.getPublicKey()); |
| 411 | if (key.equals("key.private")) |
| 412 | return Hex.toHexString(settings.getPrivateKey()); |
| 413 | |
| 414 | String s = settings.get(key); |
| 415 | if (s != null) |
| 416 | return s; |
| 417 | |
| 418 | if (args.length == 3) |
| 419 | return args[2]; |
| 420 | |
| 421 | return null; |
| 422 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 423 | } |