1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// **********************************************************************
//
// Copyright (c) 2003-2013 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/ArgVector.h>
#include <IceUtil/FileUtil.h>
#include <Ice/Communicator.h>
#include <Ice/Locator.h>
#include <Ice/Instance.h>
#include <Ice/LoggerUtil.h>
#include <IceDB/SqlTypes.h>
#include <IceGrid/SqlDB/SqlDB.h>
#include <QtCore/QCoreApplication>
#include <QtCore/QTextCodec>
using namespace IceGrid;
using namespace std;
extern "C"
{
ICE_DECLSPEC_EXPORT ::Ice::Plugin*
createSqlDB(const Ice::CommunicatorPtr& communicator, const string& name, const Ice::StringSeq& args)
{
IceUtilInternal::ArgVector av(args);
return new IceGrid::SqlDBPlugin(communicator, av.argc, av.argv);
}
}
namespace
{
//
// SQL wrappers for SQL tables
//
class SqlApplicationsWrapper : public SqlDB::Wrapper<SqlStringApplicationInfoDict, std::string, ApplicationInfo>,
public ApplicationsWrapper
{
public:
SqlApplicationsWrapper(const SqlDB::DatabaseConnectionPtr& con, const SqlStringApplicationInfoDictPtr& table) :
SqlDB::Wrapper<SqlStringApplicationInfoDict, std::string, ApplicationInfo>(con, table)
{
}
virtual Ice::Long
updateSerial(Ice::Long serial)
{
return -1; // Serials not supported
}
virtual Ice::Long
getSerial() const
{
return -1; // Serials not supported
}
};
class SqlAdaptersWrapper : public SqlDB::Wrapper<SqlStringAdapterInfoDict, std::string, AdapterInfo>,
public AdaptersWrapper
{
public:
SqlAdaptersWrapper(const SqlDB::DatabaseConnectionPtr& connection, const SqlStringAdapterInfoDictPtr& table) :
SqlDB::Wrapper<SqlStringAdapterInfoDict, std::string, AdapterInfo>(connection, table)
{
}
virtual std::vector<AdapterInfo>
findByReplicaGroupId(const std::string& name)
{
return _table->findByReplicaGroupId(_connection, name);
}
virtual Ice::Long
updateSerial(Ice::Long serial)
{
return -1; // Serials not supported
}
virtual Ice::Long
getSerial() const
{
return -1; // Serials not supported
}
};
class SqlObjectsWrapper : public SqlDB::Wrapper<SqlIdentityObjectInfoDict, Ice::Identity, ObjectInfo>,
public ObjectsWrapper
{
public:
SqlObjectsWrapper(const SqlDB::DatabaseConnectionPtr& connection, const SqlIdentityObjectInfoDictPtr& table) :
SqlDB::Wrapper<SqlIdentityObjectInfoDict, Ice::Identity, ObjectInfo>(connection, table)
{
}
virtual std::vector<ObjectInfo>
findByType(const std::string& type)
{
return _table->findByType(_connection, type);
}
virtual Ice::Long
updateSerial(Ice::Long serial)
{
return -1; // Serials not supported
}
virtual Ice::Long
getSerial() const
{
return -1; // Serials not supported
}
};
}
SqlConnectionPool::SqlConnectionPool(const Ice::CommunicatorPtr& communicator,
const string& databaseType,
const string& databaseName,
const string& hostname,
int port,
const string& username,
const string& password,
const string& tablePrefix,
const string& encoding) :
SqlDB::ConnectionPool(communicator, databaseType, databaseName, hostname, port, username, password, true,
Ice::stringToEncodingVersion(encoding))
{
IceDB::DatabaseConnectionPtr connection = getConnection();
IceDB::TransactionHolder txn(connection);
SqlDB::DatabaseConnectionPtr c = SqlDB::DatabaseConnectionPtr::dynamicCast(connection.get());
const_cast<SqlStringApplicationInfoDictPtr&>(_applications) =
new SqlStringApplicationInfoDict(c, tablePrefix + "Applications", communicator);
const_cast<SqlStringAdapterInfoDictPtr&>(_adapters) =
new SqlStringAdapterInfoDict(c, tablePrefix + "Adapters", communicator);
const_cast<SqlIdentityObjectInfoDictPtr&>(_objects) =
new SqlIdentityObjectInfoDict(c, tablePrefix + "Objects", communicator);
const_cast<SqlIdentityObjectInfoDictPtr&>(_internalObjects) =
new SqlIdentityObjectInfoDict(c, tablePrefix + "InternalObjects", communicator);
txn.commit();
}
SqlConnectionPool::~SqlConnectionPool()
{
}
ApplicationsWrapperPtr
SqlConnectionPool::getApplications(const IceDB::DatabaseConnectionPtr& connection)
{
return new SqlApplicationsWrapper(SqlDB::DatabaseConnectionPtr::dynamicCast(connection.get()), _applications);
}
AdaptersWrapperPtr
SqlConnectionPool::getAdapters(const IceDB::DatabaseConnectionPtr& connection)
{
return new SqlAdaptersWrapper(SqlDB::DatabaseConnectionPtr::dynamicCast(connection.get()), _adapters);
}
ObjectsWrapperPtr
SqlConnectionPool::getObjects(const IceDB::DatabaseConnectionPtr& connection)
{
return new SqlObjectsWrapper(SqlDB::DatabaseConnectionPtr::dynamicCast(connection.get()), _objects);
}
ObjectsWrapperPtr
SqlConnectionPool::getInternalObjects(const IceDB::DatabaseConnectionPtr& connection)
{
return new SqlObjectsWrapper(SqlDB::DatabaseConnectionPtr::dynamicCast(connection.get()), _internalObjects);
}
SqlDBPlugin::SqlDBPlugin(const Ice::CommunicatorPtr& communicator, int argc, char** argv) :
_communicator(communicator), _qtApp(0)
{
if(QCoreApplication::instance() == 0)
{
_qtApp = new QCoreApplication(argc, argv);
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
}
Ice::PluginPtr(new Ice::ThreadHookPlugin(_communicator, new SqlDB::ThreadHook()));
}
SqlDBPlugin::~SqlDBPlugin()
{
if(_qtApp != 0)
{
delete _qtApp;
_qtApp = 0;
}
}
void
SqlDBPlugin::initialize()
{
}
void
SqlDBPlugin::destroy()
{
//
// Break cyclic reference count (thread hook holds a reference on the cache and the cache holds
// a reference on the communicator through the SQL dictionaries).
//
SqlDB::ThreadHookPtr threadHook =
SqlDB::ThreadHookPtr::dynamicCast(IceInternal::getInstance(_communicator)->initializationData().threadHook);
if(threadHook)
{
threadHook->setConnectionPool(0);
}
_connectionPool = 0;
}
bool
SqlDBPlugin::initDB()
{
Ice::PropertiesPtr properties = _communicator->getProperties();
string databaseName;
string tablePrefix;
if(properties->getProperty("IceGrid.SQL.DatabaseType") == "QSQLITE")
{
string dbPath = properties->getProperty("IceGrid.Registry.Data");
if(dbPath.empty())
{
Ice::Error out(_communicator->getLogger());
out << "property `IceGrid.Registry.Data' is not set";
return false;
}
else
{
if(!IceUtilInternal::directoryExists(dbPath))
{
Ice::SyscallException ex(__FILE__, __LINE__);
ex.error = IceInternal::getSystemErrno();
Ice::Error out(_communicator->getLogger());
out << "property `IceGrid.Registry.Data' is set to an invalid path:\n" << ex;
return false;
}
}
databaseName = dbPath + "/" + properties->getPropertyWithDefault("IceGrid.SQL.DatabaseName", "registry.db");
}
else
{
databaseName = properties->getProperty("IceGrid.SQL.DatabaseName");
string replicaName = properties->getPropertyWithDefault("IceGrid.Registry.ReplicaName", "Master");
string instanceName;
if(_communicator->getDefaultLocator())
{
instanceName = _communicator->getDefaultLocator()->ice_getIdentity().category;
}
else
{
instanceName = properties->getPropertyWithDefault("IceGrid.InstanceName", "IceGrid");
}
tablePrefix = instanceName + "_" + replicaName + "_";
replace(tablePrefix.begin(), tablePrefix.end(), '.', '_');
replace(tablePrefix.begin(), tablePrefix.end(), '-', '_');
replace(tablePrefix.begin(), tablePrefix.end(), ' ', '_');
replace(tablePrefix.begin(), tablePrefix.end(), ';', '_');
}
string encodingVersionString =
properties->getPropertyWithDefault("IceGrid.SQL.EncodingVersion",
encodingVersionToString(Ice::currentEncoding));
_connectionPool = new SqlConnectionPool(_communicator,
properties->getProperty("IceGrid.SQL.DatabaseType"),
databaseName,
properties->getProperty("IceGrid.SQL.HostName"),
properties->getPropertyAsInt("IceGrid.SQL.Port"),
properties->getProperty("IceGrid.SQL.UserName"),
properties->getProperty("IceGrid.SQL.Password"),
tablePrefix,
encodingVersionString);
SqlDB::ThreadHookPtr threadHook =
SqlDB::ThreadHookPtr::dynamicCast(IceInternal::getInstance(_communicator)->initializationData().threadHook);
assert(threadHook);
threadHook->setConnectionPool(_connectionPool);
return true;
}
ConnectionPoolPtr
SqlDBPlugin::getConnectionPool()
{
return _connectionPool;
}
|