Exemplo n.º 1
0
void ClassLoaderTest::testClassLoader3()
{
	std::string path = "TestLibrary";
	path.append(SharedLibrary::suffix());

	ClassLoader<TestPlugin> cl;
	cl.loadLibrary(path);
	cl.loadLibrary(path);
	cl.unloadLibrary(path);
	
	assert (cl.manifestFor(path).size() == 3);
	
	ClassLoader<TestPlugin>::Iterator it = cl.begin();
	assert (it != cl.end());
	assert (it->first == path);
	assert (it->second->size() == 3);
	++it;
	assert (it == cl.end());
	
	cl.unloadLibrary(path);
	assertNullPtr (cl.findManifest(path));
}
Exemplo n.º 2
0
void ClassLoaderTest::testClassLoader2()
{
	std::string path = "TestLibrary";
	path.append(SharedLibrary::suffix());

	ClassLoader<TestPlugin> cl;
	cl.loadLibrary(path);

	assert (cl.begin() != cl.end());
	assertNotNullPtr (cl.findClass("PluginA"));
	assertNotNullPtr (cl.findClass("PluginB"));
	assertNotNullPtr (cl.findClass("PluginC"));
	assertNotNullPtr (cl.findManifest(path));
	
	assert (cl.isLibraryLoaded(path));
	assert (cl.manifestFor(path).size() == 3);
	
	ClassLoader<TestPlugin>::Iterator it = cl.begin();
	assert (it != cl.end());
	assert (it->first == path);
	assert (it->second->size() == 3);
	++it;
	assert (it == cl.end());
	
	TestPlugin* pPluginA = cl.classFor("PluginA").create();
	assert (pPluginA->name() == "PluginA");
	assert (!cl.classFor("PluginA").isAutoDelete(pPluginA));
	delete pPluginA;

	TestPlugin* pPluginB = cl.classFor("PluginB").create();
	assert (pPluginB->name() == "PluginB");
	delete pPluginB;
	
	pPluginB = cl.create("PluginB");
	assert (pPluginB->name() == "PluginB");
	delete pPluginB;
	
	assert (cl.canCreate("PluginA"));
	assert (cl.canCreate("PluginB"));
	assert (!cl.canCreate("PluginC"));

	TestPlugin& pluginC = cl.instance("PluginC");
	assert (pluginC.name() == "PluginC");
	
	try
	{
		TestPlugin& plgB = cl.instance("PluginB");
		fail("not a singleton - must throw");
	}
	catch (InvalidAccessException&)
	{
	}
	
	try
	{
		TestPlugin* pPluginC = cl.create("PluginC");
		fail("cannot create a singleton - must throw");
	}
	catch (InvalidAccessException&)
	{
	}

	try
	{
		const AbstractMetaObject<TestPlugin>& meta = cl.classFor("PluginC");
		meta.autoDelete(&(meta.instance()));
		fail("cannot take ownership of a singleton - must throw");
	}
	catch (InvalidAccessException&)
	{
	}
	
	const AbstractMetaObject<TestPlugin>& meta1 = cl.classFor("PluginC");
	assert (meta1.isAutoDelete(&(meta1.instance())));

	// the following must not produce memory leaks
	const AbstractMetaObject<TestPlugin>& meta2 = cl.classFor("PluginA");
	meta2.autoDelete(meta2.create());
	meta2.autoDelete(meta2.create());

	TestPlugin* pPlugin = meta2.create();
	meta2.autoDelete(pPlugin);
	assert (meta2.isAutoDelete(pPlugin));
	meta2.destroy(pPlugin);
	assert (!meta2.isAutoDelete(pPlugin));

	cl.unloadLibrary(path);
}
Object AbstractContextI::lookupObjectFactory(const String& name,
											 bool& createdbyfactory,
											 String& redirectedname) const throw (NamingException)
{
	try
	{
		redirectedname = name;
		createdbyfactory = false;
		Object object = lookupWithSyntaxCheck(name, false);
		StringAnything referencelink;
		if (object->instanceOf(referencelink))
		{
			if (isBound(referencelink->toString()))
			{
				object = lookupURL(referencelink->toString());
				redirectedname = referencelink->toString();
			}
		}
		MapAnything objectfactory;
		if (object->instanceOf(objectfactory))
		{
			if (objectfactory->containsKey(L"class") && objectfactory->containsKey(L"library"))
			{
				bool load = true;
				if (objectfactory->containsKey(L"load")) load = objectfactory->get(L"load");
				if (load)
				{
					ClassLoader classloader;
					String clazzname = objectfactory->get(L"class");

					Anything library = objectfactory->get(L"library");
					if (Anything::ANY_STRING == library->type())
					{
						StringBuffer buf = library->toString();
						if (0 == buf->indexOf(L"http://"))
						{
							classloader = new URLClassLoaderI();
						}
						else
						{
							classloader = new ClassLoaderI();
						}
						classloader->loadLibrary(library);
					}
					else
					{
						if (Anything::ANY_LIST == library->type())
						{
							classloader = new URLClassLoaderI();
							Iterator<Anything> i = library->iterator();
							while (i->hasNext())
							{
								String library = i->next();
								classloader->loadLibrary(library);
							}
						}
					}
					object = classloader->loadClass(clazzname)->newInstance();
					const_cast<AbstractContextI*>(this)->rebind(name, object);
					createdbyfactory = true;
					const_cast<AbstractContextI*>(this)->bind(
						name + L"/" + name->toMD5(), new StringAnythingI(redirectedname));
				}
			}
		}
		else
		{
			createdbyfactory = isBound(name + L"/" + name->toMD5());
			if (createdbyfactory) redirectedname = lookup(name + L"/" + name->toMD5())->toString(); 
		}
		return object;
	}
	catch (const NamingException&)
	{
		throw;
	}
	catch (const Exception& e)
	{
		throw NamingException(WITHDETAILS(e->toString()));;
	}
}