summaryrefslogtreecommitdiff
path: root/cpp/src/IceBox/ServiceManagerI.cpp
blob: 4fc49e510a04f2d8f78f7fd1d3a8d7ce9efb87ef (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// **********************************************************************
//
// Copyright (c) 2002
// Mutable Realms, 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::Service* (*SERVICE_FACTORY)(CommunicatorPtr);

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

    if(argc > 0)
    {
        _progName = argv[0];
    }

    for(int i = 1; i < argc; i++)
    {
        _argv.push_back(argv[i]);
    }

    PropertiesPtr properties = _communicator->getProperties();
    _options = properties->getCommandLineOptions();
}

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

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

int
IceBox::ServiceManagerI::run()
{
    try
    {
        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 defined in the property set
        // with the prefix "IceBox.Service.". These properties should
        // have the following format:
        //
        // IceBox.Service.Foo=entry_point [args]
        //
        const string prefix = "IceBox.Service.";
        PropertiesPtr properties = _communicator->getProperties();
        PropertyDict services = properties->getPropertiesForPrefix(prefix);
	PropertyDict::const_iterator p;
	for(p = services.begin(); p != services.end(); ++p)
	{
	    string name = p->first.substr(prefix.size());
	    const string& value = p->second;

            //
            // Separate the entry point from the arguments.
            //
            string entryPoint;
            StringSeq args;
            string::size_type pos = value.find_first_of(" \t\n");
            if(pos == string::npos)
            {
                entryPoint = value;
            }
            else
            {
                entryPoint = 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);
                    }
                }
            }

            init(name, entryPoint, args);
        }

        //
        // Invoke start() on the services.
        //
        map<string,ServiceInfo>::const_iterator r;
        for(r = _services.begin(); r != _services.end(); ++r)
        {
            try
            {
                r->second.service->start();
            }
            catch(const FailureException&)
            {
                throw;
            }
            catch(const Exception& ex)
            {
                FailureException e;
                e.reason = "ServiceManager: exception in start for service " + r->first + ": " + ex.ice_name();
                throw e;
            }
        }

        //
        // We may want to notify external scripts that the services
        // have started. This is done by defining the property:
        //
        // IceBox.PrintServicesReady=bundleName
        //
        // Where bundleName is whatever you choose to call this set of
        // services. It will be echoed back as "bundleName ready".
        //
        // This must be done after start() has been invoked on the
        // services.
        //
        string bundleName = properties->getProperty("IceBox.PrintServicesReady");
        if(!bundleName.empty())
        {
            cout << bundleName << " ready" << endl;
        }

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

        _communicator->waitForShutdown();

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

    return EXIT_SUCCESS;
}

IceBox::ServicePtr
IceBox::ServiceManagerI::init(const string& service, const string& entryPoint, const StringSeq& args)
{
    //
    // 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.
    //
    StringSeq serviceArgs;
    StringSeq::size_type j;
    for(j = 0; j < _options.size(); j++)
    {
        if(_options[j].find("--" + service + ".") == 0)
        {
            serviceArgs.push_back(_options[j]);
        }
    }
    for(j = 0; j < args.size(); j++)
    {
        serviceArgs.push_back(args[j]);
    }
    for(j = 0; j < _argv.size(); j++)
    {
        if(_argv[j].find("--" + service + ".") == 0)
        {
            serviceArgs.push_back(_argv[j]);
        }
    }

    //
    // Create the service property set.
    //
    PropertiesPtr serviceProperties = createProperties(serviceArgs);
    serviceArgs = serviceProperties->parseCommandLineOptions("Ice", serviceArgs);
    serviceArgs = serviceProperties->parseCommandLineOptions(service, serviceArgs);

    //
    // Load the entry point.
    //
    DynamicLibraryPtr library = new DynamicLibrary();
    DynamicLibrary::symbol_type sym = library->loadEntryPoint(entryPoint);
    if(sym == 0)
    {
        string msg = library->getErrorMessage();
        FailureException ex;
        ex.reason = "ServiceManager: unable to load entry point `" + entryPoint + "'";
        if(!msg.empty())
        {
            ex.reason += ": " + msg;
        }
        throw ex;
    }

    //
    // Invoke the factory function.
    //
    SERVICE_FACTORY factory = (SERVICE_FACTORY)sym;
    ServiceInfo info;
    try
    {
        info.service = factory(_communicator);
    }
    catch(const Exception& ex)
    {
        FailureException e;
        e.reason = "ServiceManager: exception in entry point `" + entryPoint + "': " + ex.ice_name();
        throw e;
    }
    catch (...)
    {
        FailureException e;
        e.reason = "ServiceManager: unknown exception in entry point `" + entryPoint + "'";
        throw e;
    }

    //
    // Invoke Service::init().
    //
    try
    {
        info.service->init(service, _communicator, serviceProperties, serviceArgs);
        info.library = library;
        _services[service] = info;
    }
    catch(const FailureException&)
    {
        throw;
    }
    catch(const Exception& ex)
    {
        FailureException e;
        e.reason = "ServiceManager: exception while initializing service " + service + ": " + ex.ice_name();
        throw e;
    }

    return info.service;
}

void
IceBox::ServiceManagerI::stop(const string& service)
{
    map<string,ServiceInfo>::iterator r = _services.find(service);
    assert(r != _services.end());
    ServiceInfo info = r->second;
    _services.erase(r);

    try
    {
        info.service->stop();
    }
    catch(const Exception& ex)
    {
        //
        // Release the service before the library
        //
        info.service = 0;
        info.library = 0;

        FailureException e;
        e.reason = "ServiceManager: exception in stop for service " + service + ": " + ex.ice_name();
        throw e;
    }

    //
    // Release the service before the library
    //
    info.service = 0;
    info.library = 0;
}

void
IceBox::ServiceManagerI::stopAll()
{
    map<string,ServiceInfo>::const_iterator r = _services.begin();
    while(r != _services.end())
    {
        try
        {
            stop((*r++).first);
        }
        catch(const FailureException& ex)
        {
            Error out(_logger);
            out << ex.reason;
        }
    }
    assert(_services.empty());
}