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