1 package org.simantics.scl.osgi.internal;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import org.osgi.framework.BundleContext;
7 import org.osgi.util.tracker.ServiceTracker;
8 import org.simantics.scl.compiler.module.repository.UpdateListener;
9 import org.simantics.scl.compiler.source.ModuleSource;
10 import org.simantics.scl.compiler.source.repository.ModuleSourceRepository;
12 import gnu.trove.procedure.TObjectProcedure;
14 public class ServiceBasedModuleSourceRepository implements ModuleSourceRepository {
16 ServiceTracker<ModuleSourceRepository, ModuleSourceRepository> sourceRepositories;
18 public ServiceBasedModuleSourceRepository(BundleContext context) {
19 sourceRepositories = new ServiceTracker<ModuleSourceRepository, ModuleSourceRepository>(
20 context, ModuleSourceRepository.class, null);
21 sourceRepositories.open();
25 public ModuleSource getModuleSource(String moduleName,
26 UpdateListener listener) {
27 ModuleSource result = null;
28 Object[] services = sourceRepositories.getServices();
30 for(Object sourceRepository_ : services) {
31 ModuleSourceRepository sourceLoader = (ModuleSourceRepository)sourceRepository_;
32 ModuleSource source = sourceLoader.getModuleSource(moduleName, listener);
35 double resultPriority = result.getPriority();
36 double sourcePriority = source.getPriority();
37 if(resultPriority > sourcePriority)
39 if(resultPriority == sourcePriority)
40 throw new RuntimeException("Module " + moduleName + " has two sources " + result + ", " +
41 source + " with the same priority.");
50 public String getDocumentation(String documentationName) {
51 // getServices is internally synchronized, so no need to synchronize here
52 Object[] services = sourceRepositories.getServices();
54 for(Object sourceRepository_ : services) {
55 ModuleSourceRepository sourceLoader = (ModuleSourceRepository)sourceRepository_;
56 String documentation = sourceLoader.getDocumentation(documentationName);
57 if(documentation != null)
64 public void forAllModules(TObjectProcedure<String> procedure) {
65 // getServices is internally synchronized, so no need to synchronize here
66 Object[] services = sourceRepositories.getServices();
68 for(Object sourceRepository_ : services) {
69 ModuleSourceRepository sourceLoader = (ModuleSourceRepository)sourceRepository_;
70 sourceLoader.forAllModules(procedure);
75 public Collection<String> getModuleNames() {
76 ArrayList<String> result = new ArrayList<>();
77 forAllModules((String name) -> {
85 public void forAllDocumentations(TObjectProcedure<String> procedure) {
86 Object[] services = sourceRepositories.getServices();
88 for(Object sourceRepository_ : services) {
89 ModuleSourceRepository sourceLoader = (ModuleSourceRepository)sourceRepository_;
90 sourceLoader.forAllDocumentations(procedure);
95 public Collection<String> getDocumentationNames() {
96 ArrayList<String> result = new ArrayList<>();
97 forAllDocumentations((String name) -> {
105 public void checkUpdates() {
106 for(Object service_ : sourceRepositories.getServices()) {
107 ModuleSourceRepository service = (ModuleSourceRepository)service_;
108 service.checkUpdates();