|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| package org.globus.cas.impl.databaseAccess; |
|
12 |
| |
|
13 |
| import java.sql.Connection; |
|
14 |
| import java.sql.Statement; |
|
15 |
| import java.sql.SQLException; |
|
16 |
| import java.sql.ResultSet; |
|
17 |
| |
|
18 |
| import java.util.Vector; |
|
19 |
| |
|
20 |
| import org.apache.commons.logging.Log; |
|
21 |
| import org.apache.commons.logging.LogFactory; |
|
22 |
| |
|
23 |
| import org.globus.cas.impl.CasConstants; |
|
24 |
| |
|
25 |
| import org.globus.cas.types.UserData; |
|
26 |
| import org.globus.cas.types.ArrayOfString; |
|
27 |
| import org.globus.cas.types.CasObjectData; |
|
28 |
| |
|
29 |
| import org.globus.util.I18n; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| public class UserDataHandler { |
|
35 |
| |
|
36 |
| static Log logger = LogFactory.getLog(UserDataHandler.class.getName() ); |
|
37 |
| |
|
38 |
| private static I18n i18n = |
|
39 |
| I18n.getI18n("org.globus.cas.impl.databaseAccess.errors", |
|
40 |
| UserDataHandler.class.getClassLoader()); |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
0
| public static String storeObject(UserData userData) throws CasDBException {
|
|
49 |
| |
|
50 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
51 |
| |
|
52 |
| |
|
53 |
0
| String columnString = "insert into " + CasDBConstants.TABLE_USER
|
|
54 |
| + " (" + CasDBConstants.COL_USER_NICKNAME + "," |
|
55 |
| + CasDBConstants.COL_SUBJECTNAME + "," |
|
56 |
| + CasDBConstants.COL_TRUST_NICKNAME + ") values ('"; |
|
57 |
0
| StringBuffer query = new StringBuffer(columnString);
|
|
58 |
0
| query.append(userData.getNickname()).append("','")
|
|
59 |
| .append(userData.getSubjectName()).append("','") |
|
60 |
| .append(userData.getTrustAnchorName()).append("')"); |
|
61 |
0
| Statement statement = null;
|
|
62 |
0
| try {
|
|
63 |
0
| statement = connection.createStatement();
|
|
64 |
0
| logger.debug("store query is " + query.toString());
|
|
65 |
0
| statement.executeUpdate(query.toString());
|
|
66 |
| } |
|
67 |
| catch (SQLException exp) { |
|
68 |
0
| logger.error(i18n.getMessage("storeErr", new Object[] {
|
|
69 |
| "user ", query }), exp); |
|
70 |
0
| throw new CasDBException(i18n.getMessage("storeErr", new Object[] {
|
|
71 |
| "user data", exp.getMessage() }), exp); |
|
72 |
| } |
|
73 |
| finally { |
|
74 |
| |
|
75 |
| |
|
76 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
77 |
0
| try {
|
|
78 |
0
| if (statement != null)
|
|
79 |
0
| statement.close();
|
|
80 |
| } |
|
81 |
| catch (SQLException exp) { |
|
82 |
0
| logger.warn(i18n.getMessage("storeErrClose", "user data"),
|
|
83 |
| exp); |
|
84 |
| } |
|
85 |
| } |
|
86 |
0
| return userData.getNickname();
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
0
| public static String[] list() throws CasDBException {
|
|
93 |
0
| String query = "select" + CasDBConstants.COL_USER_NICKNAME + " from"
|
|
94 |
| + CasDBConstants.TABLE_USER; |
|
95 |
0
| return CasDBStorage.runListQuery(query,
|
|
96 |
| CasDBConstants.COL_USER_NICKNAME); |
|
97 |
| } |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
0
| public static CasObjectData retrieveObject(String userNickname)
|
|
104 |
| throws CasDBException { |
|
105 |
| |
|
106 |
0
| logger.debug("retrieve Object called");
|
|
107 |
0
| UserData returnObject = null;
|
|
108 |
| |
|
109 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
110 |
0
| String query = "select" + CasDBConstants.COL_USERGP_NAME + " from"
|
|
111 |
| + CasDBConstants.TABLE_USERGP_ENTRY + " where " |
|
112 |
| + CasDBConstants.COL_USER_NICKNAME + " ='" + userNickname.trim() |
|
113 |
| + "'"; |
|
114 |
0
| logger.debug("query " + query);
|
|
115 |
0
| Vector groupNames = null;
|
|
116 |
0
| Statement statement = null;
|
|
117 |
0
| ResultSet resultSetGp = null;
|
|
118 |
0
| try {
|
|
119 |
0
| statement = connection.createStatement();
|
|
120 |
0
| resultSetGp = statement.executeQuery(query);
|
|
121 |
0
| if ((resultSetGp!=null) && (resultSetGp.next())) {
|
|
122 |
0
| groupNames = new Vector(resultSetGp.getFetchSize());
|
|
123 |
0
| do {
|
|
124 |
0
| groupNames.add((resultSetGp.getString(
|
|
125 |
| CasDBConstants.COL_USERGP_NAME.trim()))); |
|
126 |
0
| } while (resultSetGp.next());
|
|
127 |
| } |
|
128 |
| } |
|
129 |
| catch (SQLException exp) { |
|
130 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
131 |
0
| logger.error(i18n.getMessage("retrieveErr", new Object[] {
|
|
132 |
| "group data for user nickname " + userNickname, query}), exp); |
|
133 |
0
| throw new CasDBException(i18n.getMessage("retrieveErr",
|
|
134 |
| new Object[] { "group data for user nickname " |
|
135 |
| + userNickname, exp.getMessage()}), exp); |
|
136 |
| } |
|
137 |
| finally { |
|
138 |
0
| try {
|
|
139 |
0
| if (resultSetGp != null)
|
|
140 |
0
| resultSetGp.close();
|
|
141 |
0
| if (statement != null)
|
|
142 |
0
| statement.close();
|
|
143 |
| } |
|
144 |
| catch (SQLException exp) { |
|
145 |
0
| String err = "Error retrieving group data for user"
|
|
146 |
| + " nickname " + userNickname; |
|
147 |
0
| logger.warn(err, exp);
|
|
148 |
| } |
|
149 |
| } |
|
150 |
0
| ArrayOfString arrayOfGpnames = null;
|
|
151 |
0
| if (groupNames != null) {
|
|
152 |
0
| String groupNamesString[] = new String[groupNames.size()];
|
|
153 |
0
| groupNames.toArray(groupNamesString);
|
|
154 |
0
| arrayOfGpnames = new ArrayOfString(groupNamesString);
|
|
155 |
| } |
|
156 |
| |
|
157 |
0
| query = "select * from" + CasDBConstants.TABLE_USER + " where"
|
|
158 |
| + CasDBConstants.COL_USER_NICKNAME + " = '" + userNickname.trim() |
|
159 |
| + "'"; |
|
160 |
0
| ResultSet resultSet = null;
|
|
161 |
0
| try {
|
|
162 |
0
| statement = connection.createStatement();
|
|
163 |
0
| resultSet = statement.executeQuery(query);
|
|
164 |
0
| if (resultSet!=null) {
|
|
165 |
0
| if (resultSet.next()) {
|
|
166 |
0
| returnObject = new UserData();
|
|
167 |
0
| returnObject.setNickname(
|
|
168 |
| resultSet.getString( |
|
169 |
| CasDBConstants.COL_USER_NICKNAME.trim())); |
|
170 |
0
| returnObject.setSubjectName(
|
|
171 |
| resultSet.getString( |
|
172 |
| CasDBConstants.COL_SUBJECTNAME.trim())); |
|
173 |
0
| returnObject.setTrustAnchorName(
|
|
174 |
| resultSet.getString( |
|
175 |
| CasDBConstants.COL_TRUST_NICKNAME.trim())); |
|
176 |
0
| returnObject
|
|
177 |
| .setGroupNames(arrayOfGpnames); |
|
178 |
| } |
|
179 |
| } |
|
180 |
| } |
|
181 |
| catch (SQLException exp) { |
|
182 |
0
| logger.error(i18n.getMessage("retrieveErr", new Object[] {
|
|
183 |
| "user nickname " + userNickname, query }), exp); |
|
184 |
0
| throw new CasDBException(i18n.getMessage("retrieveErr",
|
|
185 |
| new Object[] { "user nickname " + userNickname, |
|
186 |
| exp.getMessage() }), exp); |
|
187 |
| } |
|
188 |
| finally { |
|
189 |
| |
|
190 |
| |
|
191 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
192 |
0
| try {
|
|
193 |
0
| if (resultSet != null)
|
|
194 |
0
| resultSet.close();
|
|
195 |
0
| if (statement != null)
|
|
196 |
0
| statement.close();
|
|
197 |
| } |
|
198 |
| catch (SQLException exp) { |
|
199 |
0
| logger.warn(i18n.getMessage("retrieveErrClose",
|
|
200 |
| "user nickname " + userNickname), |
|
201 |
| exp); |
|
202 |
| } |
|
203 |
| } |
|
204 |
| |
|
205 |
0
| return returnObject;
|
|
206 |
| } |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
0
| public static void deleteObject(String userNickname)
|
|
214 |
| throws CasDBException { |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
0
| if (ObjectGroupDataHandler.isMember(userNickname.trim(),
|
|
219 |
| CasConstants.USER_SPEC)) { |
|
220 |
0
| String errMesg = i18n.getMessage("memberOfGpErr", new Object[] {
|
|
221 |
| "User ", userNickname.trim() , "object"}); |
|
222 |
0
| logger.debug(errMesg);
|
|
223 |
0
| throw new CasDBException(errMesg);
|
|
224 |
| } |
|
225 |
| |
|
226 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
227 |
0
| String query = "delete from" + CasDBConstants.TABLE_USER + " where"
|
|
228 |
| + CasDBConstants.COL_USER_NICKNAME + " ='" + userNickname.trim() |
|
229 |
| + "'"; |
|
230 |
0
| Statement statement = null;
|
|
231 |
0
| int result;
|
|
232 |
0
| try {
|
|
233 |
0
| statement = connection.createStatement();
|
|
234 |
0
| result = statement.executeUpdate(query);
|
|
235 |
| } |
|
236 |
| catch (SQLException exp) { |
|
237 |
0
| logger.error(i18n.getMessage("delObjErr", new Object[] {
|
|
238 |
| "user data with nickname " + userNickname, query }), exp); |
|
239 |
0
| throw new CasDBException(i18n.getMessage("delObjErr",
|
|
240 |
| new Object[] { "user data with nickname " + userNickname, |
|
241 |
| exp.getMessage() }), exp); |
|
242 |
| } |
|
243 |
| finally { |
|
244 |
| |
|
245 |
| |
|
246 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
247 |
0
| try {
|
|
248 |
0
| if (statement != null)
|
|
249 |
0
| statement.close();
|
|
250 |
| } |
|
251 |
| catch (SQLException exp) { |
|
252 |
0
| logger.warn(i18n.getMessage("delObjErrClose", "user data"),
|
|
253 |
| exp); |
|
254 |
| } |
|
255 |
| } |
|
256 |
0
| if (result <=0) {
|
|
257 |
0
| String err = i18n.getMessage("userDelError", userNickname);
|
|
258 |
0
| logger.error(err);
|
|
259 |
0
| throw new CasDBException(err);
|
|
260 |
| } |
|
261 |
| } |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
0
| public static String getUserNickname(String subjectDN)
|
|
271 |
| throws CasDBException { |
|
272 |
| |
|
273 |
0
| String userNick = null;
|
|
274 |
0
| String query = "select" + CasDBConstants.COL_USER_NICKNAME + " from"
|
|
275 |
| + CasDBConstants.TABLE_USER + " where (" |
|
276 |
| + CasDBConstants.COL_SUBJECTNAME + "='" + subjectDN.trim() |
|
277 |
| + "')"; |
|
278 |
0
| logger.debug("query is " + query);
|
|
279 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
280 |
0
| Statement statement = null;
|
|
281 |
0
| ResultSet resultSet = null;
|
|
282 |
0
| try {
|
|
283 |
0
| statement = connection.createStatement();
|
|
284 |
0
| resultSet = statement.executeQuery(query);
|
|
285 |
0
| if (resultSet!=null) {
|
|
286 |
0
| if (resultSet.next()) {
|
|
287 |
0
| userNick =
|
|
288 |
| resultSet.getString( |
|
289 |
| CasDBConstants.COL_USER_NICKNAME.trim()); |
|
290 |
| } |
|
291 |
| } |
|
292 |
| } |
|
293 |
| catch (SQLException exp) { |
|
294 |
0
| logger.error(i18n.getMessage("retrieveErr", new Object[] {
|
|
295 |
| "user nickname for subject DN " + subjectDN, |
|
296 |
| query }), exp); |
|
297 |
0
| throw new CasDBException(i18n.getMessage("retrieveErr",
|
|
298 |
| new Object[] { "user nickname for subject DN " + subjectDN, |
|
299 |
| exp.getMessage() }), exp); |
|
300 |
| } |
|
301 |
| finally { |
|
302 |
| |
|
303 |
| |
|
304 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
305 |
0
| try {
|
|
306 |
0
| if (statement != null)
|
|
307 |
0
| statement.close();
|
|
308 |
| } |
|
309 |
| catch (SQLException exp) { |
|
310 |
0
| logger.warn(i18n.getMessage("retrieveErrClose",
|
|
311 |
| "user nickname"), exp); |
|
312 |
| } |
|
313 |
| } |
|
314 |
0
| logger.debug("userNick is " + userNick);
|
|
315 |
0
| return userNick;
|
|
316 |
| } |
|
317 |
| } |