use of org.osgi.util.promise.PromiseFactory 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);
// Map List<Repository> to Promise<List<Template>>
Promise<List<Template>> promise = repos.stream().map(repo -> promiseFactory.submit(() -> {
Map<Requirement, Collection<Capability>> providerMap = repo.findProviders(Collections.singleton(requirement));
return providerMap.get(requirement).stream().map(cap -> {
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)
return new CapabilityBasedTemplate(cap, locator, engine);
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());
}
return null;
}).filter(Objects::nonNull).collect(toList());
})).collect(toPromise(promiseFactory)).map(ll -> ll.stream().flatMap(List::stream).collect(toList()));
return promise;
}
use of org.osgi.util.promise.PromiseFactory in project bndtools by bndtools.
the class ReposTemplateLoader method activate.
@Activate
void activate() {
if (promiseFactory == null) {
localExecutor = Executors.newCachedThreadPool();
promiseFactory = new PromiseFactory(localExecutor);
}
}
use of org.osgi.util.promise.PromiseFactory in project bndtools by bndtools.
the class ExtensionRegistryTemplateLoader method activate.
@Activate
void activate() {
typeToExtPoint.put("project", "projectTemplates");
typeToExtPoint.put("workspace", "workspaceTemplates");
if (promiseFactory == null) {
localExecutor = Executors.newCachedThreadPool();
promiseFactory = new PromiseFactory(localExecutor);
}
}
use of org.osgi.util.promise.PromiseFactory in project bndtools by bndtools.
the class GitHubWorkspaceTemplateLoader method findTemplates.
@Override
public Promise<List<Template>> findTemplates(String type, Reporter reporter) {
if (!TEMPLATE_TYPE.equals(type)) {
return promiseFactory.resolved(Collections.emptyList());
}
GitHub gitHub = new GitHub(cache, promiseFactory);
Parameters githubRepos = new GitRepoPreferences().getGithubRepos();
return githubRepos.entrySet().stream().map(entry -> {
String repo = GitRepoPreferences.removeDuplicateMarker(entry.getKey());
Attrs attribs = entry.getValue();
return gitHub.loadRepoDetails(repo).map(detailsDTO -> {
if (detailsDTO.clone_url == null)
throw new IllegalArgumentException("Missing clone URL");
// Generate icon URI from the owner avatar. The s=16 parameter
// is added to select a 16x16 icon.
URI avatarUri = null;
if (detailsDTO.owner.avatar_url != null)
avatarUri = URI.create(detailsDTO.owner.avatar_url + "&s=16");
String name = attribs.get("name");
if (name == null)
name = repo;
String branch = attribs.get("branch");
final GitCloneTemplateParams params = new GitCloneTemplateParams();
params.cloneUrl = detailsDTO.clone_url;
if (branch != null)
params.branch = branch;
else
params.branch = "origin/" + detailsDTO.default_branch;
params.name = name;
params.category = "GitHub";
params.iconUri = avatarUri;
if (detailsDTO.html_url != null) {
params.helpUri = createHelpUri(repo, detailsDTO.html_url);
}
return (Template) new GitCloneTemplate(params);
});
}).collect(toPromise(promiseFactory));
}
use of org.osgi.util.promise.PromiseFactory in project bndtools by bndtools.
the class PlainGitWorkspaceTemplateLoader method activate.
@Activate
void activate(BundleContext context) {
iconUrl = context.getBundle().getEntry("icons/git-16px.png");
if (promiseFactory == null) {
localExecutor = Executors.newCachedThreadPool();
promiseFactory = new PromiseFactory(localExecutor);
}
}
Aggregations