ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / src / IceBox / Service.cpp
blob3c60ace423d9389a506ff8fabfd0c0385b491dc5
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/Options.h>
11 #include <Ice/Ice.h>
12 #include <Ice/Service.h>
13 #include <IceBox/ServiceManagerI.h>
15 using namespace std;
16 using namespace Ice;
17 using namespace IceBox;
19 namespace IceBox
22 class IceBoxService : public Ice::Service
24 public:
26 IceBoxService();
28 protected:
30 virtual bool start(int, char*[], int&);
31 virtual bool stop();
33 private:
35 void usage(const std::string&);
36 ServiceManagerIPtr _serviceManager;
41 IceBox::IceBoxService::IceBoxService()
45 bool
46 IceBox::IceBoxService::start(int argc, char* argv[], int& status)
48 // Run through the command line arguments removing all the service
49 // properties.
50 vector<string> args = Ice::argsToStringSeq(argc, argv);
51 PropertiesPtr properties = communicator()->getProperties();
52 const string prefix = "IceBox.Service.";
53 PropertyDict services = properties->getPropertiesForPrefix(prefix);
54 for(PropertyDict::const_iterator p = services.begin(); p != services.end(); ++p)
56 string name = p->first.substr(prefix.size());
57 StringSeq::iterator q = args.begin();
58 while(q != args.end())
60 if(q->find("--" + name + ".") == 0)
62 q = args.erase(q);
63 continue;
65 ++q;
69 IceUtilInternal::Options opts;
70 opts.addOpt("h", "help");
71 opts.addOpt("v", "version");
73 try
75 args = opts.parse(args);
77 catch(const IceUtilInternal::BadOptException& e)
79 error(e.reason);
80 usage(argv[0]);
81 return false;
84 if(opts.isSet("help"))
86 usage(argv[0]);
87 status = EXIT_SUCCESS;
88 return false;
90 if(opts.isSet("version"))
92 print(ICE_STRING_VERSION);
93 status = EXIT_SUCCESS;
94 return false;
97 if(!args.empty())
99 usage(argv[0]);
100 return false;
103 _serviceManager = new ServiceManagerI(communicator(), argc, argv);
105 return _serviceManager->start();
108 bool
109 IceBox::IceBoxService::stop()
111 if(_serviceManager)
113 _serviceManager->stop();
114 _serviceManager = 0;
116 return true;
119 void
120 IceBox::IceBoxService::usage(const string& appName)
122 string options =
123 "Options:\n"
124 "-h, --help Show this message.\n"
125 "-v, --version Display the Ice version.";
126 #ifndef _WIN32
127 options.append(
128 "\n"
129 "\n"
130 "--daemon Run as a daemon.\n"
131 "--pidfile FILE Write process ID into FILE.\n"
132 "--noclose Do not close open file descriptors.\n"
133 "--nochdir Do not change the current working directory."
135 #endif
136 print("Usage: " + appName + " [options]\n" + options);
139 //COMPILERFIX: Borland C++ 2010 doesn't support wmain for console applications.
140 #if defined(_WIN32 ) && !defined(__BCPLUSPLUS__)
143 wmain(int argc, wchar_t* argv[])
145 #else
148 main(int argc, char* argv[])
150 #endif
152 IceBox::IceBoxService svc;
154 InitializationData initData;
155 initData.properties = createProperties();
156 initData.properties->setProperty("Ice.Admin.DelayCreation", "1");
157 return svc.main(argc, argv, initData);