summaryrefslogtreecommitdiffstats
path: root/mcop/objectmanager.cc
blob: fc545fa69d21a4eca6013e67b791d69611ea5079 (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
    /*

    Copyright (C) 2000,2001 Stefan Westerfeld
                            [email protected]

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#include <config.h>
#include "mcoputils.h"
#include "mcopconfig.h"
#include "objectmanager.h"
#include "dispatcher.h"
#include "extensionloader.h"
#include "debug.h"
#include <stdio.h>
#include <unistd.h>
#include <iostream>

using namespace std;
using namespace Arts;

class Arts::ObjectManagerPrivate {
public:
	struct LoaderData {
		LoaderData() : init(false) { }
		bool init;
		Loader loader;
	};

	list<ExtensionLoader *> extensions;
	map<string, long> capabilities;
	map<string, LoaderData> loaders;
};

Object_skel *ObjectManager::create(const string& name)
{
	list<Factory *>::iterator i;

	/* first try: look through all factories we have */

	for(i = factories.begin();i != factories.end(); i++)
	{
		Factory *f = *i;
		if(f->interfaceName() == name ) return f->createInstance();
	}

	/* second try: look if there is a suitable extension we could load */

	TraderQuery query;
	query.supports("InterfaceName", name);
	vector<TraderOffer> *offers = query.query();

	vector<TraderOffer>::iterator oi;
	for(oi = offers->begin(); oi != offers->end(); oi++)
	{
		/*
		 * check whether we provide everything that this
		 * implementation requires to run
		 */
		bool requirementsOk = true;
		vector<string> *requires = oi->getProperty("Requires");

		vector<string>::iterator ri;
		for(ri = requires->begin(); ri != requires->end(); ri++)
		{
			if(d->capabilities[*ri] <= 0)
				requirementsOk = false;
		}
		delete requires;

		vector<string> *language = oi->getProperty("Language");
		vector<string> *libs = oi->getProperty("Library");

		if(language->size() == 0 && libs->size() == 1)
		{
			arts_warning("ObjectManager: %s - assuming 'Language=C++' because library given",name.c_str());
			language->push_back("C++");
		}
		if(language->size() != 1)
		{
			arts_warning("ObjectManager: %s - Language missing", name.c_str());
			requirementsOk = false;
		}
		if(requirementsOk && language->front() == "C++" && libs->empty())
		{
			arts_warning("ObjectManager: %s - Library missing", name.c_str());
			requirementsOk = false;
		}

		/* loading C++ components */
		if(requirementsOk && language->front() == "C++")
		{
			string library = libs->front();

			ExtensionLoader *e = new ExtensionLoader(library);
			if(e->success())
			{
				d->extensions.push_back(e);
				for(i = factories.begin();i != factories.end(); i++)
				{
					Factory *f = *i;
					if(f->interfaceName() == name)
					{
						delete language;
						delete libs;
						delete offers;
						return f->createInstance();
					}
				}
			}
			else
			{
				arts_warning("MCOP ObjectManager:"
					" Could not load extension %s.", library.c_str());
				delete e;
			}
		}
		delete libs;

		/* other languages/binary formats */
		if(requirementsOk)
		{
			string lang = language->front();
			ObjectManagerPrivate::LoaderData &ld = d->loaders[lang];

			if(!ld.init)
			{
				TraderQuery query;
				query.supports("Interface", "Arts::Loader");
				query.supports("LoadLanguage", lang);

				vector<TraderOffer> *loffers = query.query();
				
				// TODO: error checking for SubClass
				if(loffers->size() > 0)
				{
					ld.loader = SubClass(loffers->front().interfaceName());
				}
				else
				{
					ld.loader = Arts::Loader::null();
				}

				delete loffers;
			}
			ld.init = true;

			if(!ld.loader.isNull())
			{
				/*
				 * ### change when breaking BC:
				 * it's bad that ObjectManager has to return _skel()s, but
				 * well, can't change it now
				 */

				Object obj = ld.loader.loadObject(*oi);
				delete language;
				delete offers;
				return obj._base()->_copy()->_skel();
			}
		}

		delete language;
	}

	arts_warning("MCOP ObjectManager: can't find implementation for %s.",
							name.c_str());

	delete offers;
	return 0;
}

void ObjectManager::registerFactory(Factory *factory)
{
	factories.push_back(factory);
}

void ObjectManager::removeFactory(Factory *factory)
{
	list<Factory *>::iterator i;

	i = factories.begin();
	while(i != factories.end())
	{
		if(*i == factory) {
			factories.erase(i);
			i = factories.begin();
		}
		else i++;
	}
}

ObjectManager *ObjectManager::_instance = 0;

ObjectManager::ObjectManager()
{
	assert(!_instance);
	_instance = this;
	d = new ObjectManagerPrivate;
}

ObjectManager::~ObjectManager()
{
	// they should be unloaded before this object can be deleted
	assert(d->extensions.empty());
	delete d;
	assert(_instance);
	_instance = 0;
}

ObjectManager *ObjectManager::the()
{
	assert(_instance);
	return _instance;
}

void ObjectManager::shutdownExtensions()
{
	// give up references to the loaders
	d->loaders.clear();

	// shuts down all dynamically loaded extensions
	list<ExtensionLoader *>::iterator i;
	for(i=d->extensions.begin(); i != d->extensions.end(); i++)
		(*i)->shutdown();
}

void ObjectManager::removeExtensions()
{
	// unloads all dynamically loaded extensions
	list<ExtensionLoader *>::iterator i;
	for(i=d->extensions.begin(); i != d->extensions.end(); i++)
		delete *i;

	d->extensions.clear();
}

/*
 * global references
 */

bool ObjectManager::addGlobalReference(Object object, const string& name)
{
	bool result;

	result = Dispatcher::the()->globalComm().put(name,object.toString());
	if(result)
		referenceNames.push_back(name);

	return result;
}

string ObjectManager::getGlobalReference(const string& name)
{
	return Dispatcher::the()->globalComm().get(name);
}

void ObjectManager::removeGlobalReferences()
{
	list<string>::iterator i;

	for(i=referenceNames.begin(); i != referenceNames.end();i++)
		Dispatcher::the()->globalComm().erase(*i);
}

void ObjectManager::provideCapability(const string& name)
{
	d->capabilities[name]++;
}

void ObjectManager::removeCapability(const string& name)
{
	d->capabilities[name]--;
}