Calculator

The calculator example demonstrates the most basic concepts of Gaderian; the difference between <create-instance> and <invoke-factory>, the fact that services are, by default, created only as needed, and the ability of gaderian.BuilderFactory to automatically wire services together. It also demonstrates the behavior of the gaderian.LoggingInterceptor.

The logging configuration enables logging for the gaderian logger; that and the logging interceptors produces quite a bit of output. You can see that a proxy is created for services initially, and that the "core service implementation" for the service is created later ... the core service implementation consists of an instance of the service's POJO class, wrapped with any interceptors (the logging interceptor, in this case).

The Registry is built from the following module deployment descriptor:

The service-point for the Calculator service is very simple ... as the comment indicates, the BuilderFactory is capable of locating the other services (Adder, Subtracter, etc.) by their interface, rather than requiring set-service elements to connect properites to services (using the target service's ids). These properties of the Calculator implementation are autowired to the matching services. Autowriring works only because just a single service within the entire Registry implements the specific interface. You would see errors if no service implemented the interface, or if more than one did.

This module descriptor also demonstrates two new features of Gaderian:

  • A package name is provided in the <module> element, allowing class names to be abbreviated when referenced elsewhere in the descriptor.
  • The interface attribute of <service-point> is omitted, because it defaults to the service point id.

Running the examples

After compiling the examples, you can use Ant to run them:

bash-2.05b$ ant run-calculator
Buildfile: build.xml

run-calculator:
     [java] Calculator [DEBUG] Creating SingletonProxy for service examples.Calculator
     [java] Inputs:   28.0 and 4.75
     [java] Calculator [DEBUG] Constructing core service implementation for service examples.Calculator
     [java] Subtracter [DEBUG] Creating SingletonProxy for service examples.Subtracter
     [java] Calculator [DEBUG] Autowired service property subtracter to <SingletonProxy for examples.Subtracter(org.ops4j.gaderian.examples.Subtracter)>
     [java] Divider [DEBUG] Creating SingletonProxy for service examples.Divider
     [java] Calculator [DEBUG] Autowired service property divider to <SingletonProxy for org.ops4j.gaderian.examples.Divider)>
     [java] Multiplier [DEBUG] Creating SingletonProxy for service examples.Multiplier
     [java] Calculator [DEBUG] Autowired service property multiplier to <SingletonProxy for org.ops4j.gaderian.examples.Multiplier)>
     [java] Adder [DEBUG] Creating SingletonProxy for service examples.Adder
     [java] Calculator [DEBUG] Autowired service property adder to <SingletonProxy for org.ops4j.gaderian.examples.Adder)>
     [java] Calculator [DEBUG] Applying interceptor factory gaderian.LoggingInterceptor
     [java] Calculator [DEBUG] BEGIN add(28.0, 4.75)
     [java] Adder [DEBUG] Constructing core service implementation for service examples.Adder
     [java] Adder [DEBUG] Applying interceptor factory gaderian.LoggingInterceptor
     [java] Adder [DEBUG] BEGIN add(28.0, 4.75)
     [java] Adder [DEBUG] END add() [32.75]
     [java] Calculator [DEBUG] END add() [32.75]
     [java] Add:      32.75
     [java] Calculator [DEBUG] BEGIN subtract(28.0, 4.75)
     [java] Subtracter [DEBUG] Constructing core service implementation for service examples.Subtracter
     [java] Subtracter [DEBUG] Applying interceptor factory gaderian.LoggingInterceptor
     [java] Subtracter [DEBUG] BEGIN subtract(28.0, 4.75)
     [java] Subtracter [DEBUG] END subtract() [23.25]
     [java] Calculator [DEBUG] END subtract() [23.25]
     [java] Subtract: 23.25
     [java] Calculator [DEBUG] BEGIN multiply(28.0, 4.75)
     [java] Multiplier [DEBUG] Constructing core service implementation for service examples.Multiplier
     [java] Multiplier [DEBUG] Applying interceptor factory gaderian.LoggingInterceptor
     [java] Multiplier [DEBUG] BEGIN multiply(28.0, 4.75)
     [java] Multiplier [DEBUG] END multiply() [133.0]
     [java] Calculator [DEBUG] END multiply() [133.0]
     [java] Multiply: 133.0
     [java] Calculator [DEBUG] BEGIN divide(28.0, 4.75)
     [java] Divider [DEBUG] Constructing core service implementation for service examples.Divider
     [java] Divider [DEBUG] Applying interceptor factory gaderian.LoggingInterceptor
     [java] Divider [DEBUG] BEGIN divide(28.0, 4.75)
     [java] Divider [DEBUG] END divide() [5.894736842105263]
     [java] Calculator [DEBUG] END divide() [5.894736842105263]
     [java] Divide:   5.894736842105263

BUILD SUCCESSFUL
Total time: 3 seconds