summaryrefslogtreecommitdiff
path: root/TAO/tests/Quoter/Factory_Finder_Impl.cpp
blob: 1987ccb2bfbc9af17d4ac9b9ded16cd2a9bd35bc (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
// $Id$

// ============================================================================
//
// = FILENAME
//    FactoryFinder_Impl.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 "Factory_Finder_Impl.h"
#include "QuoterC.h"

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

// Constructor
Quoter_Factory_Finder_Impl::Quoter_Factory_Finder_Impl (void)
{
}

// Destructor.
Quoter_Factory_Finder_Impl::~Quoter_Factory_Finder_Impl (void)
{
}


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

  // Get a reference to the ORB.
  CORBA::ORB_ptr orb_ptr = TAO_ORB_Core_instance ()->orb ();

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

  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 (),
                                       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;
    }

  // 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, 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;
    }

  CosNaming::NamingContext_var quoterNamingContext_var = 
    CosNaming::NamingContext::_narrow (quoterNamingObj_var.in (),
                                       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;
    }
  

  // **  now a proper reference to the quoter naming context is available

  // fill in the name of the Quoter Factory
  //CosNaming::Name factoryName (1);  // max = 1 
  //factoryName.length (1);
  //factoryName[0].id = CORBA::string_dup ("Quoter_Generic_Factory");
  // for direct contact with the Quoter Factory
  // factoryName[0].id = CORBA::string_dup ("Quoter_Factory");
    // or
  // Take the key supplied to search for a Quoter Factory
  CosNaming::Name factoryName = (CosNaming::Name) factory_key;

  // Try to get a reference to a Quoter Factory
  CORBA::Object_var quoterFactoryObject_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;
    }

  // were able to get a reference to Quoter Factory

  // Check if it is a valid Quoter Factory reference
  if (CORBA::is_nil (quoterFactoryObject_var.in())) 
    { // throw a NoFactory exception  
      _env_there.exception (new CosLifeCycle::NoFactory (factory_key));      
      return 0;
    }
  else 
    {

      // create a sequence of factories object
      CosLifeCycle::Factories *factories_ptr = new CosLifeCycle::Factories (1);      

      // 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;
        }

      // using the Naming Service only one reference is available
      factories_ptr->length (1);      

      // Check if it is a valid Quoter Factory reference.
      if (CORBA::is_nil (quoterFactoryObject_var.in ())) // throw a NoFactory exception.
        {      
          _env_there.exception (new CosLifeCycle::NoFactory (factory_key));      
          return 0;
        }

      // insert the object reference 
      (*factories_ptr)[0] = CORBA::Object::_duplicate (quoterFactoryObject_var.ptr());

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