summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/IceGridDB.cpp
diff options
context:
space:
mode:
authorMark Spruiell <mes@zeroc.com>2016-03-07 16:03:27 -0800
committerMark Spruiell <mes@zeroc.com>2016-03-07 16:03:27 -0800
commit2c914caecbd192832da433b2e2461af593144809 (patch)
treefa8330c596e2d7bf25cc30987186667bb3c40df9 /cpp/src/IceGrid/IceGridDB.cpp
parentMore Copyright year updates (diff)
downloadice-2c914caecbd192832da433b2e2461af593144809.tar.bz2
ice-2c914caecbd192832da433b2e2461af593144809.tar.xz
ice-2c914caecbd192832da433b2e2461af593144809.zip
moving icegriddb/icestormdb from cpp/tools to cpp/src
Diffstat (limited to 'cpp/src/IceGrid/IceGridDB.cpp')
-rw-r--r--cpp/src/IceGrid/IceGridDB.cpp386
1 files changed, 386 insertions, 0 deletions
diff --git a/cpp/src/IceGrid/IceGridDB.cpp b/cpp/src/IceGrid/IceGridDB.cpp
new file mode 100644
index 00000000000..12b968a18eb
--- /dev/null
+++ b/cpp/src/IceGrid/IceGridDB.cpp
@@ -0,0 +1,386 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2016 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 <IceUtil/DisableWarnings.h>
+#include <IceUtil/Options.h>
+#include <IceUtil/FileUtil.h>
+#include <Ice/Application.h>
+#include <Freeze/Freeze.h>
+#include <Freeze/CatalogIndexList.h>
+#include <IceGrid/Admin.h>
+#include <IcePatch2Lib/Util.h>
+#include <DBTypes.h>
+#include <StringApplicationInfoDict.h>
+#include <StringAdapterInfoDict.h>
+#include <IdentityObjectInfoDict.h>
+#include <SerialsDict.h>
+
+using namespace std;
+using namespace Ice;
+using namespace IceGrid;
+
+class Client : public Application
+{
+public:
+
+ void usage();
+ virtual int run(int, char*[]);
+};
+
+#ifdef _WIN32
+
+int
+wmain(int argc, wchar_t* argv[])
+
+#else
+
+int
+main(int argc, char* argv[])
+
+#endif
+{
+ Client app;
+ return app.main(argc, argv);
+}
+
+void
+Client::usage()
+{
+ cerr << "Usage: " << appName() << " <options>\n";
+ cerr <<
+ "Options:\n"
+ "-h, --help Show this message.\n"
+ "-v, --version Display version.\n"
+ "--import FILE Import database from FILE.\n"
+ "--export FILE Export database to FILE.\n"
+ "--dbhome DIR The database directory.\n"
+ "-d, --debug Print debug messages.\n"
+ ;
+}
+
+int
+Client::run(int argc, char* argv[])
+{
+ IceUtilInternal::Options opts;
+ opts.addOpt("h", "help");
+ opts.addOpt("v", "version");
+ opts.addOpt("d", "debug");
+ opts.addOpt("", "import", IceUtilInternal::Options::NeedArg);
+ opts.addOpt("", "export", IceUtilInternal::Options::NeedArg);
+ opts.addOpt("", "dbhome", IceUtilInternal::Options::NeedArg);
+
+ vector<string> args;
+ try
+ {
+ args = opts.parse(argc, const_cast<const char**>(argv));
+ }
+ catch(const IceUtilInternal::BadOptException& e)
+ {
+ cerr << e.reason << endl;
+ usage();
+ return EXIT_FAILURE;
+ }
+ if(!args.empty())
+ {
+ cerr << argv[0] << ": too many arguments" << endl;
+ usage();
+ return EXIT_FAILURE;
+ }
+
+ if(opts.isSet("help"))
+ {
+ usage();
+ return EXIT_SUCCESS;
+ }
+
+ if(opts.isSet("version"))
+ {
+ cout << ICE_STRING_VERSION << endl;
+ return EXIT_SUCCESS;
+ }
+
+ if((!opts.isSet("import") && !opts.isSet("export")) || (opts.isSet("import") && opts.isSet("export")))
+ {
+ cerr << "Either --import or --export must be set" << endl;
+ usage();
+ return EXIT_FAILURE;
+ }
+
+ if(!opts.isSet("dbhome"))
+ {
+ cerr << "Database path must be specified" << endl;
+ usage();
+ return EXIT_FAILURE;
+ }
+
+ bool debug = opts.isSet("debug");
+ bool import = opts.isSet("import");
+ string dbFile = opts.optArg(import ? "import" : "export");
+ string dbPath = opts.optArg("dbhome");
+
+ try
+ {
+ IceGrid::AllData data;
+
+ EncodingVersion encoding;
+ encoding.major = 1;
+ encoding.minor = 1;
+
+ communicator()->getProperties()->setProperty("Freeze.DbEnv.Registry.DbHome", dbPath);
+
+ if(import)
+ {
+ cout << "Importing database to directory `" << dbPath << "' from file `" << dbFile << "'" << endl;
+
+ if(!IceUtilInternal::directoryExists(dbPath))
+ {
+ cerr << "Output directory does not exist: " << dbPath << endl;
+ return EXIT_FAILURE;
+ }
+
+ StringSeq files = IcePatch2Internal::readDirectory(dbPath);
+ if(!files.empty())
+ {
+ cerr << "Output directory is not empty: " << dbPath << endl;
+ return EXIT_FAILURE;
+ }
+
+ ifstream fs(dbFile.c_str(), ios::binary);
+ if(fs.fail())
+ {
+ cerr << "Could not open input file: " << strerror(errno) << endl;
+ return EXIT_FAILURE;
+ }
+ fs.unsetf(ios::skipws);
+
+ fs.seekg(0, ios::end);
+ streampos fileSize = fs.tellg();
+ fs.seekg(0, ios::beg);
+
+ vector<Ice::Byte> buf;
+ buf.reserve(static_cast<size_t>(fileSize));
+ buf.insert(buf.begin(), istream_iterator<Ice::Byte>(fs), istream_iterator<Ice::Byte>());
+
+ fs.close();
+
+ string type;
+ int version;
+
+ Ice::InputStreamPtr stream = Ice::wrapInputStream(communicator(), buf, encoding);
+ stream->read(type);
+ if(type != "IceGrid")
+ {
+ cerr << "Incorrect input file type: " << type << endl;
+ return EXIT_FAILURE;
+ }
+ stream->read(version);
+ stream->read(data);
+
+ {
+ Freeze::ConnectionPtr connection = Freeze::createConnection(communicator(), "Registry");
+ Freeze::TransactionHolder txn(connection);
+
+ if(debug)
+ {
+ cout << "Writing Applications Map:" << endl;
+ }
+
+ StringApplicationInfoDict applications(connection, "applications");
+ for(ApplicationInfoSeq::const_iterator p = data.applications.begin();
+ p != data.applications.end();
+ ++p)
+ {
+ if(debug)
+ {
+ cout << " NAME = " << p->descriptor.name << endl;
+ }
+ applications.put(StringApplicationInfoDict::value_type(p->descriptor.name, *p));
+ }
+
+
+ if(debug)
+ {
+ cout << "Writing Adapters Map:" << endl;
+ }
+
+ StringAdapterInfoDict adapters(connection, "adapters");
+ for(AdapterInfoSeq::const_iterator p = data.adapters.begin(); p != data.adapters.end(); ++p)
+ {
+ if(debug)
+ {
+ cout << " NAME = " << p->id << endl;
+ }
+ adapters.put(StringAdapterInfoDict::value_type(p->id, *p));
+ }
+
+ if(debug)
+ {
+ cout << "Writing Objects Map:" << endl;
+ }
+
+ IdentityObjectInfoDict objects(connection, "objects");
+ for(ObjectInfoSeq::const_iterator p = data.objects.begin(); p != data.objects.end(); ++p)
+ {
+ if(debug)
+ {
+ cout << " NAME = " << communicator()->identityToString(p->proxy->ice_getIdentity()) << endl;
+ }
+ objects.put(IdentityObjectInfoDict::value_type(p->proxy->ice_getIdentity(), *p));
+ }
+
+ if(debug)
+ {
+ cout << "Writing Internal Objects Map:" << endl;
+ }
+
+ IdentityObjectInfoDict internalObjects(connection, "internal-objects");
+ for(ObjectInfoSeq::const_iterator p = data.internalObjects.begin();
+ p != data.internalObjects.end();
+ ++p)
+ {
+ if(debug)
+ {
+ cout << " NAME = " << communicator()->identityToString(p->proxy->ice_getIdentity()) << endl;
+ }
+ internalObjects.put(IdentityObjectInfoDict::value_type(p->proxy->ice_getIdentity(), *p));
+ }
+
+ if(debug)
+ {
+ cout << "Writing Serials Map:" << endl;
+ }
+
+ SerialsDict serials(connection, "serials");
+ for(StringLongDict::const_iterator p = data.serials.begin(); p != data.serials.end(); ++p)
+ {
+ if(debug)
+ {
+ cout << " NAME = " << p->first << endl;
+ }
+ serials.put(SerialsDict::value_type(p->first, p->second));
+ }
+
+ txn.commit();
+ }
+ }
+ else
+ {
+ cout << "Exporting database from directory `" << dbPath << "' to file `" << dbFile << "'" << endl;
+
+ {
+ Freeze::ConnectionPtr connection = Freeze::createConnection(communicator(), "Registry");
+ Freeze::TransactionHolder txn(connection);
+
+ if(debug)
+ {
+ cout << "Reading Application Map:" << endl;
+ }
+
+ IceGrid::StringApplicationInfoDict applications(connection, "applications", false);
+ for(IceGrid::StringApplicationInfoDict::const_iterator p = applications.begin();
+ p != applications.end();
+ ++p)
+ {
+ if(debug)
+ {
+ cout << " APPLICATION = " << p->first << endl;
+ }
+ data.applications.push_back(p->second);
+ }
+
+ if(debug)
+ {
+ cout << "Reading Adapter Map:" << endl;
+ }
+
+ StringAdapterInfoDict adapters(connection, "adapters", false);
+ for(StringAdapterInfoDict::const_iterator p = adapters.begin(); p != adapters.end(); ++p)
+ {
+ if(debug)
+ {
+ cout << " ADAPTER = " << p->first << endl;
+ }
+ data.adapters.push_back(p->second);
+ }
+
+
+ if(debug)
+ {
+ cout << "Reading Object Map:" << endl;
+ }
+
+ IdentityObjectInfoDict objects(connection, "objects", false);
+ for(IdentityObjectInfoDict::const_iterator p = objects.begin(); p != objects.end(); ++p)
+ {
+ if(debug)
+ {
+ cout << " IDENTITY = " << communicator()->identityToString(p->first) << endl;
+ }
+ data.objects.push_back(p->second);
+ }
+
+ if(debug)
+ {
+ cout << "Reading Internal Object Map:" << endl;
+ }
+
+ IdentityObjectInfoDict internalObjects(connection, "internal-objects", false);
+ for(IdentityObjectInfoDict::const_iterator p = internalObjects.begin();
+ p != internalObjects.end();
+ ++p)
+ {
+ if(debug)
+ {
+ cout << " IDENTITY = " << communicator()->identityToString(p->first) << endl;
+ }
+ data.internalObjects.push_back(p->second);
+ }
+
+ if(debug)
+ {
+ cout << "Reading Serials Map:" << endl;
+ }
+
+ SerialsDict serials(connection, "serials", false);
+ for(SerialsDict::const_iterator p = serials.begin(); p != serials.end(); ++p)
+ {
+ if(debug)
+ {
+ cout << " NAME = " << p->first << endl;
+ }
+ data.serials.insert(std::make_pair(p->first, p->second));
+ }
+
+ txn.rollback();
+ }
+
+ Ice::OutputStreamPtr stream = Ice::createOutputStream(communicator(), encoding);
+ stream->write("IceGrid");
+ stream->write(ICE_INT_VERSION);
+ stream->write(data);
+ pair<const Ice::Byte*, const Ice::Byte*> buf = stream->finished();
+
+ ofstream fs(dbFile.c_str(), ios::binary);
+ if(fs.fail())
+ {
+ cerr << "Could not open output file: " << strerror(errno) << endl;
+ return EXIT_FAILURE;
+ }
+ fs.write(reinterpret_cast<const char*>(buf.first), buf.second - buf.first);
+ fs.close();
+ }
+ }
+ catch(const IceUtil::Exception& ex)
+ {
+ cerr << (import ? "Import" : "Export") << " failed:\n" << ex << endl;
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}