The following code shows a component implementation looking up a
second component and calling its interface functions:
public
class ACompImpl extends
AbstractComponent implements AComp { public String foo() throws RemoteException { try { System.out.println("foo"); ClientContextI cc = getContext().getClientContext(); BComp bcomp = (BComp)cc.getComponent("ComponentB"); String bar = bcomp.bar(); return "foo" + bar; } catch(Exception x) { throw new ComponentException(x.getMessage()); } } } |
The code above performs a global lookup which can only be performed through a client context. A component can obtain a client context by calling getClientContext() on its own component context. Global lookups are simple to achieve because they do not require any particular configuration work. Every component just behaves like an external client, using the global component name as lookup identifier. However, global lookups have some important drawbacks as known from the EJB standard.
public
class ACompImpl extends
AbstractComponent implements AComp { public String foo() throws RemoteException { try { System.out.println("foo"); BComp bcomp = (BComp)getContext().getComponent("bref"); String bar = bcomp.bar(); return "foo" + bar; } catch(Exception x) { throw new ComponentException(x.getMessage()); } } } |
However, in this case the local lookup name "bref" must be declared
in the component's deployment descriptor and mapped to a globally
defined component, implementing the appropiate interface. The following
component deployment descriptor shows the reference declaration:
<component-jar> <component> <component-name>ComponentA</component-name> <external-interface>acomp.AComp</external-interface> <component-class>acomp.ACompImpl</component-class> <component-refs> <component-ref ref-name="bref" interface="bcomp.BComp" type="external"/> </component-refs> </component> </component-jar> |
The mapping to a concrete component is not standardized in the EJB
specification and is part of the vendor-specific extension descriptor
of a J2EE application. Using the CUBA component in the wired container
requires a declaration
like this in the wired-application.xml.
<?xml version="1.0"
encoding="UTF-8"?> <wired-application> <modules> <wired>compref.jar</wired> </modules> <references> </wired-application> |
As most applications contain only a single implementation for a component interface, the wired container provides an auto-resolvement for convenience purposes.
<?xml version="1.0"
encoding="UTF-8"?> <wired-application> <modules> <wired>compref.jar</wired> </modules> <references
resolve="auto" /> |
This will cause the wired container to link any component reference
to the one-and-only matching provider component for the requested
interface. Auto-resolvement is only performed for those references
which are not resolved explicitely, i.e. manual and automatic
resolvement can also be mixed. A value
of "auto" takes all provider components in account, which either
provide exactly the required interface or a derivation of it. The value
"auto-exact" limits the automatic matching to exact interface equality.
The additional attribute info causes the wired container to
print
information about the component resolvement after successful
initialization.
A value of "auto" prints out all auto-resolvements to standard out, a
value
of "all" prints both automatic and manual resolvements. The output is
provided
as an XML sniplet, using the schema for manual resolvement
specifications as shown above.
This allows e.g. to directly copy the output of an auto resolvement to
an
application descriptor again.
public
class ACompImpl extends
AbstractComponent implements AComp { @Component BComp bref; // will be initialized by container public String foo() throws RemoteException { try { System.out.println("foo"); // Lookup no longer required here String bar = bref.bar(); return "foo" + bar; } catch(Exception x) { throw new ComponentException(x.getMessage()); } } } |
<component-jar> <component> <component-name>ComponentA</component-name> <external-interface>acomp.AComp</external-interface> <component-class>acomp.ACompImpl</component-class> <component-refs> <component-ref ref-name="bref" interface="bcomp.BComp" type="external" injection-target="bref"/> </component-refs> </component> </component-jar> |
Home | Introduction | Javadoc |