- super(id);
- this.interfaceName = interfaceName;
- }
-
- @SuppressWarnings({ "checkstyle:IllegalCatch", "unchecked" })
- @Override
- public void init(final ExtendedBlueprintContainer container) {
- super.init(container);
-
- final Class<?> interfaceClass;
- try {
- interfaceClass = container().getBundleContext().getBundle().loadClass(interfaceName);
- } catch (Exception e) {
- throw new ComponentDefinitionException(String.format("%s: Error obtaining interface class %s",
- logName(), interfaceName), e);
- }
-
- if (!RpcService.class.isAssignableFrom(interfaceClass)) {
- throw new ComponentDefinitionException(String.format(
- "%s: The specified interface %s is not an RpcService", logName(), interfaceName));
- }
-
- rpcInterface = (Class<RpcService>)interfaceClass;
- }
-
- @Override
- protected void startTracking() {
- // First get the SchemaContext. This will be used to get the RPC SchemaPaths.
-
- retrieveService("SchemaService", SchemaService.class,
- service -> retrievedSchemaContext(((SchemaService)service).getGlobalContext()));
- }
-
- private void retrievedSchemaContext(final SchemaContext schemaContext) {
- LOG.debug("{}: retrievedSchemaContext", logName());
-
- QNameModule moduleName = BindingReflections.getQNameModule(rpcInterface);
- Module module = schemaContext.findModuleByNamespaceAndRevision(moduleName.getNamespace(),
- moduleName.getRevision());
-
- LOG.debug("{}: Got Module: {}", logName(), module);
-
- rpcSchemaPaths = new HashSet<>();
- for (RpcDefinition rpcDef : module.getRpcs()) {
- rpcSchemaPaths.add(rpcDef.getPath());
- }
-
- LOG.debug("{}: Got SchemaPaths: {}", logName(), rpcSchemaPaths);
-
- // First get the DOMRpcService OSGi service. This will be used to register a listener to be notified
- // when the underlying DOM RPC service is available.
-
- retrieveService("DOMRpcService", DOMRpcService.class,
- service -> retrievedDOMRpcService((DOMRpcService)service));
- }
-
- private void retrievedDOMRpcService(final DOMRpcService domRpcService) {
- LOG.debug("{}: retrievedDOMRpcService", logName());
-
- setDependendencyDesc("Available DOM RPC for binding RPC: " + rpcInterface);
- rpcListenerReg = domRpcService.registerRpcListener(new DOMRpcAvailabilityListener() {
- @Override
- public void onRpcAvailable(final Collection<DOMRpcIdentifier> rpcs) {
- onRpcsAvailable(rpcs);
- }
-
- @Override
- public void onRpcUnavailable(final Collection<DOMRpcIdentifier> rpcs) {
- }
- });
- }
-
- protected void onRpcsAvailable(final Collection<DOMRpcIdentifier> rpcs) {
- for (DOMRpcIdentifier identifier : rpcs) {
- if (rpcSchemaPaths.contains(identifier.getType())) {
- LOG.debug("{}: onRpcsAvailable - found SchemaPath {}", logName(), identifier.getType());
-
- retrieveService("RpcProviderRegistry", RpcProviderRegistry.class, service -> {
- rpcRegistry = (RpcProviderRegistry)service;
- setSatisfied();
- });
-
- break;
- }
- }
- }
-
- @SuppressWarnings("checkstyle:IllegalCatch")
- @Override
- public Object create() throws ComponentDefinitionException {
- LOG.debug("{}: In create: interfaceName: {}", logName(), interfaceName);
-
- super.onCreate();
-
- try {
- RpcService rpcService = rpcRegistry.getRpcService(rpcInterface);
-
- LOG.debug("{}: create returning service {}", logName(), rpcService);
-
- return rpcService;
- } catch (RuntimeException e) {
- throw new ComponentDefinitionException("Error getting RPC service for " + interfaceName, e);
- }
- }
-
- @Override
- public void stopTracking() {
- super.stopTracking();
- closeRpcListenerReg();
- }
-
- private void closeRpcListenerReg() {
- if (rpcListenerReg != null) {
- rpcListenerReg.close();
- rpcListenerReg = null;
- }
- }
-
- @Override
- public void destroy(final Object instance) {
- super.destroy(instance);
- closeRpcListenerReg();