summaryrefslogtreecommitdiff
path: root/TAO/examples/Quoter/Generic_Factory_i.cpp
blob: 52522d5ad8324b3f97a5eaa40b981b214b0eedd9 (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
// $Id$

// ============================================================================
//
// = FILENAME
//    Generic_Factory_i.cpp
//
// = DESCRIPTION
//    The implementation of a LifeCycle Service GenericFactory for the 
//    Quoter example.
//
// = AUTHOR
//    Michael Kircher (mk1@cs.wustl.edu)
// ============================================================================

#include "ace/Get_Opt.h"
#include "tao/corba.h"
#include "Generic_Factory_i.h"
#include "QuoterC.h"

ACE_RCSID(Quoter, Generic_Factory_i, "$Id$")

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

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

CORBA::Boolean 
Quoter_Generic_Factory_i::supports (const CosLifeCycle::Key &factory_key,
                                       CORBA::Environment &TAO_IN_ENV_there)
{
  ACE_UNUSED_ARG (factory_key);
  ACE_UNUSED_ARG (TAO_IN_ENV_there);

  return 0;
}



CosNaming::NamingContext_ptr
Quoter_Generic_Factory_i::get_naming_context (const CosLifeCycle::Key &factory_key,
                                                 CORBA::Environment &env_here,
                                                 CORBA::Environment &TAO_IN_ENV_there)
{
  // 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  
    { 
      TAO_IN_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  
    { 
      TAO_IN_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  
    { 
      TAO_IN_ENV_there.exception (new CosLifeCycle::NoFactory (factory_key));      
      return 0;
    }


  return CosNaming::NamingContext::_duplicate (quoterNamingContext_var.in ());
}


  
CORBA::Object_ptr
Quoter_Generic_Factory_i::create_object (const CosLifeCycle::Key &factory_key, 
                                            const CosLifeCycle::Criteria &the_criteria,
                                            CORBA::Environment &TAO_IN_ENV_there)
{
  ACE_UNUSED_ARG (the_criteria);

  CORBA::Environment env_here;

  CosNaming::NamingContext_var quoterNamingContext_var = 
    this->get_naming_context (factory_key,
                              env_here, 
                              TAO_IN_ENV_there);

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

  // Fill in the name of the Quoter Factory.
  // Take the key supplied to search for a Quoter Factory
  CosNaming::Name factory_Name = (CosNaming::Name) factory_key;  

  // Try to get a reference to a Quoter Factory
  CORBA::Object_var quoterFactoryObject_var =  
    quoterNamingContext_var->resolve (factory_Name, env_here);
  
  // See if there is an exception, if yes then throw the NoFactory
  // exception.
  if (env_here.exception () != 0) // throw a NoFactory exception  
    { 
      TAO_IN_ENV_there.exception (new CosLifeCycle::NoFactory (factory_key));      
      return 0;
    }

  // We 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  
      TAO_IN_ENV_there.exception (new CosLifeCycle::NoFactory (factory_key));      
      return 0;
    }
  else 
    { 
      // Check if it is a valid Quoter Factory reference.
      if (CORBA::is_nil (quoterFactoryObject_var.in ())) // throw a NoFactory exception.
        {      
          TAO_IN_ENV_there.exception (new CosLifeCycle::NoFactory (factory_key));      
          return 0;
        }

      Stock::Quoter_Factory_var factory_var = 
        Stock::Quoter_Factory::_narrow (quoterFactoryObject_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  
        { 
          TAO_IN_ENV_there.exception (new CosLifeCycle::NoFactory (factory_key));      
          return 0;
        }
    
      if (CORBA::is_nil (factory_var.in ()))      
        ACE_ERROR_RETURN ((LM_ERROR,
                           "invalid factory.\n"),
                          0);
                       
      ACE_DEBUG ((LM_DEBUG,
                  "Factory reference OK.\n"));    
      
      // Now retrieve the Quoter obj ref corresponding to the key.
      Stock::Quoter_var quoter_var =
        factory_var->create_quoter ("test",
                                    TAO_IN_ENV_there);
      ACE_DEBUG ((LM_DEBUG,
                  "Quoter Created\n"));
    
      if (CORBA::is_nil (quoter_var.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "null quoter objref returned by factory\n"),
                          0);

      ACE_DEBUG ((LM_DEBUG,
                  "Return a object reference to a new object.\n"));

      CORBA::Object_ptr object_ptr =
        CORBA::Object::_duplicate ((CORBA::Object_ptr) quoter_var.ptr ());

      return object_ptr;
    }
}