    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: A Policy-Driven CORBA Template Library to Facilitate the
Rapid Development of DOC Middleware</title>
        <link>https://members.accu.org/index.php/articles/330</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #57 - Oct 2003</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c155/">57</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-155/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+155/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;A Policy-Driven CORBA Template Library to Facilitate the
Rapid Development of DOC Middleware</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 02 October 2003 22:56:14 +01:00 or Thu, 02 October 2003 22:56:14 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>While CORBA provides a robust, well-defined standard for the
development of distributed object computing (DOC) middleware, the
machinery needed to deploy a non-trivial application tends to be,
at best, tedious and repetitive and, at worst, a source of hardto-
discover errors. As an application/system grows in complexity, the
need for a reusable CORBA machinery abstraction becomes
self-evident. The tried-and-true practice of cutting and pasting
and the common setup function paradigm does not provide an adequate
solution to the problem. In order to address these needs and
overcome the problems of the classical design, this paper presents
a policy-driven abstraction mechanism that promotes code reuse
while preserving the rich capabilities CORBA provides.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id=
"d0e22"></a>Introduction</h2>
</div>
<p>CORBA has developed into a robust, mature distributed
objectcomputing standard over the years, but the effort in
environment setup of large systems with many servant types is
problematic. The cost of recreating the same or slightly differing
requirements for multiple servant types or for multiple
applications is unacceptable given that the reward for such work is
non-existent and the powerof modern development tools renders
classical methods [<a href="#Henning-">Henning-</a>] inefficient.
Large-scale CORBA applications and systems may provide hundreds of
disparate servant types with varying requirements on the underlying
ORB facilities. To meet the needs of CORBA server/system
developers, the following presents a C++ policy-driven library
design that eliminates the code redundancy issues in traditional
CORBA applications while maintaining the rich set of options
provided by the CORBA standard.</p>
<p>Template meta-programming has emerged as a powerful tool with
which to construct reusable, extensible libraries. The CORBA
Template Library (CTL) relies heavily on said techniques and
borrows the policy-driven design approach pioneered by Andrei
Alexandrescu in &quot;Modern C++ Design&quot; (MCD) [<a href=
"#Alexandrescu">Alexandrescu</a>]. The generic functionality this
provides is ideal for library designers as it allows an
extensibility lacking in traditional OO- and procedure-based
libraries. Template-based policies provide the necessary paradigm
to accomplish the &quot;write once use many&quot; goal, while providing a
method for future extension that is both safe and predictable, and
allowing us to leverage working code to dramatically cut down on
testing, development, and redesign time.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e35" id="d0e35"></a>Motivation</h2>
</div>
<p>Expertise in the application of CORBA and the design of
distributed computing systems is secondary to that of constructing
a system that &quot;does something.&quot; Work involved in the design and
development of the machinery needed to deploy an application using
CORBA is wasted effort because the real objective is the
functionality exposed via CORBA. For instance, in order to set up a
minimal CORBA server environment, an ORB must be initialized, a
servant must be instantiated and activated, and an active thread
must run the ORB dispatching mechanism. In the simplest case, this
is not a burden and does not indicate a need for yet another
library/abstraction. A transient servant could simply be activated
under the Root POA and theapplication's main thread would run the
dispatch loop. If, however, we were to need a system with many
servant instances, multiple POAs with differing POA policies and
ORB requirements, the task becomes substantially more difficult. If
we wish to develop a true peer-to-peer application, where
theapplication is both a client and a server, instead of a pure
server application, the necessary setup becomes even more
cumbersome.</p>
<p>Historically, these concerns have been met with procedural setup
and initialization functions, code repetition, or an OO abstraction
mechanism, none of which are ideal. Procedural solutions fall short
because of lack of easy reconfiguration of servant
initializationbehavior. Environmental changes require modification
of servant setup code, possibly in multiple locations, for
instance, calls to a number of different POA setup procedures and
object instantiation and activation functions. A simple change from
a transient, systemidentified, stack-allocated servant to a
persistent, user-identified, heap-allocated servant is error prone
and takes more effort than the alternate policy-driven model
presented here. The traditional OO-based design also fails to
simplify deployment significantly and can in fact be more difficult
to adapt than the procedural solution. Inheritance-based solutions
rely on a given base type that limits your design choices or
requires a large number of similar base types to achieve all
possible combinations, i.e., a reference counted transient servant,
a non reference counted version, and persistent version of both,
etc.</p>
<p>Addressing these limitations with a reusable and extensible
template library provides an abstraction layer that will allow
developers to concentrate on the functionality they want to expose,
not the machinery needed to expose them. To that end, the following
presents some meta-programming techniques to simplify our goal, and
a policy driven library of reusable types that simplify DOC
middleware development to a point where the only new work needed in
most cases is the development of the methods exposed by a given
interface. The following pages present the basic techniques
employed by the library and a synopsis of the library policies and
their usage.</p>
<div class="sidebar">
<p class="title c2">Basic CORBA Terminology</p>
<div class="variablelist">
<dl>
<dt><span class="term">CORBA, Common Object Request Broker
Architecture:</span></dt>
<dd>
<p>CORBA is a standardized middle-ware framework that facilitates
distributed-object system development. The CORBA standard is
maintained and developed by the OMG (Open Messaging Group, <a href=
"http://www.omg.org" target="_top">www.omg.org</a>) Among other
things the standard provides specific mappings between a platform
independent &quot;interface definition language&quot; (IDL) and a number of
development languages such as C++ and Java, a transparent messaging
infrastructure for marshalling requests for, and data to, the
remote processes, and a set of well defined Services such as the
Naming Service.</p>
</dd>
<dt><span class="term">ORB, Object Request Broker:</span></dt>
<dd>
<p>the ORB is the messaging component of a CORBA system, all
outgoing requests go through an ORB and are packaged and marshaled
across the wire, all incoming requests are de-marshaled and
dispatched by the ORB. The ORB has a number of secondary functions
as well such as bootstrapping (<tt class=
"literal">resolve_initial_references</tt>), IOR
management/manipulation (<tt class="literal">string_to_object</tt>,
<tt class="literal">object_to_string</tt>) etc.</p>
</dd>
<dt><span class="term">POA, Portable Object Adapter:</span></dt>
<dd>
<p>the POA is the bridge between the ORB and a servant skeleton.
ORBs dispatch a remote request/invocation to a POA that in turn
either rejects the message or delivers it to a concrete servant
implementation by way of its skeleton.</p>
</dd>
<dt><span class="term">CORBA Object:</span></dt>
<dd>
<p>Objects are the CORBA counterparts to servant instances, while
the servant is a process local entity, an Object is accessible
across process and machine boundaries.</p>
</dd>
<dt><span class="term">IOR, Interoperable Object
Reference:</span></dt>
<dd>
<p>IORs are encoded names that uniquely identify an Object, they
include the host's contact information, the object key that
identifies the Object to the ORB/POA, and any other data necessary
to contact the Object.</p>
</dd>
<dt><span class="term">IDL, Interface Definition
Language:</span></dt>
<dd>
<p>IDL is a platform independent language to describe interfaces
and data types. The IDL is run through an IDL compiler that
generates language specific mappings of the types. Skeleton and
stub code is generated by the IDL compiler.</p>
</dd>
<dt><span class="term">RootPOA:</span></dt>
<dd>
<p>The Root POA is the primary POA hosted by an ORB; all other POAs
are subordinates of the RootPOA. Simple transient Objects with
system ids can be activated under the RootPOA. It is also the only
POA immediately available when the ORB is initialized and the last
to be destroyed during deactivation.</p>
</dd>
<dt><span class="term">Name Service:</span></dt>
<dd>
<p>the Naming Service (CosNaming) is a CORBA service for exposing
objects in a directory like manner - advertised object references
are identified by a name and kind and placed in a naming-context,
which is similar to a directory or folder. Clients can resolve
names (files) and contexts (directories) bound to the Name Service
obtaining an Object reference for the associated name that can be
narrowed to an expected interface.</p>
</dd>
<dt><span class="term">Skeleton:</span></dt>
<dd>
<p>generated server side code that bridges the gap between the POA
and the servant, conforms to an expected interface used by the POA
and provides the mapped IDL types interface to be overridden by the
servant implementation</p>
</dd>
<dt><span class="term">Stub:</span></dt>
<dd>
<p>generated client side code that supports the IDL specified
interface and interacts with the ORB to reach a remote Object, a
remote proxy to the Object and servant.</p>
</dd>
<dt><span class="term">Servant:</span></dt>
<dd>
<p>user generated code that conforms to the interface expected by a
skeleton and implements the necessary code to fulfill the methods
of the IDL specified interface.</p>
</dd>
</dl>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e126" id="d0e126"></a>CORBA
Template Library (CTL)</h2>
</div>
<p>The CORBA Template Library (CTL) presented here attempts to
provide an extensible library of policies and composable elements
to simplify the deployment of large-scale CORBA applications. The
design concentrates on the servant activation process, the
mechanism by which the servant accesses the information it needs
during the activation process, and the organization of servant
instances in a larger service framework. The CTL is a freely
available C++ source code library, available under the BSD
license.</p>
<p>Currently the library is tied to ACE/TAO but work is underway to
make it as ORB agnostic as possible.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e133" id="d0e133"></a>Servant
Activation/Deactivation</h3>
</div>
<p>Servants represent the concrete implementation of a CORBA Object
in a particular programming language. They are local instances of a
type that are made known to the CORBA framework in order to service
remote requests. A local servant instance is &quot;activated,&quot; made
known to the CORBA framework, by first instantiating the servant,
finding or creating a suitable Portable Object Adapter (POA),
registering with that POA, and finally some secondary
object-activation activity such as Object exposition through the
Naming Service. In order to preserve the rich set of options
available to CORBA developers a policybased design was chosen that
allows every set of possible activation scenarios to be expressed
by way of four policy-type delegates: memory policies, POA-
activation/selection policies, object-activation policies, and
auxiliary policies. The servant activation (<tt class=
"function">createServant()</tt>) and deactivation (<tt class=
"function">destroyServant()</tt>) functions simply dispatch the
requests to the specified policy delegates.</p>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e144" id="d0e144"></a>Memory
Activation/Deactivation</h4>
</div>
<p>The memory policy is the first called during activation and the
last called during deactivation. Most users will simply use the
empty memory policy, which does nothing. But a number of policies
are provided to simplify garbage collection of free store allocated
servants. The memory policy also provides a convenient base type
for alternate allocation strategies, such as a pooled
allocator.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e149" id="d0e149"></a>POA
Activation/Deactivation</h4>
</div>
<p>The POA activation process is arguably the most cumbersome
aspect of CORBA applications, and generates the most redundant
code. While it is possible to use a procedural approach that
simplifies the most common-use scenarios, with seven basic policies
and a number of extension policies (pluggable protocols such as
bi-directional GIOP) an exhaustive set of POA activation procedures
is not a practical option.</p>
<p>The CTL provides two mechanisms by which a POA can be created:
two functions <tt class="literal">create_poa</tt> and <tt class=
"literal">create_from_poa</tt>, which are explicitly specialized
with the policy type desired; and a servant POA policy that will
create the POA for a servant during the activation process. The two
options allow for easy POA activation whether the POA to Object
relationship is one-to-many or one-to-one.</p>
<p>A classic POA activation scenario involves first obtaining and
narrowing a reference to the <tt class="classname">RootPOA</tt>
obtaining the <tt class="classname">POAManager</tt>, generating the
policy list, creating the new POA, and then cleaning up.</p>
<pre class="programlisting">
CORBA::Object_var obj
    = orb-&gt;resolve_initial_references(&quot;RootPOA&quot;);
PortableServer::POA_var root
    = PortableServer::POA::_narrow(obj);
PortableServer::POAManager_var poa_manager
    = root-&gt;the_POAManager();
CORBA::PolicyList policies;
policies.length(2);
policies[0] = root-&gt;create_lifespan_policy(
                       PortableServer::PERSISTENT);
policies[1] = root-&gt;create_id_assignment_policy(
                       PortableServer::USER_ID);
PortableServer::POA_var wfa_poa = root-&gt;create_POA(
                       &quot;WidgetFactoryAdmin_i&quot;,
                       poa_manager,policies);
poa_manager-&gt;activate();
for(CORBA::ULong i=0;i&lt;policies.length();++i)
  policies[i]-&gt;destroy();
</pre>
<p>The equivalent when using CTL is to specify a POA activation
policy during servant activation:</p>
<pre class="programlisting">
POAPolicy::FromRoot&lt;D,POAPolicy::PersistentUserID&lt;&gt; &gt;
// POA policies which require an id to create the
// POA also provide a set_poa_id method.
</pre>
<p>And an example when using CTL to create a new POA outside of
servant activation:</p>
<pre class="programlisting">
PortableServer::POA_var w_poa = POAPolicy::create_poa&lt;
POAPolicy::PersistentUserID&lt;&gt; &gt;(orb,&quot;Widget_i&quot;);
</pre>
<p>The POA policy types provided are composable in a linear
hierarchy, as are most of the types in the CTL. So for instance,
assuming you wanted a POA that used bi-directional GIOP, hosted
transient system id Objects, and didn't allow implicit activation
of servants, the appropriate specification would merely be a string
of nested template types:</p>
<pre class="programlisting">
POAPolicy::Bidirectional&lt;
POAPolicy::TransientSystemID&lt;
POAPolicy::NoImplicit&lt;&gt; &gt; &gt;
</pre>
<p>POA deactivation, in both the classic and CTL use scenarios, is
relatively simple. The CTL POA policy takes care of it
automatically if it created the POA. If the POA was not created
during the activation process but was instead created as above the
user is responsible for destroying the POA when it is no longer
needed, for instance:</p>
<pre class="programlisting">
w_poa-&gt;destroy(true,true);
</pre></div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e188" id="d0e188"></a>Object
Activation/Deactivation</h4>
</div>
<p>Once a suitable POA has been created a servant instance must be
tied to the POA and an Object reference that specifies the servant
to the outside world must be generated. A persistent Object
reference remains the same across different execution contexts
while a transient Object reference is unique each time the servant
is activated. CTL provides two simple policy types that activate
persistent and transient Objects under the specified POA during the
activation process. A number of other policies are also provided
such as Multicast servant activation but are not discussed
here.</p>
<p>A classic persistent Object activation scenario:</p>
<pre class="programlisting">
PortableServer::ObjectId_var oid
    = PortableServer::string_to_ObjectId(
            &quot;WidgetFactoryAdmin_i&quot;);
wfa_poa-&gt;activate_object_with_id(oid.in(),&amp;wfa);
CORBA::Object_var wfa_obj
    = wfa_poa-&gt;id_to_reference((oid.in()));
Example::WidgetFactoryAdmin_var wfa_ref
    = Example::WidgetFactoryAdmin::_narrow(wfa_obj);
</pre>
<p>In contrast, when using the CTL, activating a servant with a
persistent Object reference is simplified to specifying the object
activation policy:</p>
<pre class="programlisting">
ObjPolicy::UserID&lt;D&gt;
// object policies which require an id during
// activation expose a set_object_id method.
</pre>
<p>Object deactivation is done automatically during servant
deactivation in the case of CTL servants. A classic CORBA
application would need to explicitly deactivate the Object when it
is no longer needed:</p>
<pre class="programlisting">
PortableServer::ObjectId_var oid
    = wfa_poa-&gt;servant_to_id(&amp;wfa);
wfa_poa-&gt;deactivate_object(oid);
</pre></div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e205" id="d0e205"></a>Auxiliary
Activation/Deactivation</h4>
</div>
<p>Auxiliary policy processing is the last step during activation
and the first during deactivation. It provides a point during the
activation/deactivation process after the servant has been
associated with a CORBA Object and before, in the case of
deactivation, that Object has been torn down. As such it is an
ideal point from which to expose the Object to some form of
external lookup such as the Naming Service; or to set up messaging
policies to be used, such as timeout policies. Two simple
exposition policies are presented here: one that binds the newly
activated Object to the ORB's IOR table, and one that binds the
Object to the root naming-context of the Naming Service.</p>
<p>The IOR table provides a mechanism by which external ORB
processes can discover an Object and obtain a reference to the
Object. When a call to <tt class=
"literal">orb-&gt;resolve_initial_references(&quot;NameService&quot;)</tt> is
made, if an initial reference was not supplied during ORB
initialization a multicast request is sent to external ORB
processes that in turn query their IOR table to try to fulfill the
request for the name specified(&quot;NameService&quot; in this case). A CTL
servant can request IOR table binding and unbinding during
activation and deactivation respectively, by providing the
appropriate Auxiliary policy:</p>
<pre class="programlisting">
AUXPolicy::BindIORTable&lt;D&gt;
// The BindIORTable policy exposes a method
// set_iortable_id to specify the ID the Object
// reference will be associated with in the table.
</pre>
<p>The equivalent classic CORBA application would need to:</p>
<pre class="programlisting">
obj = orb-&gt;resolve_initial_references(&quot;IORTable&quot;);
IORTable::Table_var tbl
    = IORTable::Table::_narrow(obj);
CORBA::String_var str
    = orb1-&gt;object_to_string(wfa_obj);
tbl-&gt;rebind(&quot;WidgetFactoryAdmin_i&quot;,str.in());
</pre>
<p>in order to bind the Object reference to the IOR table. And in
order to unbind do the complementary</p>
<pre class="programlisting">
tbl-&gt;unbind(&quot;WidgetFactoryAdmin_i&quot;);
</pre>
<p>during deactivation. This of course entails either maintaining
the above <tt class="literal">IORTable::Table_var</tt> or
reacquiring it when needed. The corresponding CTL use is
transparent and automatically done during the servant deactivation
process.</p>
<p>The Naming Service provides a directory service for exposing
&quot;well known&quot; servants to distributed systems. Much like the IOR
Table it provides a mechanism for exposition and lookup. CTL
servants that wish to take advantage of the Naming Service simply
specify the appropriate auxiliary policy such as:</p>
<pre class="programlisting">
AUXPolicy::BindRootNamingContext&lt;D&gt;
// The BindRootNamingContext policy provides a
// set_cosnaming_data method to specify the id and
// kind that will be bound to the root naming-context.
</pre>
<p>to be used during activation and deactivation. A classic CORBA
application on the other hand needs to manually register with the
Naming Service after the Object has been activated:</p>
<pre class="programlisting">
CosNaming::Name name;
name.length(1);
name[0].id
    = CORBA::string_dup(&quot;WidgetFactoryAdmin_i&quot;);
name[0].kind
    = CORBA::string_dup(&quot;WidgetFactoryAdmin_i&quot;);
obj = orb-&gt;resolve_initial_references(&quot;NameService&quot;);
CosNaming::NamingContext_var nc
    = CosNaming::NamingContext::_narrow(obj);
nc-&gt;rebind(name,wfa_obj);
</pre>
<p>And correspondingly unbind before deactivation:</p>
<pre class="programlisting">
nc-&gt;unbind(name);
</pre>
<p>Like the other CTL policy types the Auxiliary policies may be
combined into a composite. So if a servant wanted to bind both to
the IOR table and the Naming Service a composite policy type:</p>
<pre class="programlisting">
AUXPolicy&lt;BindIORTable&lt;D,
        AUXPolicy::BindRootNamingContext&lt;D&gt; &gt;
</pre>
<p>could be used.</p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e249" id="d0e249"></a>Mix-in
Composition: Host and Delegate</h3>
</div>
<p>The CTL makes extensive use of the Curiously Recurring Template
Pattern and Parameterized Inheritance to deliver a &quot;composable&quot;
design. Composable in this context refers to the ability to extend
an inheritance hierarchy by nesting template types. The main use
for this technique in the CTL is to provide a consistent means by
which to retrieve the necessary data needed during servant
activation/deactivation. The choice of whether to &quot;host&quot; the data
in the servant type or to &quot;delegate&quot; the request to some other type
is left up to the user. Hosts store the data as a member of the
servant type and provide a common interface used by the policies to
retrieve that data. Delegates provide the same interface but act as
proxies for the data. The CTL provides a number of Host and
Delegate mix-in types in the <tt class=
"literal">CTL::MIX::Host</tt> and <tt class=
"literal">CTL::MIX::Delegate</tt> namespaces respectively.</p>
<p>All the types in the MIX namespace use both a D (Derived) and B
(Base) template parameter and are therefore &quot;composable&quot; as nested
templates. Assuming S is the servant base type and we wished to
construct a servant that hosts its own <tt class="literal">ORB_var
(CTL::MIX::Host::ORB)</tt> and <tt class="literal">POA_var
(CTL::MIX::Host::POA)</tt> we could form the composite type by
writing out the template <tt class=
"literal">CTL::MIX::Host::ORB&lt;D, CTL::MIX::Host::POA&lt;D, S&gt;
&gt;</tt> where <tt class="literal">D</tt>, as noted above,
represents the most derived type i.e. the type that inherits from
the composite. To simplify the above, which gets rather ugly when
many types are composed, the CTL::Compose namespace provides a
family of templates that will allow the type to be expressed in a
simpler manner. For instance, the <tt class=
"literal">composeDB</tt> type could be use to rewrite the above as
<tt class="literal">CTL::Compose::composeDB&lt;D,S,
CTL::MIX::Host::ORB, CTL::MIX::Host::POA&gt;::type</tt>, or without
the fully qualified names <tt class="literal">composeDB&lt;D,S,ORB,
POA&gt;::type</tt>.</p>
<p>For a further discussion of composition see the sidebar Template
Techniques Used in the CTL (next page), and the material in the
references section. The following sections present some of the more
important elements of the <tt class="literal">CTL::MIX</tt>
namespace.</p>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e288" id="d0e288"></a>Mix-in ORB</h4>
</div>
<p>Every servant needs a reference to an ORB so that a POA can
either be found or created, and an Object can be activated. The ORB
mix-in type provides an interface for accessing the <tt class=
"literal">ORB_var</tt> that the servant type will use. The Host
variation maintains a member <tt class="literal">ORB_var</tt> and
provides an additional method <tt class="literal">set_orb</tt>,
while the Delegate dispatches the request to the servant's
&quot;container&quot;.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e302" id="d0e302"></a>Mix-in
ORBTask</h4>
</div>
<p>CTL provides a helper type <tt class="literal">CTL::OrbTask</tt>
that is simply an <tt class="literal">ORB_var</tt> and thread that
runs the ORB reactor loop. The mix-in type <tt class=
"literal">CTL::MIX::Host::ORBTask</tt> helps simplify many
multi-orb applications.</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e316" id="d0e316"></a>Mix-in POA</h4>
</div>
<p>Once a POA has been created or found on behalf of a servant, it
needs to be stored for subsequent use during Object activation and
deactivation. The mix-in types <tt class=
"literal">CTL::MIX::Host::POA</tt> and <tt class=
"literal">CTL::MIX::Delegate::POA</tt> provide an interface that
returns a <tt class="literal">PortableServer::POA_var&amp;</tt>
that the activation/deactivation policy methods can use to store
and gain access to the associated POA. The Host variety also
provides a <tt class="literal">set_poa</tt> method.</p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e333" id="d0e333"></a>Servant
Holders</h3>
</div>
<p>Once a servant has been activated and an Object reference (IOR)
has been generated that data needs to be stored somewhere. The CTL
provides two simple holder types, <tt class=
"literal">CTL::BasicServant</tt> and <tt class=
"literal">CTL::TunneledServant</tt>. The only difference between
the two is that <tt class="literal">CTL::TunneledServant</tt>
allows the storage of a second IOR that identifies the servant, for
instance an alternate route through a DSI gateway. Both Holder
types support a <tt class="literal">getServantPtr</tt> and
<tt class="literal">setServantPtr</tt> method. <tt class=
"literal">CTL::TunneledServant</tt> supports the additional methods
<tt class="literal">getTServantPtr</tt> and <tt class=
"literal">setTServantPtr</tt>.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e362" id="d0e362"></a>Container
Holders</h3>
</div>
<p>An optional Holder type that maintains a
parameterized-backpointer to some arbitrary type is also provided.
<tt class="literal">CTL::BasicContainer</tt> maintains this data
and provides a simple interface for modifying and accessing the
information. The Delegate types in <tt class=
"literal">CTL::MIX::Delegate</tt> use this information to redirect
requests for activation data to the type specified to <tt class=
"literal">CTL::BasicContainer</tt>. The interface supported is very
simple: <tt class="literal">getContainer</tt>, <tt class=
"literal">attachToContainer</tt> and <tt class=
"literal">detachFromContainer</tt>.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e385" id="d0e385"></a>Servant
State</h3>
</div>
<p><tt class="literal">CTL::ServantState</tt> acts as a common base
that servants can use to report activation and deactivation
failure. The interface is simple and merely provides a method of
setting and querying a bit field that represents different servant
states. All CTL servants support the <tt class=
"literal">CTL::ServantState</tt> interface.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e395" id="d0e395"></a>A Complete
Servant</h3>
</div>
<p>All CTL servants provide a simple interface: <tt class=
"literal">createServant()</tt> and <tt class=
"literal">destroyServant()</tt>. These in turn use the policy types
provided to activate and deactivate the servant. Though the
interface is simple the templated nature of the policy driven
design allows for an unlimited set of servant
activation/deactivation scenarios. Two base servant types are
presented below. They are identical except that the second inherits
from an extra &quot;container&quot; template type that represents a container
holder type as described in section 3.4. The number of template
parameters may seem extravagant but they allow us to formulate a
generic servant activation/deactivation mechanism.</p>
<pre class="programlisting">
template &lt;typename D,typename S, typename SS,
          typename PP = CTL::POAPolicy::Empty,
          typename OP = CTL::ObjPolicy::Empty,
          typename MP = CTL::MemPolicy::Empty,
          typename AP = CTL::AUXPolicy::Empty,
          typename B = CTL::EmptyType &gt;
struct CompositionServant : CTL::CompositionType&lt;D,B&gt;,
                    S,SS,MP,PP,OP,AP,CTL::ServantState {
  typedef B BASE;
  typedef D DERIVED;
  virtual void createServant() {
    initializeMemoryPolicy(
               static_cast&lt;DERIVED*&gt;(this));
    initializePOAPolicy(
               static_cast&lt;DERIVED*&gt;(this));
    initializeObjectPolicy(
               static_cast&lt;DERIVED*&gt;(this));
    initializeAUXPolicy(
               static_cast&lt;DERIVED*&gt;(this));
  }

  virtual void destroyServant() {
    destroyAUXPolicy(static_cast&lt;DERIVED*&gt;(this));
    destroyObjectPolicy(
               static_cast&lt;DERIVED*&gt;(this));
    destroyPOAPolicy(static_cast&lt;DERIVED*&gt;(this));
    destroyMemoryPolicy(
               static_cast&lt;DERIVED*&gt;(this));
  }
  virtual ~CompositionServant() {}
};
</pre>
<p>And the similar servant base type that also provides a template
parameter for a container holder type.</p>
<div class="sidebar">
<p class="title c2">Template Techniques used in the CTL</p>
<p>Fulfilling the requirements of an extensible, reusable library
requires a number of template techniques. A basic understanding of
template meta-programming is assumed, as is a familiarity with
policy-driven design as expressed in &quot;Modern C++ Design&quot; (MCD)
[<a href="#Alexandrescu">Alexandrescu</a>]. Beyond basic template
techniques and the concepts propounded in MCD, a number of other
mechanisms are used. The major techniques used in CTL are briefly
described below.</p>
<p><span class="bold"><b>Policy-based Design</b></span></p>
<p>Though the concept of policy driven designs is not completely
new (POA policies), the concept has been adopted in recent years by
the generic programming community, thanks in large part to the
pioneering work presented in &quot;Modern C++ Design&quot; (MCD) and the Loki
library. Policies represent the behavior exposed by a type
(<tt class="literal">P</tt>) when dealing with a particular type
(<tt class="literal">T</tt>) and, therefore, are groups of related
functions, in contrast to traits, which describe a particular type
(<tt class="literal">T</tt>) by way of a set of descriptive
typedefs and values.</p>
<p>There are a number of ways to represent policies, the simplest
of which is a class template that is made available to the client
type via a template template type. Unfortunately, template template
types are just now being handled consistently across different C++
compiler implementations. Therefore, CTL uses the simpler form of a
specialized templated struct specialized by the client, except in
the situation where the policy is not inherited. In those cases the
policy design is simplified and a template type, whose methods are
static, is expected. A complete discussion of policy-based
metaprogramming is not given here, but for the sake of completeness
the following gives a quick demonstration.</p>
<p>A simple example:</p>
<pre class="programlisting">
template &lt;typename T&gt;
struct P {void f() {}};
</pre>
<p><tt class="literal">P</tt> is a very simple policy for an
arbitrary type <tt class="literal">T</tt>. In the above example,
<tt class="literal">P</tt> makes no demands on the type <tt class=
"literal">T</tt>. This is of course not the general case but is
merely a simplification used to illustrate the policy paradigm.</p>
<pre class="programlisting">
template &lt;template &lt;typename U&gt; class T&gt;
struct C : T&lt;C&lt;T&gt; &gt; {};
</pre>
<p>C is an arbitrary client of policies. It designates that one
arbitrary policy is needed (T) and will inherit from said policy,
specifying itself as the parameter type.</p>
<pre class="programlisting">
typedef C&lt;P&gt; CP;
</pre>
<p><tt class="literal">CP</tt> is a completely defined type, which
uses the <tt class="literal">P</tt> policy above in conjunction
with the <tt class="literal">C</tt> policy client.</p>
<p>A number of resources dealing with policies [<a href=
"#Alexandrescu">Alexandrescu</a>] and templates [<a href=
"#Vandevoorde-">Vandevoorde-</a>] are provided in the
references.</p>
<p><span class="bold"><b>The Curiously Recurring Template
Pattern</b></span></p>
<p>The Curiously Recurring Template Pattern (CRTP) [<a href=
"#Coplien">Coplien</a>], for lack of a better term, is a template
technique that introduces the most derived type as a parameter
known to the base type at compile time. This fact allows the base
type to make upcalls via a static cast of the instance to the
derived type's complete interface. Effectively, this imposes
interface requirements on the derived type. This is similar to an
abstract interface, which needs to be overridden in the concrete
type, without the necessity for a virtual function and the extra
baggage of a formal abstract interface. Templates allow us to
extend the interface concept by breaking away from the large-scale
(whole interface) contract, to a method-by-method contract. The
base type does not require that the derived type inherit from a
particular base, only that it provides the methods we need.</p>
<p>A simplified example:</p>
<pre class="programlisting">
template &lt;typename D&gt;
struct U {
  void f() {static_cast&lt;D*&gt;(this)-&gt;g();}
};
</pre>
<p><tt class="literal">U</tt> represents a template base type. It
requires that derived types implement some function: <tt class=
"literal">&lt;arbitrary return type&gt; g()</tt>. Obviously
<tt class="literal">U</tt> can require any signature for <tt class=
"literal">g</tt>. The simplest possible signature is required in
the above example.</p>
<pre class="programlisting">
struct V : U&lt;V&gt; {void g() {}};
</pre>
<p><tt class="literal">V</tt> represents a complete hierarchy. It
implements the method as required by the base type (<tt class=
"literal">U</tt>) and inherits from a specialization of <tt class=
"literal">U</tt>, designating itself as the most derived type.</p>
<p><span class="bold"><b>Parameterized Inheritance</b></span></p>
<p>Parameterized inheritance allows us another degree of freedom
when creating a type. While traditional inheritance allows us to
specify the access of the base type (public, protected, private),
the number of copies of the base to keep (virtual), as well as
whether a single or multiple base type(s) are the ancestor of the
type, parameterized inheritance leverages the template system to
inherit from an arbitrary type on demand. This allows us to reuse
the same method implementation without regard to base type, thereby
producing a true mix-in on-demand development strategy, which
greatly enhances our ability to reuse code.</p>
<p>A simplified example:</p>
<pre class="programlisting">
template &lt;typename B&gt;
struct M : B {virtual void f() {}};
</pre>
<p><tt class="literal">M</tt> represents a parameterized
inheritance mix-in type, which provide an implementation of a
method <tt class="literal">f()</tt> that can be used to make
disparate abstract types concrete (assuming they only require an
override of a pure method <tt class="literal">void f()</tt>).</p>
<pre class="programlisting">
struct U {virtual void f()=0;};
struct V {virtual void f()=0;};
</pre>
<p><tt class="literal">U</tt> and <tt class="literal">V</tt>
represent two arbitrary unrelated abstract types with a common
method (<tt class="literal">void f()</tt>). Derived types &quot;could&quot;
either reproduce the common implementation in the concrete types,
leading to code redundancy, or delegate to a non-member function
common to both. Among other issues, this requires us to provide a
delegating implementation in both concrete types (code redundancy)
and, unless the common method is templated, either does not allow
us to access state information encapsulated in the instance or
forces us to inherit from a fixed base and use a polymorphic call
from the common function being delegated to. Note that when
combined with the above technique (CRTP), <tt class=
"literal">M</tt>'s implementation of <tt class="literal">f()</tt>
can use state members and methods of the complete type.</p>
<pre class="programlisting">
struct CU : M&lt;U&gt; {};
struct CV : M&lt;V&gt; {};
</pre>
<p><tt class="literal">CU</tt> and <tt class="literal">CV</tt> are
concrete implementations of <tt class="literal">U</tt> and
<tt class="literal">V</tt>, respectively. They are complete as a
consequence of the shared implementation of <tt class=
"literal">f()</tt> implemented in <tt class="literal">M</tt>.</p>
<p><span class="bold"><b>Parameterized Back-Pointer</b></span></p>
<p>Parameterized Back-Pointer formalizes a common methodology and
abstracts it by using the generic meta-programming power of C++
templates. It is common practice to expect a member type to use a
reference to the containing type.</p>
<pre class="programlisting">
struct S {
  struct T {
    T(S* s) : s_(s) {}
    S* s_;
  };
  S() : t_(this) {}
  T t_;
};
</pre>
<p>A similar scenario arises when we simply want a type to have a
reference to an unrelated type that exposes an expected
interface.</p>
<p>A simple example:</p>
<pre class="programlisting">
template &lt;typename C&gt;
struct CT {
  C* c_;
  CT(C* c) : c_(c) {}
  void g() {c_-&gt;f();}
};
</pre>
<p><tt class="literal">CT</tt> represents a simple type that uses a
parameterized backpointer. It expects a reference to some arbitrary
type (<tt class="literal">C</tt>) that has a matching signature for
<tt class="literal">f()</tt>. The relation between the parameter
<tt class="literal">C</tt> and <tt class="literal">CT</tt> is not
necessarily one of containment (i.e., <tt class="literal">CT</tt>
is a member of <tt class="literal">C</tt>) but we use the term
containment in lieu of a better term.</p>
<pre class="programlisting">
struct U {
  void f() {}
  CT&lt;U&gt; ct_; U() : ct_(this) {}
};
</pre>
<p><tt class="literal">U</tt> is an arbitrary type that supports
the method <tt class="literal">f()</tt> as expected by <tt class=
"literal">CT</tt>, and contains an instance of <tt class=
"literal">CT (ct_)</tt> that is constructed with a pointer to the
current <tt class="literal">U</tt> instance.</p>
</div>
<pre class="programlisting">
template &lt;typename D, typename S, typename SS,
          typename PP = CTL::POAPolicy::Empty,
          typename OP = CTL::ObjPolicy::Empty,
          typename MP = CTL::MemPolicy::Empty,
          typename AP = CTL::AUXPolicy::Empty,
          typename C = CTL::EmptyType,
          typename B = CTL::EmptyType &gt;
struct ContainedCompositionServant
             : CTL::CompositionServant&lt;
                                D,S,SS,MP,PP,OP,AP,B&gt;, C {
  typedef B BASE;
  typedef D DERIVED;
  virtual ~ContainedCompositionServant() {}
};

// The D parameter represents the Derived type.
// The S parameter represents a Servant holder type
//       i.e. BasicServant&lt;Example::Widget&gt;
// The SS parameter represents the skeleton type
//       i.e. POA_Example::Widget
// PP is the POA policy
// OP is the object-activation policy
// MP is the memory policy
// AP is the Auxiliary policy
// B is an arbitrary base type
// C, which is used in the second type, is the
//       Container Holder type.
</pre>
<p>Note that the resultant types inherit from all the template
parameters and have their combined interface.</p>
<p>As shown above the <tt class=
"literal">CTL::ContainedCompositionServant</tt> type reuses the
<tt class="literal">CTL::CompositionServant</tt> type. The
<tt class="literal">CTL::CompositionType</tt> is not discussed
here, but can be considered as <tt class="literal">B</tt> (the
arbitrary base parameter).</p>
<p>To illustrate that the above is not as complicated as it seems
from initial inspection, let's consider a simple servant. The
servant will have a transient system id POA, and register with the
Naming Service when activated.</p>
<pre class="programlisting">
module Example {
  interface MyType { string fn(); }
};
</pre>
<p>The types <tt class="literal">Example::MyType</tt> and
<tt class="literal">POA_Example::MyType</tt> are generated from the
above IDL. Below is a ready to use servant type and server. Note
that the fully qualified names are dropped for readability.</p>
<pre class="programlisting">
struct simple : composeDB&lt;
    simple, CompositionServant&lt;
          simple,
          BasicServant&lt;Example::MyType&gt;,
          POA_Example::MyType, POAPolicy::FromRoot&lt;
                POAPolicy::TransientSystemID&lt;&gt; &gt;,
          ObjPolicy::SystemID&lt;&gt;, MemPolicy::Empty,
          AUXPolicy::BindRootNamingContext&lt;
                simple&gt; &gt;,
    Host::ORB, Host::POA &gt;::type {
  simple() {
    set_cosnaming_data(&quot;simple&quot;, &quot;simple kind&quot;);
  }
  char* fn() {
    return CORBA::string_dup(&quot;Hello World&quot;);
  }
};

int main(int argc,char* argv[]) {
  simple s;
  s.orb_ = CORBA::ORB_init(argc,argv);
  s.createServant();
  s.orb_-&gt;run();
  return 0;
}
</pre>
<p>The above is a complete server application, <tt class=
"literal">simple::createServant()</tt> will construct the POA,
activate the servant and register it with the Naming Service using
the id &quot;simple&quot; and the kind &quot;simple kind&quot;. If a client resolved
the Object reference in the Naming Service and invoked the
<tt class="literal">fn()</tt> method, the string &quot;Hello World&quot;
would be returned.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e663" id="d0e663"></a>A Widget
Factory Example</h2>
</div>
<p>To illustrate the power and ease of use of the CTL, the
following presents an example that uses many of the CTL features.
Experienced CORBA developers will note how much simpler the code is
than a similar traditional application [<a href=
"#Henning-">Henning-</a>]. An equivalent classic application can be
found on the ACCU website. Beyond the exotic-looking base types,
the code is almost reduced to that of a standard C++ application.
Furthermore, if an error exists in the setup machinery, we have a
single point of failure and only a single policy that needs to be
debugged.</p>
<p>The example demonstrates a simple Widget server application. The
server uses two ORBs, one for public access to the factory's
interface and one that provides private access (from the local
machine) to the factory's administrative interface. The two
different servants that implement these interfaces share common
information and are in fact part of a larger Widget Factory
&quot;component&quot;. Both the servants of the factory component are
persistent Objects and each advertises its existence with the
Naming Service and IOR table. The publicly accessible interface
provides a method for Widget creation while the private admin
interface provides a method to destroy all the Widgets created and
a method to shut down the Widget Factory.</p>
<pre class="programlisting">
&lt;example.idl&gt;
module Example {
  interface Widget {
    void do_something();
  };
  interface WidgetFactory {
    Widget create_widget();
  };
  interface WidgetFactoryAdmin {
    void destroy_all_widgets();
    void shutdown_widget_factory();
  };
</pre>
<p>The above IDL describes the three basic interfaces and types
that the following Widget factory will implement. Once it is run
through an IDL compiler a skeleton and stub will be generated for
<tt class="literal">Widget</tt>, <tt class=
"literal">WidgetFactory</tt>, and <tt class=
"literal">WidgetFactoryAdmin</tt>.</p>
<p>The servants presented here make use of a simple helper type
<tt class="literal">CTL::RefCountImpl</tt> that extends the
functionality of <tt class=
"literal">PortableServer::RefCountServantBase</tt> by providing
methods to get the current reference count and to wait on a
specific count.</p>
<pre class="programlisting">
template &lt;typename D,typename S,
          typename SS,typename C&gt;
struct factory_servant
     : ContainedCompositionServant&lt;
            D,BasicServant&lt;S&gt;,
            SS, MemPolicy::Empty,
            POAPolicy::FromRoot&lt;
                 D,POAPolicy::PersistantUserID&lt;&gt; &gt;,
            ObjPolicy::UserID&lt;D&gt;,
            AUXPolicy::BindRootNamingContext&lt;
                 D,AUXPolicy::BindIORTable&lt;D&gt; &gt;,
            BasicContainer&lt;C*&gt;,
            RefCountImpl&lt;
                 D,PortableServer
                       ::RefCountServantBase&gt; &gt; {};
template &lt;typename D,typename S,
          typename SS,typename C&gt;
struct simple_servant
     : ContainedCompositionServant&lt;
            D,BasicServant&lt;S&gt;,
            SS, MemPolicy::Empty,
            POAPolicy::Empty,
            ObjPolicy::SystemID&lt;D&gt;,
            AUXPolicy::Empty,
            BasicContainer&lt;C*&gt;,
            RefCountImpl&lt;
                 D,PortableServer
                       ::RefCountServantBase&gt; &gt; {};
</pre>
<p>These two servant base types will allow us to reuse the groups
of policies without restating them for each servant type. The
<tt class="literal">factory_servant</tt> base type will be shared
between the <tt class="literal">WidgetFactory_i</tt> and <tt class=
"literal">WidgetFactoryAdmin_i</tt> types. The Widget_i type will
use the base type <tt class="literal">simple_servant</tt>.</p>
<pre class="programlisting">
template &lt;typename C&gt;
struct Widget_I
     : composeDB&lt;Widget_i&lt;C&gt;,
             simple_servant&lt;Widget_i&lt;C&gt;,
                 Example::Widget,
                 POA_Example::Widget,C&gt;,
             Delegate::ORB,Delegate::POA&gt;::type {
  void do_something() throw (CORBA::Exception){}
};
</pre>
<p>Widgets are the product created by our factory for use by remote
clients. Once <tt class="literal">Widget_i</tt> has been
specialized for the container type it is a ready-to-use servant
type. When <tt class="literal">createServant()</tt> is called the
corresponding CORBA Object is activated and an Object reference is
generated. <tt class="literal">WidgetFactoryAdmin_i</tt> will
eventually call <tt class="literal">deactivateServant()</tt> for
each of the <tt class="literal">Widget_i</tt> servants created by
the factory.</p>
<pre class="programlisting">
template &lt;typename C&gt;
struct WidgetFactory_I
     : composeDB&lt;WidgetFactory_i&lt;C&gt;,
           factory_servant&lt;WidgetFactory_i&lt;C&gt;,
               Example::WidgetFactory,
               POA_Example::WidgetFactory, C&gt;,
           Host::ORBTask,
           Host::POA &gt;::type {
  WidgetFactory_i() {
    set_cosnaming_data(&quot;WidgetFactory_i&quot;,
                       &quot;WidgetFactory_i&quot;);
    set_object_id(&quot;WidgetFactory_i&quot;);
    set_poa_id(&quot;WidgetFactory_i&quot;);
    set_iortable_id(&quot;WidgetFactory_i&quot;);
  }
  Example::Widget_ptr create_widget()
               throw (CORBA::SystemException) {
    ACE_Guard&lt;ACE_Thread_Mutex&gt;
                        grd(getContainer()-&gt;mtx_);
    Widget_i&lt;C&gt; * p = new Widget_i&lt;C&gt;;
                  //exception unsafe for clarity
    p-&gt;attachToContainer(getContainer());
    p-&gt;createServant();
    getContainer()-&gt;widget_data_.push_back(p);
    return p-&gt;getServantPtr();
  }
};
</pre>
<p>The <tt class="literal">WidgetFactory</tt> interface is the
publicly visible portion of our component; any remote client can
request a new <tt class="literal">Widget</tt>. <tt class=
"literal">WidgetFactory_i</tt>, a servant type that supports the
<tt class="literal">WidgetFactory</tt> interface, is a template
that generates a servant once it is specialized with a container
type. The <tt class="literal">C</tt> (container) parameter will
later be provided as <tt class="literal">WidgetFactoryData</tt>.
Before the servant is activated, the user will provide a <tt class=
"literal">(C*) WidgetFactoryData*</tt> by way of <tt class=
"literal">attachToContainer</tt>, allowing us to gain access to the
vector of <tt class=
"literal">Widget_i&lt;WidgetFactoryData&gt;*</tt> that is shared
between the public factory interface's servant and the private
factory admin interface's servant that is a member of <tt class=
"literal">WidgetFactoryData</tt>.</p>
<pre class="programlisting">
template &lt;typename C&gt;
struct WidgetFactoryAdmin_I
     : composeDB&lt;WidgetFactoryAdmin_i&lt;C&gt;,
           factory_servant&lt;
                WidgetFactoryAdmin_i&lt;C&gt;,
                Example::WidgetFactoryAdmin,
                POA_Example::WidgetFactoryAdmin,C&gt;,
           Host::ORBTask,
           Host::POA &gt;::type {
  WidgetFactoryAdmin_i() {
    set_cosnaming_data(&quot;WidgetFactoryAdmin_i&quot;,
                       &quot;WidgetFactoryAdmin_i&quot;);
    set_object_id(&quot;WidgetFactoryAdmin_i&quot;);
    set_poa_id(&quot;WidgetFactoryAdmin_i&quot;);
    set_iortable_id(&quot;WidgetFactoryAdmin_i&quot;);
  }

  void destroy_all_widgets()
               throw (CORBA::SystemException) {
    ACE_Guard&lt;ACE_Thread_Mutex&gt;
               grd(getContainer()-&gt;mtx_);
    for(std::size_t i=0;
        i&lt;getContainer()-&gt;widget_data_.size();
        ++i) {
      //simple loop for clarity
      getContainer()
             -&gt;widget_data_[i]-&gt;destroyServant();
      getContainer()
             -&gt;widget_data_[i]-&gt;_remove_ref();
    }
    getContainer()-&gt;widget_data_.clear();
  }

  void shutdown_widget_factory()
               throw (CORBA::SystemException) {
    getContainer()-&gt;event_.signal();
  }
};
</pre>
<p><tt class="literal">WidgetFactoryAdmin_i</tt> is similar to the
above <tt class="literal">WidgetFactory_i</tt>, except for the
interface the servant will expose to remote clients. It also
requires a template parameter to designate its &quot;container&quot; type,
which will later be specified as <tt class=
"literal">WidgetFactoryData</tt>. Like the above servant type it
also hosts its own <tt class="literal">CTL::OrbTask</tt> and POA.
Because <tt class="literal">WidgetFactory_i</tt> and <tt class=
"literal">WidgetFactoryAdmin_i</tt> operate on independent ORBs a
request coming in on <tt class="literal">WidgetFactory_i</tt>'s ORB
can not gain access to the <tt class=
"literal">WidgetFactoryAdmin_i</tt>. Therefore if the ORB hosting
the admin servant is listening on <tt class=
"literal">localhost:10000</tt> only local access is granted to the
admin interface.</p>
<pre class="programlisting">
struct WidgetFactoryData {
  ACE_Event event_;
  ACE_Thread_Mutex mtx_;
  PortableServer::POA_var widget_poa_;
  WidgetFactory_i&lt;WidgetFactoryData&gt; factory_;
  WidgetFactoryAdmin_i&lt;WidgetFactoryData&gt;
                             factory_admin_;
  std::vector&lt;Widget_i&lt;WidgetFactoryData&gt;*&gt;
                             widget_data_;
  // widget delegates to container to
  // determine orb to use
  CORBA::ORB_var&amp; orb(Widget_i&lt;
       WidgetFactoryData&gt;*) {return factory_.orb_;}
  // widget delegates to container
  // to determine which POA to use
  PortableServer::POA_var&amp; poa(Widget_i&lt;
  WidgetFactoryData&gt;*) {return widget_poa_;}
};
</pre>
<p><tt class="literal">WidgetFactoryData</tt> is a &quot;container&quot; for
all three servant-types. It provides the necessary interface for
the <tt class="literal">Widget_i</tt> servant delegates, and
specializes the <tt class="literal">WidgetFactory_i</tt> and
<tt class="literal">WidgetFactoryAdmin_i</tt> templates with itself
as the container type. Because <tt class="literal">Widget</tt>s are
publicly available we reuse the public ORB specified by the
<tt class="literal">WidgetFactory_i</tt> servant. <tt class=
"literal">WidgetFactoryData</tt> stores all the information that's
shared across our &quot;component&quot;.</p>
<pre class="programlisting">
int main(int argc,char* argv[]) {
  try {
    WidgetFactoryData data;
    data.factory_.orb_.initialize(
                           argc,argv,&quot;public&quot;);
    data.factory_admin_.orb_.initialize(
                           argc,argv,&quot;local&quot;);
</pre>
<p>Instantiate the &quot;container&quot; and initialize/activate the two
independent ORBs</p>
<pre class="programlisting">
    data.factory_.attachToContainer(&amp;data);
    data.factory_admin_.attachToContainer(&amp;data);
    data.factory_.createServant();
    data.factory_admin_.createServant();
</pre>
<p>Attach the two factory servant types to the container and create
the Objects, which will by virtue of the policies given
automatically register themselves with the IOR table and the Naming
Service.</p>
<pre class="programlisting">
    data.widget_poa_
      = POAPolicy::create_from_poa&lt;
               POAPolicy::TransientSystemID&lt;&gt; &gt;
               (data.factory_.orb_,
               data.factory_.poa_,
               &quot;widget_poa&quot;);
</pre>
<p>Create the POA that Widget_Is will be activated in.</p>
<pre class="programlisting">
    data.event_.wait();
</pre>
<p>Wait for the <tt class="literal">WidgetFactoryAdmin_i</tt>
servant to signal that the application should shut down. And then
proceed to final cleanup.</p>
<pre class="programlisting">
    data.factory_admin_.destroy_all_widgets();
</pre>
<p>Destroy all Widget_i instances that were created on behalf of
clients.</p>
<pre class="programlisting">
    data.widget_poa_-&gt;destroy(true,true);
</pre>
<p>Destroy the POA we manually created above</p>
<pre class="programlisting">
    data.factory_admin_.destroyServant();
    data.factory_.destroyServant();
</pre>
<p>Destroy the two factory servants</p>
<pre class="programlisting">
    data.factory_.orb_-&gt;destroy();
    data.factory_admin_.orb_-&gt;destroy ();
</pre>
<p>Finally, shutdown the two ORBs and their associated threads.</p>
<pre class="programlisting">
  }
  catch(const CORBA::Exception&amp;) {return -1;}
  catch(...) {return -2;}
  return 0;
}
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e854" id=
"d0e854"></a>Conclusion</h2>
</div>
<p>With the aid of a policy-driven CORBA template library, the
deployment of the machinery needed to build large-scale distributed
object computing facilities has been greatly simplified. Expertise
about the CORBA setup mechanisms has been encapsulated in an
extensible and easily applied framework of policies, thus allowing
rapid development that concentrates on the application's
functionality and lowers the barrier to entry for developers. Code
reuse is promoted without relying on methods that are, at best,
hard to extend (procedural, simple objectoriented inheritance or
cut and paste). Therefore, the developer's task is reduced to that
of providing the application's core functionality, freed of the
burdens imposed by classic CORBA application designs. Easy
extensibility ensures that future needs such as Real-Time ORB
usage, SSLIOP setup, and Trading Service registration can be added
once as policies and deployed with minimal effort.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e859" id="d0e859"></a>References</h2>
</div>
<div class="bibliomixed"><a name="Henning-" id="Henning-"></a>
<p class="bibliomixed">[Henning-] Michi Henning, Steve Vinoski,
Advanced CORBA <span class="citetitle"><i class=
"citetitle">Programming with C++</i></span>, 1999,
Addison-Wesley</p>
</div>
<div class="bibliomixed"><a name="Alexandrescu" id=
"Alexandrescu"></a>
<p class="bibliomixed">[Alexandrescu] Andrei Alexandrescu,
<span class="citetitle"><i class="citetitle">Modern C++
Design</i></span>, 2001, Addison Wesley</p>
</div>
<div class="bibliomixed"><a name="Delaney" id="Delaney"></a>
<p class="bibliomixed">[Delaney] Mark Delaney, &quot;Parameterized
Inheritance Considered Helpful&quot; <span class="citetitle"><i class=
"citetitle">CUJ</i></span>, January 2003</p>
</div>
<div class="bibliomixed"><a name="Vandevoorde-" id=
"Vandevoorde-"></a>
<p class="bibliomixed">[Vandevoorde-] David Vandevoorde, Nicolai M.
Josuttis, <span class="citetitle"><i class="citetitle">C++
Templates: The Complete Guide</i></span>, 2002, Addison Wesley</p>
</div>
<div class="bibliomixed"><a name="Coplien" id="Coplien"></a>
<p class="bibliomixed">[Coplien] Jim Coplien, &quot;The Curiously
Recurring Template Pattern&quot;, <span class="citetitle"><i class=
"citetitle">C++ Report</i></span>, Feb. 1995, 24-27</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
