Mercurial > hg > ltpda
annotate m-toolbox/classes/@LTPDADatabaseConnectionManager/LTPDADatabaseConnectionManager.m @ 2:18e956c96a1b database-connection-manager
Add LTPDADatabaseConnectionManager implementation. Matlab code
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Sun, 04 Dec 2011 21:23:09 +0100 |
parents | |
children | e3c5468b1bfe |
rev | line source |
---|---|
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 classdef LTPDADatabaseConnectionManager < handle |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 properties(SetAccess=private) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 connections = {}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 credentials = {}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 p_credentialsExpiry = 3600; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 p_cachePassword = 2; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 p_maxConnectionsNumber = 10; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 end % private properties |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 properties(Dependent=true) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 credentialsExpiry; % seconds |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 cachePassword; % 0=no 1=yes 2=ask |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 maxConnectionsNumber; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 end % dependent properties |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 methods(Static) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 function reset() |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 % RESET Resets the state of the connection manager. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 % This static method removes the LTPDADatabaseConnectionManager |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 % instance data from the appdata storage. Causes the reset of the |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 % credentials cache and the removal of all the connections from |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % the connection pool. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 rmappdata(0, LTPDADatabaseConnectionManager.appdataKey); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 function key = appdataKey() |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 % APPDATAKEY Returns the key used to store instance data in appdata. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 % This is defined as static method, and not has an instance constant |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 % property, to to be accessible by the reset static method. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 key = 'LTPDADatabaseConnectionManager'; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 end % static methods |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 methods |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 function cm = LTPDADatabaseConnectionManager() |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 % LTPDACONNECTIONMANAGER Manages credentials and database connections. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 % This constructor returns an handler to a LTPDADatabaseConnectionManager |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 % class instance. Database connections can be obtained trough the |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 % obtained object with the connect() method. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 % The purpose of this class it to keep track of open database connections |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 % and to cache database credentials. It must be used in all LTPDA toolbox |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 % functions that required to obtain database connections. Its behaviour can |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 % be configured via LTPDA toolbox user preferences. The object status is |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 % persisted trough the appdata matlab facility. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 % import credentials class |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 import utils.credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 % load state from appdata |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 acm = getappdata(0, cm.appdataKey()); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 if isempty(acm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 % store state in appdata |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 setappdata(0, cm.appdataKey(), cm); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 utils.helper.msg(msg.PROC1, 'new connection manager'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75 cm = acm; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 function str = disp(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 disp(sprintf('%s()\n', class(cm))); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84 function val = get.credentialsExpiry(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85 % obtain from user preferences |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86 %p = getappdata(0, 'LTPDApreferences'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87 val = cm.p_credentialsExpiry; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 function val = get.cachePassword(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 % obtain from user preferences |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93 %p = getappdata(0, 'LTPDApreferences'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 val = cm.p_cachePassword; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 function val = get.maxConnectionsNumber(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 % obtain from user preferences |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 %p = getappdata(0, 'LTPDApreferences'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 val = cm.p_maxConnectionsNumber; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 function n = count(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 % COUNT Returns the number of open connections in the connections pool. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108 % This method has the side effect of removing all closed connections from |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109 % the connections pool, so that the underlying objects can be garbage |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110 % collected. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 % find closed connections in the pool |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
115 mask = false(numel(cm.connections), 1); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
116 for kk = 1:numel(cm.connections) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
117 if cm.connections{kk}.isClosed() |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
118 utils.helper.msg(msg.PROC1, 'connection id=%d closed', kk); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
119 mask(kk) = true; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
120 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
121 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
122 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
123 % remove them |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
124 cm.connections(mask) = []; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
125 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
126 % count remainig ones |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127 n = numel(cm.connections); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
129 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 function clear(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
131 % CLEAR Removes all cached credentials from the connection manager. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
132 cm.credentials = {}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
135 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
136 function conn = connect(cm, varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
137 % CONNECT Uses provided credential to establish a database connection. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
138 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
139 % CONNECT(hostname, database, username, password) Returns an object |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
140 % implementing the java.sql.Connection interface handing a connection to |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
141 % the specified database. Any of the parameter is optional. The user will |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
142 % be queried for the missing information. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
143 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
144 % The returned connection are added to a connections pool. When the number |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
145 % of connections in the pool exceeds a configurable maximum, no more |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
146 % connection are instantiated. Closed connections are automatically |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
147 % removed from the pool. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
148 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
149 % CONNECT(pl) Works as the above but the parameters are obtained from the |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
150 % plist object PL. If the 'connection' parameter in the plist contains an |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
151 % object implementing the java.sql.Connection interface, this object is |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
152 % returned instead that opening a new connection. In this case the |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
153 % connection in not added to the connection pool. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
154 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
155 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
156 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
157 % save current credentials cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
158 cache = cm.credentials; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
159 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
160 % count open connections in the pool |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
161 count = cm.count(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
162 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
163 % check parameters |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
164 if numel(varargin) == 1 && isa(varargin{1}, 'plist') |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
165 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
166 % extract parameters from plist |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
167 pl = varargin{1}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
168 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
169 % check if we have a connection parameter |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
170 conn = find(pl, 'conn'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
171 if ~isempty(conn) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
172 % check that it implements java.sql.Connection interface |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
173 if ~isa(conn, 'java.sql.Connection') |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
174 error('### connection is not valid database connection'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
175 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
176 % return this connection |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
177 return; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
178 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
179 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
180 % otherwise |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
181 hostname = find(pl, 'hostname'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
182 database = find(pl, 'database'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
183 username = find(pl, 'username'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
184 password = find(pl, 'password'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
185 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
186 % if there is no hostname ignore other parameters |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
187 if ~ischar(hostname) || isempty(hostname) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
188 varargin = {}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
189 % if there is no database ignore other parameters |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
190 elseif ~ischar(database) || isempty(database) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
191 varargin = {hostname}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
192 % if there is no username ignore other parameters |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
193 elseif ~ischar(username) || isempty(username) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
194 varargin = {hostname, database}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
195 % password can not be null but can be an empty string |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
196 elseif ~ischar(password) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
197 varargin = {hostname, database, username}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
198 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
199 varargin = {hostname, database, username, password}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
200 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
201 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
202 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
203 % check number of connections |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
204 if count > cm.maxConnectionsNumber |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
205 error('### too many open connections'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
206 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
207 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
208 % connect |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
209 try |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
210 conn = cm.getConnection(varargin{:}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
211 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
212 % add connection to pool |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
213 utils.helper.msg(msg.PROC1, 'add connection to pool'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
214 cm.connections{end+1} = conn; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
215 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
216 catch ex |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
217 % restore our copy of the credentials cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
218 utils.helper.msg(msg.PROC1, 'undo cache changes'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
219 cm.credentials = cache; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
220 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
221 % hide implementation details |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
222 ex.throw(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
223 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
224 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
225 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
226 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
227 function close(cm, ids) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
228 % CLOSE Forces connections to be closed. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
229 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
230 % In the case bugs in other routines working with database connections |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
231 % produce orphan connections, this method can be used to force the close |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
232 % of those connections. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
233 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
234 % CLOSE(ids) Closes the connections with the corresponding IDs in the |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
235 % connections pool. If no ID is given all connections in the pool are |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
236 % closed. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
237 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
238 if nargin < 2 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
239 ids = 1:numel(cm.connections); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
240 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
241 cellfun(@close, cm.connections(ids)); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
242 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
243 % remove closed connections from pool |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
244 cm.count(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
245 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
246 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
247 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
248 function add(cm, c) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
249 % ADD Adds credentials to the credentials cache. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
250 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
251 % This method can be used to initialize or add to the cache, credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
252 % that will be used in subsequent connections attempts. This method accepts |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
253 % only credentials in the form of utils.credentials objects. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
254 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
255 % check input arguments |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
256 if nargin < 2 || ~isa(c, 'utils.credentials') |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
257 error('### invalid call'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
258 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
259 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
260 % add to the cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
261 cm.cacheCredentials(c); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
262 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
263 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
264 end % methods |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
265 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
266 methods(Access=private) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
267 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
268 function conn = getConnection(cm, varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
269 % GETCONNECTION Where the implementation of the connect method really is. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
270 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
271 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
272 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
273 % handle variable number of arguments |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
274 switch numel(varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
275 case 0 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
276 % find credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
277 [hostname, database, username] = cm.selectDatabase([cm.credentials{:}]); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
278 conn = cm.getConnection(hostname, database, username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
279 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
280 case 1 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
281 % find credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
282 cred = cm.findCredentials(varargin{:}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
283 if numel(cred) == 0 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
284 cred = utils.credentials(varargin{:}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
285 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
286 [hostname, database, username] = cm.selectDatabase(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
287 conn = cm.getConnection(hostname, database, username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
288 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
289 case 2 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
290 % find credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
291 cred = cm.findCredentials(varargin{:}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
292 switch numel(cred) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
293 case 0 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
294 conn = cm.getConnection(varargin{1}, varargin{2}, []); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
295 case 1 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
296 conn = cm.getConnection(cred.hostname, cred.database, cred.username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
297 otherwise |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
298 [hostname, database, username] = cm.selectDatabase(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
299 conn = cm.getConnection(hostname, database, username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
300 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
301 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
302 case 3 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
303 % find credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
304 cred = cm.findCredentials(varargin{1}, varargin{2}, varargin{3}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
305 switch numel(cred) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
306 case 0 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
307 % no credentials found |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
308 usernames = { varargin{3} }; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
309 if isempty(varargin{3}) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
310 % use usernames for same hostname |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
311 tmp = cm.findCredentials(varargin{1}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
312 if ~isempty(tmp) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
313 usernames = { tmp(:).username }; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
314 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
315 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
316 % build credentials objects |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
317 tmp = {}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
318 for kk = 1:numel(usernames) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
319 tmp{kk} = utils.credentials(varargin{1}, varargin{2}, usernames{kk}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
320 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
321 % convert from cell array to array |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
322 cred = [tmp{:}]; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
323 case 1 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
324 % credentials in cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
325 utils.helper.msg(msg.PROC1, 'use cached credentials'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
326 otherwise |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
327 % we should not have more than one credentials set |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
328 error('### more than one credentials set matching'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
329 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
330 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
331 cache = false; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
332 if (numel(cred) > 1) || ~cred.complete |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
333 % ask for password |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
334 [username, password, cache] = cm.inputCredentials(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
335 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
336 % cache credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
337 cred = utils.credentials(varargin{1}, varargin{2}, username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
338 cm.cacheCredentials(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
339 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
340 % add password to credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
341 cred.password = password; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
342 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
343 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
344 % try to connect |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
345 try |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
346 conn = utils.mysql.connect(cred.hostname, cred.database, cred.username, cred.password); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
347 catch ex |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
348 % look for access denied errors |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
349 if strcmp(ex.identifier, 'utils:mysql:connect:AccessDenied') |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
350 % ask for new new credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
351 utils.helper.msg(msg.IMPORTANT, ex.message); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
352 conn = cm.getConnection(varargin{1}, varargin{2}, varargin{3}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
353 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
354 % error out |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
355 throw(ex); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
356 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
357 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
358 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
359 % cache password |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
360 if cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
361 utils.helper.msg(msg.PROC1, 'cache password'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
362 cm.cacheCredentials(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
363 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
364 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
365 case 4 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
366 % connect |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
367 conn = utils.mysql.connect(varargin{1}, varargin{2}, varargin{3}, varargin{4}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
368 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
369 if cm.cachePassword == 1 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
370 % cache credentials with password |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
371 cred = utils.credentials(varargin{1}, varargin{2}, varargin{3}, varargin{4}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
372 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
373 % cache credentials without password |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
374 cred = utils.credentials(varargin{1}, varargin{2}, varargin{3}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
375 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
376 cm.cacheCredentials(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
377 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
378 otherwise |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
379 error('### invalid call') |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
380 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
381 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
382 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
383 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
384 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
385 function ids = findCredentialsId(cm, varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
386 % FINDCREDENTIALSID Find credentials in the cache and returns their IDs. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
387 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
388 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
389 ids = []; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
390 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
391 for kk = 1:numel(cm.credentials) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
392 % invalidate expired passwords |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
393 if expired(cm.credentials{kk}) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
394 utils.helper.msg(msg.PROC1, 'cache entry id=%d expired', kk); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
395 cm.credentials{kk}.password = []; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
396 cm.credentials{kk}.expiry = 0; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
397 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
398 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
399 % match input with cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
400 if match(cm.credentials{kk}, varargin{:}) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
401 ids = [ ids kk ]; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
402 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
403 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
404 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
405 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
406 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
407 function cred = findCredentials(cm, varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
408 % FINDCREDENTIALS Find credentials in the cache and returns them in a list. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
409 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
410 % default |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
411 cred = []; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
412 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
413 % search |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
414 ids = findCredentialsId(cm, varargin{:}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
415 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
416 % return a credentials objects array |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
417 if ~isempty(ids) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
418 cred = [cm.credentials{ids}]; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
419 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
420 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
421 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
422 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
423 function cacheCredentials(cm, c) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
424 % CACHECREDENTIALS Adds to or updates the credentials cache. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
425 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
426 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
427 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
428 % find entry to update |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
429 ids = findCredentialsId(cm, c.hostname, c.database, c.username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
430 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
431 % set password expiry time |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
432 if ischar(c.password) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
433 c.expiry = double(time()) + cm.credentialsExpiry; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
434 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
435 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
436 if isempty(ids) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
437 % add at the end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
438 utils.helper.msg(msg.PROC1, 'add cache entry %s', char(c)); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
439 cm.credentials{end+1} = c; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
440 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
441 for id = ids |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
442 % update only if the cached informations are less than the one we have |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
443 if length(c) > length(cm.credentials{id}) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
444 utils.helper.msg(msg.PROC1, 'update cache entry id=%d %s', id, char(c)); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
445 cm.credentials{id} = c; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
446 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
447 % always update expiry time |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
448 cm.credentials{id}.expiry = c.expiry; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
449 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
450 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
451 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
452 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
453 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
454 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
455 function [username, password, cache] = inputCredentials(cm, cred) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
456 % INPUTCREDENTIALS Queries the user for database username and password. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
457 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
458 % build a cell array of usernames |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
459 users = {}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
460 for id = 1:numel(cred) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
461 if ~isempty(cred(id).username) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
462 users = [ users { cred(id).username } ]; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
463 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
464 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
465 users = sort(unique(users)); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
466 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
467 parent = com.mathworks.mde.desk.MLDesktop.getInstance().getMainFrame(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
468 dialog = javaObjectEDT('connectionmanager.CredentialsDialog', ... |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
469 parent, cred(1).hostname, cred(1).database, users, cm.cachePassword); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
470 dialog.show(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
471 if dialog.cancelled |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
472 throw(MException('utils:mysql:connect:UserCancelled', '### user cancelled')); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
473 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
474 username = char(dialog.username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
475 password = char(dialog.password); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
476 cache = logical(dialog.cache); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
477 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
478 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
479 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
480 function [hostname, database, username] = selectDatabase(cm, credentials) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
481 % SELECTDATABASE Makes the user choose to which database connect to. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
482 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
483 parent = com.mathworks.mde.desk.MLDesktop.getInstance().getMainFrame(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
484 dialog = javaObjectEDT('connectionmanager.DatabaseSelectorDialog', parent); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
485 for c = credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
486 dialog.add(c.hostname, c.database, c.username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
487 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
488 dialog.show(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
489 if dialog.cancelled |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
490 throw(MException('utils:mysql:connect:UserCancelled', '### user cancelled')); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
491 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
492 hostname = char(dialog.hostname); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
493 database = char(dialog.database); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
494 username = char(dialog.username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
495 if isempty(username) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
496 username = []; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
497 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
498 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
499 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
500 end % private methods |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
501 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
502 end % classdef |