annotate LTPDAConnectionManager.m @ 1:c4b57991935a

Tell emacs to use org mode for this file.
author Daniele Nicolodi <daniele@science.unitn.it>
date Sun, 23 May 2010 22:06:57 +0200
parents d5fef23867bb
children c706c10a76bd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
1 classdef LTPDAConnectionManager < handle
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
2
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
3 properties(SetAccess=private)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
4
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
5 connections = {};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
6 credentials = {};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
7
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
8 end % private properties
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
9
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
10 properties(Dependent=true)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
11
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
12 credentialsExpiry; % seconds
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
13 cachePassword; % 0=no 1=yes 2=ask
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
14 maxConnectionsNumber;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
15
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
16 end % dependent properties
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
17
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
18 methods(Static)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
19
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
20 function reset()
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
21 setappdata(0, LTPDAConnectionManager.appdataKey, []);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
22 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
23
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
24 function key = appdataKey()
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
25 % defined as static method to be acessible by the reset static method
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
26 key = 'LTPDAConnectionManager';
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
27 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
28
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
29 end % static methods
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
30
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
31 methods
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
32
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
33 function cm = LTPDAConnectionManager(pl)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
34
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
35 % load state from appdata
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
36 acm = getappdata(0, cm.appdataKey());
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
37
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
38 if isempty(acm)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
39 % take those from user preferences
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
40 cm.credentials{end+1} = credentials('localhost', 'one');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
41 cm.credentials{end+1} = credentials('localhost', 'two', 'daniele');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
42
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
43 % store state in appdata
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
44 setappdata(0, cm.appdataKey(), cm);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
45
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
46 import utils.const.*
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
47 utils.helper.msg(msg.PROC1, 'new connection manager');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
48 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
49 cm = acm;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
50 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
51 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
52
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
53
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
54 function val = get.credentialsExpiry(cm)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
55 % obtain from user preferences
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
56 p = getappdata(0, 'LTPDApreferences');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
57 val = p.cm.credentialsExpiry;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
58 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
59
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
60
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
61 function val = get.cachePassword(cm)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
62 % obtain from user preferences
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
63 p = getappdata(0, 'LTPDApreferences');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
64 val = p.cm.cachePassword;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
65 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
66
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
67
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
68 function val = get.maxConnectionsNumber(cm)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
69 % obtain from user preferences
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
70 p = getappdata(0, 'LTPDApreferences');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
71 val = p.cm.maxConnectionsNumber;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
72 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
73
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
74
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
75 function n = count(cm)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
76 import utils.const.*
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
77 % find closed connections in the pool
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
78 mask = false(numel(cm.connections), 1);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
79 for kk = 1:numel(cm.connections)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
80 if cm.connections{kk}.isClosed()
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
81 utils.helper.msg(msg.PROC1, 'connection id=%d closed', kk);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
82 mask(kk) = true;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
83 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
84 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
85
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
86 % remove them
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
87 cm.connections(mask) = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
88
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
89 % count remainig ones
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
90 n = numel(cm.connections);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
91 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
92
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
93
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
94 function clear(cm)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
95 % remove all cached credentials
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
96 cm.credentials = {};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
97 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
98
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
99
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
100 function conn = connect(cm, varargin)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
101 import utils.const.*
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
102
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
103 % save current credentials cache
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
104 cache = cm.credentials;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
105
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
106 % count open connections in the pool
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
107 count = cm.count();
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
108
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
109 % check parameters
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
110 if numel(varargin) == 1 && isa(varargin{1}, 'plist')
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
111
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
112 % extract parameters from plist
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
113 pl = varargin{1};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
114
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
115 % check if we have a connection parameter
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
116 conn = find(pl, 'connection');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
117 if ~isempty(conn)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
118 % check that it implements java.sql.Connection interface
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
119 if ~isa(conn, 'java.sql.Connection')
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
120 error('### connection is not valid database connection');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
121 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
122 % return this connection
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
123 return;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
124 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
125
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
126 % otherwise
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
127 hostname = find(pl, 'hostname');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
128 database = find(pl, 'database');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
129 username = find(pl, 'username');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
130 password = find(pl, 'password');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
131
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
132 % if there is no hostname and database ignore other parameters
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
133 if ~ischar(hostname) || ~ischar(database)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
134 varargin = {};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
135 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
136 % password can not be null but can be an empty string
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
137 if ~ischar(password)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
138 varargin = {hostname, database, username};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
139 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
140 varargin = {hostname, database, username, password};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
141 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
142 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
143
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
144 % check number of connections
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
145 if count > cm.maxConnectionsNumber
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
146 error('### too many open connections');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
147 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
148
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
149 % connect
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
150 try
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
151 conn = cm.getConnection(varargin{:});
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
152 catch ex
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
153 % restore our copy of the credentials cache
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
154 utils.helper.msg(msg.PROC1, 'undo cache changes');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
155 cm.credentials = cache;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
156
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
157 % hide implementation details
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
158 %ex.throwAsCaller();
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
159 ex.rethrow()
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
160 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
161 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
162
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
163
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
164 function close(cm, ids)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
165 if nargin < 2
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
166 ids = 1:numel(cm.connections);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
167 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
168 cellfun(@close, cm.connections(ids));
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
169 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
170
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
171
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
172 function add(cm, c)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
173 if nargin < 2 || ~isa(c, 'credentials')
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
174 error('### invalid call');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
175 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
176 cm.cacheCredentials(c);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
177 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
178
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
179 end % methods
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
180
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
181 methods(Access=private)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
182
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
183 function conn = getConnection(cm, varargin)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
184
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
185 import utils.const.*
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
186
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
187 switch numel(varargin)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
188 case 0
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
189 [hostname, database, username] = cm.selectDatabase();
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
190 conn = cm.getConnection(hostname, database, username);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
191
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
192 case 2
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
193 conn = cm.getConnection(varargin{1}, varargin{2}, []);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
194
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
195 case 3
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
196 % find credentials
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
197 cred = cm.findCredentials(varargin{1}, varargin{2}, varargin{3});
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
198 if isempty(cred)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
199 % no credentials found
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
200 cred = credentials(varargin{1}, varargin{2}, varargin{3});
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
201 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
202 utils.helper.msg(msg.PROC1, 'use cached credentials');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
203 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
204
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
205 cache = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
206 if numel(cred) > 1 || ~cred.complete
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
207 % ask for which username and password to use
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
208 [username, password, cache] = cm.inputCredentials(cred);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
209
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
210 % cache credentials
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
211 cred = credentials(varargin{1}, varargin{2}, username);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
212 cm.cacheCredentials(cred);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
213
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
214 % add password to credentials
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
215 cred.password = password;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
216 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
217
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
218 % try to connect
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
219 conn = cm.getConnection(cred.hostname, cred.database, cred.username, cred.password);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
220
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
221 % cache password
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
222 if cache
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
223 utils.helper.msg(msg.PROC1, 'cache password');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
224 cm.cacheCredentials(cred);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
225 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
226
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
227 case 4
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
228 try
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
229 % connect
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
230 conn = connect(varargin{1}, varargin{2}, varargin{3}, varargin{4});
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
231
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
232 % cache credentials without password
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
233 cred = credentials(varargin{1}, varargin{2}, varargin{3}, []);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
234 cm.cacheCredentials(cred);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
235
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
236 catch ex
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
237 % look for access denied errors
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
238 if strcmp(ex.identifier, 'utils:jmysql:connect:AccessDenied')
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
239 % ask for new new credentials
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
240 utils.helper.msg(msg.PROC1, ex.message);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
241 conn = cm.getConnection(varargin{1}, varargin{2}, varargin{3});
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
242 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
243 % error out
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
244 throw(MException('', '### connection error').addCause(ex));
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
245 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
246 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
247
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
248 % add connection to pool
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
249 utils.helper.msg(msg.PROC1, 'add connection to pool');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
250 cm.connections{end+1} = conn;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
251
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
252 otherwise
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
253 error('### invalid call')
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
254 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
255
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
256 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
257
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
258
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
259 function ids = findCredentialsId(cm, varargin)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
260 import utils.const.*
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
261 ids = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
262
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
263 for kk = 1:numel(cm.credentials)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
264 % invalidate expired passwords
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
265 if expired(cm.credentials{kk})
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
266 utils.helper.msg(msg.PROC1, 'cache entry id=%d expired', kk);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
267 cm.credentials{kk}.password = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
268 cm.credentials{kk}.expiry = 0;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
269 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
270
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
271 % match input with cache
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
272 if match(cm.credentials{kk}, varargin{:})
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
273 ids = [ ids kk ];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
274 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
275 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
276 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
277
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
278
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
279 function cred = findCredentials(cm, varargin)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
280 % default
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
281 cred = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
282
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
283 % search
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
284 ids = findCredentialsId(cm, varargin{:});
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
285
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
286 % return an array credentials
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
287 if ~isempty(ids)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
288 cred = [ cm.credentials{ids} ];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
289 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
290 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
291
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
292
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
293 function cacheCredentials(cm, c)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
294 import utils.const.*
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
295
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
296 % find entry to update
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
297 id = findCredentialsId(cm, c.hostname, c.database, c.username);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
298
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
299 % sanity check
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
300 if numel(id) > 1
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
301 error('### more than one cache entry for %s', char(c, 'short'));
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
302 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
303
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
304 % set password expiry time
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
305 if ischar(c.password)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
306 c.expiry = double(time()) + cm.credentialsExpiry;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
307 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
308
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
309 if isempty(id)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
310 % add at the end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
311 utils.helper.msg(msg.PROC1, 'add cache entry %s', char(c));
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
312 cm.credentials{end+1} = c;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
313 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
314 % update only if the cached informations are less than the one we have
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
315 if ~complete(cm.credentials{id})
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
316 utils.helper.msg(msg.PROC1, 'update cache entry id=%d %s', id, char(c));
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
317 cm.credentials{id} = c;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
318 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
319 % always update expiry time
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
320 cm.credentials{id}.expiry = c.expiry;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
321 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
322 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
323 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
324
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
325
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
326 function [username, password, cache] = inputCredentials(cm, cred)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
327 % this is a stubb
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
328
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
329 % build a cell array of usernames and passwords
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
330 users = { cred(:).username };
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
331 passw = { cred(:).password };
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
332
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
333 % default to the latest used username
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
334 [e, ids] = sort([ cred(:).expiry ]);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
335 default = users{ids(1)};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
336
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
337 username = choose('Username', users, default);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
338
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
339 % pick the corresponding password
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
340 ids = find(strcmp(users, username));
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
341 if ~isempty(ids)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
342 default = passw{ids(1)};
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
343 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
344 default = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
345 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
346
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
347 password = ask('Password', '');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
348
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
349 if cm.cachePassword == 2
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
350 cache = ask('Store credentials', 'n');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
351 if ~isempty(cache) && cache(1) == 'y'
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
352 cache = true;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
353 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
354 cache = false;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
355 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
356 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
357 cache = logical(cm.cachePassword);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
358 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
359 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
360
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
361
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
362 function [hostname, database, username] = selectDatabase(cm)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
363 % this is a stubb
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
364
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
365 for kk = 1:numel(cm.credentials)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
366 fprintf('% 2d. %s\n', char(cm.credentials{kk}));
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
367 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
368 fprintf('%d. NEW (default)\n', numel(cm.credentials)+1);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
369 str = input('Select connection: ', 's');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
370 if isempty(str)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
371 id = numel(cm.credentials)+1;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
372 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
373 id = eval(str);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
374 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
375 if id > numel(cm.credentials)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
376 hostname = input('Hostname: ', 's');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
377 database = input('Database: ', 's');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
378 username = [];
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
379 else
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
380 hostname = cm.credentials{kk}.hostname;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
381 database = cm.credentials{kk}.database;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
382 username = cm.credentials{kk}.username;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
383 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
384 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
385
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
386 end % private methods
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
387
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
388 end % classdef
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
389
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
390
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
391 % this should become utils.jmysql.connect
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
392 function conn = connect(hostname, database, username, password)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
393
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
394 % informative message
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
395 import utils.const.*
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
396 utils.helper.msg(msg.PROC1, 'connection to mysql://%s/%s username=%s', hostname, database, username);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
397
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
398 % connection credential
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
399 uri = sprintf('jdbc:mysql://%s/%s', hostname, database);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
400 db = javaObject('com.mysql.jdbc.Driver');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
401 pl = javaObject('java.util.Properties');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
402 pl.setProperty(db.USER_PROPERTY_KEY, username);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
403 pl.setProperty(db.PASSWORD_PROPERTY_KEY, password);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
404
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
405 try
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
406 % connect
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
407 conn = db.connect(uri, pl);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
408 catch ex
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
409 % haven't decided yet if this code should be here or higher in the stack
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
410 if strcmp(ex.identifier, 'MATLAB:Java:GenericException')
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
411 % exceptions handling in matlab sucks
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
412 if ~isempty(strfind(ex.message, 'java.sql.SQLException: Access denied'))
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
413 throw(MException('utils:jmysql:connect:AccessDenied', '### access denied').addCause(ex));
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
414 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
415 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
416 rethrow(ex);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
417 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
418 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
419
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
420
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
421 function str = ask(msg, default)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
422 str = input(sprintf('%s (default: %s): ', msg, default), 's');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
423 if isempty(str)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
424 str = default;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
425 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
426 if ~ischar(str)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
427 str = char(str);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
428 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
429 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
430
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
431 function str = choose(msg, choices, default)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
432 options = sprintf('%s, ', choices{:});
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
433 options = options(1:end-2);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
434 str = input(sprintf('%s (options: %s, default: %s): ', msg, options, default), 's');
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
435 if isempty(str)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
436 str = default;
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
437 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
438 if ~ischar(str)
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
439 str = char(str);
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
440 end
d5fef23867bb First workig implementation.
Daniele Nicolodi <daniele@science.unitn.it>
parents:
diff changeset
441 end