ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / src / IceStorm / Admin.cpp
blob55b4909311a917ab257db311a8c33b80b3bb7086
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 #include <IceUtil/DisableWarnings.h>
11 #include <IceUtil/Options.h>
12 #include <Ice/Application.h>
13 #include <Ice/SliceChecksums.h>
14 #include <IceStorm/Parser.h>
16 using namespace std;
17 using namespace Ice;
18 using namespace IceStorm;
20 class Client : public Application
22 public:
24 void usage();
25 virtual int run(int, char*[]);
28 //COMPILERFIX: Borland C++ 2010 doesn't support wmain for console applications.
29 #if defined(_WIN32 ) && !defined(__BCPLUSPLUS__)
31 int
32 wmain(int argc, wchar_t* argv[])
34 #else
36 int
37 main(int argc, char* argv[])
39 #endif
41 Client app;
42 Ice::InitializationData id;
43 Ice::StringSeq args = Ice::argsToStringSeq(argc, argv);
44 id.properties = Ice::createProperties(args);
46 // We don't want to load DB plug-ins with icestormadmin, as this will
47 // cause FileLock issues when run with the same configuration file
48 // used by the service.
50 id.properties->setProperty("Ice.Plugin.DB", "");
51 int rc = app.main(argc, argv, id);
52 return rc;
55 void
56 Client::usage()
58 cerr << "Usage: " << appName() << " [options]\n";
59 cerr <<
60 "Options:\n"
61 "-h, --help Show this message.\n"
62 "-v, --version Display the Ice version.\n"
63 "-e COMMANDS Execute COMMANDS.\n"
64 "-d, --debug Print debug messages.\n"
68 int
69 Client::run(int argc, char* argv[])
71 string commands;
72 bool debug;
74 IceUtilInternal::Options opts;
75 opts.addOpt("h", "help");
76 opts.addOpt("v", "version");
77 opts.addOpt("e", "", IceUtilInternal::Options::NeedArg, "", IceUtilInternal::Options::Repeat);
78 opts.addOpt("d", "debug");
80 vector<string> args;
81 try
83 args = opts.parse(argc, (const char**)argv);
85 catch(const IceUtilInternal::BadOptException& e)
87 cerr << e.reason << endl;
88 usage();
89 return EXIT_FAILURE;
91 if(!args.empty())
93 cerr << argv[0] << ": too many arguments" << endl;
94 usage();
95 return EXIT_FAILURE;
98 if(opts.isSet("help"))
100 usage();
101 return EXIT_SUCCESS;
103 if(opts.isSet("version"))
105 cout << ICE_STRING_VERSION << endl;
106 return EXIT_SUCCESS;
108 if(opts.isSet("e"))
110 vector<string> optargs = opts.argVec("e");
111 for(vector<string>::const_iterator i = optargs.begin(); i != optargs.end(); ++i)
113 commands += *i + ";";
116 debug = opts.isSet("debug");
118 // The complete set of Ice::Identity -> manager proxies.
119 map<Ice::Identity, IceStorm::TopicManagerPrx> managers;
120 PropertiesPtr properties = communicator()->getProperties();
121 IceStorm::TopicManagerPrx defaultManager;
123 Ice::PropertyDict props = communicator()->getProperties()->getPropertiesForPrefix("IceStormAdmin.TopicManager.");
125 for(Ice::PropertyDict::const_iterator p = props.begin(); p != props.end(); ++p)
128 // Ignore proxy property settings. eg IceStormAdmin.TopicManager.*.LocatorCacheTimeout
130 if(p->first.find('.', strlen("IceStormAdmin.TopicManager.")) == string::npos)
134 IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::uncheckedCast(
135 communicator()->propertyToProxy(p->first));
136 managers.insert(map<Ice::Identity, IceStorm::TopicManagerPrx>::value_type(
137 manager->ice_getIdentity(), manager));
139 catch(const Ice::ProxyParseException&)
141 cerr << appName() << ": malformed proxy: " << p->second << endl;
142 return EXIT_FAILURE;
147 string managerProxy = properties->getProperty("IceStormAdmin.TopicManager.Default");
148 if(!managerProxy.empty())
150 defaultManager = IceStorm::TopicManagerPrx::uncheckedCast(
151 communicator()->stringToProxy(managerProxy));
153 else if(!managers.empty())
155 defaultManager = managers.begin()->second;
159 if(!defaultManager)
161 cerr << appName() << ": no manager proxies configured" << endl;
162 return EXIT_FAILURE;
165 ParserPtr p = Parser::createParser(communicator(), defaultManager, managers);
166 int status = EXIT_SUCCESS;
168 if(!commands.empty()) // Commands were given
170 int parseStatus = p->parse(commands, debug);
171 if(parseStatus == EXIT_FAILURE)
173 status = EXIT_FAILURE;
176 else // No commands, let's use standard input
178 p->showBanner();
180 int parseStatus = p->parse(stdin, debug);
181 if(parseStatus == EXIT_FAILURE)
183 status = EXIT_FAILURE;
187 return status;