summaryrefslogtreecommitdiff
path: root/TAO/tao/CORBANAME_Parser.cpp
blob: 2b934e81b41f49b836047069b0a44d76c4abbcf3 (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
// -*- C++ -*-
#include "tao/CORBANAME_Parser.h"

#if (TAO_HAS_CORBANAME_PARSER == 1)

#include "tao/ORB.h"
#include "tao/Object.h"
#include "tao/SystemException.h"
#include "tao/UB_String_Arguments.h"
#include "tao/Invocation_Adapter.h"
#include "tao/debug.h"

#include "ace/Log_Msg.h"
#include "ace/SString.h"
#include "ace/OS_NS_string.h"

static const char corbaname_prefix[] = "corbaname:";

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

bool
TAO_CORBANAME_Parser::match_prefix (const char *ior_string) const
{
  return (ACE_OS::strncmp (ior_string,
                           corbaname_prefix,
                           sizeof corbaname_prefix - 1) == 0);
}

CORBA::Object_ptr
TAO_CORBANAME_Parser::
parse_string_dynamic_request_helper (CORBA::Object_ptr naming_context,
                                     ACE_CString &key_string)
{
  TAO::Arg_Traits<CORBA::Object>::ret_val _tao_retval;
  TAO::Arg_Traits<CORBA::Char *>::in_arg_val _tao_id (key_string.c_str ());

  TAO::Argument *_tao_signature [] =
      {
        &_tao_retval,
        &_tao_id
      };

  TAO::Invocation_Adapter tao_call (naming_context,
                                    _tao_signature,
                                    2,
                                    "resolve_str",
                                    11,
                                    TAO::TAO_CO_NONE | TAO::TAO_CO_THRU_POA_STRATEGY);

  tao_call.invoke (nullptr, 0);

  return _tao_retval.retn ();
}

CORBA::Object_ptr
TAO_CORBANAME_Parser::parse_string (const char *ior, CORBA::ORB_ptr orb)
{

  // Skip the prefix, we know it is there because this method in only
  // called if <match_prefix> returns 1.
  const char *corbaname =
    ior + sizeof corbaname_prefix - 1;

  CORBA::Object_ptr obj = CORBA::Object::_nil ();

  try
    {
      // The position of the seperator between the obj_addr and key
      // string
      ACE_CString::size_type pos_seperator = 0;

      ACE_CString corbaname_str (corbaname, nullptr, 1);

      pos_seperator = corbaname_str.find ("#", 0);

      // Get the Key String
      ACE_CString key_string;

      if (pos_seperator != ACE_CString::npos)
        {
          key_string = corbaname_str.substring (pos_seperator + 1,
                                                ACE_CString::npos);
        }

      // Prepare a suitable corbaloc string for the name service.
      // CORBALOC assumes "NameService" for the object key if none
      // is provided, so just pass everything between "corbaname:"
      // and "#" as the address
      ACE_CString corbaloc_addr ("corbaloc:", nullptr, 1);
      corbaloc_addr += corbaname_str.substring (0, pos_seperator);

      // Obtain a reference to the naming context
      CORBA::Object_var name_context =
        orb->string_to_object (corbaloc_addr.c_str ());

      // Check if the Object reference is nil.
      if (CORBA::is_nil (name_context.in ()))
        TAOLIB_ERROR_RETURN ((LM_ERROR,
                           "Cannot resolve Naming Service: CORBANAME_Parser\n"),
                          0);

      CORBA::Boolean is_a =
        name_context->_is_a ("IDL:omg.org/CosNaming/NamingContextExt:1.0");

      if (!is_a)
        {
          TAOLIB_ERROR_RETURN ((LM_ERROR,
                             "Cannot narrow Naming Service: "
                             "CORBANAME_Parser\n"),
                            0);
        }

      if (key_string.length () != 0)
        {

          // Make a dynamic request for resolve_str in this naming context
          obj = this->parse_string_dynamic_request_helper (name_context.in (),
                                                           key_string);
        }
      else
        { // There was no key string which implies that the caller wants
          // the object reference of the naming service.
          obj = name_context._retn ();
        }
    }
  catch (const ::CORBA::SystemException& ex)
    {
      if (TAO_debug_level >= 4)
        {
          ex._tao_print_exception ("CORBANAME_Parser");
        }
    }

  return obj;
}


ACE_STATIC_SVC_DEFINE (TAO_CORBANAME_Parser,
                       ACE_TEXT ("CORBANAME_Parser"),
                       ACE_SVC_OBJ_T,
                       &ACE_SVC_NAME (TAO_CORBANAME_Parser),
                       ACE_Service_Type::DELETE_THIS |
                                  ACE_Service_Type::DELETE_OBJ,
                       0)

ACE_FACTORY_DEFINE (TAO, TAO_CORBANAME_Parser)

TAO_END_VERSIONED_NAMESPACE_DECL

#endif /* TAO_HAS_CORBANAME_PARSER == 1 */