summaryrefslogtreecommitdiff
path: root/TAO/tao/Environment.cpp
blob: bdd144703c3e74d2575614b4858d601203da30cb (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// $Id$

#include "tao/Environment.h"
#include "tao/ORB_Core.h"
#include "tao/Exception.h"

#if !defined (__ACE_INLINE__)
# include "tao/Environment.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(tao, Environment, "$Id$")

#if 0
CORBA_Environment::CORBA_Environment (void)
  : exception_ (0),
    previous_ (TAO_ORB_Core_instance ()->default_environment ())
{
  TAO_ORB_Core_instance ()->default_environment (this);
}

CORBA_Environment::CORBA_Environment (const CORBA_Environment& rhs)
  : exception_ (rhs.exception_),
    previous_ (TAO_ORB_Core_instance ()->default_environment ())
{
  TAO_ORB_Core_instance ()->default_environment (this);
  exception_->_incr_refcnt ();
}

CORBA_Environment::CORBA_Environment (TAO_ORB_Core* orb_core)
  : exception_ (0),
    previous_ (orb_core->default_environment ())
{
  orb_core->default_environment (this);
}

#else

CORBA_Environment::CORBA_Environment (void)
  : exception_ (0),
    previous_ (0),
    refcount_ (1)
{
  //  TAO_ORB_Core_instance ()->default_environment (this);
}

CORBA_Environment::CORBA_Environment (const CORBA_Environment& rhs)
  : exception_ (rhs.exception_),
    previous_ (0),
    refcount_ (1)
{
  //  TAO_ORB_Core_instance ()->default_environment (this);
  if (this->exception_)
    this->exception_->_incr_refcnt ();
}

CORBA_Environment::CORBA_Environment (TAO_ORB_Core* orb_core)
  : exception_ (0),
    previous_ (orb_core->default_environment ())
{
  orb_core->default_environment (this);
}
#endif /* 0 */

CORBA::ULong
CORBA_Environment::_incr_refcnt (void)
{
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->refcount_lock_, 0);
  return refcount_++;
}

CORBA::ULong
CORBA_Environment::_decr_refcnt (void)
{
  {
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->refcount_lock_, 0);

    this->refcount_--;
    if (this->refcount_ != 0)
      return this->refcount_;
  }

  delete this;
  return 0;
}

CORBA_Environment&
CORBA_Environment::operator= (const CORBA_Environment& rhs)
{
  if (this != &rhs)
    {
      this->clear ();
      this->exception (rhs.exception_);
    }
  return *this;
}

CORBA_Environment::~CORBA_Environment (void)
{
  this->clear ();

  // If previous is 0 then this is the first Environment, allocated
  // with the ORB, it shouldn't try to pop because the ORB is beign
  // destroyed also.
  if (this->previous_ != 0)
    TAO_ORB_Core_instance ()->default_environment (this->previous_);
}

void
CORBA_Environment::exception (CORBA_Exception *ex)
{
  if (ex != this->exception_)
    {
      this->clear ();
      this->exception_ = ex;
    }
  if (this->exception_ != 0)
    {
      this->exception_->_incr_refcnt ();
#if defined (TAO_HAS_EXCEPTIONS)
      this->exception_->_raise ();
#endif /* TAO_HAS_EXCEPTIONS */
    }
}

void
CORBA_Environment::clear (void)
{
  if (this->exception_)
    this->exception_->_decr_refcnt ();

  this->exception_ = 0;
}

CORBA_Environment&
CORBA_Environment::default_environment ()
{
#if defined (TAO_HAS_EXCEPTIONS)
  //
  // If we are using native C++ exceptions the user is *not* supposed
  // to clear the environment every time she calls into TAO, in fact
  // the user is not supposed to use the environment at all!
  //
  // But TAO is using the default environment internally, thus
  // somebody has to clear it. Since TAO passes the environment around
  // this function should only be called when going from the user code
  // into TAO's code.
  //
  // This is not an issue when using the alternative C++ mapping (with
  // the Environment argument) because then the user is supposed to
  // clear the environment before calling into the ORB.
  //
  TAO_ORB_Core_instance ()->default_environment ()->clear ();
#endif /* TAO_HAS_EXCEPTIONS */

  return CORBA::default_environment ();
}

// Convenience -- say if the exception is a system exception or not.

int
CORBA::Environment::exception_type (void) const
{
  // @@ Carlos, is this stuff that's properly "transformed" for EBCDIC
  //    platforms?!
  // @@ Doug: Yes, they are used to compare against the _id() of the
  //    exception, which should have been mappend to the native
  //    codeset.  Notice the "should" we haven't tried that stuff yet,
  //    and i find it hard to keep track of all the transformations
  //    going on, specially for the TypeCodes that are generated by
  //    the IDL compiler vs. the ones hard-coded in
  //    $TAO_ROOT/tao/Typecode_Constants.cpp

  static char sysex_prefix [] = "IDL:omg.org/CORBA/";
  static char typecode_extra [] = "TypeCode/";
  static char poa_prefix [] = "IDL:PortableServer/";

  if (!this->exception_)
    return CORBA::NO_EXCEPTION;

  // All exceptions currently (CORBA 2.0) defined in the CORBA scope
  // are system exceptions ... except for a couple that are related to
  // TypeCodes.

  const char *id = this->exception_->_id ();

  if ((ACE_OS::strncmp (id,
                        sysex_prefix,
                        sizeof sysex_prefix - 1) == 0
       && ACE_OS::strncmp (id + sizeof sysex_prefix - 1,
                           typecode_extra,
                           sizeof typecode_extra - 1) != 0)
      || ACE_OS::strncmp (id,
                          poa_prefix,
                          sizeof poa_prefix - 1) == 0)
    return CORBA::SYSTEM_EXCEPTION;
  else
    return CORBA::USER_EXCEPTION;
}

const char*
CORBA_Environment::exception_id (void) const
{
  if (this->exception_ == 0)
    return 0;

  return this->exception_->_id ();
}

// Diagnostic utility routine: describe the exception onto the
// standard I/O stream passed as a parameter.

void
CORBA::Environment::print_exception (const char *info,
                                     FILE *) const
{
  if (this->exception_)
    {
      const char *id = this->exception_->_id ();

      ACE_DEBUG ((LM_ERROR,
                  "TAO: (%P|%t) EXCEPTION, %s\n",
                  info));

      CORBA::SystemException *x2 =
        CORBA_SystemException::_narrow (this->exception_);

      if (x2 != 0)
        x2->_tao_print_system_exception ();
      else
        // @@ we can use the exception's typecode to dump all the data
        // held within it ...

        ACE_DEBUG ((LM_ERROR,
                    "TAO: (%P|%t) user exception, ID '%s'\n",
                    id));
    }
  else
    ACE_DEBUG ((LM_ERROR,
                "TAO: (%P|%t) no exception, %s\n", info));
}

CORBA_Environment_var &
CORBA_Environment_var::operator= (CORBA_Environment_ptr p)
{
  if (this->ptr_ != p)
  {
    if (this->ptr_ != 0)
    delete (this->ptr_);

    this->ptr_ = p;
  }
  return *this;
}

CORBA_Environment_var &
CORBA_Environment_var::operator= (const CORBA_Environment_var &r)
{
  if (this->ptr_ != 0)
    {
      delete this->ptr_;
      this->ptr_ = 0;
    }

  ACE_NEW_RETURN (this->ptr_,
                  CORBA::Environment (*r.ptr_), *this);
  return *this;
}