def __try_instantiate(self, component_context, instance): # type: (ComponentContext, object) -> bool """ Instantiates a component, if all of its handlers are there. Returns False if a handler is missing. :param component_context: A ComponentContext bean :param instance: The component instance :return: True if the component has started, False if a handler is missing """ with self.__instances_lock: # Extract information about the component factory_context = component_context.factory_context handlers_ids = factory_context.get_handlers_ids() name = component_context.name factory_name = factory_context.name try: # Get handlers handler_factories = self.__get_handler_factories(handlers_ids) except KeyError: # A handler is missing, stop here return False # Instantiate the handlers all_handlers = set() # type: Set[Any] for handler_factory in handler_factories: handlers = handler_factory.get_handlers( component_context, instance ) if handlers: all_handlers.update(handlers) # Prepare the stored instance stored_instance = StoredInstance( self, component_context, instance, all_handlers ) # Manipulate the properties for handler in all_handlers: handler.manipulate(stored_instance, instance) # Store the instance self.__instances[name] = stored_instance # Start the manager stored_instance.start() # Notify listeners now that every thing is ready to run self._fire_ipopo_event( constants.IPopoEvent.INSTANTIATED, factory_name, name ) # Try to validate it stored_instance.update_bindings() stored_instance.check_lifecycle() return True
def __try_instantiate(self, component_context, instance): # type: (ComponentContext, object) -> bool """ Instantiates a component, if all of its handlers are there. Returns False if a handler is missing. :param component_context: A ComponentContext bean :param instance: The component instance :return: True if the component has started, False if a handler is missing """ with self.__instances_lock: # Extract information about the component factory_context = component_context.factory_context handlers_ids = factory_context.get_handlers_ids() name = component_context.name factory_name = factory_context.name try: # Get handlers handler_factories = self.__get_handler_factories(handlers_ids) except KeyError: # A handler is missing, stop here return False # Instantiate the handlers all_handlers = set() # type: Set[Any] for handler_factory in handler_factories: handlers = handler_factory.get_handlers( component_context, instance) if handlers: all_handlers.update(handlers) # Prepare the stored instance stored_instance = StoredInstance(self, component_context, instance, all_handlers) # Manipulate the properties for handler in all_handlers: handler.manipulate(stored_instance, instance) # Store the instance self.__instances[name] = stored_instance # Start the manager stored_instance.start() # Notify listeners now that every thing is ready to run self._fire_ipopo_event(constants.IPopoEvent.INSTANTIATED, factory_name, name) # Try to validate it stored_instance.update_bindings() stored_instance.check_lifecycle() return True
def instantiate(self, factory_name, name, properties=None): """ Instantiates a component from the given factory, with the given name :param factory_name: Name of the component factory :param name: Name of the instance to be started :return: The component instance :raise TypeError: The given factory is unknown :raise ValueError: The given name or factory name is invalid, or an instance with the given name already exists :raise Exception: Something wrong occurred in the factory """ # Test parameters if not factory_name or not is_string(factory_name): raise ValueError("Invalid factory name") if not name or not is_string(name): raise ValueError("Invalid component name") if not self.running: # Stop working if the framework is stopping raise ValueError("Framework is stopping") with self.__instances_lock: if name in self.__instances: raise ValueError("'{0}' is an already running instance name" \ .format(name)) with self.__factories_lock: # Can raise a ValueError exception factory = self.__factories.get(factory_name, None) if factory is None: raise TypeError("Unknown factory '{0}'" \ .format(factory_name)) # Get the factory context factory_context = getattr(factory, \ constants.IPOPO_FACTORY_CONTEXT, None) if factory_context is None: raise TypeError("Factory context missing in '{0}'" \ .format(factory_name)) try: # Look for the required handlers handler_factories = set() for handler_id in factory_context.get_handlers_ids(): # Not a 'set-comprehension': handler_id must be visible handler_factories.add(self._handlers[handler_id]) except KeyError: raise TypeError("Missing handler '{0}' for factory '{1}', " "component '{2}'" \ .format(handler_id, factory_name, name)) # Create component instance try: instance = factory() except: _logger.exception("Error creating the instance '%s' " \ "from factory '%s'", name, factory_name) raise TypeError("Factory '{0}' failed to create '{1}'" \ .format(factory_name, name)) # Normalize the given properties properties = self._prepare_instance_properties(properties, factory_context.properties) # Set up the component instance context component_context = ComponentContext(factory_context, name, \ properties) # Instantiate the handlers all_handlers = set() for handler_factory in handler_factories: handlers = handler_factory.get_handlers(component_context, instance) if handlers: all_handlers.update(handlers) # Prepare the stored instance stored_instance = StoredInstance(self, component_context, instance, all_handlers) # Manipulate the properties for handler in all_handlers: handler.manipulate(stored_instance, instance) # Store the instance self.__instances[name] = stored_instance # Start the manager stored_instance.start() # Notify listeners now that every thing is ready to run self._fire_ipopo_event(constants.IPopoEvent.INSTANTIATED, factory_name, name) # Try to validate it stored_instance.update_bindings() stored_instance.check_lifecycle() return instance