use of aQute.bnd.annotation.plugin.BndPlugin in project bnd by bndtools.
the class Workspace method addPlugin.
/**
* Add a plugin
*
* @param plugin
* @throws Exception
*/
public boolean addPlugin(Class<?> plugin, String alias, Map<String, String> parameters, boolean force) throws Exception {
BndPlugin ann = plugin.getAnnotation(BndPlugin.class);
if (alias == null) {
if (ann != null)
alias = ann.name();
else {
alias = Strings.getLastSegment(plugin.getName()).toLowerCase();
if (alias.endsWith("plugin")) {
alias = alias.substring(0, alias.length() - "plugin".length());
}
}
}
if (!Verifier.isBsn(alias)) {
error("Not a valid plugin name %s", alias);
}
File ext = getFile(Workspace.CNFDIR + "/" + Workspace.EXT);
IO.mkdirs(ext);
File f = new File(ext, alias + ".bnd");
if (!force) {
if (f.exists()) {
error("Plugin %s already exists", alias);
return false;
}
} else {
IO.delete(f);
}
Object l = plugin.getConstructor().newInstance();
try (Formatter setup = new Formatter()) {
setup.format(//
"#\n" + //
"# Plugin %s setup\n" + "#\n", alias);
setup.format("-plugin.%s = %s", alias, plugin.getName());
for (Map.Entry<String, String> e : parameters.entrySet()) {
setup.format("; \\\n \t%s = '%s'", e.getKey(), escaped(e.getValue()));
}
setup.format("\n\n");
String out = setup.toString();
if (l instanceof LifeCyclePlugin) {
out = ((LifeCyclePlugin) l).augmentSetup(out, alias, parameters);
((LifeCyclePlugin) l).init(this);
}
logger.debug("setup {}", out);
IO.store(out, f);
}
refresh();
for (LifeCyclePlugin lp : getPlugins(LifeCyclePlugin.class)) {
lp.addedPlugin(this, plugin.getName(), alias, parameters);
}
return true;
}
Aggregations