Mercurial > hg > ltpda
annotate m-toolbox/classes/@LTPDADatabaseConnectionManager/LTPDADatabaseConnectionManager.m @ 42:f90d4f666cc7 database-connection-manager
Cleanup
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 18:04:34 +0100 |
parents | 6def6533cb16 |
children |
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 = {}; |
41
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
7 userMessage = []; |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 end % private properties |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 properties(Dependent=true) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 credentialsExpiry; % seconds |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 cachePassword; % 0=no 1=yes 2=ask |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 maxConnectionsNumber; |
33
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
16 knownRepositories; % known repositories stored into prefereces |
2
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 end % dependent properties |
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 methods(Static) |
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 function reset() |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 % RESET Resets the state of the connection manager. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 % This static method removes the LTPDADatabaseConnectionManager |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 % 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
|
27 % 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
|
28 % the connection pool. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 rmappdata(0, LTPDADatabaseConnectionManager.appdataKey); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 function key = appdataKey() |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 % 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
|
36 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 % 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
|
38 % 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
|
39 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 key = 'LTPDADatabaseConnectionManager'; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 end |
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 end % static 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 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 function cm = LTPDADatabaseConnectionManager() |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 % LTPDACONNECTIONMANAGER Manages credentials and database connections. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 % This constructor returns an handler to a LTPDADatabaseConnectionManager |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 % class instance. Database connections can be obtained trough the |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 % obtained object with the connect() method. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 % 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
|
55 % 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
|
56 % 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
|
57 % 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
|
58 % persisted trough the appdata matlab facility. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 % import credentials class |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 import utils.credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 % load state from appdata |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 acm = getappdata(0, cm.appdataKey()); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 if isempty(acm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 % store state in appdata |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 setappdata(0, cm.appdataKey(), cm); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 utils.helper.msg(msg.PROC1, 'new connection manager'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 cm = acm; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 end |
33
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
75 |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
76 % add known repositories from preferences |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
77 repos = cm.knownRepositories; |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
78 for kk = 1:numel(repos) |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
79 repo = repos{kk}; |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
80 for jj = 1:3 |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
81 % null empty parameters |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
82 if isempty(repo{jj}) |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
83 repo{jj} = []; |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
84 end |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
85 end |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
86 cm.add(utils.credentials(repo{:})); |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
87 end |
41
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
88 |
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
89 % reset user message |
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
90 cm.userMessage = []; |
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 |
33
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
93 |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 function str = disp(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 disp(sprintf('%s()\n', class(cm))); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 end |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 function val = get.credentialsExpiry(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 % obtain from user preferences |
4
e3c5468b1bfe
Integrate with LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
2
diff
changeset
|
101 p = getappdata(0, 'LTPDApreferences'); |
e3c5468b1bfe
Integrate with LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
2
diff
changeset
|
102 val = double(p.getRepoPrefs().getExpiry()); |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103 end |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 function val = get.cachePassword(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 % obtain from user preferences |
4
e3c5468b1bfe
Integrate with LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
2
diff
changeset
|
108 p = getappdata(0, 'LTPDApreferences'); |
e3c5468b1bfe
Integrate with LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
2
diff
changeset
|
109 val = double(p.getRepoPrefs().getCachePassword()); |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110 end |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 function val = get.maxConnectionsNumber(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 % obtain from user preferences |
4
e3c5468b1bfe
Integrate with LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
2
diff
changeset
|
115 p = getappdata(0, 'LTPDApreferences'); |
e3c5468b1bfe
Integrate with LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
2
diff
changeset
|
116 val = double(p.getRepoPrefs().getMaxConnectionsNumber()); |
2
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 |
33
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
120 function val = get.knownRepositories(cm) |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
121 % obtain from user preferences |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
122 p = getappdata(0, 'LTPDApreferences'); |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
123 val = cell(p.getRepoPrefs().getRepositories().toArray()); |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
124 end |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
125 |
5e7477b94d94
Add known repositories list to LTPDAPreferences
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
4
diff
changeset
|
126 |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127 function n = count(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 % 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
|
129 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 % 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
|
131 % 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
|
132 % collected. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134 import utils.const.* |
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 % find closed connections in the pool |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
137 mask = false(numel(cm.connections), 1); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
138 for kk = 1:numel(cm.connections) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
139 if cm.connections{kk}.isClosed() |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
140 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
|
141 mask(kk) = true; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
142 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
143 end |
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 % remove them |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
146 cm.connections(mask) = []; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
147 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
148 % count remainig ones |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
149 n = numel(cm.connections); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
150 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
151 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
152 function clear(cm) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
153 % CLEAR Removes all cached credentials from the connection manager. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
154 cm.credentials = {}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
155 end |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
158 function conn = connect(cm, varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
159 % CONNECT Uses provided credential to establish a database connection. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
160 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
161 % CONNECT(hostname, database, username, password) Returns an object |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
162 % 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
|
163 % 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
|
164 % be queried for the missing information. |
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 % 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
|
167 % 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
|
168 % connection are instantiated. Closed connections are automatically |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
169 % removed from the pool. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
170 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
171 % 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
|
172 % 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
|
173 % 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
|
174 % 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
|
175 % connection in not added to the connection pool. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
176 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
177 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
178 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
179 % save current credentials cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
180 cache = cm.credentials; |
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 % count open connections in the pool |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
183 count = cm.count(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
184 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
185 % check parameters |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
186 if numel(varargin) == 1 && isa(varargin{1}, 'plist') |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
187 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
188 % extract parameters from plist |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
189 pl = varargin{1}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
190 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
191 % check if we have a connection parameter |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
192 conn = find(pl, 'conn'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
193 if ~isempty(conn) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
194 % check that it implements java.sql.Connection interface |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
195 if ~isa(conn, 'java.sql.Connection') |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
196 error('### connection is not valid database connection'); |
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 % return this connection |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
199 return; |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
202 % otherwise |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
203 hostname = find(pl, 'hostname'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
204 database = find(pl, 'database'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
205 username = find(pl, 'username'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
206 password = find(pl, 'password'); |
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 % if there is no hostname ignore other parameters |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
209 if ~ischar(hostname) || isempty(hostname) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
210 varargin = {}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
211 % if there is no database ignore other parameters |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
212 elseif ~ischar(database) || isempty(database) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
213 varargin = {hostname}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
214 % if there is no username ignore other parameters |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
215 elseif ~ischar(username) || isempty(username) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
216 varargin = {hostname, database}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
217 % 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
|
218 elseif ~ischar(password) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
219 varargin = {hostname, database, username}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
220 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
221 varargin = {hostname, database, username, password}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
222 end |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
225 % check number of connections |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
226 if count > cm.maxConnectionsNumber |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
227 error('### too many open connections'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
228 end |
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 % connect |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
231 try |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
232 conn = cm.getConnection(varargin{:}); |
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 % add connection to pool |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
235 utils.helper.msg(msg.PROC1, 'add connection to pool'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
236 cm.connections{end+1} = conn; |
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 catch ex |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
239 % restore our copy of the credentials cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
240 utils.helper.msg(msg.PROC1, 'undo cache changes'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
241 cm.credentials = cache; |
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 % hide implementation details |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
244 ex.throw(); |
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 end |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
249 function close(cm, ids) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
250 % CLOSE Forces connections to be closed. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
251 % |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
252 % 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
|
253 % 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
|
254 % of those connections. |
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 % 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
|
257 % 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
|
258 % closed. |
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 if nargin < 2 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
261 ids = 1:numel(cm.connections); |
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 cellfun(@close, cm.connections(ids)); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
264 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
265 % remove closed connections from pool |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
266 cm.count(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
267 end |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
270 function add(cm, c) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
271 % ADD Adds credentials to the credentials cache. |
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 % 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
|
274 % 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
|
275 % only credentials in the form of utils.credentials objects. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
276 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
277 % check input arguments |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
278 if nargin < 2 || ~isa(c, 'utils.credentials') |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
279 error('### invalid call'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
280 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
281 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
282 % add to the cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
283 cm.cacheCredentials(c); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
284 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
285 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
286 end % methods |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
287 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
288 methods(Access=private) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
289 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
290 function conn = getConnection(cm, varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
291 % 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
|
292 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
293 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
294 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
295 % handle variable number of arguments |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
296 switch numel(varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
297 case 0 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
298 % find credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
299 [hostname, database, username] = cm.selectDatabase([cm.credentials{:}]); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
300 conn = cm.getConnection(hostname, database, username); |
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 1 |
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{:}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
305 if numel(cred) == 0 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
306 cred = utils.credentials(varargin{:}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
307 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
308 [hostname, database, username] = cm.selectDatabase(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
309 conn = cm.getConnection(hostname, database, username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
310 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
311 case 2 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
312 % find credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
313 cred = cm.findCredentials(varargin{:}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
314 switch numel(cred) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
315 case 0 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
316 conn = cm.getConnection(varargin{1}, varargin{2}, []); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
317 case 1 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
318 conn = cm.getConnection(cred.hostname, cred.database, cred.username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
319 otherwise |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
320 [hostname, database, username] = cm.selectDatabase(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
321 conn = cm.getConnection(hostname, database, username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
322 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
323 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
324 case 3 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
325 % find credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
326 cred = cm.findCredentials(varargin{1}, varargin{2}, varargin{3}); |
42 | 327 if numel(cred) == 0 |
328 % no credentials found | |
329 usernames = { varargin{3} }; | |
330 if isempty(varargin{3}) | |
331 % use usernames for same hostname | |
332 tmp = cm.findCredentials(varargin{1}); | |
333 if ~isempty(tmp) | |
334 usernames = { tmp(:).username }; | |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
335 end |
42 | 336 end |
337 % build credentials objects | |
338 tmp = {}; | |
339 for kk = 1:numel(usernames) | |
340 tmp{kk} = utils.credentials(varargin{1}, varargin{2}, usernames{kk}); | |
341 end | |
342 % convert from cell array to array | |
343 cred = [tmp{:}]; | |
344 else | |
345 % credentials in cache | |
346 utils.helper.msg(msg.PROC1, 'use cached credentials'); | |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
347 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
348 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
349 cache = false; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
350 if (numel(cred) > 1) || ~cred.complete |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
351 % ask for password |
41
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
352 [username, password, cache] = cm.inputCredentials(cred, cm.userMessage); |
42 | 353 |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
354 % cache credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
355 cred = utils.credentials(varargin{1}, varargin{2}, username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
356 cm.cacheCredentials(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
357 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
358 % add password to credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
359 cred.password = password; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
360 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
361 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
362 % try to connect |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
363 try |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
364 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
|
365 catch ex |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
366 % look for access denied errors |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
367 if strcmp(ex.identifier, 'utils:mysql:connect:AccessDenied') |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
368 % ask for new new credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
369 utils.helper.msg(msg.IMPORTANT, ex.message); |
41
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
370 cm.userMessage = 'Authentication error!'; |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
371 conn = cm.getConnection(varargin{1}, varargin{2}, varargin{3}); |
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 % error out |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
374 throw(ex); |
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 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 % cache password |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
379 if cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
380 utils.helper.msg(msg.PROC1, 'cache password'); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
381 cm.cacheCredentials(cred); |
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 case 4 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
385 % connect |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
386 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
|
387 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
388 if cm.cachePassword == 1 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
389 % cache credentials with password |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
390 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
|
391 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
392 % cache credentials without password |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
393 cred = utils.credentials(varargin{1}, varargin{2}, varargin{3}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
394 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
395 cm.cacheCredentials(cred); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
396 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
397 otherwise |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
398 error('### invalid call') |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
401 end |
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 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
404 function ids = findCredentialsId(cm, varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
405 % 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
|
406 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
407 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
408 ids = []; |
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 for kk = 1:numel(cm.credentials) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
411 % invalidate expired passwords |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
412 if expired(cm.credentials{kk}) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
413 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
|
414 cm.credentials{kk}.password = []; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
415 cm.credentials{kk}.expiry = 0; |
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 % match input with cache |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
419 if match(cm.credentials{kk}, varargin{:}) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
420 ids = [ ids kk ]; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
421 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
422 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
423 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
424 |
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 function cred = findCredentials(cm, varargin) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
427 % 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
|
428 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
429 % default |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
430 cred = []; |
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 % search |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
433 ids = findCredentialsId(cm, varargin{:}); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
434 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
435 % return a credentials objects array |
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 cred = [cm.credentials{ids}]; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
438 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
439 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
440 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
441 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
442 function cacheCredentials(cm, c) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
443 % CACHECREDENTIALS Adds to or updates the credentials cache. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
444 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
445 import utils.const.* |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
446 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
447 % find entry to update |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
448 ids = findCredentialsId(cm, c.hostname, c.database, c.username); |
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 % set password expiry time |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
451 if ischar(c.password) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
452 c.expiry = double(time()) + cm.credentialsExpiry; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
453 end |
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 if isempty(ids) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
456 % add at the end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
457 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
|
458 cm.credentials{end+1} = c; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
459 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
460 for id = ids |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
461 % 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
|
462 if length(c) > length(cm.credentials{id}) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
463 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
|
464 cm.credentials{id} = c; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
465 else |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
466 % always update expiry time |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
467 cm.credentials{id}.expiry = c.expiry; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
468 end |
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 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
471 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
472 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
473 |
41
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
474 function [username, password, cache] = inputCredentials(cm, cred, msg) |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
475 % INPUTCREDENTIALS Queries the user for database username and password. |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
476 |
41
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
477 % msg is an optional argument |
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
478 if nargin < 3 |
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
479 msg = []; |
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
480 end |
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
481 |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
482 % build a cell array of usernames |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
483 users = {}; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
484 for id = 1:numel(cred) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
485 if ~isempty(cred(id).username) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
486 users = [ users { cred(id).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 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
489 users = sort(unique(users)); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
490 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
491 parent = com.mathworks.mde.desk.MLDesktop.getInstance().getMainFrame(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
492 dialog = javaObjectEDT('connectionmanager.CredentialsDialog', ... |
41
6def6533cb16
Report authentication errors to user
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
33
diff
changeset
|
493 parent, cred(1).hostname, cred(1).database, users, cm.cachePassword, msg); |
2
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
494 dialog.show(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
495 if dialog.cancelled |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
496 throw(MException('utils:mysql:connect:UserCancelled', '### user cancelled')); |
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 username = char(dialog.username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
499 password = char(dialog.password); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
500 cache = logical(dialog.cache); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
501 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
502 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
503 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
504 function [hostname, database, username] = selectDatabase(cm, credentials) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
505 % 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
|
506 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
507 parent = com.mathworks.mde.desk.MLDesktop.getInstance().getMainFrame(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
508 dialog = javaObjectEDT('connectionmanager.DatabaseSelectorDialog', parent); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
509 for c = credentials |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
510 dialog.add(c.hostname, c.database, c.username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
511 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
512 dialog.show(); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
513 if dialog.cancelled |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
514 throw(MException('utils:mysql:connect:UserCancelled', '### user cancelled')); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
515 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
516 hostname = char(dialog.hostname); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
517 database = char(dialog.database); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
518 username = char(dialog.username); |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
519 if isempty(username) |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
520 username = []; |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
521 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
522 end |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
523 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
524 end % private methods |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
525 |
18e956c96a1b
Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
526 end % classdef |