<?xml version="1.0"?>
<!-- 
   Copyright 2004, 2005 The Apache Software Foundation

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE document PUBLIC "-//HM//DTD Documentation V1.0//EN" "hm-document.dtd"
  
[
    <!ENTITY projectroot '../'>
    <!ENTITY % common-links SYSTEM "../links.ent">
    %common-links;
]>
<document>

  <header>
    <title>Calculator</title>
    </header>
    
  <body>

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

<p>
The logging configuration enables logging for the <code>hivemind</code> logger; that and the logging interceptors produces
quite a bit of output. You can see that a <em>proxy</em> 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).
</p>

<p>
The Registry is built from the following module deployment descriptor:
</p>
<xmlsourcefile file="examples/src/descriptor/META-INF/examples.xml" />

<p>
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 
<em>interface</em>, rather than requiring <code>set-service</code> elements to connect properites 
to services (using the target service's ids). These <em>properties</em> of the Calculator <em>implementation</em>
are <em>autowired</em> to the matching services.  Autowriring works only because just a single service within the
<em>entire</em> Registry implements the specific interface.  You would see errors if no service implemented
the interface, or if more than one did.
</p>    

<p>
This module descriptor also demonstrates two new feature of HiveMind 1.1:
</p>

<ul>
<li>A package name is provided
in the &module; element, allowing class names to be abbreviated when referenced elsewhere in the descriptor.</li>
<li>The interface attribute of &service-point; is omitted, because it defaults to the service point id.</li>
</ul>
<section>
  <title>Running the examples</title>
<p>
After compiling the examples, you can use Ant to run them:
</p>

<source><![CDATA[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.apache.hivemind.examples.Subtracter)>
     [java] Divider [DEBUG] Creating SingletonProxy for service examples.Divider
     [java] Calculator [DEBUG] Autowired service property divider to <SingletonProxy for examples.Divider(org.apache.hivemind.examples.Divider)>
     [java] Multiplier [DEBUG] Creating SingletonProxy for service examples.Multiplier
     [java] Calculator [DEBUG] Autowired service property multiplier to <SingletonProxy for examples.Multiplier(org.apache.hivemind.examples.Multiplier)>
     [java] Adder [DEBUG] Creating SingletonProxy for service examples.Adder
     [java] Calculator [DEBUG] Autowired service property adder to <SingletonProxy for examples.Adder(org.apache.hivemind.examples.Adder)>
     [java] Calculator [DEBUG] Applying interceptor factory hivemind.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 hivemind.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 hivemind.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 hivemind.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 hivemind.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]]></source>
</section>
  </body>
</document>
