예제 #1
0
//----------------------------------------
//	main
//----------------------------------------
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);

	ScopedLogMessage msg("SharedLibraryTest ", "start", "end");

	std::string path(kSharedLibraryName);
	path.append(Poco::SharedLibrary::suffix());
	Poco::SharedLibrary library;
	try
	{
		library.load(path);
		if(library.isLoaded())
		{
			msg.Message(Poco::format(" \"%s\" is loaded", path));
			std::string str;
			try
			{
				HelloFunc func = reinterpret_cast<HelloFunc>(library.getSymbol(kFunctionName));
				func(str);
				library.unload();
				msg.Message(Poco::format(" %s", str));
			}
			catch(Poco::NotFoundException& exc)
			{
				msg.Message(Poco::format(" %s \"%s\"", std::string(exc.name()), kFunctionName));
			}
		}
	}
	catch(Poco::LibraryLoadException& exc)
	{
		msg.Message(Poco::format(" %s \"%s\"", std::string(exc.name()), path));
	}

	return 0;
}
	BundleMetadata const& BundleLoader::loadLibrary( string const& path )
	{
		if ( isLibraryLoaded( path ) )
		{
			BundleInfoConstIterator it = m_LoadedBundles.find( path );
			return it->second.metainfo->getBundleData();
		}

		typedef void ( *MetainfoFunc )( bundle::BundleMetainfo &info );

		Poco::SharedLibrary *lib;
		try
		{
			lib = new Poco::SharedLibrary( path );
		}
		catch ( Poco::Exception &e )
		{
			ostringstream msg;
			msg << e.what() << " " << e.message() << " lib could not be loaded";
			throw NotFoundException( msg.str() );
		}

		Metainfo *info = new Metainfo();

		if ( lib->hasSymbol( "getBundleMetainfo" ) )
		{
			try
			{
				BundleInfo bundleInfo;
				MetainfoFunc func = ( MetainfoFunc ) lib->getSymbol( "getBundleMetainfo" );

				bundle::BundleMetainfo metainfo( *info );
				func( metainfo );
				bundleInfo.library = lib;
				bundleInfo.metainfo = info;

				bundleInfo.metainfo->setInstallDirectory( path );
				bundleInfo.metainfo->cleanup();

				m_LoadedBundles.insert( make_pair( path, bundleInfo ) );
			}
			catch ( Exception &e )
			{
				delete lib;
				delete info;
				throw e;
			}
			BundleMetadata const& meta = info->getBundleData();
			if ( meta.getName() == "undefined" )
			{
				ostringstream msg;
				msg << "bundle " << meta.getInstallDirectory() << " does not define a name";
				throw NotFoundException( msg.str() );
			}

			return meta;
		}
		else
		{
			delete lib;
			delete info;

			ostringstream msg;
			msg << "shared library " << path << " does not contain metadata";
			throw NotFoundException( msg.str() );
		}
	}