diff options
Diffstat (limited to 'cpp/src/IceGrid/FreezeDB/FreezeDB.cpp')
-rw-r--r-- | cpp/src/IceGrid/FreezeDB/FreezeDB.cpp | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/cpp/src/IceGrid/FreezeDB/FreezeDB.cpp b/cpp/src/IceGrid/FreezeDB/FreezeDB.cpp new file mode 100644 index 00000000000..ee0707d35d2 --- /dev/null +++ b/cpp/src/IceGrid/FreezeDB/FreezeDB.cpp @@ -0,0 +1,179 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +#include <IceGrid/FreezeDB/FreezeDB.h> + +#include <IceGrid/FreezeDB/StringApplicationInfoDict.h> +#include <IceGrid/FreezeDB/StringAdapterInfoDict.h> +#include <IceGrid/FreezeDB/IdentityObjectInfoDict.h> + +#include <sys/types.h> +#include <sys/stat.h> + +#ifdef _WIN32 +# include <direct.h> +# ifdef _MSC_VER +# define S_ISDIR(mode) ((mode) & _S_IFDIR) +# define S_ISREG(mode) ((mode) & _S_IFREG) +# endif +#else +# include <unistd.h> +#endif + +using namespace IceGrid; +using namespace std; + +extern "C" +{ + +ICE_DECLSPEC_EXPORT ::Ice::Plugin* +createFreezeDB(const Ice::CommunicatorPtr& communicator, const string& name, const Ice::StringSeq& args) +{ + return new IceGrid::FreezeDBPlugin(communicator); +} + +} + +namespace +{ + +// +// Freeze wrappers for Freeze dictionaries +// + +class FreezeApplicationsWrapper : public FreezeDB::Wrapper<StringApplicationInfoDict, std::string, ApplicationInfo>, + public ApplicationsWrapper +{ +public: + + FreezeApplicationsWrapper(const Freeze::ConnectionPtr& connection, const std::string& dbName) : + FreezeDB::Wrapper<StringApplicationInfoDict, std::string, ApplicationInfo>(connection, dbName) + { + } +}; + +class FreezeAdaptersWrapper : public FreezeDB::Wrapper<StringAdapterInfoDict, std::string, AdapterInfo>, + public AdaptersWrapper +{ +public: + + FreezeAdaptersWrapper(const Freeze::ConnectionPtr& connection, const std::string& dbName) : + FreezeDB::Wrapper<StringAdapterInfoDict, std::string, AdapterInfo>(connection, dbName) + { + } + + std::vector<AdapterInfo> + findByReplicaGroupId(const std::string& name) + { + std::vector<AdapterInfo> result; + for(StringAdapterInfoDict::const_iterator p = _dict.findByReplicaGroupId(name, true); p != _dict.end(); ++p) + { + result.push_back(p->second); + } + return result; + } +}; + +class FreezeObjectsWrapper : public FreezeDB::Wrapper<IdentityObjectInfoDict, Ice::Identity, ObjectInfo>, + public ObjectsWrapper +{ +public: + + FreezeObjectsWrapper(const Freeze::ConnectionPtr& connection, const std::string& name) : + FreezeDB::Wrapper<IdentityObjectInfoDict, Ice::Identity, ObjectInfo>(connection, name) + { + } + + std::vector<ObjectInfo> + findByType(const std::string& type) + { + std::vector<ObjectInfo> result; + for(IdentityObjectInfoDict::const_iterator p = _dict.findByType(type); p != _dict.end(); ++p) + { + result.push_back(p->second); + } + return result; + } +}; + +} + +FreezeDatabaseCache::FreezeDatabaseCache(const Ice::CommunicatorPtr& communicator) : + FreezeDB::DatabaseCache(communicator, "Registry") +{ +} + +ApplicationsWrapperPtr +FreezeDatabaseCache::getApplications(const IceDB::DatabaseConnectionPtr& connection) +{ + FreezeDB::DatabaseConnection* c = dynamic_cast<FreezeDB::DatabaseConnection*>(connection.get()); + return new FreezeApplicationsWrapper(c->freezeConnection(), "applications"); +} + +AdaptersWrapperPtr +FreezeDatabaseCache::getAdapters(const IceDB::DatabaseConnectionPtr& connection) +{ + FreezeDB::DatabaseConnection* c = dynamic_cast<FreezeDB::DatabaseConnection*>(connection.get()); + return new FreezeAdaptersWrapper(c->freezeConnection(), "adapters"); +} + +ObjectsWrapperPtr +FreezeDatabaseCache::getObjects(const IceDB::DatabaseConnectionPtr& connection) +{ + FreezeDB::DatabaseConnection* c = dynamic_cast<FreezeDB::DatabaseConnection*>(connection.get()); + return new FreezeObjectsWrapper(c->freezeConnection(), "objects"); +} + +ObjectsWrapperPtr +FreezeDatabaseCache::getInternalObjects(const IceDB::DatabaseConnectionPtr& connection) +{ + FreezeDB::DatabaseConnection* c = dynamic_cast<FreezeDB::DatabaseConnection*>(connection.get()); + return new FreezeObjectsWrapper(c->freezeConnection(), "internal-objects"); +} + +FreezeDBPlugin::FreezeDBPlugin(const Ice::CommunicatorPtr& communicator) : _communicator(communicator) +{ + string dbPath = _communicator->getProperties()->getProperty("IceGrid.Registry.Data"); + if(dbPath.empty()) + { + throw Ice::PluginInitializationException(__FILE__, __LINE__, "property `IceGrid.Registry.Data' is not set"); + } + else + { + struct stat filestat; + if(stat(dbPath.c_str(), &filestat) != 0 || !S_ISDIR(filestat.st_mode)) + { + ostringstream os; + Ice::SyscallException ex(__FILE__, __LINE__); + ex.error = IceInternal::getSystemErrno(); + os << "property `IceGrid.Registry.Data' is set to an invalid path:\n" << ex; + throw Ice::PluginInitializationException(__FILE__, __LINE__, os.str()); + } + } + + _communicator->getProperties()->setProperty("Freeze.DbEnv.Registry.DbHome", dbPath); +} + +void +FreezeDBPlugin::initialize() +{ + _databaseCache = new FreezeDatabaseCache(_communicator); +} + +void +FreezeDBPlugin::destroy() +{ + _databaseCache = 0; +} + +DatabaseCachePtr +FreezeDBPlugin::getDatabaseCache() +{ + return _databaseCache; +} |