summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/PropertiesAdminI.cpp
blob: 5e254bf4e0f4e18242d03fb69f7d2f614e13b26e (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
268
269
270
// **********************************************************************
//
// Copyright (c) 2003-present ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Ice/PropertiesAdminI.h>
#include <Ice/Instance.h>
#include <Ice/Logger.h>
#include <Ice/LoggerUtil.h>
#include <Ice/ThreadPool.h>

using namespace std;
using namespace Ice;

namespace
{

const char* traceCategory = "Admin.Properties";

}

#ifndef ICE_CPP11_MAPPING
PropertiesAdminUpdateCallback::~PropertiesAdminUpdateCallback()
{
    // Out of line to avoid weak vtable
}
#endif

NativePropertiesAdmin::~NativePropertiesAdmin()
{
    // Out of line to avoid weak vtable
}

namespace IceInternal
{

PropertiesAdminI::PropertiesAdminI(const InstancePtr& instance) :
    _properties(instance->initializationData().properties),
    _logger(instance->initializationData().logger)
{
}

string
#ifdef ICE_CPP11_MAPPING
PropertiesAdminI::getProperty(string name, const Current&)
#else
PropertiesAdminI::getProperty(const string& name, const Current&)
#endif
{
    Lock sync(*this);
    return _properties->getProperty(name);
}

PropertyDict
#ifdef ICE_CPP11_MAPPING
PropertiesAdminI::getPropertiesForPrefix(string prefix, const Current&)
#else
PropertiesAdminI::getPropertiesForPrefix(const string& prefix, const Current&)
#endif
{
    Lock sync(*this);
    return _properties->getPropertiesForPrefix(prefix);
}

void
#ifdef ICE_CPP11_MAPPING
PropertiesAdminI::setProperties(PropertyDict props, const Current&)
#else
PropertiesAdminI::setProperties(const PropertyDict& props, const Current&)
#endif
{
    Lock sync(*this);

    PropertyDict old = _properties->getPropertiesForPrefix("");
    PropertyDict::const_iterator p;
    const int traceLevel = _properties->getPropertyAsInt("Ice.Trace.Admin.Properties");

    //
    // Compute the difference between the new property set and the existing property set:
    //
    // 1) Any properties in the new set that were not defined in the existing set.
    //
    // 2) Any properties that appear in both sets but with different values.
    //
    // 3) Any properties not present in the new set but present in the existing set.
    //    In other words, the property has been removed.
    //
    PropertyDict added, changed, removed;
    for(p = props.begin(); p != props.end(); ++p)
    {
        PropertyDict::iterator q = old.find(p->first);
        if(q == old.end())
        {
            if(!p->second.empty())
            {
                //
                // This property is new.
                //
                added.insert(*p);
            }
        }
        else
        {
            if(p->second != q->second)
            {
                if(p->second.empty())
                {
                    //
                    // This property was removed.
                    //
                    removed.insert(*p);
                }
                else
                {
                    //
                    // This property has changed.
                    //
                    changed.insert(*p);
                }
            }
        }
    }

    if(traceLevel > 0 && (!added.empty() || !changed.empty() || !removed.empty()))
    {
        Trace out(_logger, traceCategory);

        out << "Summary of property changes";

        if(!added.empty())
        {
            out << "\nNew properties:";
            for(p = added.begin(); p != added.end(); ++p)
            {
                out << "\n  " << p->first;
                if(traceLevel > 1)
                {
                    out << " = " << p->second;
                }
            }
        }

        if(!changed.empty())
        {
            out << "\nChanged properties:";
            for(p = changed.begin(); p != changed.end(); ++p)
            {
                out << "\n  " << p->first;
                if(traceLevel > 1)
                {
                    out << " = " << p->second << " (old value = " << _properties->getProperty(p->first) << ")";
                }
            }
        }

        if(!removed.empty())
        {
            out << "\nRemoved properties:";
            for(p = removed.begin(); p != removed.end(); ++p)
            {
                out << "\n  " << p->first;
            }
        }
    }

    //
    // Update the property set.
    //

    for(p = added.begin(); p != added.end(); ++p)
    {
        _properties->setProperty(p->first, p->second);
    }

    for(p = changed.begin(); p != changed.end(); ++p)
    {
        _properties->setProperty(p->first, p->second);
    }

    for(p = removed.begin(); p != removed.end(); ++p)
    {
        _properties->setProperty(p->first, "");
    }

    if(!_updateCallbacks.empty())
    {
        PropertyDict changes = added;
        changes.insert(changed.begin(), changed.end());
        changes.insert(removed.begin(), removed.end());

        // Copy callbacks to allow callbacks to update callbacks
#ifdef ICE_CPP11_MAPPING
        auto callbacks = _updateCallbacks;
        for(const auto& cb : callbacks)
#else
        vector<PropertiesAdminUpdateCallbackPtr> callbacks = _updateCallbacks;
        for(vector<PropertiesAdminUpdateCallbackPtr>::const_iterator q = callbacks.begin(); q != callbacks.end(); ++q)
#endif
        {
            try
            {
#ifdef ICE_CPP11_MAPPING
                cb(changes);
#else
                (*q)->updated(changes);
#endif
            }
            catch(const std::exception& ex)
            {
                if(_properties->getPropertyAsIntWithDefault("Ice.Warn.Dispatch", 1) > 1)
                {
                    Warning out(_logger);
                    out << "properties admin update callback raised unexpected exception:\n" << ex;
                }
            }
            catch(...)
            {
                if(_properties->getPropertyAsIntWithDefault("Ice.Warn.Dispatch", 1) > 1)
                {
                    Warning out(_logger);
                    out << "properties admin update callback raised unexpected exception:\nunknown c++ exception";
                }
            }
        }
    }
}

#ifdef ICE_CPP11_MAPPING

std::function<void()>
PropertiesAdminI::addUpdateCallback(std::function<void(const Ice::PropertyDict&)> cb)
{
    Lock sync(*this);

    auto p = _updateCallbacks.insert(_updateCallbacks.end(), std::move(cb));
    auto propertiesAdmin = shared_from_this();

    return [p, propertiesAdmin] { propertiesAdmin->removeUpdateCallback(p); };
}

void
PropertiesAdminI::removeUpdateCallback(std::list<std::function<void(const Ice::PropertyDict&)>>::iterator p)
{
    Lock sync(*this);
    _updateCallbacks.erase(p);
}

#else

void
PropertiesAdminI::addUpdateCallback(const PropertiesAdminUpdateCallbackPtr& cb)
{
    Lock sync(*this);
    _updateCallbacks.push_back(cb);
}

void
PropertiesAdminI::removeUpdateCallback(const PropertiesAdminUpdateCallbackPtr& cb)
{
    Lock sync(*this);
    _updateCallbacks.erase(remove(_updateCallbacks.begin(), _updateCallbacks.end(), cb), _updateCallbacks.end());
}

#endif

}