DPML
Service Lookup
HomeUtilitiesStationMetro
Service Lookup

The ability of a component to be container introduces the possibility for the construction of arbitarially complex heirachies of components that make up an application solution. In practive we need a mechanisms through which one component can declare a dependency on another component. Unlike our earlier context example that have used new values exclusively, a service lookup enables an enclosing component to mediate a request for a service. Typically the enclosing component will evaluate its internal components and return a reference as required. If no internal components can fulfill the service request the mediation is handed-off to the mediating components parent.

Component Example

tutorial/components/lookup:

Demo.java The demo class updated to use a Gizmo service to locate a Widget.
Gizmo.java The Gizmo class including the Wdget dependency declared in its context.
Widget.java The Widget class.

The important update is presented in the our project defintion. Our demo component part has been updated to include two nested components (widget and gizmo). In this example we have associated a reference to the Widget service in the definition of a gizmo component context via a lookup entry.

component definition::

<component xmlns="dpml:metro" class="org.acme.Demo" name="demo">
  <parts>
    <component key="widget" type="org.acme.Widget" name="widget">
      <context>
        <entry key="color" method="BLUE"/>
      </context>
    </component>
    <component key="gizmo" type="org.acme.Gizmo" name="gizmo">
      <context>
        <entry key="widget" lookup="org.acme.Widget"/>
      </context>
    </component>
  </parts>
</component>
Testing the component

The following debug level logging for the demo category provides us with a trace of the internal container activity during the establishment of our demo component.

test:
    [junit] Executing forked test.
    [junit] Running org.acme.test.DemoTestCase
    [junit] [13031] [FINE   ] (demo): established per-thread lifestyle handler for [org.acme.Demo]
    [junit] [13031] [FINE   ] (demo): building internal parts
    [junit] [13031] [FINE   ] (demo.widget): established transient lifestyle handler for [org.acme.Widget]
    [junit] [13031] [FINE   ] (demo.gizmo): established per-thread lifestyle handler for [org.acme.Gizmo]
    [junit] [13031] [FINE   ] (demo): commissioning internal parts
    [junit] [13031] [FINE   ] (demo.gizmo): instantiated [27165481 ]
    [junit] [13031] [FINE   ] (demo.gizmo): activated [27165481 ]
    [junit] [13031] [FINE   ] (demo): mediating lookup for [org.acme.Widget]
    [junit] [13031] [FINE   ] (demo.widget): instantiated [849515   ]
    [junit] [13031] [FINE   ] (demo.widget): activated [849515   ]
    [junit] [13031] [INFO   ] (demo): located the color java.awt.Color[r=0,g=0,b=255]
    [junit] [13031] [FINE   ] (demo): instantiated [23438274 ]
    [junit] [13031] [FINE   ] (demo): activated [23438274 ]
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.937 sec
Summary

In this tutorial we have updated the Demo component to include two subsidiary components - Widget and Gizmo. We have declare a context dependency in Gizmo on a Widget service. The main purpose is to demonstrate the lookup mechanism used on the Gizmo component definition to resolve a Widget reference.