Mercurial > hg > ltpda
comparison m-toolbox/classes/+utils/@credentials/credentials.m @ 2:18e956c96a1b database-connection-manager
Add LTPDADatabaseConnectionManager implementation. Matlab code
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Sun, 04 Dec 2011 21:23:09 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:2014ba5b353a | 2:18e956c96a1b |
---|---|
1 classdef credentials | |
2 | |
3 properties | |
4 | |
5 hostname = []; | |
6 database = []; | |
7 username = []; | |
8 password = []; | |
9 expiry = 0; | |
10 | |
11 end % properties | |
12 | |
13 methods | |
14 | |
15 function obj = credentials(hostname, database, username, password) | |
16 % CREDENTIALS Constructor for credentials objects. | |
17 % | |
18 % Those are simple container objects to hold credentials required for | |
19 % establishing a connection to a database server, in addition to an | |
20 % expiry time. | |
21 % | |
22 % CREDENTIALS(hostname, database, username, password) The constructor can | |
23 % be called with any number of arguments. The default value for the object | |
24 % properties is the empty vector. | |
25 | |
26 switch nargin | |
27 case 1 | |
28 obj.hostname = hostname; | |
29 case 2 | |
30 obj.hostname = hostname; | |
31 obj.database = database; | |
32 case 3 | |
33 obj.hostname = hostname; | |
34 obj.database = database; | |
35 obj.username = username; | |
36 case 4 | |
37 obj.hostname = hostname; | |
38 obj.database = database; | |
39 obj.username = username; | |
40 obj.password = password; | |
41 end | |
42 end | |
43 | |
44 function str = char(obj, mode) | |
45 % CHAR Convert a credentials object to string representation. | |
46 % | |
47 % It takes an optional second argument that defines the representation to | |
48 % use. The default is to replace the password, if present, with the YES | |
49 % string, other possible modes are SHORT where password is omitted, or FULL | |
50 % where the password is shown at it is. | |
51 | |
52 if nargin < 2 | |
53 mode = ''; | |
54 end | |
55 switch mode | |
56 case 'short' | |
57 % do not show password | |
58 frm = 'mysql://%s/%s username=%s'; | |
59 str = sprintf(frm, obj.hostname, obj.database, obj.username); | |
60 case 'full' | |
61 % show password | |
62 frm = 'mysql://%s/%s username=%s password=%s'; | |
63 str = sprintf(frm, obj.hostname, obj.database, obj.username, obj.password); | |
64 otherwise | |
65 % by default only show if a password is known | |
66 passwd = []; | |
67 if ischar(obj.password) | |
68 passwd = 'YES'; | |
69 end | |
70 frm = 'mysql://%s/%s username=%s password=%s'; | |
71 str = sprintf(frm, obj.hostname, obj.database, obj.username, passwd); | |
72 end | |
73 end | |
74 | |
75 function disp(objs) | |
76 % DISP Overloaded display method for credentials objects. | |
77 % | |
78 % Uses the default string representation of the char() method where the | |
79 % password, if present, is replaced with the string YES. | |
80 | |
81 for obj = objs | |
82 disp([' ' char(obj) char(10)]); | |
83 end | |
84 end | |
85 | |
86 function len = length(obj) | |
87 % LENGTH Returns the number of not null fields in the object. | |
88 | |
89 len = 0; | |
90 if ~isempty(obj.hostname) | |
91 len = len + 1; | |
92 end | |
93 if ~isempty(obj.database) | |
94 len = len + 1; | |
95 end | |
96 if ~isempty(obj.username) | |
97 len = len + 1; | |
98 end | |
99 if ~isempty(obj.password) | |
100 len = len + 1; | |
101 end | |
102 end | |
103 | |
104 function rv = complete(obj) | |
105 % COMPLETE Checks if the credentials are complete. | |
106 % | |
107 % Credentials object are complete when they contains all the required | |
108 % information to connect to a database. Namely the HOSTNAME, DATABASE | |
109 % and USERNAME properties should not be empty, the PASSWORD property is | |
110 % allowed to be an empty string '' but not []. | |
111 | |
112 info = {'hostname', 'database', 'username'}; | |
113 for kk = 1:numel(info) | |
114 if isempty(obj.(info{kk})) | |
115 rv = false; | |
116 return; | |
117 end | |
118 end | |
119 if ~ischar(obj.password) | |
120 rv = false; | |
121 return; | |
122 end | |
123 rv = true; | |
124 end | |
125 | |
126 function rv = expired(obj) | |
127 % EXPIRED Checks if the credentials are expired. | |
128 % | |
129 % Credential objects expire when their expiry time is smaller than the | |
130 % current time in seconds since the epoch, as obtained by the time() | |
131 % function. Credentials with zero or negative expiry time never expire. | |
132 | |
133 rv = false; | |
134 if obj.expiry > 0 && double(time()) > obj.expiry | |
135 rv = true; | |
136 end | |
137 end | |
138 | |
139 function rv = match(obj, hostname, database, username) | |
140 % MATCH Check if the credentials object matches the given information. | |
141 % | |
142 % MATCH(obj, hostname) Returns true when HOSTANAME parameter match | |
143 % the object properties. | |
144 % | |
145 % MATCH(obj, hostname, database) Returns true when HOSTANAME and | |
146 % DATABASE parameters match the object properties. | |
147 % | |
148 % MATCH(obj, hostname, database, username) Returns true when | |
149 % HOSTANAME, DATABASE, and USERNAME parameters match the object | |
150 % properties. | |
151 | |
152 % default arguments | |
153 switch nargin | |
154 case 4 | |
155 case 3 | |
156 username = []; | |
157 case 2 | |
158 username = []; | |
159 database = []; | |
160 otherwise | |
161 error('### wrong number of parameters'); | |
162 end | |
163 | |
164 % default return value | |
165 rv = true; | |
166 | |
167 if ~strcmp(obj.hostname, hostname) | |
168 rv = false; | |
169 return; | |
170 end | |
171 if ischar(database) && ~strcmp(obj.database, database) | |
172 rv = false; | |
173 return; | |
174 end | |
175 if ischar(username) && ~strcmp(obj.username, username) | |
176 rv = false; | |
177 return; | |
178 end | |
179 end | |
180 | |
181 end % methods | |
182 | |
183 end |