|
38 | 38 | */ |
39 | 39 | @Component(immediate = true) |
40 | 40 | public class JavascriptModuleListener implements BundleListener { |
41 | | - private static final Logger logger = LoggerFactory.getLogger(JavascriptModuleListener.class); |
42 | | - private GraalVMEngine engine; |
43 | | - private final Queue<Registrar> registrars = new ConcurrentLinkedQueue<>(); |
44 | | - |
45 | | - @Reference(cardinality = ReferenceCardinality.MANDATORY) |
46 | | - public void setEngine(GraalVMEngine engine) { |
47 | | - this.engine = engine; |
48 | | - } |
49 | | - |
50 | | - @Reference(service = Registrar.class, policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY) |
51 | | - public void addRegistrar(Registrar registrar) { |
52 | | - for (Bundle bundle : getJavascriptModules()) { |
53 | | - registrar.register(bundle); |
54 | | - } |
55 | | - |
56 | | - registrars.add(registrar); |
57 | | - } |
58 | | - |
59 | | - public void removeRegistrar(Registrar registrar) { |
60 | | - registrars.remove(registrar); |
61 | | - |
62 | | - for (Bundle bundle : getJavascriptModules()) { |
63 | | - registrar.unregister(bundle); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - @Activate |
68 | | - public void activate(BundleContext context) { |
69 | | - for (Bundle bundle : getJavascriptModules()) { |
70 | | - engine.enableJavascriptModule(bundle); |
71 | | - } |
72 | | - |
73 | | - context.addBundleListener(this); |
74 | | - } |
75 | | - |
76 | | - @Deactivate |
77 | | - public void deactivate(BundleContext context) { |
78 | | - context.removeBundleListener(this); |
79 | | - |
80 | | - for (Bundle bundle : getJavascriptModules()) { |
81 | | - engine.disableJavascriptModule(bundle); |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - @Override |
86 | | - public void bundleChanged(BundleEvent event) { |
87 | | - try { |
88 | | - Bundle bundle = event.getBundle(); |
89 | | - if (isJavascriptModule(bundle)) { |
90 | | - if (event.getType() == BundleEvent.STARTED) { |
91 | | - engine.enableJavascriptModule(bundle); |
92 | | - for (Registrar registrar : registrars) { |
93 | | - registrar.register(bundle); |
94 | | - } |
95 | | - } else if (event.getType() == BundleEvent.STOPPED) { |
96 | | - for (Registrar registrar : registrars) { |
97 | | - registrar.unregister(bundle); |
98 | | - } |
99 | | - engine.disableJavascriptModule(bundle); |
100 | | - } |
101 | | - } |
102 | | - } catch (Exception e) { |
103 | | - logger.error("Cannot handle event {}", event.toString(), e); |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - public List<Bundle> getJavascriptModules() { |
108 | | - return Arrays.stream(engine.getBundleContext().getBundles()) |
109 | | - .filter(bundle -> bundle.getState() == Bundle.ACTIVE && isJavascriptModule(bundle)) |
110 | | - .collect(Collectors.toList()); |
111 | | - } |
112 | | - |
113 | | - public boolean isJavascriptModule(Bundle bundle) { |
114 | | - return bundle.getBundleId() != engine.getBundleContext().getBundle().getBundleId() && |
115 | | - bundle.getHeaders().get(BUNDLE_HEADER_JAVASCRIPT_INIT_SCRIPT) != null; |
116 | | - } |
| 41 | + private static final Logger logger = LoggerFactory.getLogger(JavascriptModuleListener.class); |
| 42 | + private GraalVMEngine engine; |
| 43 | + private final Queue<Registrar> registrars = new ConcurrentLinkedQueue<>(); |
| 44 | + |
| 45 | + @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 46 | + public void setEngine(GraalVMEngine engine) { |
| 47 | + this.engine = engine; |
| 48 | + } |
| 49 | + |
| 50 | + @Reference(service = Registrar.class, policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY) |
| 51 | + public void addRegistrar(Registrar registrar) { |
| 52 | + for (Bundle bundle : getJavascriptModules()) { |
| 53 | + registrar.register(bundle); |
| 54 | + } |
| 55 | + |
| 56 | + registrars.add(registrar); |
| 57 | + } |
| 58 | + |
| 59 | + public void removeRegistrar(Registrar registrar) { |
| 60 | + registrars.remove(registrar); |
| 61 | + |
| 62 | + for (Bundle bundle : getJavascriptModules()) { |
| 63 | + registrar.unregister(bundle); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Activate |
| 68 | + public void activate(BundleContext context) { |
| 69 | + for (Bundle bundle : getJavascriptModules()) { |
| 70 | + engine.enableJavascriptModule(bundle); |
| 71 | + } |
| 72 | + |
| 73 | + context.addBundleListener(this); |
| 74 | + } |
| 75 | + |
| 76 | + @Deactivate |
| 77 | + public void deactivate(BundleContext context) { |
| 78 | + context.removeBundleListener(this); |
| 79 | + |
| 80 | + for (Bundle bundle : getJavascriptModules()) { |
| 81 | + engine.disableJavascriptModule(bundle); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void bundleChanged(BundleEvent event) { |
| 87 | + try { |
| 88 | + Bundle bundle = event.getBundle(); |
| 89 | + if (isJavascriptModule(bundle)) { |
| 90 | + if (event.getType() == BundleEvent.STARTED) { |
| 91 | + engine.enableJavascriptModule(bundle); |
| 92 | + for (Registrar registrar : registrars) { |
| 93 | + registrar.register(bundle); |
| 94 | + } |
| 95 | + } else if (event.getType() == BundleEvent.STOPPED) { |
| 96 | + for (Registrar registrar : registrars) { |
| 97 | + registrar.unregister(bundle); |
| 98 | + } |
| 99 | + engine.disableJavascriptModule(bundle); |
| 100 | + } |
| 101 | + } |
| 102 | + } catch (Exception e) { |
| 103 | + logger.error("Cannot handle event {}", event.toString(), e); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public List<Bundle> getJavascriptModules() { |
| 108 | + return Arrays.stream(engine.getBundleContext().getBundles()) |
| 109 | + .filter(bundle -> bundle.getState() == Bundle.ACTIVE && isJavascriptModule(bundle)) |
| 110 | + .collect(Collectors.toList()); |
| 111 | + } |
| 112 | + |
| 113 | + public boolean isJavascriptModule(Bundle bundle) { |
| 114 | + return bundle.getBundleId() != engine.getBundleContext().getBundle().getBundleId() && |
| 115 | + bundle.getHeaders().get(BUNDLE_HEADER_JAVASCRIPT_INIT_SCRIPT) != null; |
| 116 | + } |
117 | 117 | } |
0 commit comments