1 package org.simantics.scl.osgi.internal;
3 import gnu.trove.procedure.TObjectProcedure;
5 import org.osgi.framework.BundleContext;
6 import org.osgi.util.tracker.ServiceTracker;
7 import org.simantics.scl.compiler.module.repository.UpdateListener;
8 import org.simantics.scl.compiler.source.ModuleSource;
9 import org.simantics.scl.compiler.source.repository.ModuleSourceRepository;
11 public class ServiceBasedModuleSourceRepository implements ModuleSourceRepository {
13 ServiceTracker<ModuleSourceRepository, ModuleSourceRepository> sourceRepositories;
15 public ServiceBasedModuleSourceRepository(BundleContext context) {
16 sourceRepositories = new ServiceTracker<ModuleSourceRepository, ModuleSourceRepository>(
17 context, ModuleSourceRepository.class, null);
18 sourceRepositories.open();
22 public ModuleSource getModuleSource(String moduleName,
23 UpdateListener listener) {
24 ModuleSource result = null;
25 Object[] services = sourceRepositories.getServices();
27 for(Object sourceRepository_ : services) {
28 ModuleSourceRepository sourceLoader = (ModuleSourceRepository)sourceRepository_;
29 ModuleSource source = sourceLoader.getModuleSource(moduleName, listener);
32 double resultPriority = result.getPriority();
33 double sourcePriority = source.getPriority();
34 if(resultPriority > sourcePriority)
36 if(resultPriority == sourcePriority)
37 throw new RuntimeException("Module " + moduleName + " has two sources " + result + ", " +
38 source + " with the same priority.");
47 public String getDocumentation(String documentationName) {
48 Object[] services = sourceRepositories.getServices();
50 for(Object sourceRepository_ : services) {
51 ModuleSourceRepository sourceLoader = (ModuleSourceRepository)sourceRepository_;
52 String documentation = sourceLoader.getDocumentation(documentationName);
53 if(documentation != null)
60 public void forAllModules(TObjectProcedure<String> procedure) {
61 for(Object sourceRepository_ : sourceRepositories.getServices()) {
62 ModuleSourceRepository sourceLoader = (ModuleSourceRepository)sourceRepository_;
63 sourceLoader.forAllModules(procedure);
68 public void forAllDocumentations(TObjectProcedure<String> procedure) {
69 for(Object sourceRepository_ : sourceRepositories.getServices()) {
70 ModuleSourceRepository sourceLoader = (ModuleSourceRepository)sourceRepository_;
71 sourceLoader.forAllDocumentations(procedure);
76 public void checkUpdates() {
77 for(Object service_ : sourceRepositories.getServices()) {
78 ModuleSourceRepository service = (ModuleSourceRepository)service_;
79 service.checkUpdates();