@Override public <T> void registerAsService(Class<T> serviceClass, T serviceBean, VaadinApplicationContext applicationContext) { registerAsService(serviceClass, serviceBean, applicationContext, new Hashtable<String,Object>()); }
@Override public VaadinApplicationContext createApplicationContext(VaadinApplicationContextCreator creator) { VaadinApplicationContext newContext = creator.create(this); VaadinApplicationContext oldContext = getService(VaadinApplicationContext.class, newContext); if (oldContext != null) return oldContext; registerAsService(VaadinApplicationContext.class, newContext, newContext); return newContext; }
@Override public <T> void registerAsService(Class<T> serviceClass, T serviceBean, VaadinApplicationContext applicationContext, Dictionary<String,Object> properties) { if (serviceBean == null || serviceClass == null) return; ServiceRegistration<T> serviceRegistration = bundleContext.registerService(serviceClass, serviceBean, getProperties(applicationContext, properties)); serviceRegistrations.put(serviceBean, serviceRegistration); }
@Override public void bundleChanged(BundleEvent event) { if (event == null) return; if (BundleEvent.STARTING == event.getType() && shouldAutoExportOnmsServices(event.getBundle())) { autoExportDefaultServices(event.getBundle().getBundleContext()); } else if (BundleEvent.STOPPED == event.getType()) { // we do not need to remove any services the underlying OSGi-container should // have handled that for us } }
@Override public <T> T getService(Class<T> clazz, VaadinApplicationContext applicationContext) { List<T> services = getServices(clazz, applicationContext, new Hashtable<String,Object>()); if (services.isEmpty()) return null; return services.get(0); }
private String getFilter(VaadinApplicationContext applicationContext, Hashtable<String,Object> additionalProperties) { if (applicationContext == null && additionalProperties.isEmpty()) return null; String filter = "(&%s%s)"; String sessionFilter = "(sessionId=%s)(uiId=%s)"; String additionalPropertiesFilterString = getAdditionalPropertiesString(additionalProperties); String sessionFilterString = applicationContext == null ? "" : String.format(sessionFilter, applicationContext.getSessionId(), Integer.toString(applicationContext.getUiId())); return String.format(filter, sessionFilterString, additionalPropertiesFilterString); }
@Override public <T> List<T> getServices(Class<T> clazz, VaadinApplicationContext applicationContext, Hashtable<String,Object> additionalProperties) { if (additionalProperties == null) { additionalProperties = new Hashtable<String,Object>(); } List<T> services = new ArrayList<>(); try { Collection<ServiceReference<T>> serviceReferences = bundleContext.getServiceReferences(clazz, getFilter(applicationContext, additionalProperties)); if (serviceReferences != null) { for (ServiceReference<T> eachServiceReference : serviceReferences) { T service = bundleContext.getService(eachServiceReference); if (service == null) continue; services.add(service); } } } catch (InvalidSyntaxException e) { LOG.error("Invalid Filter definition in getService()", e); } return services; }
private void autoExportDefaultServices(BundleContext bundleContext) { final long bundleId = bundleContext.getBundle().getBundleId(); Log.info("Auto export of default Services for bundle (id: {}) enabled", bundleId); final OnmsServiceManager serviceManager = new OnmsServiceManagerImpl(bundleContext); Dictionary<String,Object> props = new Hashtable<String,Object>(); props.put("bundleId", bundleContext.getBundle().getBundleId()); Log.info("Registering OnmsServiceManager and SessionListener for bundle (id: {})", bundleId); bundleContext.registerService( new String[]{OnmsServiceManager.class.getName(), SessionListener.class.getName()}, serviceManager, props); Log.info("Registering EventRegistry for bundle (id: {})", bundleId); bundleContext.registerService(EventRegistry.class.getName(), new EventRegistry(bundleContext), props); }