Mercurial > hg > ltpda-connection-manager
comparison LTPDAConnectionManager.m @ 4:c706c10a76bd
Add help comments.
author | Daniele Nicolodi <daniele@science.unitn.it> |
---|---|
date | Mon, 24 May 2010 00:14:37 +0200 |
parents | d5fef23867bb |
children | 35f1cfcaa5a9 |
comparison
equal
deleted
inserted
replaced
3:b4f2f4c10918 | 4:c706c10a76bd |
---|---|
16 end % dependent properties | 16 end % dependent properties |
17 | 17 |
18 methods(Static) | 18 methods(Static) |
19 | 19 |
20 function reset() | 20 function reset() |
21 setappdata(0, LTPDAConnectionManager.appdataKey, []); | 21 % RESET Resets the state of the connection manager. |
22 end | 22 % |
23 % This static method removes the LTPDAConnectionManager instance data from | |
24 % the appdata storage. Causes the reset of the credentials cache and the | |
25 % removal of all the connections from the connection pool. | |
26 | |
27 rmappdata(0, LTPDAConnectionManager.appdataKey); | |
28 end | |
29 | |
23 | 30 |
24 function key = appdataKey() | 31 function key = appdataKey() |
25 % defined as static method to be acessible by the reset static method | 32 % APPDATAKEY Returns the key used to store instance data in appdata. |
33 % | |
34 % This is defined as static method, and not has an instance constant | |
35 % property, to to be accessible by the reset static method. | |
36 | |
26 key = 'LTPDAConnectionManager'; | 37 key = 'LTPDAConnectionManager'; |
27 end | 38 end |
28 | 39 |
29 end % static methods | 40 end % static methods |
30 | 41 |
31 methods | 42 methods |
32 | 43 |
33 function cm = LTPDAConnectionManager(pl) | 44 function cm = LTPDAConnectionManager() |
45 % LTPDACONNECTIONMANAGER Manages credentials and database connections. | |
46 % | |
47 % This constructor returns an handler to a LTPDAConnectionManager class | |
48 % instance. Database connections can be obtained trough the obtained | |
49 % object with the connect() method. | |
50 % | |
51 % The purpose of this class it to keep track of open database connections | |
52 % and to cache database credentials. It must be used in all LTPDA toolbox | |
53 % functions that required to obtain database connections. Its behaviour can | |
54 % be configured via LTPDA toolbox user preferences. The object status is | |
55 % persisted trough the appdata matlab facility. | |
34 | 56 |
35 % load state from appdata | 57 % load state from appdata |
36 acm = getappdata(0, cm.appdataKey()); | 58 acm = getappdata(0, cm.appdataKey()); |
37 | 59 |
38 if isempty(acm) | 60 if isempty(acm) |
71 val = p.cm.maxConnectionsNumber; | 93 val = p.cm.maxConnectionsNumber; |
72 end | 94 end |
73 | 95 |
74 | 96 |
75 function n = count(cm) | 97 function n = count(cm) |
98 % COUNT Returns the number of open connections in the connections pool. | |
99 % | |
100 % This method has the side effect of removing all closed connections from | |
101 % the connections pool, so that the underlying objects can be garbage | |
102 % collected. | |
103 | |
76 import utils.const.* | 104 import utils.const.* |
105 | |
77 % find closed connections in the pool | 106 % find closed connections in the pool |
78 mask = false(numel(cm.connections), 1); | 107 mask = false(numel(cm.connections), 1); |
79 for kk = 1:numel(cm.connections) | 108 for kk = 1:numel(cm.connections) |
80 if cm.connections{kk}.isClosed() | 109 if cm.connections{kk}.isClosed() |
81 utils.helper.msg(msg.PROC1, 'connection id=%d closed', kk); | 110 utils.helper.msg(msg.PROC1, 'connection id=%d closed', kk); |
88 | 117 |
89 % count remainig ones | 118 % count remainig ones |
90 n = numel(cm.connections); | 119 n = numel(cm.connections); |
91 end | 120 end |
92 | 121 |
93 | |
94 function clear(cm) | 122 function clear(cm) |
95 % remove all cached credentials | 123 % CLEAR Removes all cached credentials from the connection manager. |
96 cm.credentials = {}; | 124 cm.credentials = {}; |
97 end | 125 end |
98 | 126 |
99 | 127 |
100 function conn = connect(cm, varargin) | 128 function conn = connect(cm, varargin) |
129 % CONNECT Uses provided credential to establish a database connection. | |
130 % | |
131 % CONNECT(hostname, database, username, password) Returns an object | |
132 % implementing the java.sql.Connection interface handing a connection to | |
133 % the specified database. Any of the parameter is optional. The user will | |
134 % be queried for the missing information. | |
135 % | |
136 % The returned connection are added to a connections pool. When the number | |
137 % of connections in the pool exceeds a configurable maximum, no more | |
138 % connection are instantiated. Closed connections are automatically | |
139 % removed from the pool. | |
140 % | |
141 % CONNECT(pl) Works as the above but the parameters are obtained from the | |
142 % plist object PL. If the 'connection' parameter in the plist contains an | |
143 % object implementing the java.sql.Connection interface, this object is | |
144 % returned instead that opening a new connection. In this case the | |
145 % connection in not added to the connection pool. | |
146 | |
101 import utils.const.* | 147 import utils.const.* |
102 | 148 |
103 % save current credentials cache | 149 % save current credentials cache |
104 cache = cm.credentials; | 150 cache = cm.credentials; |
105 | 151 |
160 end | 206 end |
161 end | 207 end |
162 | 208 |
163 | 209 |
164 function close(cm, ids) | 210 function close(cm, ids) |
211 % CLOSE Forces connections to be closed. | |
212 % | |
213 % In the case bugs in other routines working with database connections | |
214 % produce orphan connections, this method can be used to force the close | |
215 % of those connections. | |
216 % | |
217 % CLOSE(ids) Closes the connections with the corresponding IDs in the | |
218 % connections pool. If no ID is given all connections in the pool are | |
219 % closed. | |
220 | |
165 if nargin < 2 | 221 if nargin < 2 |
166 ids = 1:numel(cm.connections); | 222 ids = 1:numel(cm.connections); |
167 end | 223 end |
168 cellfun(@close, cm.connections(ids)); | 224 cellfun(@close, cm.connections(ids)); |
169 end | 225 end |
170 | 226 |
171 | 227 |
172 function add(cm, c) | 228 function add(cm, c) |
229 % ADD Adds credentials to the credentials cache. | |
230 % | |
231 % This method can be used to initialize or add to the cache, credentials | |
232 % that will be used in subsequent connections attempts. This method accepts | |
233 % only credentials in the form of utils.jmysql.credentials objects. | |
234 | |
235 % check input arguments | |
173 if nargin < 2 || ~isa(c, 'credentials') | 236 if nargin < 2 || ~isa(c, 'credentials') |
174 error('### invalid call'); | 237 error('### invalid call'); |
175 end | 238 end |
239 | |
240 % add to the cache | |
176 cm.cacheCredentials(c); | 241 cm.cacheCredentials(c); |
177 end | 242 end |
178 | 243 |
179 end % methods | 244 end % methods |
180 | 245 |
181 methods(Access=private) | 246 methods(Access=private) |
182 | 247 |
183 function conn = getConnection(cm, varargin) | 248 function conn = getConnection(cm, varargin) |
249 % GETCONNECTION Where the implementation of the connect method really is. | |
184 | 250 |
185 import utils.const.* | 251 import utils.const.* |
186 | 252 |
253 % handle variable number of arguments | |
187 switch numel(varargin) | 254 switch numel(varargin) |
188 case 0 | 255 case 0 |
189 [hostname, database, username] = cm.selectDatabase(); | 256 [hostname, database, username] = cm.selectDatabase(); |
190 conn = cm.getConnection(hostname, database, username); | 257 conn = cm.getConnection(hostname, database, username); |
191 | 258 |
255 | 322 |
256 end | 323 end |
257 | 324 |
258 | 325 |
259 function ids = findCredentialsId(cm, varargin) | 326 function ids = findCredentialsId(cm, varargin) |
327 % FINDCREDENTIALSID Find credentials in the cache and returns their IDs. | |
328 | |
260 import utils.const.* | 329 import utils.const.* |
261 ids = []; | 330 ids = []; |
262 | 331 |
263 for kk = 1:numel(cm.credentials) | 332 for kk = 1:numel(cm.credentials) |
264 % invalidate expired passwords | 333 % invalidate expired passwords |
275 end | 344 end |
276 end | 345 end |
277 | 346 |
278 | 347 |
279 function cred = findCredentials(cm, varargin) | 348 function cred = findCredentials(cm, varargin) |
349 % FINDCREDENTIALS Find credentials in the cache and returns them in a list. | |
350 | |
280 % default | 351 % default |
281 cred = []; | 352 cred = []; |
282 | 353 |
283 % search | 354 % search |
284 ids = findCredentialsId(cm, varargin{:}); | 355 ids = findCredentialsId(cm, varargin{:}); |
289 end | 360 end |
290 end | 361 end |
291 | 362 |
292 | 363 |
293 function cacheCredentials(cm, c) | 364 function cacheCredentials(cm, c) |
365 % CACHECREDENTIALS Adds to or updates the credentials cache. | |
366 | |
294 import utils.const.* | 367 import utils.const.* |
295 | 368 |
296 % find entry to update | 369 % find entry to update |
297 id = findCredentialsId(cm, c.hostname, c.database, c.username); | 370 id = findCredentialsId(cm, c.hostname, c.database, c.username); |
298 | 371 |
322 end | 395 end |
323 end | 396 end |
324 | 397 |
325 | 398 |
326 function [username, password, cache] = inputCredentials(cm, cred) | 399 function [username, password, cache] = inputCredentials(cm, cred) |
327 % this is a stubb | 400 % INPUTCREDENTIALS Queries the user for database username and password. |
401 | |
402 % this is a stubb that must be replaced by a graphical interface | |
328 | 403 |
329 % build a cell array of usernames and passwords | 404 % build a cell array of usernames and passwords |
330 users = { cred(:).username }; | 405 users = { cred(:).username }; |
331 passw = { cred(:).password }; | 406 passw = { cred(:).password }; |
332 | 407 |
358 end | 433 end |
359 end | 434 end |
360 | 435 |
361 | 436 |
362 function [hostname, database, username] = selectDatabase(cm) | 437 function [hostname, database, username] = selectDatabase(cm) |
363 % this is a stubb | 438 % SELECTDATABASE Makes the user choose to which database connect to. |
439 | |
440 % this is a stubb that must be replaced by a graphical interface | |
364 | 441 |
365 for kk = 1:numel(cm.credentials) | 442 for kk = 1:numel(cm.credentials) |
366 fprintf('% 2d. %s\n', char(cm.credentials{kk})); | 443 fprintf('% 2d. %s\n', char(cm.credentials{kk})); |
367 end | 444 end |
368 fprintf('%d. NEW (default)\n', numel(cm.credentials)+1); | 445 fprintf('%d. NEW (default)\n', numel(cm.credentials)+1); |
386 end % private methods | 463 end % private methods |
387 | 464 |
388 end % classdef | 465 end % classdef |
389 | 466 |
390 | 467 |
391 % this should become utils.jmysql.connect | |
392 function conn = connect(hostname, database, username, password) | 468 function conn = connect(hostname, database, username, password) |
469 % CONNECT Opens a connection to the given database. | |
470 % | |
471 % This function returns a Java object implementing the java.sql.Connection | |
472 % interface connected to the given database using the provided credentials. | |
473 % If the connection fails because the given username and password pair is not | |
474 % accepted by the server an utils:jmysql:connect:AccessDenied error is thrown. | |
475 | |
476 % this should become utils.jmysql.connect | |
393 | 477 |
394 % informative message | 478 % informative message |
395 import utils.const.* | 479 import utils.const.* |
396 utils.helper.msg(msg.PROC1, 'connection to mysql://%s/%s username=%s', hostname, database, username); | 480 utils.helper.msg(msg.PROC1, 'connection to mysql://%s/%s username=%s', hostname, database, username); |
397 | 481 |