summaryrefslogtreecommitdiff
path: root/TAO/tests/Quoter/QuoterFactoryFinder.cpp
blob: 1d2e59f16875004d2d8ea502d254fee083d21cb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// $Id$
// ============================================================================
//
// = FILENAME
//    QuoterFactoryFinder.cpp
//
// = DESCRIPTION
//    A Factory Finder for the Quoter example. This example conforms
//    to the CosLifeCycle Factory Finder notion.
//
// = AUTHOR
//    Michael Kircher (mk1@cs.wustl.edu)
//
// ============================================================================

#include "ace/Get_Opt.h"
#include "tao/corba.h"
#include "QuoterFactoryFinder.h"

static const char usage [] = 
"[-? |\n"
"            [-O[RBport] ORB port number]]";


// Constructor
QuoterFactoryFinder_i::QuoterFactoryFinder_i () {

  TAO_TRY {

    // get a reference to the ORB
    CORBA::ORB_var orb_var = TAO_ORB_Core_instance()->orb();
    TAO_CHECK_ENV;

    // Get the Naming Service object reference.
    CORBA::Object_var namingObj_var = 
      orb_var->resolve_initial_references ("NameService");
    TAO_CHECK_ENV;

    if (CORBA::is_nil (namingObj_var.in ())) {

      ACE_ERROR((LM_ERROR,
          " (%P|%t) Unable get the Naming Service.\n"));
    }

    // narrow the object reference to a Naming Context
    CosNaming::NamingContext_var namingContext_var =
      CosNaming::NamingContext::_narrow (namingObj_var.in (),
                                        TAO_TRY_ENV);
    TAO_CHECK_ENV;

    // get the IDL_Quoter naming context
    CosNaming::Name quoterContextName (1);  // max = 1 
    quoterContextName.length(1);
    quoterContextName[0].id = CORBA::string_dup ("IDL_Quoter");
    CORBA::Object_var quoterNamingObj_var = 
        namingContext_var->resolve (quoterContextName, TAO_TRY_ENV);
    TAO_CHECK_ENV;

    quoterNamingContext_var_ = 
      CosNaming::NamingContext::_narrow (quoterNamingObj_var.in (),
                                        TAO_TRY_ENV);
       

    // bind the QuoterFactory Finder to the IDL_Quoter naming context

    CosNaming::Name quoterFactoryFinderName_ (1);
    quoterFactoryFinderName_.length (1);
    quoterFactoryFinderName_[0].id = CORBA::string_dup ("QuoterFactoryFinder");
    this->quoterNamingContext_var_->bind (quoterFactoryFinderName_,
                                          (CORBA::Object *)this,
                                          TAO_TRY_ENV);
    TAO_CHECK_ENV;

  }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("SYS_EX");
    }
  TAO_ENDTRY;
}

// Destructor
QuoterFactoryFinder_i::~QuoterFactoryFinder_i () { 
}


CosLifeCycle::Factories *
QuoterFactoryFinder_i::find_factories (const CosLifeCycle::Key & factory_key,
                                       CORBA::Environment &_env_there) {

  CORBA::Environment env_here;

  // fill in the name of the Quoter Factory
  // CosNaming::Name factoryName (1);  // max = 1 
  // factoryName.length(1);
  // factoryName[0].id = CORBA::string_dup ("quoter_factory");
  // or
  CosNaming::Name factoryName = (CosNaming::Name) factory_key;


  // Try to get a reference to a Quoter Factory
  CORBA::Object_var quoterObject_var =  
    quoterNamingContext_var_->resolve (factoryName, env_here);
  
  // see if there is an exception, if yes then throw the NoFactory exception
  if (env_here.exception () != 0) {

    // throw a NoFactory exception  
    _env_there.exception (new CosLifeCycle::NoFactory (factory_key));      
    return 0;
  }

  // Check if it is a valid Quoter Factory reference
  if (CORBA::is_nil (quoterObject_var.in())) {

    // throw a NoFactory exception  
    _env_there.exception (new CosLifeCycle::NoFactory (factory_key));      
    return 0;
  }

  // were able to get a reference to Quoter Factory
  else { 

    // create a sequence of factories object
    CosLifeCycle::Factories *factories_ptr = new CosLifeCycle::Factories (1);      
    // using the Naming Service only one reference is available
    factories_ptr->length (1);      

    // dereference the Object pointer
    (*factories_ptr)[0] = quoterObject_var;

    ACE_DEBUG ((LM_DEBUG,"Have reference to a Quoter Factory.\n"));

    return factories_ptr;
  }
}

// Function get_options.

static u_int
get_options (int argc,
             char *argv [])
{
  // We need the 'O' in get_opt() because we also want to have ORB
  // parameters, they all start with 'O'.
  ACE_Get_Opt get_opt (argc, argv, "O?");
  int opt;

  while ((opt = get_opt ()) != EOF)
    {
      switch (opt) 
        {
        case '?':
          ACE_DEBUG ((LM_DEBUG,
                      "Usage: %s %s\n",
                      argv[0], usage));
          ACE_OS::exit (0);
          break;
        default:
          ACE_ERROR_RETURN ((LM_ERROR,
                             "%s: unknown arg, -%c\n"
                             "Usage: %s %s\n",
                             argv[0], char(opt),
                             argv[0],
                             usage),
                            1);
        }
    }

  if (argc != get_opt.optind)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%s: too many arguments\n"
                       "Usage: %s %s\n",
                       argv[0],
                       argv[0],
                       usage),
                      1);

  return 0;
}

// function main

int
main (int argc, char *argv [])
{
  TAO_TRY
    {
      // Initialize ORB.

      CORBA::ORB_var orb_var = CORBA::ORB_init (argc,
                                                argv,
                                                "internet",
                                                TAO_TRY_ENV);
      TAO_CHECK_ENV;

      // Connect to the RootPOA.
      CORBA::Object_var poa_object = orb_var->resolve_initial_references("RootPOA");
      if (CORBA::is_nil (poa_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to initialize the POA.\n"),
                          1);

      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (poa_object.in (), TAO_TRY_ENV);
      TAO_CHECK_ENV;

      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager (TAO_TRY_ENV);
      TAO_CHECK_ENV;

      // get the Options
      if (get_options (argc, argv))
        ACE_OS::exit (-1);

      // instantiate the QuoterFactoryFinder
      QuoterFactoryFinder_i *quoterFactoryFinder_i_ptr_;
      ACE_NEW_RETURN (quoterFactoryFinder_i_ptr_,
                      QuoterFactoryFinder_i (),
                      -1);

  
      ACE_DEBUG ((LM_DEBUG,
                  "Quoter Factory Finder is instantiated.\n"));

      // The POA Manager has to be activated before starting the ORB
      // event loop.
      poa_manager->activate (TAO_TRY_ENV);
      TAO_CHECK_ENV;

      // Run the ORB.
      if (orb_var->run () == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%p\n",
                           "CORBA::ORB::run"),
                          -1);
      TAO_CHECK_ENV;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("SYS_EX");
    }
  TAO_ENDTRY;

  return 0;
}