Use local copy of latest bndlib code for pre-release testing purposes
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1347815 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/CircularDependencyException.java b/bundleplugin/src/main/java/aQute/bnd/build/CircularDependencyException.java
new file mode 100644
index 0000000..f08255b
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/CircularDependencyException.java
@@ -0,0 +1,10 @@
+package aQute.bnd.build;
+
+public class CircularDependencyException extends Exception {
+ public CircularDependencyException(String string) {
+ super(string);
+ }
+
+ private static final long serialVersionUID = 1L;
+
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/Container.java b/bundleplugin/src/main/java/aQute/bnd/build/Container.java
new file mode 100644
index 0000000..2fb38d9
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/Container.java
@@ -0,0 +1,210 @@
+package aQute.bnd.build;
+
+import java.io.*;
+import java.util.*;
+import java.util.jar.*;
+
+import aQute.bnd.service.RepositoryPlugin.Strategy;
+import aQute.lib.osgi.*;
+
+public class Container {
+ public enum TYPE {
+ REPO, PROJECT, PROJECT_BUNDLE, EXTERNAL, LIBRARY, ERROR
+ }
+
+ final File file;
+ final TYPE type;
+ final String bsn;
+ final String version;
+ final String error;
+ final Project project;
+ volatile Map<String, String> attributes;
+ private long manifestTime;
+ private Manifest manifest;
+
+ Container(Project project, String bsn, String version, TYPE type, File source, String error,
+ Map<String, String> attributes) {
+ this.bsn = bsn;
+ this.version = version;
+ this.type = type;
+ this.file = source != null ? source : new File("/" + bsn + ":" + version + ":" + type);
+ this.project = project;
+ this.error = error;
+ if (attributes == null || attributes.isEmpty())
+ this.attributes = Collections.emptyMap();
+ else
+ this.attributes = attributes;
+ }
+
+ public Container(Project project, File file) {
+ this(project, file.getName(), "project", TYPE.PROJECT, file, null, null);
+ }
+
+ public Container(File file) {
+ this(null, file.getName(), "project", TYPE.EXTERNAL, file, null, null);
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ /**
+ * Iterate over the containers and get the files they represent
+ *
+ * @param files
+ * @return
+ * @throws Exception
+ */
+ public boolean contributeFiles(List<File> files, Processor reporter) throws Exception {
+ switch (type) {
+ case EXTERNAL:
+ case REPO:
+ files.add(file);
+ return true;
+
+ case PROJECT:
+ File[] fs = project.build();
+ reporter.getInfo(project);
+ if (fs == null)
+ return false;
+
+ for (File f : fs)
+ files.add(f);
+ return true;
+
+ case LIBRARY:
+ List<Container> containers = getMembers();
+ for (Container container : containers) {
+ if (!container.contributeFiles(files, reporter))
+ return false;
+ }
+ return true;
+
+ case ERROR:
+ reporter.error(error);
+ return false;
+ }
+ return false;
+ }
+
+ public String getBundleSymbolicName() {
+ return bsn;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public TYPE getType() {
+ return type;
+ }
+
+ public String getError() {
+ return error;
+ }
+
+ public boolean equals(Object other) {
+ if (other instanceof Container)
+ return file.equals(((Container) other).file);
+ else
+ return false;
+ }
+
+ public int hashCode() {
+ return file.hashCode();
+ }
+
+ public Project getProject() {
+ return project;
+ }
+
+ /**
+ * Must show the file name or the error formatted as a file name
+ *
+ * @return
+ */
+ public String toString() {
+ if (getError() != null)
+ return "/error/" + getError();
+ else
+ return getFile().getAbsolutePath();
+ }
+
+ public Map<String, String> getAttributes() {
+ return attributes;
+ }
+
+ public void putAttribute(String name, String value) {
+ if (attributes == Collections.<String,String>emptyMap())
+ attributes = new HashMap<String, String>(1);
+ attributes.put(name, value);
+ }
+
+ /**
+ * Return the this if this is anything else but a library. If it is a
+ * library, return the members. This could work recursively, e.g., libraries
+ * can point to libraries.
+ *
+ * @return
+ * @throws Exception
+ */
+ public List<Container> getMembers() throws Exception {
+ List<Container> result = project.newList();
+
+ // Are ww a library? If no, we are the result
+ if (getType() == TYPE.LIBRARY) {
+ // We are a library, parse the file. This is
+ // basically a specification clause per line.
+ // I.e. you can do bsn; version, bsn2; version. But also
+ // spread it out over lines.
+ InputStream in = null;
+ BufferedReader rd = null;
+ String line;
+ try {
+ in = new FileInputStream(file);
+ rd = new BufferedReader(new InputStreamReader(in,
+ Constants.DEFAULT_CHARSET));
+ while ((line = rd.readLine()) != null) {
+ line = line.trim();
+ if (!line.startsWith("#") && line.length() > 0) {
+ List<Container> list = project.getBundles(Strategy.HIGHEST, line, null);
+ result.addAll(list);
+ }
+ }
+ } finally {
+ if (rd != null) {
+ rd.close();
+ }
+ if (in != null) {
+ in.close();
+ }
+ }
+ } else
+ result.add(this);
+
+ return result;
+ }
+
+ /**
+ * Answer the manifest for this container (if possible). Manifest is cached
+ * until the file is renewed.
+ */
+
+ public Manifest getManifest() throws Exception {
+ if (getError() != null || getFile() == null)
+ return null;
+
+ if (manifestTime < getFile().lastModified()) {
+ InputStream in = new FileInputStream(getFile());
+ try {
+ JarInputStream jin = new JarInputStream(in);
+ manifest = jin.getManifest();
+ jin.close();
+ manifestTime = getFile().lastModified();
+ } finally {
+ in.close();
+ }
+ }
+ return manifest;
+ }
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/Project.java b/bundleplugin/src/main/java/aQute/bnd/build/Project.java
new file mode 100644
index 0000000..034a364
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/Project.java
@@ -0,0 +1,2092 @@
+package aQute.bnd.build;
+
+import java.io.*;
+import java.lang.reflect.*;
+import java.net.*;
+import java.util.*;
+import java.util.Map.Entry;
+import java.util.concurrent.*;
+import java.util.concurrent.locks.*;
+import java.util.jar.*;
+
+import aQute.bnd.help.*;
+import aQute.bnd.maven.support.*;
+import aQute.bnd.service.*;
+import aQute.bnd.service.RepositoryPlugin.Strategy;
+import aQute.bnd.service.action.*;
+import aQute.lib.io.*;
+import aQute.lib.osgi.*;
+import aQute.lib.osgi.eclipse.*;
+import aQute.libg.generics.*;
+import aQute.libg.header.*;
+import aQute.libg.sed.*;
+import aQute.libg.version.*;
+
+/**
+ * This class is NOT threadsafe
+ *
+ * @author aqute
+ *
+ */
+
+public class Project extends Processor {
+
+ final static String DEFAULT_ACTIONS = "build; label='Build', test; label='Test', run; label='Run', clean; label='Clean', release; label='Release', refreshAll; label=Refresh, deploy;label=Deploy";
+ public final static String BNDFILE = "bnd.bnd";
+ public final static String BNDCNF = "cnf";
+ final Workspace workspace;
+ boolean preparedPaths;
+ final Collection<Project> dependson = new LinkedHashSet<Project>();
+ final Collection<Container> classpath = new LinkedHashSet<Container>();
+ final Collection<Container> buildpath = new LinkedHashSet<Container>();
+ final Collection<Container> testpath = new LinkedHashSet<Container>();
+ final Collection<Container> runpath = new LinkedHashSet<Container>();
+ final Collection<Container> runbundles = new LinkedHashSet<Container>();
+ File runstorage;
+ final Collection<File> sourcepath = new LinkedHashSet<File>();
+ final Collection<File> allsourcepath = new LinkedHashSet<File>();
+ final Collection<Container> bootclasspath = new LinkedHashSet<Container>();
+ final Lock lock = new ReentrantLock(true);
+ volatile String lockingReason;
+ volatile Thread lockingThread;
+ File output;
+ File target;
+ boolean inPrepare;
+ int revision;
+ File files[];
+ static List<Project> trail = new ArrayList<Project>();
+ boolean delayRunDependencies = false;
+
+ public Project(Workspace workspace, File projectDir, File buildFile) throws Exception {
+ super(workspace);
+ this.workspace = workspace;
+ setFileMustExist(false);
+ setProperties(buildFile);
+ assert workspace != null;
+ // For backward compatibility reasons, we also read
+ readBuildProperties();
+ }
+
+ public Project(Workspace workspace, File buildDir) throws Exception {
+ this(workspace, buildDir, new File(buildDir, BNDFILE));
+ }
+
+ private void readBuildProperties() throws Exception {
+ try {
+ File f = getFile("build.properties");
+ if (f.isFile()) {
+ Properties p = loadProperties(f);
+ for (Enumeration<?> e = p.propertyNames(); e.hasMoreElements();) {
+ String key = (String) e.nextElement();
+ String newkey = key;
+ if (key.indexOf('$') >= 0) {
+ newkey = getReplacer().process(key);
+ }
+ setProperty(newkey, p.getProperty(key));
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static Project getUnparented(File propertiesFile) throws Exception {
+ propertiesFile = propertiesFile.getAbsoluteFile();
+ Workspace workspace = new Workspace(propertiesFile.getParentFile());
+ Project project = new Project(workspace, propertiesFile.getParentFile());
+ project.setProperties(propertiesFile);
+ project.setFileMustExist(true);
+ return project;
+ }
+
+ public synchronized boolean isValid() {
+ return getBase().isDirectory() && getPropertiesFile().isFile();
+ }
+
+ /**
+ * Return a new builder that is nicely setup for this project. Please close
+ * this builder after use.
+ *
+ * @param parent
+ * The project builder to use as parent, use this project if null
+ * @return
+ * @throws Exception
+ */
+ public synchronized ProjectBuilder getBuilder(ProjectBuilder parent) throws Exception {
+
+ ProjectBuilder builder;
+
+ if (parent == null)
+ builder = new ProjectBuilder(this);
+ else
+ builder = new ProjectBuilder(parent);
+
+ builder.setBase(getBase());
+
+ return builder;
+ }
+
+ public synchronized int getChanged() {
+ return revision;
+ }
+
+ /*
+ * Indicate a change in the external world that affects our build. This will
+ * clear any cached results.
+ */
+ public synchronized void setChanged() {
+ // if (refresh()) {
+ preparedPaths = false;
+ files = null;
+ revision++;
+ // }
+ }
+
+ public Workspace getWorkspace() {
+ return workspace;
+ }
+
+ public String toString() {
+ return getBase().getName();
+ }
+
+ /**
+ * Set up all the paths
+ */
+
+ public synchronized void prepare() throws Exception {
+ if (!isValid()) {
+ warning("Invalid project attempts to prepare: %s", this);
+ return;
+ }
+
+ if (inPrepare)
+ throw new CircularDependencyException(trail.toString() + "," + this);
+
+ trail.add(this);
+ try {
+ if (!preparedPaths) {
+ inPrepare = true;
+ try {
+ dependson.clear();
+ buildpath.clear();
+ sourcepath.clear();
+ allsourcepath.clear();
+ bootclasspath.clear();
+ testpath.clear();
+ runpath.clear();
+ runbundles.clear();
+
+ // We use a builder to construct all the properties for
+ // use.
+ setProperty("basedir", getBase().getAbsolutePath());
+
+ // If a bnd.bnd file exists, we read it.
+ // Otherwise, we just do the build properties.
+ if (!getPropertiesFile().isFile() && new File(getBase(), ".classpath").isFile()) {
+ // Get our Eclipse info, we might depend on other
+ // projects
+ // though ideally this should become empty and void
+ doEclipseClasspath();
+ }
+
+ // Calculate our source directory
+
+ File src = getSrc();
+ if (src.isDirectory()) {
+ sourcepath.add(src);
+ allsourcepath.add(src);
+ } else
+ sourcepath.add(getBase());
+
+ // Set default bin directory
+ output = getFile(getProperty("bin", "bin")).getAbsoluteFile();
+ if (!output.exists()) {
+ output.mkdirs();
+ getWorkspace().changedFile(output);
+ }
+ if (!output.isDirectory())
+ error("Can not find output directory: " + output);
+ else {
+ Container c = new Container(this, output);
+ if (!buildpath.contains(c))
+ buildpath.add(c);
+ }
+
+ // Where we store all our generated stuff.
+ target = getFile(getProperty("target", "generated"));
+ if (!target.exists()) {
+ target.mkdirs();
+ getWorkspace().changedFile(target);
+ }
+
+ // Where the launched OSGi framework stores stuff
+ String runStorageStr = getProperty(Constants.RUNSTORAGE);
+ runstorage = runStorageStr != null ? getFile(runStorageStr) : null;
+
+ // We might have some other projects we want build
+ // before we do anything, but these projects are not in
+ // our path. The -dependson allows you to build them before.
+
+ List<Project> dependencies = new ArrayList<Project>();
+ // dependencies.add( getWorkspace().getProject("cnf"));
+
+ String dp = getProperty(Constants.DEPENDSON);
+ Set<String> requiredProjectNames = new Parameters(dp).keySet();
+ List<DependencyContributor> dcs = getPlugins(DependencyContributor.class);
+ for (DependencyContributor dc : dcs)
+ dc.addDependencies(this, requiredProjectNames);
+
+ for (String p : requiredProjectNames) {
+ Project required = getWorkspace().getProject(p);
+ if (required == null)
+ error("No such project " + p + " on " + Constants.DEPENDSON);
+ else {
+ dependencies.add(required);
+ }
+
+ }
+
+ // We have two paths that consists of repo files, projects,
+ // or some other stuff. The doPath routine adds them to the
+ // path and extracts the projects so we can build them
+ // before.
+
+ doPath(buildpath, dependencies, parseBuildpath(), bootclasspath);
+ doPath(testpath, dependencies, parseTestpath(), bootclasspath);
+ if (!delayRunDependencies) {
+ doPath(runpath, dependencies, parseRunpath(), null);
+ doPath(runbundles, dependencies, parseRunbundles(), null);
+ }
+
+ // We now know all dependent projects. But we also depend
+ // on whatever those projects depend on. This creates an
+ // ordered list without any duplicates. This of course
+ // assumes
+ // that there is no circularity. However, this is checked
+ // by the inPrepare flag, will throw an exception if we
+ // are circular.
+
+ Set<Project> done = new HashSet<Project>();
+ done.add(this);
+ allsourcepath.addAll(sourcepath);
+
+ for (Project project : dependencies)
+ project.traverse(dependson, done);
+
+ for (Project project : dependson) {
+ allsourcepath.addAll(project.getSourcePath());
+ }
+ if (isOk())
+ preparedPaths = true;
+ } finally {
+ inPrepare = false;
+ }
+ }
+ } finally {
+ trail.remove(this);
+ }
+ }
+
+ public File getSrc() {
+ return new File(getBase(), getProperty("src", "src"));
+ }
+
+ private void traverse(Collection<Project> dependencies, Set<Project> visited) throws Exception {
+ if (visited.contains(this))
+ return;
+
+ visited.add(this);
+
+ for (Project project : getDependson())
+ project.traverse(dependencies, visited);
+
+ dependencies.add(this);
+ }
+
+ /**
+ * Iterate over the entries and place the projects on the projects list and
+ * all the files of the entries on the resultpath.
+ *
+ * @param resultpath
+ * The list that gets all the files
+ * @param projects
+ * The list that gets any projects that are entries
+ * @param entries
+ * The input list of classpath entries
+ */
+ private void doPath(Collection<Container> resultpath, Collection<Project> projects,
+ Collection<Container> entries, Collection<Container> bootclasspath) {
+ for (Container cpe : entries) {
+ if (cpe.getError() != null)
+ error(cpe.getError());
+ else {
+ if (cpe.getType() == Container.TYPE.PROJECT) {
+ projects.add(cpe.getProject());
+ }
+ if (bootclasspath != null && cpe.getBundleSymbolicName().startsWith("ee.")
+ || cpe.getAttributes().containsKey("boot"))
+ bootclasspath.add(cpe);
+ else
+ resultpath.add(cpe);
+ }
+ }
+ }
+
+ /**
+ * Parse the list of bundles that are a prerequisite to this project.
+ *
+ * Bundles are listed in repo specific names. So we just let our repo
+ * plugins iterate over the list of bundles and we get the highest version
+ * from them.
+ *
+ * @return
+ */
+
+ private List<Container> parseBuildpath() throws Exception {
+ List<Container> bundles = getBundles(Strategy.LOWEST, getProperty(Constants.BUILDPATH),
+ Constants.BUILDPATH);
+ appendPackages(Strategy.LOWEST, getProperty(Constants.BUILDPACKAGES), bundles,
+ ResolverMode.build);
+ return bundles;
+ }
+
+ private List<Container> parseRunpath() throws Exception {
+ return getBundles(Strategy.HIGHEST, getProperty(Constants.RUNPATH), Constants.RUNPATH);
+ }
+
+ private List<Container> parseRunbundles() throws Exception {
+ return getBundles(Strategy.HIGHEST, getProperty(Constants.RUNBUNDLES), Constants.RUNBUNDLES);
+ }
+
+ private List<Container> parseTestpath() throws Exception {
+ return getBundles(Strategy.HIGHEST, getProperty(Constants.TESTPATH), Constants.TESTPATH);
+ }
+
+ /**
+ * Analyze the header and return a list of files that should be on the
+ * build, test or some other path. The list is assumed to be a list of bsns
+ * with a version specification. The special case of version=project
+ * indicates there is a project in the same workspace. The path to the
+ * output directory is calculated. The default directory ${bin} can be
+ * overridden with the output attribute.
+ *
+ * @param strategy
+ * STRATEGY_LOWEST or STRATEGY_HIGHEST
+ * @param spec
+ * The header
+ * @return
+ */
+
+ public List<Container> getBundles(Strategy strategyx, String spec, String source)
+ throws Exception {
+ List<Container> result = new ArrayList<Container>();
+ Parameters bundles = new Parameters(spec);
+
+ try {
+ for (Iterator<Entry<String, Attrs>> i = bundles.entrySet().iterator(); i.hasNext();) {
+ Entry<String, Attrs> entry = i.next();
+ String bsn = entry.getKey();
+ Map<String, String> attrs = entry.getValue();
+
+ Container found = null;
+
+ String versionRange = attrs.get("version");
+
+ if (versionRange != null) {
+ if (versionRange.equals("latest") || versionRange.equals("snapshot")) {
+ found = getBundle(bsn, versionRange, strategyx, attrs);
+ }
+ }
+ if (found == null) {
+ if (versionRange != null
+ && (versionRange.equals("project") || versionRange.equals("latest"))) {
+ Project project = getWorkspace().getProject(bsn);
+ if (project != null && project.exists()) {
+ File f = project.getOutput();
+ found = new Container(project, bsn, versionRange,
+ Container.TYPE.PROJECT, f, null, attrs);
+ } else {
+ error("Reference to project that does not exist in workspace\n"
+ + " Project %s\n" + " Specification %s", bsn, spec);
+ continue;
+ }
+ } else if (versionRange != null && versionRange.equals("file")) {
+ File f = getFile(bsn);
+ String error = null;
+ if (!f.exists())
+ error = "File does not exist: " + f.getAbsolutePath();
+ if (f.getName().endsWith(".lib")) {
+ found = new Container(this, bsn, "file", Container.TYPE.LIBRARY, f,
+ error, attrs);
+ } else {
+ found = new Container(this, bsn, "file", Container.TYPE.EXTERNAL, f,
+ error, attrs);
+ }
+ } else {
+ found = getBundle(bsn, versionRange, strategyx, attrs);
+ }
+ }
+
+ if (found != null) {
+ List<Container> libs = found.getMembers();
+ for (Container cc : libs) {
+ if (result.contains(cc))
+ warning("Multiple bundles with the same final URL: " + cc);
+
+ result.add(cc);
+ }
+ } else {
+ // Oops, not a bundle in sight :-(
+ Container x = new Container(this, bsn, versionRange, Container.TYPE.ERROR,
+ null, bsn + ";version=" + versionRange + " not found", attrs);
+ result.add(x);
+ warning("Can not find URL for bsn " + bsn);
+ }
+ }
+ } catch (CircularDependencyException e) {
+ String message = e.getMessage();
+ if (source != null)
+ message = String.format("%s (from property: %s)", message, source);
+ error("Circular dependency detected from project %s: %s", e, getName(), message);
+ } catch (Exception e) {
+ error("Unexpected error while trying to get the bundles from " + spec, e);
+ e.printStackTrace();
+ }
+ return result;
+ }
+
+ /**
+ * Just calls a new method with a default parm.
+ *
+ * @throws Exception
+ *
+ */
+ Collection<Container> getBundles(Strategy strategy, String spec) throws Exception {
+ return getBundles(strategy, spec, null);
+ }
+
+ /**
+ * Calculates the containers required to fulfil the {@code -buildpackages}
+ * instruction, and appends them to the existing list of containers.
+ *
+ * @param strategyx
+ * The package-version disambiguation strategy.
+ * @param spec
+ * The value of the @{code -buildpackages} instruction.
+ * @throws Exception
+ */
+ public void appendPackages(Strategy strategyx, String spec, List<Container> resolvedBundles,
+ ResolverMode mode) throws Exception {
+ Map<File, Container> pkgResolvedBundles = new HashMap<File, Container>();
+
+ List<Entry<String, Attrs>> queue = new LinkedList<Map.Entry<String, Attrs>>();
+ queue.addAll(new Parameters(spec).entrySet());
+
+ while (!queue.isEmpty()) {
+ Entry<String, Attrs> entry = queue.remove(0);
+
+ String pkgName = entry.getKey();
+ Map<String, String> attrs = entry.getValue();
+
+ Container found = null;
+
+ String versionRange = attrs.get(Constants.VERSION_ATTRIBUTE);
+ if ("latest".equals(versionRange) || "snapshot".equals(versionRange))
+ found = getPackage(pkgName, versionRange, strategyx, attrs, mode);
+
+ if (found == null)
+ found = getPackage(pkgName, versionRange, strategyx, attrs, mode);
+
+ if (found != null) {
+ if (resolvedBundles.contains(found)) {
+ // Don't add his bundle because it was already included
+ // using -buildpath
+ } else {
+ List<Container> libs = found.getMembers();
+ for (Container cc : libs) {
+ Container existing = pkgResolvedBundles.get(cc.file);
+ if (existing != null)
+ addToPackageList(existing, attrs.get("packages"));
+ else {
+ addToPackageList(cc, attrs.get("packages"));
+ pkgResolvedBundles.put(cc.file, cc);
+ }
+
+ String importUses = cc.getAttributes().get("import-uses");
+ if (importUses != null)
+ queue.addAll(0, new Parameters(importUses).entrySet());
+ }
+ }
+ } else {
+ // Unable to resolve
+ Container x = new Container(this, "X", versionRange, Container.TYPE.ERROR, null,
+ "package " + pkgName + ";version=" + versionRange + " not found", attrs);
+ resolvedBundles.add(x);
+ warning("Can not find URL for package " + pkgName);
+ }
+ }
+
+ for (Container container : pkgResolvedBundles.values()) {
+ resolvedBundles.add(container);
+ }
+ }
+
+ static void mergeNames(String names, Set<String> set) {
+ StringTokenizer tokenizer = new StringTokenizer(names, ",");
+ while (tokenizer.hasMoreTokens())
+ set.add(tokenizer.nextToken().trim());
+ }
+
+ static String flatten(Set<String> names) {
+ StringBuilder builder = new StringBuilder();
+ boolean first = true;
+ for (String name : names) {
+ if (!first)
+ builder.append(',');
+ builder.append(name);
+ first = false;
+ }
+ return builder.toString();
+ }
+
+ static void addToPackageList(Container container, String newPackageNames) {
+ Set<String> merged = new HashSet<String>();
+
+ String packageListStr = container.attributes.get("packages");
+ if (packageListStr != null)
+ mergeNames(packageListStr, merged);
+ if (newPackageNames != null)
+ mergeNames(newPackageNames, merged);
+
+ container.putAttribute("packages", flatten(merged));
+ }
+
+ /**
+ * Find a container to fulfil a package requirement
+ *
+ * @param packageName
+ * The package required
+ * @param range
+ * The package version range required
+ * @param strategyx
+ * The package-version disambiguation strategy
+ * @param attrs
+ * Other attributes specified by the search.
+ * @return
+ * @throws Exception
+ */
+ public Container getPackage(String packageName, String range, Strategy strategyx,
+ Map<String, String> attrs, ResolverMode mode) throws Exception {
+ if ("snapshot".equals(range))
+ return new Container(this, "", range, Container.TYPE.ERROR, null,
+ "snapshot not supported for package lookups", null);
+
+ if (attrs == null)
+ attrs = new HashMap<String, String>(2);
+ attrs.put("package", packageName);
+ attrs.put("mode", mode.name());
+
+ Strategy useStrategy = findStrategy(attrs, strategyx, range);
+
+ List<RepositoryPlugin> plugins = getPlugins(RepositoryPlugin.class);
+ for (RepositoryPlugin plugin : plugins) {
+ try {
+ File result = plugin.get(null, range, useStrategy, attrs);
+ if (result != null) {
+ if (result.getName().endsWith("lib"))
+ return new Container(this, result.getName(), range, Container.TYPE.LIBRARY,
+ result, null, attrs);
+ else
+ return new Container(this, result.getName(), range, Container.TYPE.REPO,
+ result, null, attrs);
+ }
+ } catch (Exception e) {
+ // Ignore... lots of repos will fail here
+ }
+ }
+
+ return new Container(this, "X", range, Container.TYPE.ERROR, null, "package " + packageName
+ + ";version=" + range + " Not found in " + plugins, null);
+ }
+
+ private Strategy findStrategy(Map<String, String> attrs, Strategy defaultStrategy,
+ String versionRange) {
+ Strategy useStrategy = defaultStrategy;
+ String overrideStrategy = attrs.get("strategy");
+ if (overrideStrategy != null) {
+ if ("highest".equalsIgnoreCase(overrideStrategy))
+ useStrategy = Strategy.HIGHEST;
+ else if ("lowest".equalsIgnoreCase(overrideStrategy))
+ useStrategy = Strategy.LOWEST;
+ else if ("exact".equalsIgnoreCase(overrideStrategy))
+ useStrategy = Strategy.EXACT;
+ }
+ if ("latest".equals(versionRange))
+ useStrategy = Strategy.HIGHEST;
+ return useStrategy;
+ }
+
+ /**
+ * The user selected pom in a path. This will place the pom as well as its
+ * dependencies on the list
+ *
+ * @param strategyx
+ * the strategy to use.
+ * @param result
+ * The list of result containers
+ * @param attrs
+ * The attributes
+ * @throws Exception
+ * anything goes wrong
+ */
+ public void doMavenPom(Strategy strategyx, List<Container> result, String action)
+ throws Exception {
+ File pomFile = getFile("pom.xml");
+ if (!pomFile.isFile())
+ error("Specified to use pom.xml but the project directory does not contain a pom.xml file");
+ else {
+ ProjectPom pom = getWorkspace().getMaven().createProjectModel(pomFile);
+ if (action == null)
+ action = "compile";
+ Pom.Scope act = Pom.Scope.valueOf(action);
+ Set<Pom> dependencies = pom.getDependencies(act);
+ for (Pom sub : dependencies) {
+ File artifact = sub.getArtifact();
+ Container container = new Container(artifact);
+ result.add(container);
+ }
+ }
+ }
+
+ public Collection<Project> getDependson() throws Exception {
+ prepare();
+ return dependson;
+ }
+
+ public Collection<Container> getBuildpath() throws Exception {
+ prepare();
+ return buildpath;
+ }
+
+ public Collection<Container> getTestpath() throws Exception {
+ prepare();
+ return testpath;
+ }
+
+ /**
+ * Handle dependencies for paths that are calculated on demand.
+ *
+ * @param testpath2
+ * @param parseTestpath
+ */
+ private void justInTime(Collection<Container> path, List<Container> entries) {
+ if (delayRunDependencies && path.isEmpty())
+ doPath(path, dependson, entries, null);
+ }
+
+ public Collection<Container> getRunpath() throws Exception {
+ prepare();
+ justInTime(runpath, parseRunpath());
+ return runpath;
+ }
+
+ public Collection<Container> getRunbundles() throws Exception {
+ prepare();
+ justInTime(runbundles, parseRunbundles());
+ return runbundles;
+ }
+
+ public File getRunStorage() throws Exception {
+ prepare();
+ return runstorage;
+ }
+
+ public boolean getRunBuilds() {
+ boolean result;
+ String runBuildsStr = getProperty(Constants.RUNBUILDS);
+ if (runBuildsStr == null)
+ result = !getPropertiesFile().getName().toLowerCase()
+ .endsWith(Constants.DEFAULT_BNDRUN_EXTENSION);
+ else
+ result = Boolean.parseBoolean(runBuildsStr);
+ return result;
+ }
+
+ public Collection<File> getSourcePath() throws Exception {
+ prepare();
+ return sourcepath;
+ }
+
+ public Collection<File> getAllsourcepath() throws Exception {
+ prepare();
+ return allsourcepath;
+ }
+
+ public Collection<Container> getBootclasspath() throws Exception {
+ prepare();
+ return bootclasspath;
+ }
+
+ public File getOutput() throws Exception {
+ prepare();
+ return output;
+ }
+
+ private void doEclipseClasspath() throws Exception {
+ EclipseClasspath eclipse = new EclipseClasspath(this, getWorkspace().getBase(), getBase());
+ eclipse.setRecurse(false);
+
+ // We get the file directories but in this case we need
+ // to tell ant that the project names
+ for (File dependent : eclipse.getDependents()) {
+ Project required = workspace.getProject(dependent.getName());
+ dependson.add(required);
+ }
+ for (File f : eclipse.getClasspath()) {
+ buildpath.add(new Container(f));
+ }
+ for (File f : eclipse.getBootclasspath()) {
+ bootclasspath.add(new Container(f));
+ }
+ sourcepath.addAll(eclipse.getSourcepath());
+ allsourcepath.addAll(eclipse.getAllSources());
+ output = eclipse.getOutput();
+ }
+
+ public String _p_dependson(String args[]) throws Exception {
+ return list(args, toFiles(getDependson()));
+ }
+
+ private Collection<?> toFiles(Collection<Project> projects) {
+ List<File> files = new ArrayList<File>();
+ for (Project p : projects) {
+ files.add(p.getBase());
+ }
+ return files;
+ }
+
+ public String _p_buildpath(String args[]) throws Exception {
+ return list(args, getBuildpath());
+ }
+
+ public String _p_testpath(String args[]) throws Exception {
+ return list(args, getRunpath());
+ }
+
+ public String _p_sourcepath(String args[]) throws Exception {
+ return list(args, getSourcePath());
+ }
+
+ public String _p_allsourcepath(String args[]) throws Exception {
+ return list(args, getAllsourcepath());
+ }
+
+ public String _p_bootclasspath(String args[]) throws Exception {
+ return list(args, getBootclasspath());
+ }
+
+ public String _p_output(String args[]) throws Exception {
+ if (args.length != 1)
+ throw new IllegalArgumentException("${output} should not have arguments");
+ return getOutput().getAbsolutePath();
+ }
+
+ private String list(String[] args, Collection<?> list) {
+ if (args.length > 3)
+ throw new IllegalArgumentException("${" + args[0]
+ + "[;<separator>]} can only take a separator as argument, has "
+ + Arrays.toString(args));
+
+ String separator = ",";
+
+ if (args.length == 2) {
+ separator = args[1];
+ }
+
+ return join(list, separator);
+ }
+
+ protected Object[] getMacroDomains() {
+ return new Object[] { workspace };
+ }
+
+ public File release(Jar jar) throws Exception {
+ String name = getProperty(Constants.RELEASEREPO);
+ return release(name, jar);
+ }
+
+ /**
+ * Release
+ *
+ * @param name
+ * The repository name
+ * @param jar
+ * @return
+ * @throws Exception
+ */
+ public File release(String name, Jar jar) throws Exception {
+ trace("release %s", name);
+ List<RepositoryPlugin> plugins = getPlugins(RepositoryPlugin.class);
+ RepositoryPlugin rp = null;
+ for (RepositoryPlugin plugin : plugins) {
+ if (!plugin.canWrite()) {
+ continue;
+ }
+ if (name == null) {
+ rp = plugin;
+ break;
+ } else if (name.equals(plugin.getName())) {
+ rp = plugin;
+ break;
+ }
+ }
+
+ if (rp != null) {
+ try {
+ File file = rp.put(jar);
+ trace("Released %s to file %s in repository %s", jar.getName(), file, rp);
+ } catch (Exception e) {
+ error("Deploying " + jar.getName() + " on " + rp.getName(), e);
+ } finally {
+ jar.close();
+ }
+ } else if (name == null)
+ error("There is no writable repository (no repo name specified)");
+ else
+ error("Cannot find a writeable repository with the name %s from the repositiories %s",
+ name, plugins);
+
+ return null;
+
+ }
+
+ public void release(boolean test) throws Exception {
+ String name = getProperty(Constants.RELEASEREPO);
+ release(name, test);
+ }
+
+ /**
+ * Release
+ *
+ * @param name
+ * The respository name
+ * @param test
+ * Run testcases
+ * @throws Exception
+ */
+ public void release(String name, boolean test) throws Exception {
+ trace("release");
+ File[] jars = build(test);
+ // If build fails jars will be null
+ if (jars == null) {
+ trace("no jars being build");
+ return;
+ }
+ trace("build ", Arrays.toString(jars));
+ for (File jar : jars) {
+ Jar j = new Jar(jar);
+ try {
+ release(name, j);
+ } finally {
+ j.close();
+ }
+ }
+
+ }
+
+ /**
+ * Get a bundle from one of the plugin repositories. If an exact version is
+ * required we just return the first repository found (in declaration order
+ * in the build.bnd file).
+ *
+ * @param bsn
+ * The bundle symbolic name
+ * @param range
+ * The version range
+ * @param lowest
+ * set to LOWEST or HIGHEST
+ * @return the file object that points to the bundle or null if not found
+ * @throws Exception
+ * when something goes wrong
+ */
+
+ public Container getBundle(String bsn, String range, Strategy strategy,
+ Map<String, String> attrs) throws Exception {
+
+ if (range == null)
+ range = "0";
+
+ if ("snapshot".equals(range)) {
+ return getBundleFromProject(bsn, attrs);
+ }
+
+ Strategy useStrategy = strategy;
+
+ if ("latest".equals(range)) {
+ Container c = getBundleFromProject(bsn, attrs);
+ if (c != null)
+ return c;
+
+ useStrategy = Strategy.HIGHEST;
+ }
+
+ useStrategy = overrideStrategy(attrs, useStrategy);
+
+ List<RepositoryPlugin> plugins = workspace.getRepositories();
+
+ if (useStrategy == Strategy.EXACT) {
+
+ // For an exact range we just iterate over the repos
+ // and return the first we find.
+
+ for (RepositoryPlugin plugin : plugins) {
+ File result = plugin.get(bsn, range, Strategy.EXACT, attrs);
+ if (result != null)
+ return toContainer(bsn, range, attrs, result);
+ }
+ } else {
+ VersionRange versionRange = "latest".equals(range) ? new VersionRange("0")
+ : new VersionRange(range);
+
+ // We have a range search. Gather all the versions in all the repos
+ // and make a decision on that choice. If the same version is found
+ // in
+ // multiple repos we take the first
+
+ SortedMap<Version, RepositoryPlugin> versions = new TreeMap<Version, RepositoryPlugin>();
+ for (RepositoryPlugin plugin : plugins) {
+ try {
+ List<Version> vs = plugin.versions(bsn);
+ if (vs != null) {
+ for (Version v : vs) {
+ if (!versions.containsKey(v) && versionRange.includes(v))
+ versions.put(v, plugin);
+ }
+ }
+ } catch (UnsupportedOperationException ose) {
+ // We have a plugin that cannot list versions, try
+ // if it has this specific version
+ // The main reaosn for this code was the Maven Remote
+ // Repository
+ // To query, we must have a real version
+ if (!versions.isEmpty() && Verifier.isVersion(range)) {
+ File file = plugin.get(bsn, range, useStrategy, attrs);
+ // and the entry must exist
+ // if it does, return this as a result
+ if (file != null)
+ return toContainer(bsn, range, attrs, file);
+ }
+ }
+ }
+
+ // Verify if we found any, if so, we use the strategy to pick
+ // the first or last
+
+ if (!versions.isEmpty()) {
+ Version provider = null;
+
+ switch (useStrategy) {
+ case HIGHEST:
+ provider = versions.lastKey();
+ break;
+
+ case LOWEST:
+ provider = versions.firstKey();
+ break;
+ }
+ if (provider != null) {
+ RepositoryPlugin repo = versions.get(provider);
+ String version = provider.toString();
+ File result = repo.get(bsn, version, Strategy.EXACT, attrs);
+ if (result != null)
+ return toContainer(bsn, version, attrs, result);
+ } else
+ error("Unexpected, found versions %s but then not provider for startegy %s?",
+ versions, useStrategy);
+ }
+ }
+
+ //
+ // If we get this far we ran into an error somewhere
+
+ return new Container(this, bsn, range, Container.TYPE.ERROR, null, bsn + ";version="
+ + range + " Not found in " + plugins, null);
+
+ }
+
+ /**
+ * @param attrs
+ * @param useStrategy
+ * @return
+ */
+ protected Strategy overrideStrategy(Map<String, String> attrs, Strategy useStrategy) {
+ if (attrs != null) {
+ String overrideStrategy = attrs.get("strategy");
+
+ if (overrideStrategy != null) {
+ if ("highest".equalsIgnoreCase(overrideStrategy))
+ useStrategy = Strategy.HIGHEST;
+ else if ("lowest".equalsIgnoreCase(overrideStrategy))
+ useStrategy = Strategy.LOWEST;
+ else if ("exact".equalsIgnoreCase(overrideStrategy))
+ useStrategy = Strategy.EXACT;
+ }
+ }
+ return useStrategy;
+ }
+
+ /**
+ * @param bsn
+ * @param range
+ * @param attrs
+ * @param result
+ * @return
+ */
+ protected Container toContainer(String bsn, String range, Map<String, String> attrs, File result) {
+ File f = result;
+ if (f == null) {
+ error("Result file for toContainer is unexpectedly null, not sure what to do");
+ f = new File("was null");
+ }
+ if (f.getName().endsWith("lib"))
+ return new Container(this, bsn, range, Container.TYPE.LIBRARY, f, null, attrs);
+ else
+ return new Container(this, bsn, range, Container.TYPE.REPO, f, null, attrs);
+ }
+
+ /**
+ * Look for the bundle in the workspace. The premise is that the bsn must
+ * start with the project name.
+ *
+ * @param bsn
+ * The bsn
+ * @param attrs
+ * Any attributes
+ * @return
+ * @throws Exception
+ */
+ private Container getBundleFromProject(String bsn, Map<String, String> attrs) throws Exception {
+ String pname = bsn;
+ while (true) {
+ Project p = getWorkspace().getProject(pname);
+ if (p != null && p.isValid()) {
+ Container c = p.getDeliverable(bsn, attrs);
+ return c;
+ }
+
+ int n = pname.lastIndexOf('.');
+ if (n <= 0)
+ return null;
+ pname = pname.substring(0, n);
+ }
+ }
+
+ /**
+ * Deploy the file (which must be a bundle) into the repository.
+ *
+ * @param name
+ * The repository name
+ * @param file
+ * bundle
+ */
+ public void deploy(String name, File file) throws Exception {
+ List<RepositoryPlugin> plugins = getPlugins(RepositoryPlugin.class);
+
+ RepositoryPlugin rp = null;
+ for (RepositoryPlugin plugin : plugins) {
+ if (!plugin.canWrite()) {
+ continue;
+ }
+ if (name == null) {
+ rp = plugin;
+ break;
+ } else if (name.equals(plugin.getName())) {
+ rp = plugin;
+ break;
+ }
+ }
+
+ if (rp != null) {
+ Jar jar = new Jar(file);
+ try {
+ rp.put(jar);
+ return;
+ } catch (Exception e) {
+ error("Deploying " + file + " on " + rp.getName(), e);
+ } finally {
+ jar.close();
+ }
+ return;
+ }
+ trace("No repo found " + file);
+ throw new IllegalArgumentException("No repository found for " + file);
+ }
+
+ /**
+ * Deploy the file (which must be a bundle) into the repository.
+ *
+ * @param file
+ * bundle
+ */
+ public void deploy(File file) throws Exception {
+ String name = getProperty(Constants.DEPLOYREPO);
+ deploy(name, file);
+ }
+
+ /**
+ * Deploy the current project to a repository
+ *
+ * @throws Exception
+ */
+ public void deploy() throws Exception {
+ Parameters deploy = new Parameters(getProperty(DEPLOY));
+ if (deploy.isEmpty()) {
+ warning("Deploying but %s is not set to any repo", DEPLOY);
+ return;
+ }
+ File[] outputs = getBuildFiles();
+ for (File output : outputs) {
+ Jar jar = new Jar(output);
+ try {
+ for (Deploy d : getPlugins(Deploy.class)) {
+ trace("Deploying %s to: %s", jar, d);
+ try {
+ if (d.deploy(this, jar))
+ trace("deployed %s successfully to %s", output, d);
+ } catch (Exception e) {
+ error("Error while deploying %s, %s", this, e);
+ e.printStackTrace();
+ }
+ }
+ } finally {
+ jar.close();
+ }
+ }
+ }
+
+ /**
+ * Macro access to the repository
+ *
+ * ${repo;<bsn>[;<version>[;<low|high>]]}
+ */
+
+ public String _repo(String args[]) throws Exception {
+ if (args.length < 2)
+ throw new IllegalArgumentException(
+ "Too few arguments for repo, syntax=: ${repo ';'<bsn> [ ; <version> [; ('HIGHEST'|'LOWEST')]}");
+
+ String bsns = args[1];
+ String version = null;
+ Strategy strategy = Strategy.HIGHEST;
+
+ if (args.length > 2) {
+ version = args[2];
+ if (args.length == 4) {
+ if (args[3].equalsIgnoreCase("HIGHEST"))
+ strategy = Strategy.HIGHEST;
+ else if (args[3].equalsIgnoreCase("LOWEST"))
+ strategy = Strategy.LOWEST;
+ else if (args[3].equalsIgnoreCase("EXACT"))
+ strategy = Strategy.EXACT;
+ else
+ error("${repo;<bsn>;<version>;<'highest'|'lowest'|'exact'>} macro requires a strategy of 'highest' or 'lowest', and is "
+ + args[3]);
+ }
+ }
+
+ Collection<String> parts = split(bsns);
+ List<String> paths = new ArrayList<String>();
+
+ for (String bsn : parts) {
+ Container container = getBundle(bsn, version, strategy, null);
+ add(paths, container);
+ }
+ return join(paths);
+ }
+
+ private void add(List<String> paths, Container container) throws Exception {
+ if (container.getType() == Container.TYPE.LIBRARY) {
+ List<Container> members = container.getMembers();
+ for (Container sub : members) {
+ add(paths, sub);
+ }
+ } else {
+ if (container.getError() == null)
+ paths.add(container.getFile().getAbsolutePath());
+ else {
+ paths.add("<<${repo} = " + container.getBundleSymbolicName() + "-"
+ + container.getVersion() + " : " + container.getError() + ">>");
+
+ if (isPedantic()) {
+ warning("Could not expand repo path request: %s ", container);
+ }
+ }
+
+ }
+ }
+
+ public File getTarget() throws Exception {
+ prepare();
+ return target;
+ }
+
+ /**
+ * This is the external method that will pre-build any dependencies if it is
+ * out of date.
+ *
+ * @param underTest
+ * @return
+ * @throws Exception
+ */
+ public File[] build(boolean underTest) throws Exception {
+ if (isNoBundles())
+ return null;
+
+ if (getProperty("-nope") != null) {
+ warning("Please replace -nope with %s", NOBUNDLES);
+ return null;
+ }
+
+ if (isStale()) {
+ trace("Building " + this);
+ files = buildLocal(underTest);
+ }
+
+ return files;
+ }
+
+ /**
+ * Return the files
+ */
+
+ public File[] getFiles() {
+ return files;
+ }
+
+ /**
+ * Check if this project needs building. This is defined as:
+ *
+ */
+ public boolean isStale() throws Exception {
+ // When we do not generate anything ...
+ if (isNoBundles())
+ return false;
+
+ long buildTime = 0;
+
+ files = getBuildFiles(false);
+ if (files == null)
+ return true;
+
+ for (File f : files) {
+ if (f.lastModified() < lastModified())
+ return true;
+
+ if (buildTime < f.lastModified())
+ buildTime = f.lastModified();
+ }
+
+ for (Project dependency : getDependson()) {
+ if (dependency.isStale())
+ return true;
+
+ if (dependency.isNoBundles())
+ continue;
+
+ File[] deps = dependency.getBuildFiles();
+ for (File f : deps) {
+ if (f.lastModified() >= buildTime)
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * This method must only be called when it is sure that the project has been
+ * build before in the same session.
+ *
+ * It is a bit yucky, but ant creates different class spaces which makes it
+ * hard to detect we already build it.
+ *
+ * This method remembers the files in the appropriate instance vars.
+ *
+ * @return
+ */
+
+ public File[] getBuildFiles() throws Exception {
+ return getBuildFiles(true);
+ }
+
+ public File[] getBuildFiles(boolean buildIfAbsent) throws Exception {
+ if (files != null)
+ return files;
+
+ File f = new File(getTarget(), BUILDFILES);
+ if (f.isFile()) {
+ BufferedReader rdr = IO.reader(f);
+ try {
+ List<File> files = newList();
+ for (String s = rdr.readLine(); s != null; s = rdr.readLine()) {
+ s = s.trim();
+ File ff = new File(s);
+ if (!ff.isFile()) {
+ // Originally we warned the user
+ // but lets just rebuild. That way
+ // the error is not noticed but
+ // it seems better to correct,
+ // See #154
+ rdr.close();
+ f.delete();
+ break;
+ } else
+ files.add(ff);
+ }
+ return this.files = files.toArray(new File[files.size()]);
+ } finally {
+ rdr.close();
+ }
+ }
+ if (buildIfAbsent)
+ return files = buildLocal(false);
+ else
+ return files = null;
+ }
+
+ /**
+ * Build without doing any dependency checking. Make sure any dependent
+ * projects are built first.
+ *
+ * @param underTest
+ * @return
+ * @throws Exception
+ */
+ public File[] buildLocal(boolean underTest) throws Exception {
+ if (isNoBundles())
+ return null;
+
+ File bfs = new File(getTarget(), BUILDFILES);
+ bfs.delete();
+
+ files = null;
+ ProjectBuilder builder = getBuilder(null);
+ if (underTest)
+ builder.setProperty(Constants.UNDERTEST, "true");
+ Jar jars[] = builder.builds();
+ File[] files = new File[jars.length];
+
+ for (int i = 0; i < jars.length; i++) {
+ Jar jar = jars[i];
+ files[i] = saveBuild(jar);
+ }
+ getInfo(builder);
+ builder.close();
+ if (isOk()) {
+ this.files = files;
+
+ // Write out the filenames in the buildfiles file
+ // so we can get them later evenin another process
+ Writer fw = IO.writer(bfs);
+ try {
+ for (File f : files) {
+ fw.append(f.getAbsolutePath());
+ fw.append("\n");
+ }
+ } finally {
+ fw.close();
+ }
+ getWorkspace().changedFile(bfs);
+ return files;
+ } else
+ return null;
+ }
+
+ /**
+ * Answer if this project does not have any output
+ *
+ * @return
+ */
+ public boolean isNoBundles() {
+ return getProperty(NOBUNDLES) != null;
+ }
+
+ public File saveBuild(Jar jar) throws Exception {
+ try {
+ String bsn = jar.getName();
+ File f = getOutputFile(bsn);
+ String msg = "";
+ if (!f.exists() || f.lastModified() < jar.lastModified()) {
+ reportNewer(f.lastModified(), jar);
+ f.delete();
+ if (!f.getParentFile().isDirectory())
+ f.getParentFile().mkdirs();
+ jar.write(f);
+
+ getWorkspace().changedFile(f);
+ } else {
+ msg = "(not modified since " + new Date(f.lastModified()) + ")";
+ }
+ trace(jar.getName() + " (" + f.getName() + ") " + jar.getResources().size() + " " + msg);
+ return f;
+ } finally {
+ jar.close();
+ }
+ }
+
+ public File getOutputFile(String bsn) throws Exception {
+ return new File(getTarget(), bsn + ".jar");
+ }
+
+ private void reportNewer(long lastModified, Jar jar) {
+ if (isTrue(getProperty(Constants.REPORTNEWER))) {
+ StringBuilder sb = new StringBuilder();
+ String del = "Newer than " + new Date(lastModified);
+ for (Map.Entry<String, Resource> entry : jar.getResources().entrySet()) {
+ if (entry.getValue().lastModified() > lastModified) {
+ sb.append(del);
+ del = ", \n ";
+ sb.append(entry.getKey());
+ }
+ }
+ if (sb.length() > 0)
+ warning(sb.toString());
+ }
+ }
+
+ /**
+ * Refresh if we are based on stale data. This also implies our workspace.
+ */
+ public boolean refresh() {
+ boolean changed = false;
+ if (isCnf()) {
+ changed = workspace.refresh();
+ }
+ return super.refresh() || changed;
+ }
+
+ public boolean isCnf() {
+ return getBase().getName().equals(Workspace.CNFDIR);
+ }
+
+ public void propertiesChanged() {
+ super.propertiesChanged();
+ preparedPaths = false;
+ files = null;
+
+ }
+
+ public String getName() {
+ return getBase().getName();
+ }
+
+ public Map<String, Action> getActions() {
+ Map<String, Action> all = newMap();
+ Map<String, Action> actions = newMap();
+ fillActions(all);
+ getWorkspace().fillActions(all);
+
+ for (Map.Entry<String, Action> action : all.entrySet()) {
+ String key = getReplacer().process(action.getKey());
+ if (key != null && key.trim().length() != 0)
+ actions.put(key, action.getValue());
+ }
+ return actions;
+ }
+
+ public void fillActions(Map<String, Action> all) {
+ List<NamedAction> plugins = getPlugins(NamedAction.class);
+ for (NamedAction a : plugins)
+ all.put(a.getName(), a);
+
+ Parameters actions = new Parameters(getProperty("-actions", DEFAULT_ACTIONS));
+ for (Entry<String, Attrs> entry : actions.entrySet()) {
+ String key = Processor.removeDuplicateMarker(entry.getKey());
+ Action action;
+
+ if (entry.getValue().get("script") != null) {
+ // TODO check for the type
+ action = new ScriptAction(entry.getValue().get("type"), entry.getValue().get(
+ "script"));
+ } else {
+ action = new ReflectAction(key);
+ }
+ String label = entry.getValue().get("label");
+ all.put(label.toLowerCase(), action);
+ }
+ }
+
+ public void release() throws Exception {
+ release(false);
+ }
+
+ /**
+ * Release.
+ *
+ * @param name
+ * The repository name
+ * @throws Exception
+ */
+ public void release(String name) throws Exception {
+ release(name, false);
+ }
+
+ public void clean() throws Exception {
+ File target = getTarget();
+ if (target.isDirectory() && target.getParentFile() != null) {
+ IO.delete(target);
+ target.mkdirs();
+ }
+ if (getOutput().isDirectory())
+ IO.delete(getOutput());
+ getOutput().mkdirs();
+ }
+
+ public File[] build() throws Exception {
+ return build(false);
+ }
+
+ public void run() throws Exception {
+ ProjectLauncher pl = getProjectLauncher();
+ pl.setTrace(isTrace());
+ pl.launch();
+ }
+
+ public void test() throws Exception {
+ clear();
+ ProjectTester tester = getProjectTester();
+ tester.setContinuous(isTrue(getProperty(Constants.TESTCONTINUOUS)));
+ tester.prepare();
+
+ if (!isOk()) {
+ return;
+ }
+ int errors = tester.test();
+ if (errors == 0) {
+ System.err.println("No Errors");
+ } else {
+ if (errors > 0) {
+ System.err.println(errors + " Error(s)");
+
+ } else
+ System.err.println("Error " + errors);
+ }
+ }
+
+ /**
+ * This methods attempts to turn any jar into a valid jar. If this is a
+ * bundle with manifest, a manifest is added based on defaults. If it is a
+ * bundle, but not r4, we try to add the r4 headers.
+ *
+ * @param descriptor
+ * @param in
+ * @return
+ * @throws Exception
+ */
+ public Jar getValidJar(File f) throws Exception {
+ Jar jar = new Jar(f);
+ return getValidJar(jar, f.getAbsolutePath());
+ }
+
+ public Jar getValidJar(URL url) throws Exception {
+ InputStream in = url.openStream();
+ try {
+ Jar jar = new Jar(url.getFile().replace('/', '.'), in, System.currentTimeMillis());
+ return getValidJar(jar, url.toString());
+ } finally {
+ in.close();
+ }
+ }
+
+ public Jar getValidJar(Jar jar, String id) throws Exception {
+ Manifest manifest = jar.getManifest();
+ if (manifest == null) {
+ trace("Wrapping with all defaults");
+ Builder b = new Builder(this);
+ b.addClasspath(jar);
+ b.setProperty("Bnd-Message", "Wrapped from " + id + "because lacked manifest");
+ b.setProperty(Constants.EXPORT_PACKAGE, "*");
+ b.setProperty(Constants.IMPORT_PACKAGE, "*;resolution:=optional");
+ jar = b.build();
+ } else if (manifest.getMainAttributes().getValue(Constants.BUNDLE_MANIFESTVERSION) == null) {
+ trace("Not a release 4 bundle, wrapping with manifest as source");
+ Builder b = new Builder(this);
+ b.addClasspath(jar);
+ b.setProperty(Constants.PRIVATE_PACKAGE, "*");
+ b.mergeManifest(manifest);
+ String imprts = manifest.getMainAttributes().getValue(Constants.IMPORT_PACKAGE);
+ if (imprts == null)
+ imprts = "";
+ else
+ imprts += ",";
+ imprts += "*;resolution=optional";
+
+ b.setProperty(Constants.IMPORT_PACKAGE, imprts);
+ b.setProperty("Bnd-Message", "Wrapped from " + id + "because had incomplete manifest");
+ jar = b.build();
+ }
+ return jar;
+ }
+
+ public String _project(String args[]) {
+ return getBase().getAbsolutePath();
+ }
+
+ /**
+ * Bump the version of this project. First check the main bnd file. If this
+ * does not contain a version, check the include files. If they still do not
+ * contain a version, then check ALL the sub builders. If not, add a version
+ * to the main bnd file.
+ *
+ * @param mask
+ * the mask for bumping, see {@link Macro#_version(String[])}
+ * @throws Exception
+ */
+ public void bump(String mask) throws Exception {
+ String pattern = "(Bundle-Version\\s*(:|=)\\s*)(([0-9]+(\\.[0-9]+(\\.[0-9]+)?)?))";
+ String replace = "$1${version;" + mask + ";$3}";
+ try {
+ // First try our main bnd file
+ if (replace(getPropertiesFile(), pattern, replace))
+ return;
+
+ trace("no version in bnd.bnd");
+
+ // Try the included filed in reverse order (last has highest
+ // priority)
+ List<File> included = getIncluded();
+ if (included != null) {
+ List<File> copy = new ArrayList<File>(included);
+ Collections.reverse(copy);
+
+ for (File file : copy) {
+ if (replace(file, pattern, replace)) {
+ trace("replaced version in file %s", file);
+ return;
+ }
+ }
+ }
+ trace("no version in included files");
+
+ boolean found = false;
+
+ // Replace in all sub builders.
+ for (Builder sub : getSubBuilders()) {
+ found |= replace(sub.getPropertiesFile(), pattern, replace);
+ }
+
+ if (!found) {
+ trace("no version in sub builders, add it to bnd.bnd");
+ String bndfile = IO.collect(getPropertiesFile());
+ bndfile += "\n# Added by by bump\nBundle-Version: 0.0.0\n";
+ IO.store(bndfile, getPropertiesFile());
+ }
+ } finally {
+ forceRefresh();
+ }
+ }
+
+ boolean replace(File f, String pattern, String replacement) throws IOException {
+ Sed sed = new Sed(getReplacer(), f);
+ sed.replace(pattern, replacement);
+ return sed.doIt() > 0;
+ }
+
+ public void bump() throws Exception {
+ bump(getProperty(BUMPPOLICY, "=+0"));
+ }
+
+ public void action(String command) throws Throwable {
+ Map<String, Action> actions = getActions();
+
+ Action a = actions.get(command);
+ if (a == null)
+ a = new ReflectAction(command);
+
+ before(this, command);
+ try {
+ a.execute(this, command);
+ } catch (Throwable t) {
+ after(this, command, t);
+ throw t;
+ }
+ }
+
+ /**
+ * Run all before command plugins
+ *
+ */
+ void before(Project p, String a) {
+ List<CommandPlugin> testPlugins = getPlugins(CommandPlugin.class);
+ for (CommandPlugin testPlugin : testPlugins) {
+ testPlugin.before(this, a);
+ }
+ }
+
+ /**
+ * Run all after command plugins
+ */
+ void after(Project p, String a, Throwable t) {
+ List<CommandPlugin> testPlugins = getPlugins(CommandPlugin.class);
+ for (int i = testPlugins.size() - 1; i >= 0; i--) {
+ testPlugins.get(i).after(this, a, t);
+ }
+ }
+
+ public String _findfile(String args[]) {
+ File f = getFile(args[1]);
+ List<String> files = new ArrayList<String>();
+ tree(files, f, "", new Instruction(args[2]));
+ return join(files);
+ }
+
+ void tree(List<String> list, File current, String path, Instruction instr) {
+ if (path.length() > 0)
+ path = path + "/";
+
+ String subs[] = current.list();
+ if (subs != null) {
+ for (String sub : subs) {
+ File f = new File(current, sub);
+ if (f.isFile()) {
+ if (instr.matches(sub) && !instr.isNegated())
+ list.add(path + sub);
+ } else
+ tree(list, f, path + sub, instr);
+ }
+ }
+ }
+
+ public void refreshAll() {
+ workspace.refresh();
+ refresh();
+ }
+
+ @SuppressWarnings("unchecked") public void script(String type, String script) throws Exception {
+ // TODO check tyiping
+ List<Scripter> scripters = getPlugins(Scripter.class);
+ if (scripters.isEmpty()) {
+ error("Can not execute script because there are no scripters registered: %s", script);
+ return;
+ }
+ @SuppressWarnings("rawtypes") Map x = (Map) getProperties();
+ scripters.get(0).eval((Map<String, Object>) x, new StringReader(script));
+ }
+
+ public String _repos(String args[]) throws Exception {
+ List<RepositoryPlugin> repos = getPlugins(RepositoryPlugin.class);
+ List<String> names = new ArrayList<String>();
+ for (RepositoryPlugin rp : repos)
+ names.add(rp.getName());
+ return join(names, ", ");
+ }
+
+ public String _help(String args[]) throws Exception {
+ if (args.length == 1)
+ return "Specify the option or header you want information for";
+
+ Syntax syntax = Syntax.HELP.get(args[1]);
+ if (syntax == null)
+ return "No help for " + args[1];
+
+ String what = null;
+ if (args.length > 2)
+ what = args[2];
+
+ if (what == null || what.equals("lead"))
+ return syntax.getLead();
+ if (what == null || what.equals("example"))
+ return syntax.getExample();
+ if (what == null || what.equals("pattern"))
+ return syntax.getPattern();
+ if (what == null || what.equals("values"))
+ return syntax.getValues();
+
+ return "Invalid type specified for help: lead, example, pattern, values";
+ }
+
+ /**
+ * Returns containers for the deliverables of this project. The deliverables
+ * is the project builder for this project if no -sub is specified.
+ * Otherwise it contains all the sub bnd files.
+ *
+ * @return A collection of containers
+ *
+ * @throws Exception
+ */
+ public Collection<Container> getDeliverables() throws Exception {
+ List<Container> result = new ArrayList<Container>();
+ Collection<? extends Builder> builders = getSubBuilders();
+
+ for (Builder builder : builders) {
+ Container c = new Container(this, builder.getBsn(), builder.getVersion(),
+ Container.TYPE.PROJECT, getOutputFile(builder.getBsn()), null, null);
+ result.add(c);
+ }
+ return result;
+
+ }
+
+ /**
+ * Return the builder associated with the give bnd file or null. The bnd.bnd
+ * file can contain -sub option. This option allows specifying files in the
+ * same directory that should drive the generation of multiple deliverables.
+ * This method figures out if the bndFile is actually one of the bnd files
+ * of a deliverable.
+ *
+ * @param bndFile
+ * A file pointing to a bnd file.
+ * @return null or the builder for a sub file.
+ * @throws Exception
+ */
+ public Builder getSubBuilder(File bndFile) throws Exception {
+ bndFile = bndFile.getCanonicalFile();
+
+ // Verify that we are inside the project.
+ File base = getBase().getCanonicalFile();
+ if (!bndFile.getAbsolutePath().startsWith(base.getAbsolutePath()))
+ return null;
+
+ Collection<? extends Builder> builders = getSubBuilders();
+ for (Builder sub : builders) {
+ File propertiesFile = sub.getPropertiesFile();
+ if (propertiesFile != null) {
+ if (propertiesFile.getCanonicalFile().equals(bndFile)) {
+ // Found it!
+ return sub;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Answer the container associated with a given bsn.
+ *
+ * @param bndFile
+ * A file pointing to a bnd file.
+ * @return null or the builder for a sub file.
+ * @throws Exception
+ */
+ public Container getDeliverable(String bsn, Map<String, String> attrs) throws Exception {
+ Collection<? extends Builder> builders = getSubBuilders();
+ for (Builder sub : builders) {
+ if (sub.getBsn().equals(bsn))
+ return new Container(this, getOutputFile(bsn));
+ }
+ return null;
+ }
+
+ /**
+ * Get a list of the sub builders. A bnd.bnd file can contain the -sub
+ * option. This will generate multiple deliverables. This method returns the
+ * builders for each sub file. If no -sub option is present, the list will
+ * contain a builder for the bnd.bnd file.
+ *
+ * @return A list of builders.
+ * @throws Exception
+ */
+ public Collection<? extends Builder> getSubBuilders() throws Exception {
+ return getBuilder(null).getSubBuilders();
+ }
+
+ /**
+ * Calculate the classpath. We include our own runtime.jar which includes
+ * the test framework and we include the first of the test frameworks
+ * specified.
+ *
+ * @throws Exception
+ */
+ Collection<File> toFile(Collection<Container> containers) throws Exception {
+ ArrayList<File> files = new ArrayList<File>();
+ for (Container container : containers) {
+ container.contributeFiles(files, this);
+ }
+ return files;
+ }
+
+ public Collection<String> getRunVM() {
+ Parameters hdr = getParameters(RUNVM);
+ return hdr.keySet();
+ }
+
+ public Map<String, String> getRunProperties() {
+ return OSGiHeader.parseProperties(getProperty(RUNPROPERTIES));
+ }
+
+ /**
+ * Get a launcher.
+ *
+ * @return
+ * @throws Exception
+ */
+ public ProjectLauncher getProjectLauncher() throws Exception {
+ return getHandler(ProjectLauncher.class, getRunpath(), LAUNCHER_PLUGIN,
+ "biz.aQute.launcher");
+ }
+
+ public ProjectTester getProjectTester() throws Exception {
+ return getHandler(ProjectTester.class, getTestpath(), TESTER_PLUGIN, "biz.aQute.junit");
+ }
+
+ private <T> T getHandler(Class<T> target, Collection<Container> containers, String header,
+ String defaultHandler) throws Exception {
+ Class<? extends T> handlerClass = target;
+
+ // Make sure we find at least one handler, but hope to find an earlier
+ // one
+ List<Container> withDefault = Create.list();
+ withDefault.addAll(containers);
+ withDefault.addAll(getBundles(Strategy.HIGHEST, defaultHandler, null));
+ trace("candidates for tester %s", withDefault);
+
+ for (Container c : withDefault) {
+ Manifest manifest = c.getManifest();
+
+ if (manifest != null) {
+ String launcher = manifest.getMainAttributes().getValue(header);
+ if (launcher != null) {
+ Class<?> clz = getClass(launcher, c.getFile());
+ if (clz != null) {
+ if (!target.isAssignableFrom(clz)) {
+ error("Found a %s class in %s but it is not compatible with: %s", clz,
+ c, target);
+ } else {
+ handlerClass = clz.asSubclass(target);
+ Constructor<? extends T> constructor = handlerClass
+ .getConstructor(Project.class);
+ return constructor.newInstance(this);
+ }
+ }
+ }
+ }
+ }
+
+ throw new IllegalArgumentException("Default handler for " + header + " not found in "
+ + defaultHandler);
+ }
+
+ public synchronized boolean lock(String reason) throws InterruptedException {
+ if (!lock.tryLock(5, TimeUnit.SECONDS)) {
+ error("Could not acquire lock for %s, was locked by %s for %s", reason, lockingThread,
+ lockingReason);
+ System.err.printf("Could not acquire lock for %s, was locked by %s for %s%n", reason,
+ lockingThread, lockingReason);
+ System.err.flush();
+ return false;
+ }
+ this.lockingReason = reason;
+ this.lockingThread = Thread.currentThread();
+ return true;
+ }
+
+ public void unlock() {
+ lockingReason = null;
+ lock.unlock();
+ }
+
+ /**
+ * Make this project delay the calculation of the run dependencies.
+ *
+ * The run dependencies calculation can be done in prepare or until the
+ * dependencies are actually needed.
+ */
+ public void setDelayRunDependencies(boolean x) {
+ delayRunDependencies = x;
+ }
+
+ /**
+ * Sets the package version on an exported package
+ *
+ * @param packageName
+ * The package name
+ * @param version
+ * The new package version
+ */
+ public void setPackageInfo(String packageName, Version version) {
+ try {
+ updatePackageInfoFile(packageName, version);
+ } catch (Exception e) {
+ error(e.getMessage(), e);
+ }
+ }
+
+ void updatePackageInfoFile(String packageName, Version newVersion) throws Exception {
+
+ File file = getPackageInfoFile(packageName);
+
+ // If package/classes are copied into the bundle through Private-Package
+ // etc, there will be no source
+ if (!file.getParentFile().exists()) {
+ return;
+ }
+
+ Version oldVersion = getPackageInfo(packageName);
+
+ if (newVersion.compareTo(oldVersion) == 0) {
+ return;
+ } else {
+ PrintWriter pw = IO.writer(file);
+ pw.println("version " + newVersion);
+ pw.flush();
+ pw.close();
+
+ String path = packageName.replace('.', '/') + "/packageinfo";
+ File binary = IO.getFile(getOutput(), path);
+ binary.getParentFile().mkdirs();
+ IO.copy(file, binary);
+
+ refresh();
+ }
+ }
+
+ File getPackageInfoFile(String packageName) throws IOException {
+ String path = packageName.replace('.', '/') + "/packageinfo";
+ return IO.getFile(getSrc(), path);
+
+ }
+
+ public Version getPackageInfo(String packageName) throws IOException {
+ File packageInfoFile = getPackageInfoFile(packageName);
+ if (!packageInfoFile.exists()) {
+ return Version.emptyVersion;
+ }
+ BufferedReader reader = null;
+ try {
+ reader = IO.reader(packageInfoFile);
+ String line;
+ while ((line = reader.readLine()) != null) {
+ line = line.trim();
+ if (line.startsWith("version ")) {
+ return Version.parseVersion(line.substring(8));
+ }
+ }
+ } finally {
+ if (reader != null) {
+ IO.close(reader);
+ }
+ }
+ return Version.emptyVersion;
+ }
+
+ /**
+ * bnd maintains a class path that is set by the environment, i.e. bnd is
+ * not in charge of it.
+ */
+
+ public void addClasspath( File f) {
+ if ( !f.isFile()) {
+ error("Adding non existent file to the classpath %s", f);
+ }
+ Container container = new Container(f);
+ classpath.add(container);
+ }
+
+ public void clearClasspath() {
+ classpath.clear();
+ }
+
+ public Collection<Container> getClasspath() {
+ return classpath;
+ }
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/ProjectBuilder.java b/bundleplugin/src/main/java/aQute/bnd/build/ProjectBuilder.java
new file mode 100644
index 0000000..d00b71b
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/ProjectBuilder.java
@@ -0,0 +1,77 @@
+package aQute.bnd.build;
+
+import java.io.*;
+import java.util.*;
+
+import aQute.lib.osgi.*;
+
+public class ProjectBuilder extends Builder {
+ Project project;
+ boolean initialized;
+
+ public ProjectBuilder(Project project) {
+ super(project);
+ this.project = project;
+ }
+
+ public ProjectBuilder(ProjectBuilder builder) {
+ super(builder);
+ this.project = builder.project;
+ }
+
+ @Override
+ public long lastModified() {
+ return Math.max(project.lastModified(), super.lastModified());
+ }
+
+ /**
+ * We put our project and our workspace on the macro path.
+ */
+ protected Object[] getMacroDomains() {
+ return new Object[] { project, project.getWorkspace() };
+ }
+
+ public Builder getSubBuilder() throws Exception {
+ return project.getBuilder(this);
+ }
+
+ public Project getProject() {
+ return project;
+ }
+
+ public void init() {
+ try {
+ if (!initialized) {
+ initialized = true;
+ for (Container file : project.getClasspath()) {
+ addClasspath(file.getFile());
+ }
+
+ for (Container file : project.getBuildpath()) {
+ addClasspath(file.getFile());
+ }
+
+ for (Container file : project.getBootclasspath()) {
+ addClasspath(file.getFile());
+ }
+
+ for (File file : project.getAllsourcepath()) {
+ addSourcepath(file);
+ }
+
+ }
+ } catch (Exception e) {
+ error("init project builder fails", e);
+ }
+ }
+
+ public List<Jar> getClasspath() {
+ init();
+ return super.getClasspath();
+ }
+
+ @Override
+ protected void changedFile(File f) {
+ project.getWorkspace().changedFile(f);
+ }
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/ProjectLauncher.java b/bundleplugin/src/main/java/aQute/bnd/build/ProjectLauncher.java
new file mode 100644
index 0000000..1b7bac0
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/ProjectLauncher.java
@@ -0,0 +1,371 @@
+package aQute.bnd.build;
+
+import java.io.*;
+import java.util.*;
+import java.util.Map.Entry;
+import java.util.concurrent.*;
+import java.util.jar.*;
+
+import aQute.bnd.service.RepositoryPlugin.Strategy;
+import aQute.lib.osgi.*;
+import aQute.libg.command.*;
+import aQute.libg.generics.*;
+import aQute.libg.header.*;
+
+/**
+ * A Project Launcher is a base class to be extended by launchers. Launchers are
+ * JARs that launch a framework and install a number of bundles and then run the
+ * framework. A launcher jar must specify a Launcher-Class manifest header. This
+ * class is instantiated and cast to a LauncherPlugin. This plug in is then
+ * asked to provide a ProjectLauncher. This project launcher is then used by the
+ * project to run the code. Launchers must extend this class.
+ *
+ */
+public abstract class ProjectLauncher {
+ private final Project project;
+ private long timeout = 0;
+ private final List<String> classpath = new ArrayList<String>();
+ private List<String> runbundles = Create.list();
+ private final List<String> runvm = new ArrayList<String>();
+ private Map<String, String> runproperties;
+ private Command java;
+ private Parameters runsystempackages;
+ private final List<String> activators = Create.list();
+ private File storageDir;
+ private final List<String> warnings = Create.list();
+ private final List<String> errors = Create.list();
+
+ private boolean trace;
+ private boolean keep;
+ private int framework;
+
+ public final static int SERVICES = 10111;
+ public final static int NONE = 20123;
+
+ // MUST BE ALIGNED WITH LAUNCHER
+ public final static int OK = 0;
+ public final static int WARNING = -1;
+ public final static int ERROR = -2;
+ public final static int TIMEDOUT = -3;
+ public final static int UPDATE_NEEDED = -4;
+ public final static int CANCELED = -5;
+ public final static int DUPLICATE_BUNDLE = -6;
+ public final static int RESOLVE_ERROR = -7;
+ public final static int ACTIVATOR_ERROR = -8;
+ public final static int CUSTOM_LAUNCHER = -128;
+
+ public final static String EMBEDDED_ACTIVATOR = "Embedded-Activator";
+
+ public ProjectLauncher(Project project) throws Exception {
+ this.project = project;
+
+ updateFromProject();
+ }
+
+ /**
+ * Collect all the aspect from the project and set the local fields from
+ * them. Should be called
+ *
+ * @throws Exception
+ */
+ protected void updateFromProject() throws Exception {
+ // pkr: could not use this because this is killing the runtests.
+ // project.refresh();
+ runbundles.clear();
+ Collection<Container> run = project.getRunbundles();
+
+ for (Container container : run) {
+ File file = container.getFile();
+ if (file != null && (file.isFile() || file.isDirectory())) {
+ runbundles.add(file.getAbsolutePath());
+ } else {
+ warning("Bundle file \"%s\" does not exist", file);
+ }
+ }
+
+ if (project.getRunBuilds()) {
+ File[] builds = project.build();
+ if (builds != null)
+ for (File file : builds)
+ runbundles.add(file.getAbsolutePath());
+ }
+
+ Collection<Container> runpath = project.getRunpath();
+ runsystempackages = project.getParameters(Constants.RUNSYSTEMPACKAGES);
+ framework = getRunframework(project.getProperty(Constants.RUNFRAMEWORK));
+ trace = Processor.isTrue(project.getProperty(Constants.RUNTRACE));
+
+ timeout = Processor.getDuration(project.getProperty(Constants.RUNTIMEOUT), 0);
+ trace = Processor.isTrue(project.getProperty(Constants.RUNTRACE));
+
+ // For backward compatibility with bndtools launcher
+ List<Container> fws = project.getBundles(Strategy.HIGHEST, project.getProperty("-runfw"),
+ "-runfw");
+ runpath.addAll(fws);
+
+ for (Container c : runpath) {
+ addClasspath(c);
+ }
+
+ runvm.addAll(project.getRunVM());
+ runproperties = project.getRunProperties();
+
+ storageDir = project.getRunStorage();
+ if (storageDir == null) {
+ storageDir = new File(project.getTarget(), "fw");
+ }
+ }
+
+ private int getRunframework(String property) {
+ if (Constants.RUNFRAMEWORK_NONE.equalsIgnoreCase(property))
+ return NONE;
+ else if (Constants.RUNFRAMEWORK_SERVICES.equalsIgnoreCase(property))
+ return SERVICES;
+
+ return SERVICES;
+ }
+
+ public void addClasspath(Container container) throws Exception {
+ if (container.getError() != null) {
+ project.error("Cannot launch because %s has reported %s", container.getProject(),
+ container.getError());
+ } else {
+ Collection<Container> members = container.getMembers();
+ for (Container m : members) {
+ String path = m.getFile().getAbsolutePath();
+ if (!classpath.contains(path)) {
+ classpath.add(path);
+
+ Manifest manifest = m.getManifest();
+
+ if (manifest != null) {
+ Parameters exports = project.parseHeader(manifest.getMainAttributes()
+ .getValue(Constants.EXPORT_PACKAGE));
+ for (Entry<String, Attrs> e : exports.entrySet()) {
+ if (!runsystempackages.containsKey(e.getKey()))
+ runsystempackages.put(e.getKey(), e.getValue());
+ }
+
+ // Allow activators on the runpath. They are called
+ // after
+ // the framework is completely initialized wit the
+ // system
+ // context.
+ String activator = manifest.getMainAttributes()
+ .getValue(EMBEDDED_ACTIVATOR);
+ if (activator != null)
+ activators.add(activator);
+ }
+ }
+ }
+ }
+ }
+
+ public void addRunBundle(String f) {
+ runbundles.add(f);
+ }
+
+ public Collection<String> getRunBundles() {
+ return runbundles;
+ }
+
+ public void addRunVM(String arg) {
+ runvm.add(arg);
+ }
+
+ public List<String> getRunpath() {
+ return classpath;
+ }
+
+ public Collection<String> getClasspath() {
+ return classpath;
+ }
+
+ public Collection<String> getRunVM() {
+ return runvm;
+ }
+
+ public Collection<String> getArguments() {
+ return Collections.emptySet();
+ }
+
+ public Map<String, String> getRunProperties() {
+ return runproperties;
+ }
+
+ public File getStorageDir() {
+ return storageDir;
+ }
+
+ public abstract String getMainTypeName();
+
+ public abstract void update() throws Exception;
+
+ public int launch() throws Exception {
+ prepare();
+ java = new Command();
+ java.add(project.getProperty("java", "java"));
+ java.add("-cp");
+ java.add(Processor.join(getClasspath(), File.pathSeparator));
+ java.addAll(getRunVM());
+ java.add(getMainTypeName());
+ java.addAll(getArguments());
+ if (timeout != 0)
+ java.setTimeout(timeout + 1000, TimeUnit.MILLISECONDS);
+
+ try {
+ int result = java.execute((InputStream)null, System.err, System.err);
+ if (result == Integer.MIN_VALUE)
+ return TIMEDOUT;
+ reportResult(result);
+ return result;
+ } finally {
+ cleanup();
+ }
+ }
+
+ /**
+ * Is called after the process exists. Can you be used to cleanup
+ * the properties file.
+ */
+
+ public void cleanup() {
+ // do nothing by default
+ }
+
+ protected void reportResult(int result) {
+ switch (result) {
+ case OK:
+ project.trace("Command terminated normal %s", java);
+ break;
+ case TIMEDOUT:
+ project.error("Launch timedout: %s", java);
+ break;
+
+ case ERROR:
+ project.error("Launch errored: %s", java);
+ break;
+
+ case WARNING:
+ project.warning("Launch had a warning %s", java);
+ break;
+ default:
+ project.error("Exit code remote process %d: %s", result, java);
+ break;
+ }
+ }
+
+ public void setTimeout(long timeout, TimeUnit unit) {
+ this.timeout = unit.convert(timeout, TimeUnit.MILLISECONDS);
+ }
+
+ public long getTimeout() {
+ return this.timeout;
+ }
+
+ public void cancel() {
+ java.cancel();
+ }
+
+ public Map<String, ? extends Map<String, String>> getSystemPackages() {
+ return runsystempackages.asMapMap();
+ }
+
+ public void setKeep(boolean keep) {
+ this.keep = keep;
+ }
+
+ public boolean isKeep() {
+ return keep;
+ }
+
+ public void setTrace(boolean level) {
+ this.trace = level;
+ }
+
+ public boolean getTrace() {
+ return this.trace;
+ }
+
+ /**
+ * Should be called when all the changes to the launchers are set. Will
+ * calculate whatever is necessary for the launcher.
+ *
+ * @throws Exception
+ */
+ public abstract void prepare() throws Exception;
+
+ public Project getProject() {
+ return project;
+ }
+
+ public boolean addActivator(String e) {
+ return activators.add(e);
+ }
+
+ public Collection<String> getActivators() {
+ return Collections.unmodifiableCollection(activators);
+ }
+
+ /**
+ * Either NONE or SERVICES to indicate how the remote end launches. NONE
+ * means it should not use the classpath to run a framework. This likely
+ * requires some dummy framework support. SERVICES means it should load the
+ * framework from the claspath.
+ *
+ * @return
+ */
+ public int getRunFramework() {
+ return framework;
+ }
+
+ public void setRunFramework(int n) {
+ assert n == NONE || n == SERVICES;
+ this.framework = n;
+ }
+
+ /**
+ * Add the specification for a set of bundles the runpath if it does not
+ * already is included. This can be used by subclasses to ensure the proper
+ * jars are on the classpath.
+ *
+ * @param defaultSpec
+ * The default spec for default jars
+ */
+ public void addDefault(String defaultSpec) throws Exception {
+ Collection<Container> deflts = project.getBundles(Strategy.HIGHEST, defaultSpec, null);
+ for (Container c : deflts)
+ addClasspath(c);
+ }
+
+ /**
+ * Create a self executable.
+ */
+
+ public Jar executable() throws Exception {
+ throw new UnsupportedOperationException();
+ }
+
+ public void clear() {
+ errors.clear();
+ warnings.clear();
+ }
+
+ public List<String> getErrors() {
+ return Collections.unmodifiableList(errors);
+ }
+
+ public List<String> getWarnings() {
+ return Collections.unmodifiableList(warnings);
+ }
+
+ protected void error(String message, Object... args) {
+ String formatted = String.format(message, args);
+ errors.add(formatted);
+ }
+
+ protected void warning(String message, Object... args) {
+ String formatted = String.format(message, args);
+ warnings.add(formatted);
+ }
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/ProjectTester.java b/bundleplugin/src/main/java/aQute/bnd/build/ProjectTester.java
new file mode 100644
index 0000000..5060d2f
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/ProjectTester.java
@@ -0,0 +1,75 @@
+package aQute.bnd.build;
+
+import java.io.*;
+import java.util.*;
+
+public abstract class ProjectTester {
+ final Project project;
+ final Collection<Container> testbundles;
+ final ProjectLauncher launcher;
+ final List<String> tests = new ArrayList<String>();
+ File reportDir;
+ boolean continuous = true;
+
+ public ProjectTester(Project project) throws Exception {
+ this.project = project;
+ launcher = project.getProjectLauncher();
+ testbundles = project.getTestpath();
+ for (Container c : testbundles) {
+ launcher.addClasspath(c);
+ }
+ reportDir = new File(project.getTarget(), project.getProperty("test-reports",
+ "test-reports"));
+ }
+
+ public ProjectLauncher getProjectLauncher() {
+ return launcher;
+ }
+
+ public void addTest(String test) {
+ tests.add(test);
+ }
+
+ public Collection<String> getTests() {
+ return tests;
+ }
+
+ public Collection<File> getReports() {
+ List<File> reports = new ArrayList<File>();
+ for (File report : reportDir.listFiles()) {
+ if (report.isFile() )
+ reports.add(report);
+ }
+ return reports;
+ }
+
+ public File getReportDir() {
+ return reportDir;
+ }
+
+ public void setReportDir(File reportDir) {
+ this.reportDir = reportDir;
+ }
+
+ public Project getProject() {
+ return project;
+ }
+
+ public boolean getContinuous() {
+ return continuous;
+ }
+
+ public void setContinuous(boolean b) {
+ this.continuous = b;
+ }
+
+ public boolean prepare() throws Exception {
+ reportDir.mkdirs();
+ for ( File file : reportDir.listFiles() ) {
+ file.delete();
+ }
+ return true;
+ }
+
+ public abstract int test() throws Exception;
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/ReflectAction.java b/bundleplugin/src/main/java/aQute/bnd/build/ReflectAction.java
new file mode 100644
index 0000000..55c2861
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/ReflectAction.java
@@ -0,0 +1,22 @@
+package aQute.bnd.build;
+
+import java.lang.reflect.*;
+
+import aQute.bnd.service.action.*;
+
+public class ReflectAction implements Action {
+ String what;
+
+ public ReflectAction(String what) {
+ this.what = what;
+ }
+
+ public void execute(Project project, String action) throws Exception {
+ Method m = project.getClass().getMethod(what);
+ m.invoke(project);
+ }
+
+ public String toString() {
+ return "ra:" + what;
+ }
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/ResolverMode.java b/bundleplugin/src/main/java/aQute/bnd/build/ResolverMode.java
new file mode 100644
index 0000000..1a98e76
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/ResolverMode.java
@@ -0,0 +1,5 @@
+package aQute.bnd.build;
+
+public enum ResolverMode {
+ build, runtime
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/ScriptAction.java b/bundleplugin/src/main/java/aQute/bnd/build/ScriptAction.java
new file mode 100644
index 0000000..8ca55fd
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/ScriptAction.java
@@ -0,0 +1,18 @@
+package aQute.bnd.build;
+
+import aQute.bnd.service.action.*;
+
+public class ScriptAction implements Action {
+ final String script;
+ final String type;
+
+ public ScriptAction(String type, String script) {
+ this.script = script;
+ this.type = type;
+ }
+
+ public void execute(Project project, String action) throws Exception {
+ project.script(type, script);
+ }
+
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/Workspace.java b/bundleplugin/src/main/java/aQute/bnd/build/Workspace.java
new file mode 100644
index 0000000..44a24de
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/Workspace.java
@@ -0,0 +1,354 @@
+package aQute.bnd.build;
+
+import java.io.*;
+import java.lang.ref.*;
+import java.util.*;
+import java.util.concurrent.*;
+import java.util.concurrent.locks.*;
+import java.util.jar.*;
+
+import javax.naming.*;
+
+import aQute.bnd.maven.support.*;
+import aQute.bnd.service.*;
+import aQute.bnd.service.action.*;
+import aQute.lib.deployer.*;
+import aQute.lib.io.*;
+import aQute.lib.osgi.*;
+import aQute.libg.reporter.*;
+
+public class Workspace extends Processor {
+ public static final String BUILDFILE = "build.bnd";
+ public static final String CNFDIR = "cnf";
+ public static final String BNDDIR = "bnd";
+ public static final String CACHEDIR = "cache";
+
+ static Map<File, WeakReference<Workspace>> cache = newHashMap();
+ final Map<String, Project> models = newHashMap();
+ final Map<String, Action> commands = newMap();
+ final File buildDir;
+ final Maven maven = new Maven(Processor.getExecutor());
+
+ /**
+ * This static method finds the workspace and creates a project (or returns
+ * an existing project)
+ *
+ * @param projectDir
+ * @return
+ */
+ public static Project getProject(File projectDir) throws Exception {
+ projectDir = projectDir.getAbsoluteFile();
+ assert projectDir.isDirectory();
+
+ Workspace ws = getWorkspace(projectDir.getParentFile());
+ return ws.getProject(projectDir.getName());
+ }
+
+ public static Workspace getWorkspace(File parent) throws Exception {
+ File workspaceDir = parent.getAbsoluteFile();
+
+ // the cnf directory can actually be a
+ // file that redirects
+ while (workspaceDir.isDirectory()) {
+ File test = new File(workspaceDir, CNFDIR);
+
+ if (!test.exists())
+ test = new File(workspaceDir, BNDDIR);
+
+ if (test.isDirectory())
+ break;
+
+ if (test.isFile()) {
+ String redirect = IO.collect(test).trim();
+ test = getFile(test.getParentFile(), redirect).getAbsoluteFile();
+ workspaceDir = test;
+ }
+ if (!test.exists())
+ throw new IllegalArgumentException("No Workspace found from: " + parent);
+ }
+
+ synchronized (cache) {
+ WeakReference<Workspace> wsr = cache.get(workspaceDir);
+ Workspace ws;
+ if (wsr == null || (ws = wsr.get()) == null) {
+ ws = new Workspace(workspaceDir);
+ cache.put(workspaceDir, new WeakReference<Workspace>(ws));
+ }
+ return ws;
+ }
+ }
+
+ public Workspace(File dir) throws Exception {
+ dir = dir.getAbsoluteFile();
+ dir.mkdirs();
+ assert dir.isDirectory();
+
+ File buildDir = new File(dir, BNDDIR).getAbsoluteFile();
+ if (!buildDir.isDirectory())
+ buildDir = new File(dir, CNFDIR).getAbsoluteFile();
+
+ this.buildDir = buildDir;
+
+ File buildFile = new File(buildDir, BUILDFILE).getAbsoluteFile();
+ if (!buildFile.isFile())
+ warning("No Build File in " + dir);
+
+ setProperties(buildFile, dir);
+ propertiesChanged();
+
+ }
+
+ public Project getProject(String bsn) throws Exception {
+ synchronized (models) {
+ Project project = models.get(bsn);
+ if (project != null)
+ return project;
+
+ File projectDir = getFile(bsn);
+ project = new Project(this, projectDir);
+ if (!project.isValid())
+ return null;
+
+ models.put(bsn, project);
+ return project;
+ }
+ }
+
+ public boolean isPresent(String name) {
+ return models.containsKey(name);
+ }
+
+ public Collection<Project> getCurrentProjects() {
+ return models.values();
+ }
+
+ public boolean refresh() {
+ if (super.refresh()) {
+ for (Project project : getCurrentProjects()) {
+ project.propertiesChanged();
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override public void propertiesChanged() {
+ super.propertiesChanged();
+ File extDir = new File(this.buildDir, "ext");
+ File[] extensions = extDir.listFiles();
+ if (extensions != null) {
+ for (File extension : extensions) {
+ String extensionName = extension.getName();
+ if (extensionName.endsWith(".bnd")) {
+ extensionName = extensionName.substring(0, extensionName.length() - ".bnd".length());
+ try {
+ doIncludeFile(extension, false, getProperties(), "ext." + extensionName);
+ } catch (Exception e) {
+ error("PropertiesChanged: " + e.getMessage());
+ }
+ }
+ }
+ }
+ }
+
+ public String _workspace(String args[]) {
+ return getBase().getAbsolutePath();
+ }
+
+ public void addCommand(String menu, Action action) {
+ commands.put(menu, action);
+ }
+
+ public void removeCommand(String menu) {
+ commands.remove(menu);
+ }
+
+ public void fillActions(Map<String, Action> all) {
+ all.putAll(commands);
+ }
+
+ public Collection<Project> getAllProjects() throws Exception {
+ List<Project> projects = new ArrayList<Project>();
+ for (File file : getBase().listFiles()) {
+ if (new File(file, Project.BNDFILE).isFile())
+ projects.add(getProject(file));
+ }
+ return projects;
+ }
+
+ /**
+ * Inform any listeners that we changed a file (created/deleted/changed).
+ *
+ * @param f
+ * The changed file
+ */
+ public void changedFile(File f) {
+ List<BndListener> listeners = getPlugins(BndListener.class);
+ for (BndListener l : listeners)
+ try {
+ l.changed(f);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void bracket(boolean begin) {
+ List<BndListener> listeners = getPlugins(BndListener.class);
+ for (BndListener l : listeners)
+ try {
+ if (begin)
+ l.begin();
+ else
+ l.end();
+ } catch (Exception e) {
+ // who cares?
+ }
+ }
+
+
+ /**
+ * Signal a BndListener plugin.
+ * We ran an infinite bug loop :-(
+ */
+ final ThreadLocal<Reporter> signalBusy = new ThreadLocal<Reporter>();
+ public void signal(Reporter reporter) {
+ if ( signalBusy.get() != null)
+ return;
+
+ signalBusy.set(reporter);
+ try {
+ List<BndListener> listeners = getPlugins(BndListener.class);
+ for (BndListener l : listeners)
+ try {
+ l.signal(this);
+ } catch (Exception e) {
+ // who cares?
+ }
+ } catch (Exception e) {
+ // Ignore
+ } finally {
+ signalBusy.set(null);
+ }
+ }
+
+ @Override public void signal() {
+ signal(this);
+ }
+
+ private void copy(InputStream in, OutputStream out) throws Exception {
+ byte data[] = new byte[10000];
+ int size = in.read(data);
+ while (size > 0) {
+ out.write(data, 0, size);
+ size = in.read(data);
+ }
+ }
+
+ class CachedFileRepo extends FileRepo {
+ final Lock lock = new ReentrantLock();
+ boolean inited;
+
+ CachedFileRepo() {
+ super("cache", getFile(buildDir, CACHEDIR), false);
+ }
+
+ public String toString() {
+ return "bnd-cache";
+ }
+
+ protected void init() throws Exception {
+ if (lock.tryLock(50, TimeUnit.SECONDS) == false)
+ throw new TimeLimitExceededException(
+ "Cached File Repo is locked and can't acquire it");
+ try {
+ if (!inited) {
+ inited = true;
+ root.mkdirs();
+ if (!root.isDirectory())
+ throw new IllegalArgumentException("Cannot create cache dir " + root);
+
+ InputStream in = getClass().getResourceAsStream(EMBEDDED_REPO);
+ if (in != null)
+ unzip(in, root);
+ else {
+ System.err.println("!!!! Couldn't find embedded-repo.jar in bundle ");
+ error("Couldn't find embedded-repo.jar in bundle ");
+ }
+ }
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ void unzip(InputStream in, File dir) throws Exception {
+ try {
+ JarInputStream jin = new JarInputStream(in);
+ JarEntry jentry = jin.getNextJarEntry();
+ while (jentry != null) {
+ if (!jentry.isDirectory()) {
+ File dest = Processor.getFile(dir, jentry.getName());
+ if (!dest.isFile() || dest.lastModified() < jentry.getTime()
+ || jentry.getTime() == 0) {
+ dest.getParentFile().mkdirs();
+ FileOutputStream out = new FileOutputStream(dest);
+ try {
+ copy(jin, out);
+ } finally {
+ out.close();
+ }
+ }
+ }
+ jentry = jin.getNextJarEntry();
+ }
+ } finally {
+ in.close();
+ }
+ }
+ }
+
+ public List<RepositoryPlugin> getRepositories() {
+ return getPlugins(RepositoryPlugin.class);
+ }
+
+ public Collection<Project> getBuildOrder() throws Exception {
+ List<Project> result = new ArrayList<Project>();
+ for (Project project : getAllProjects()) {
+ Collection<Project> dependsOn = project.getDependson();
+ getBuildOrder(dependsOn, result);
+ if (!result.contains(project)) {
+ result.add(project);
+ }
+ }
+ return result;
+ }
+
+ private void getBuildOrder(Collection<Project> dependsOn, List<Project> result) throws Exception {
+ for (Project project : dependsOn) {
+ Collection<Project> subProjects = project.getDependson();
+ for (Project subProject : subProjects) {
+ if (!result.contains(subProject)) {
+ result.add(subProject);
+ }
+ }
+ if (!result.contains(project)) {
+ result.add(project);
+ }
+ }
+ }
+
+ public static Workspace getWorkspace(String path) throws Exception {
+ File file = IO.getFile(new File(""), path);
+ return getWorkspace(file);
+ }
+
+ public Maven getMaven() {
+ return maven;
+ }
+
+ @Override protected void setTypeSpecificPlugins(Set<Object> list) {
+ super.setTypeSpecificPlugins(list);
+ list.add(maven);
+ list.add(new CachedFileRepo());
+ }
+
+}
diff --git a/bundleplugin/src/main/java/aQute/bnd/build/packageinfo b/bundleplugin/src/main/java/aQute/bnd/build/packageinfo
new file mode 100644
index 0000000..5035fd2
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/bnd/build/packageinfo
@@ -0,0 +1 @@
+version 1.44.0