Latest bnd code

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1351156 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/Project.java b/bundleplugin/src/main/java/aQute/bnd/build/Project.java
index 192be5d..1ec2c33 100644
--- a/bundleplugin/src/main/java/aQute/bnd/build/Project.java
+++ b/bundleplugin/src/main/java/aQute/bnd/build/Project.java
@@ -199,7 +199,7 @@
 						sourcepath.add(getBase());
 
 					// Set default bin directory
-					output = getFile(getProperty("bin", "bin")).getAbsoluteFile();
+					output = getOutput0();
 					if (!output.exists()) {
 						output.mkdirs();
 						getWorkspace().changedFile(output);
@@ -213,11 +213,7 @@
 					}
 
 					// Where we store all our generated stuff.
-					target = getFile(getProperty("target", "generated"));
-					if (!target.exists()) {
-						target.mkdirs();
-						getWorkspace().changedFile(target);
-					}
+					target = getTarget0();
 
 					// Where the launched OSGi framework stores stuff
 					String runStorageStr = getProperty(Constants.RUNSTORAGE);
@@ -289,6 +285,25 @@
 		}
 	}
 
+	/**
+	 * @return
+	 */
+	private File getOutput0() {
+		return getFile(getProperty("bin", "bin")).getAbsoluteFile();
+	}
+
+	/**
+	 * 
+	 */
+	private File getTarget0() {
+		File target = getFile(getProperty("target", "generated"));
+		if (!target.exists()) {
+			target.mkdirs();
+			getWorkspace().changedFile(target);
+		}
+		return target;
+	}
+
 	public File getSrc() {
 		return new File(getBase(), getProperty("src", "src"));
 	}
@@ -1542,14 +1557,15 @@
 	}
 
 	public void clean() throws Exception {
-		File target = getTarget();
+		File target = getTarget0();
 		if (target.isDirectory() && target.getParentFile() != null) {
 			IO.delete(target);
 			target.mkdirs();
 		}
+		File output = getOutput0();
 		if (getOutput().isDirectory())
-			IO.delete(getOutput());
-		getOutput().mkdirs();
+			IO.delete(output);
+		output.mkdirs();
 	}
 
 	public File[] build() throws Exception {
@@ -1783,8 +1799,8 @@
 			return;
 		}
 		@SuppressWarnings("rawtypes")
-		Map x = (Map) getProperties();
-		scripters.get(0).eval((Map<String,Object>) x, new StringReader(script));
+		Map x = getProperties();
+		scripters.get(0).eval(x, new StringReader(script));
 	}
 
 	public String _repos(String args[]) throws Exception {
@@ -1809,11 +1825,11 @@
 
 		if (what == null || what.equals("lead"))
 			return syntax.getLead();
-		if (what == null || what.equals("example"))
+		if (what.equals("example"))
 			return syntax.getExample();
-		if (what == null || what.equals("pattern"))
+		if (what.equals("pattern"))
 			return syntax.getPattern();
-		if (what == null || what.equals("values"))
+		if (what.equals("values"))
 			return syntax.getValues();
 
 		return "Invalid type specified for help: lead, example, pattern, values";
@@ -2030,7 +2046,7 @@
 		}
 	}
 
-	File getPackageInfoFile(String packageName) throws IOException {
+	File getPackageInfoFile(String packageName) {
 		String path = packageName.replace('.', '/') + "/packageinfo";
 		return IO.getFile(getSrc(), path);
 
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/ProjectLauncher.java b/bundleplugin/src/main/java/aQute/bnd/build/ProjectLauncher.java
index 6a97311..78d12e3 100644
--- a/bundleplugin/src/main/java/aQute/bnd/build/ProjectLauncher.java
+++ b/bundleplugin/src/main/java/aQute/bnd/build/ProjectLauncher.java
@@ -210,7 +210,7 @@
 			java.setTimeout(timeout + 1000, TimeUnit.MILLISECONDS);
 
 		try {
-			int result = java.execute((InputStream) null, System.err, System.err);
+			int result = java.execute(System.in, System.err, System.err);
 			if (result == Integer.MIN_VALUE)
 				return TIMEDOUT;
 			reportResult(result);
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/Workspace.java b/bundleplugin/src/main/java/aQute/bnd/build/Workspace.java
index daee3d3..573a61e 100644
--- a/bundleplugin/src/main/java/aQute/bnd/build/Workspace.java
+++ b/bundleplugin/src/main/java/aQute/bnd/build/Workspace.java
@@ -242,7 +242,7 @@
 		signal(this);
 	}
 
-	private void copy(InputStream in, OutputStream out) throws Exception {
+	void copy(InputStream in, OutputStream out) throws Exception {
 		byte data[] = new byte[10000];
 		int size = in.read(data);
 		while (size > 0) {
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/WorkspaceRepository.java b/bundleplugin/src/main/java/aQute/bnd/build/WorkspaceRepository.java
new file mode 100644
index 0000000..9e71975
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/WorkspaceRepository.java
@@ -0,0 +1,157 @@
+package aQute.bnd.build;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import aQute.bnd.service.RepositoryPlugin;
+import aQute.lib.osgi.Jar;
+import aQute.libg.version.Version;
+import aQute.libg.version.VersionRange;
+
+public class WorkspaceRepository implements RepositoryPlugin {
+	private final Workspace	workspace;
+
+	public WorkspaceRepository(Workspace workspace) {
+		this.workspace = workspace;
+	}
+
+	public File[] get(String bsn, String range) throws Exception {
+		Collection<Project> projects = workspace.getAllProjects();
+		SortedMap<Version,File> foundVersion = new TreeMap<Version,File>();
+		for (Project project : projects) {
+			File[] build = project.build(false);
+			if (build != null) {
+				for (File file : build) {
+					Jar jar = new Jar(file);
+					if (bsn.equals(jar.getBsn())) {
+						Version version = new Version(jar.getVersion());
+						boolean exact = range.matches("[0-9]+\\.[0-9]+\\.[0-9]+\\..*");
+						if ("latest".equals(range) || matchVersion(range, version, exact)) {
+							foundVersion.put(version, file);
+						}
+					}
+				}
+			}
+		}
+
+		File[] result = new File[foundVersion.size()];
+		result = foundVersion.values().toArray(result);
+		if (!"latest".equals(range)) {
+			return result;
+		} else {
+			if (result.length > 0) {
+				return new File[] {
+					result[0]
+				};
+			} else {
+				return new File[0];
+			}
+		}
+	}
+
+	public File get(String bsn, String range, Strategy strategy, Map<String,String> properties) throws Exception {
+		File[] files = get(bsn, range);
+
+		if (files.length == 0) {
+			return null;
+		}
+
+		if (strategy == Strategy.EXACT) {
+			return files[0];
+		} else if (strategy == Strategy.HIGHEST) {
+			return files[files.length - 1];
+		} else if (strategy == Strategy.LOWEST) {
+			return files[0];
+		}
+
+		return null;
+	}
+
+	private boolean matchVersion(String range, Version version, boolean exact) {
+		if (range == null || range.trim().length() == 0)
+			return true;
+		VersionRange vr = new VersionRange(range);
+
+		boolean result;
+		if (exact) {
+			if (vr.isRange())
+				result = false;
+			else
+				result = vr.getHigh().equals(version);
+		} else {
+			result = vr.includes(version);
+		}
+		return result;
+	}
+
+	public boolean canWrite() {
+		return false;
+	}
+
+	public File put(Jar jar) throws Exception {
+		return null;
+	}
+
+	public List<String> list(String regex) throws Exception {
+		List<String> names = new ArrayList<String>();
+		Collection<Project> projects = workspace.getAllProjects();
+		for (Project project : projects) {
+			File[] build = project.build(false);
+			if (build != null) {
+				for (File file : build) {
+					Jar jar = new Jar(file);
+					String bsn = jar.getBsn();
+					if (regex != null) {
+						Pattern pattern = Pattern.compile(regex);
+						Matcher matcher = pattern.matcher(bsn);
+						if (matcher.matches()) {
+							if (!names.contains(bsn)) {
+								names.add(bsn);
+							}
+						}
+					} else {
+						if (!names.contains(bsn)) {
+							names.add(bsn);
+						}
+					}
+				}
+			}
+		}
+
+		return names;
+	}
+
+	public List<Version> versions(String bsn) throws Exception {
+		List<Version> versions = new ArrayList<Version>();
+		Collection<Project> projects = workspace.getAllProjects();
+		for (Project project : projects) {
+			File[] build = project.build(false);
+			if (build != null) {
+				for (File file : build) {
+					Jar jar = new Jar(file);
+					if (bsn.equals(jar.getBsn())) {
+						versions.add(new Version(jar.getVersion()));
+					}
+				}
+			}
+		}
+
+		return versions;
+	}
+
+	public String getName() {
+		return "Workspace";
+	}
+
+	public String getLocation() {
+		return "Workspace";
+	}
+
+}