annotate LTPDAConnectionManager.m @ 5:35f1cfcaa5a9

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