use of org.bndtools.templating.TemplateEngine in project bndtools by bndtools.
the class ReposTemplateLoader method findTemplates.
@Override
public Promise<List<Template>> findTemplates(String templateType, final Reporter reporter) {
String filterStr = String.format("(%s=%s)", NS_TEMPLATE, templateType);
final Requirement requirement = new CapReqBuilder(NS_TEMPLATE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filterStr).buildSyntheticRequirement();
// Try to get the repositories and BundleLocator from the workspace
List<Repository> workspaceRepos;
BundleLocator tmpLocator;
try {
if (workspace == null)
workspace = Central.getWorkspace();
workspaceRepos = workspace.getPlugins(Repository.class);
tmpLocator = new RepoPluginsBundleLocator(workspace.getRepositories());
} catch (Exception e) {
workspaceRepos = Collections.emptyList();
tmpLocator = new DirectDownloadBundleLocator();
}
final BundleLocator locator = tmpLocator;
// Setup the repos
List<Repository> repos = new ArrayList<>(workspaceRepos.size() + 1);
repos.addAll(workspaceRepos);
addPreferenceConfiguredRepos(repos, reporter);
// Generate a Promise<List<Template>> for each repository and add to an accumulator
Promise<List<Template>> accumulator = Promises.resolved((List<Template>) new LinkedList<Template>());
for (final Repository repo : repos) {
final Deferred<List<Template>> deferred = new Deferred<>();
final Promise<List<Template>> current = deferred.getPromise();
accumulator = accumulator.then(new Success<List<Template>, List<Template>>() {
@Override
public Promise<List<Template>> call(Promise<List<Template>> resolved) throws Exception {
final List<Template> prefix = resolved.getValue();
return current.map(new Function<List<Template>, List<Template>>() {
@Override
public List<Template> apply(List<Template> t) {
return CollectionUtils.append(prefix, t);
}
});
}
});
executor.submit(new Runnable() {
@Override
public void run() {
List<Template> templates = new LinkedList<>();
Map<Requirement, Collection<Capability>> providerMap = repo.findProviders(Collections.singleton(requirement));
if (providerMap != null) {
Collection<Capability> candidates = providerMap.get(requirement);
if (candidates != null) {
for (Capability cap : candidates) {
IdentityCapability idcap = ResourceUtils.getIdentityCapability(cap.getResource());
Object id = idcap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
Object ver = idcap.getAttributes().get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
try {
String engineName = (String) cap.getAttributes().get("engine");
if (engineName == null)
engineName = "stringtemplate";
TemplateEngine engine = engines.get(engineName);
if (engine != null)
templates.add(new CapabilityBasedTemplate(cap, locator, engine));
else
reporter.error("Error loading template from resource '%s' version %s: no Template Engine available matching '%s'", id, ver, engineName);
} catch (Exception e) {
reporter.error("Error loading template from resource '%s' version %s: %s", id, ver, e.getMessage());
}
}
}
}
deferred.resolve(templates);
}
});
}
return accumulator;
}
use of org.bndtools.templating.TemplateEngine in project bndtools by bndtools.
the class BuiltInTemplate method generateOutputs.
@Override
public ResourceMap generateOutputs(Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
BundleContext context = bundle.getBundleContext();
Collection<ServiceReference<TemplateEngine>> svcRefs = context.getServiceReferences(TemplateEngine.class, String.format("(name=%s)", engineName));
if (svcRefs == null || svcRefs.isEmpty())
throw new Exception(String.format("Unable to generate built-in template '%s': no Template Engine available matching '%s'", name, engineName));
ServiceReference<TemplateEngine> svcRef = svcRefs.iterator().next();
TemplateEngine engine = context.getService(svcRef);
if (engine == null)
throw new Exception(String.format("Unable to generate built-in template '%s': no Template Engine available matching '%s'", name, engineName));
try {
return engine.generateOutputs(inputResources, parameters, monitor);
} finally {
context.ungetService(svcRef);
}
}
Aggregations