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