summaryrefslogtreecommitdiff
path: root/cpp/src/IceBox/ServiceManagerI.cpp
blob: 99ae44a97355b1a113f6197d59b296d85779b9b8 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// **********************************************************************
//
// Copyright (c) 2002
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <Ice/Ice.h>
#include <Ice/DynamicLibrary.h>
#include <IceBox/ServiceManagerI.h>

using namespace Ice;
using namespace IceInternal;
using namespace std;

typedef IceBox::ServicePtr (*SERVICE_FACTORY)(CommunicatorPtr);

IceBox::ServiceManagerI::ServiceManagerI(CommunicatorPtr communicator)
    : _communicator(communicator)
{
}

IceBox::ServiceManagerI::~ServiceManagerI()
{
}

void
IceBox::ServiceManagerI::shutdown(const Current& current)
{
    _communicator->shutdown();
}

int
IceBox::ServiceManagerI::run(int& argc, char* argv[])
{
    try
    {
        _logger = _communicator->getLogger();

        ServiceManagerPtr obj = this;

        //
        // Create an object adapter. Services probably should NOT share
        // this object adapter, as the endpoint(s) for this object adapter
        // will most likely need to be firewalled for security reasons.
        //
        ObjectAdapterPtr adapter = _communicator->createObjectAdapterFromProperty("ServiceManagerAdapter",
                                                                                  "IceBox.ServiceManager.Endpoints");
        adapter->add(obj, stringToIdentity("ServiceManager"));

        //
        // Load and initialize the services.
        //
        if (!initServices(argc, argv))
        {
            stopServices();
            return EXIT_FAILURE;
        }

        //
        // Invoke start() on the services.
        //
        if (!startServices())
        {
            stopServices();
            return EXIT_FAILURE;
        }

        //
        // Start request dispatching after we've started the services.
        //
        adapter->activate();

        _communicator->waitForShutdown();

        //
        // Invoke stop() on the services.
        //
        stopServices();
    }
    catch (const Exception& ex)
    {
        Error out(_logger);
        out << "ServiceManager: " << ex;
        stopServices();
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

bool
IceBox::ServiceManagerI::initServices(int& argc, char* argv[])
{
    //
    // Retrieve all properties with the prefix "IceBox.Service.".
    // These properties should have the following format:
    //
    // IceBox.Service.Foo=Package.Foo [args]
    //
    const string prefix = "IceBox.Service.";
    PropertiesPtr properties = _communicator->getProperties();
    StringSeq allOptions = properties->getCommandLineOptions();
    StringSeq arr = properties->getProperties(prefix);
    for (StringSeq::size_type i = 0; i < arr.size(); i += 2)
    {
        string name = arr[i].substr(prefix.size());
        string value = arr[i + 1];

        //
        // Separate the factory function from the arguments.
        //
        string factoryFunc;
        StringSeq args;
        string::size_type pos = value.find_first_of(" \t\n");
        if (pos == string::npos)
        {
            factoryFunc = value;
        }
        else
        {
            factoryFunc = value.substr(0, pos);
            string::size_type beg = value.find_first_not_of(" \t\n", pos);
            while (beg != string::npos)
            {
                string::size_type end = value.find_first_of(" \t\n", beg);
                if (end == string::npos)
                {
                    args.push_back(value.substr(beg));
                    beg = end;
                }
                else
                {
                    args.push_back(value.substr(beg, end - beg));
                    beg = value.find_first_not_of(" \t\n", end);
                }
            }
        }

        //
        // Now we need to create a property set to pass to init().
        // The property set is populated from a number of sources.
        // The precedence order (from lowest to highest) is:
        //
        // 1. Properties defined in the server property set (e.g.,
        //    that were defined in the server's configuration file)
        // 2. Service arguments
        // 3. Server arguments
        //
        // We'll compose an array of arguments in the above order.
        //
        vector<string> l;
        StringSeq::size_type j;
        int k;
        for (j = 0; j < allOptions.size(); j++)
        {
            if (allOptions[j].find("--" + name + ".") == 0)
            {
                l.push_back(allOptions[j]);
            }
        }
        for (j = 0; j < args.size(); j++)
        {
            l.push_back(args[j]);
        }
        for (k = 1; k < argc; k++)
        {
            string s = argv[k];
            if (s.find("--" + name + ".") == 0)
            {
                l.push_back(s);
            }
        }

        //
        // Create the service property set.
        //
        addArgumentPrefix(name);
        int serviceArgc = static_cast<int>(l.size() + 1);
        char** serviceArgv = new char*[serviceArgc + 1];
        serviceArgv[0] = argv[0];
        for (k = 1; k < serviceArgc; k++)
        {
            serviceArgv[k] = const_cast<char*>(l[k - 1].c_str());
        }
        PropertiesPtr serviceProperties = createProperties(serviceArgc, serviceArgv);
        StringSeq serviceArgs;
        for (k = 1; k < serviceArgc; k++)
        {
            serviceArgs.push_back(serviceArgv[k]);
        }
        delete[] serviceArgv;

        //
        // Load the dynamic library.
        //
        string::size_type colon = factoryFunc.rfind(':');
        if (colon == string::npos || colon == factoryFunc.size() - 1)
        {
            Error out(_logger);
            out << "ServiceManager: invalid factory function `" << factoryFunc << "'";
            return false;
        }
        string libName = factoryFunc.substr(0, colon);
        string funcName = factoryFunc.substr(colon + 1);
        DynamicLibraryPtr library = new DynamicLibrary();
        if (!library->load(libName))
        {
            string msg = library->getErrorMessage();
            Error out(_logger);
            out << "ServiceManager: unable to load library `" << libName << "'";
            if (!msg.empty())
            {
                out << ": " << msg;
            }
            return false;
        }

        //
        // Lookup the factory function and invoke it.
        //
        DynamicLibrary::symbol_type sym = library->getSymbol(funcName);
        if (sym == 0)
        {
            string msg = library->getErrorMessage();
            Error out(_logger);
            out << "ServiceManager: unable to load symbol `" << funcName << "'";
            if (!msg.empty())
            {
                out << ": " << msg;
            }
            return false;
        }
        SERVICE_FACTORY factory = (SERVICE_FACTORY)sym;
        ServicePtr service;
        try
        {
            service = factory(_communicator);
        }
        catch (const Exception& ex)
        {
            Error out(_logger);
            out << "ServiceManager: exception in factory function `" << funcName << "': " << ex;
            return false;
        }

        //
        // Invoke Service::init().
        //
        try
        {
            service->init(name, _communicator, serviceProperties, serviceArgs);
            _services[name] = service;
            _libraries.push_back(library);
        }
        catch (const ServiceFailureException& ex)
        {
            Error out(_logger);
            out << "ServiceManager: initialization failed for service " << name;
            return false;
        }
        catch (const Exception& ex)
        {
            Error out(_logger);
            out << "ServiceManager: exception while initializing service " << name << ": " << ex;
            return false;
        }
    }

    return true;
}

bool
IceBox::ServiceManagerI::startServices()
{
    map<string,ServicePtr>::const_iterator r;
    for (r = _services.begin(); r != _services.end(); ++r)
    {
        try
        {
            (*r).second->start();
        }
        catch (const ServiceFailureException& ex)
        {
            Error out(_logger);
            out << "ServiceManager: start failed for service " << (*r).first;
            return false;
        }
        catch (const Exception& ex)
        {
            Error out(_logger);
            out << "ServiceManager: exception in start for service " << (*r).first << ": " << ex;
            return false;
        }
    }

    return true;
}

void
IceBox::ServiceManagerI::stopServices()
{
    map<string,ServicePtr>::const_iterator r;
    for (r = _services.begin(); r != _services.end(); ++r)
    {
        try
        {
            (*r).second->stop();
        }
        catch (const Exception& ex)
        {
            Error out(_logger);
            out << "ServiceManager: exception in stop for service " << (*r).first << ": " << ex;
        }
    }
    _services.clear();
}