Child pages
  • NewIn2JavaDefinitionAPI
Skip to end of metadata
Go to start of metadata

Java Registry Definition API

The Registry Definition API is a collection of interfaces for the definition of the constituents of a registry. This includes modules, extension points (services and configurations),
extensions (implementation, contributions, interceptors etc.) and their relationships.

The hivemind-framework library includes a default implementation of these interfaces.

The following example shows how two services (Calculator and Adder) are defined and wired:

public class JavaDefinitionExample
{
    public static void main(String[] args)
    {
        JavaDefinitionExample example = new JavaDefinitionExample();
        Registry registry = example.constructRegistry();
        
        Calculator calculator = (Calculator) registry.getService(Calculator.class);
        double result = calculator.add(10, 20);
        System.out.println("Result: " + result);
    }
    
    private Registry constructRegistry()
    {
        RegistryDefinition registryDefinition = new RegistryDefinitionImpl();
        ModuleDefinition module = defineModule(registryDefinition);
        registryDefinition.addModule(module);
        
        RegistryBuilder builder = new RegistryBuilder(registryDefinition);
        Registry registry = builder.constructRegistry();
        return registry;
    }

    private ModuleDefinition defineModule(RegistryDefinition registryDefinition)
    {
        ModuleDefinitionImpl module = new ModuleDefinitionImpl("calculator", createLocation());
         
        // Define the calculator service
        ServicePointDefinitionImpl calculatorService = new ServicePointDefinitionImpl(module, 
                "Calculator", null, Visibility.PUBLIC, Calculator.class.getName());

        // Define inline implementation constructor, that wires the Adder service
        ImplementationConstructor calculatorConstructor = new AbstractServiceImplementationConstructor(createLocation())
        {
            public Object constructCoreServiceImplementation(ImplementationConstructionContext context)
            {
                CalculatorImpl result = new CalculatorImpl();
                result.setAdder((Adder) context.getService(Adder.class));
                return result;
            }
        };
        ImplementationDefinition calculatorImplementation = new ImplementationDefinitionImpl(module, null, 
                calculatorConstructor, ServiceModel.PRIMITIVE, true);
        calculatorService.addImplementation(calculatorImplementation);
        module.addServicePoint(calculatorService);
        
        // Define the adder service
        ServicePointDefinitionImpl adderService = new ServicePointDefinitionImpl(module, 
                "Adder", null, Visibility.PUBLIC, Adder.class.getName());

        ImplementationDefinition adderImplementation = new ImplementationDefinitionImpl(module, null, 
                new CreateClassServiceConstructor(createLocation(), AdderImpl.class.getName()), 
                ServiceModel.PRIMITIVE, true);
        adderService.addImplementation(adderImplementation);
        module.addServicePoint(adderService);
        
        return module;
    }
    
}

In comparison to the XML or annotation based definition this is quite verbose, but it is the most versatile technique.

A main concept of the definition API is the use of constructor callbacks. The service implementation CalculatorImpl
of the Calculator service is constructed by a call to an implementation of the ImplementationConstructor interface.
The use of callbacks allows to realize lazy instantiation and service models (POOLED, THREADED) which require the
construction of multiple service instances.

The core services of the hivemind-framework library are defined by this API too. Have a look at CoreServicesProvider to see more example code.

  • No labels