Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / POA / NewPOA / NewPOA.cpp
blob0eea1764decd061508917e2d4bd9005832e4ee85
2 //=============================================================================
3 /**
4 * @file NewPOA.cpp
6 * This program demonstrates creation of new POAs, as children of the root POA or the
7 * existing POA.
8 * There are three new POA created in this example.
9 * The hierarchy of POAs looks like this.
11 * /-->first_poa-->first_poa/second_poa
12 * RootPOA--
13 * \-->third_poa
15 * @author Irfan Pyarali
17 //=============================================================================
20 #include "tao/PortableServer/PortableServer.h"
22 #include "tao/ORB.h"
24 #include "ace/SString.h"
25 #include "ace/Log_Msg.h"
27 void
28 print_poa (PortableServer::POA_ptr poa)
30 CORBA::String_var poa_name =
31 poa->the_name ();
33 CORBA::OctetSeq_var poa_id =
34 poa->id ();
36 ACE_DEBUG ((LM_DEBUG,
37 "POA name = %C\n",
38 poa_name.in ()));
40 ACE_DEBUG ((LM_DEBUG,
41 "POA id = "));
43 for (CORBA::ULong i = 0;
44 i != poa_id->length ();
45 ++i)
47 ACE_DEBUG ((LM_DEBUG,
48 "%o",
49 poa_id[i]));
52 ACE_DEBUG ((LM_DEBUG,
53 "\n"));
55 PortableServer::POAList_var children =
56 poa->the_children ();
58 for (CORBA::ULong index = 0;
59 index != children->length ();
60 ++index)
62 print_poa (children[index]);
66 int
67 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
70 try
72 // The first step Initialize the ORB
73 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
75 // Obtain the RootPOA.
76 CORBA::Object_var obj =
77 orb->resolve_initial_references ("RootPOA");
79 // _narrow() the Object to get the POA object, i.e., the root_poa.
80 PortableServer::POA_var root_poa =
81 PortableServer::POA::_narrow (obj.in ());
83 // Policies for the new POAs
84 CORBA::PolicyList policies (2);
85 policies.length (2);
87 // Threading policy
88 policies[0] =
89 root_poa->create_thread_policy (PortableServer::ORB_CTRL_MODEL);
91 // Lifespan policy
92 policies[1] =
93 root_poa->create_lifespan_policy (PortableServer::TRANSIENT);
95 // Creation of the firstPOA
96 ACE_CString name = "firstPOA";
97 PortableServer::POA_var first_poa =
98 root_poa->create_POA (name.c_str (),
99 PortableServer::POAManager::_nil (),
100 policies);
102 // Creation of the new POA, i.e. firstPOA/secondPOA
103 name = "secondPOA";
104 PortableServer::POA_var second_poa =
105 first_poa->create_POA (name.c_str (),
106 PortableServer::POAManager::_nil (),
107 policies);
109 // Creating thirdPOA.
110 name = "thirdPOA";
112 PortableServer::POA_var third_poa =
113 root_poa->create_POA (name.c_str (),
114 PortableServer::POAManager::_nil (),
115 policies);
117 // Creation of the new POAs over, so destroy the Policy_ptr's.
118 for (CORBA::ULong i = 0;
119 i < policies.length ();
120 ++i)
122 CORBA::Policy_ptr policy = policies[i];
123 policy->destroy ();
126 // Get the names of all the POAs and print them out.
128 CORBA::String_var root_poa_name =
129 root_poa->the_name ();
131 CORBA::String_var first_poa_name =
132 first_poa->the_name ();
134 CORBA::String_var second_poa_name =
135 second_poa->the_name ();
137 CORBA::String_var third_poa_name =
138 third_poa->the_name ();
140 ACE_DEBUG ((LM_DEBUG,
141 "%C\n%C\n%C\n%C\n",
142 root_poa_name.in (),
143 first_poa_name.in (),
144 second_poa_name.in (),
145 third_poa_name.in ()));
147 print_poa (root_poa.in ());
149 orb->destroy ();
151 catch (const CORBA::Exception& ex)
153 ex._tao_print_exception ("Exception caught in main ");
154 return -1;
157 return 0;