Search in sources :

Example 1 with BndPlugin

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;
}
Also used : LifeCyclePlugin(aQute.bnd.service.lifecycle.LifeCyclePlugin) Formatter(java.util.Formatter) BndPlugin(aQute.bnd.annotation.plugin.BndPlugin) File(java.io.File) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

BndPlugin (aQute.bnd.annotation.plugin.BndPlugin)1 LifeCyclePlugin (aQute.bnd.service.lifecycle.LifeCyclePlugin)1 File (java.io.File)1 Formatter (java.util.Formatter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1