]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.osgi/src/org/simantics/scl/osgi/internal/BundleTestScriptRepository.java
SCL-compiler should activate installed bundles
[simantics/platform.git] / bundles / org.simantics.scl.osgi / src / org / simantics / scl / osgi / internal / BundleTestScriptRepository.java
1 package org.simantics.scl.osgi.internal;
2
3 import java.net.URL;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Enumeration;
7
8 import org.osgi.framework.Bundle;
9 import org.osgi.framework.BundleContext;
10 import org.osgi.framework.BundleEvent;
11 import org.osgi.service.component.ComponentContext;
12 import org.osgi.service.component.annotations.Activate;
13 import org.osgi.service.component.annotations.Component;
14 import org.osgi.service.component.annotations.Deactivate;
15 import org.osgi.util.tracker.BundleTracker;
16 import org.simantics.scl.compiler.testing.TestRunnable;
17 import org.simantics.scl.compiler.testing.repository.TestRepository;
18
19 import gnu.trove.map.hash.THashMap;
20
21 @Component
22 public class BundleTestScriptRepository implements TestRepository {
23     
24     Tracker tracker;
25     THashMap<String, BundleTestScriptRunnable> testsRunnables = new THashMap<String, BundleTestScriptRunnable>();
26     THashMap<Bundle, ArrayList<String>> testsPerBundle = new THashMap<Bundle, ArrayList<String>>();
27
28     @Activate
29     public void activate(ComponentContext context) {
30         tracker = new Tracker(context.getBundleContext());
31         tracker.open();
32     }
33     
34     @Deactivate
35     public void deactivate() {
36         tracker.close();
37     }
38     
39     class Tracker extends BundleTracker<Bundle> {
40         public Tracker(BundleContext context) {
41             super(context, 0xffffffff, null);
42         }
43         
44         @Override
45         synchronized public Bundle addingBundle(Bundle bundle, BundleEvent event) {
46             Enumeration<URL> urls = bundle.findEntries("sclTests", "*.sts", true);
47             if(urls != null) {
48                 ArrayList<String> modulesInThisBundle = new ArrayList<String>();
49                 while(urls.hasMoreElements()) {
50                     URL url = urls.nextElement();
51                     String path = url.getPath();
52                     String testName = path.substring(10, path.length()-4);
53                     testsRunnables.put(testName, new BundleTestScriptRunnable(testName, url));
54                     modulesInThisBundle.add(testName);
55                 }
56                 testsPerBundle.put(bundle, modulesInThisBundle);
57             }
58             return bundle;
59         }
60         
61         @Override
62         synchronized public void removedBundle(Bundle bundle, BundleEvent event,
63                 Bundle object) {
64             ArrayList<String> moduleList = testsPerBundle.get(bundle);
65             if(moduleList != null)
66                 for(String moduleName : moduleList)
67                     testsRunnables.remove(moduleName);
68         }
69     };
70     
71     @Override
72     public void collectTests(Collection<TestRunnable> tests) {
73         tests.addAll(testsRunnables.values());
74     }
75
76 }