|
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.CasObjectData; |
|
26 |
| import org.globus.cas.types.UserGroupData; |
|
27 |
| import org.globus.cas.types.ArrayOfString; |
|
28 |
| |
|
29 |
| import org.globus.util.I18n; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| public class UserGroupDataHandler { |
|
35 |
| |
|
36 |
| static Log logger = LogFactory.getLog(UserGroupData.class.getName() ); |
|
37 |
| |
|
38 |
| private static I18n i18n = |
|
39 |
| I18n.getI18n("org.globus.cas.impl.databaseAccess.errors", |
|
40 |
| UserGroupDataHandler.class.getClassLoader()); |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
0
| public static String storeObject(UserGroupData userGpData)
|
|
48 |
| throws CasDBException { |
|
49 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
50 |
0
| String groupName = userGpData.getGroupName();
|
|
51 |
| |
|
52 |
0
| String query = "insert into " + CasDBConstants.TABLE_USERGP + " ("
|
|
53 |
| + CasDBConstants.COL_USERGP_NAME + ") values ('" |
|
54 |
| + userGpData.getGroupName() + "')"; |
|
55 |
0
| Statement statement = null;
|
|
56 |
0
| try {
|
|
57 |
0
| statement = connection.createStatement();
|
|
58 |
0
| logger.debug("store query is " + query);
|
|
59 |
0
| statement.executeUpdate(query);
|
|
60 |
| } |
|
61 |
| catch (SQLException exp) { |
|
62 |
0
| logger.error(i18n.getMessage("storeErr", new Object[] {
|
|
63 |
| "user group", query }), exp); |
|
64 |
0
| throw new CasDBException(i18n.getMessage("storeErr", new Object[] {
|
|
65 |
| "user group", exp.getMessage() }), exp); |
|
66 |
| } |
|
67 |
| finally { |
|
68 |
| |
|
69 |
| |
|
70 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
71 |
0
| try {
|
|
72 |
0
| if (statement != null)
|
|
73 |
0
| statement.close();
|
|
74 |
| } |
|
75 |
| catch (SQLException exp) { |
|
76 |
0
| logger.warn(i18n.getMessage("storeErrClose", "user group"),
|
|
77 |
| exp); |
|
78 |
| } |
|
79 |
| } |
|
80 |
0
| return groupName;
|
|
81 |
| } |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
0
| public static String[] list() throws CasDBException {
|
|
87 |
0
| StringBuffer query = new StringBuffer("select ");
|
|
88 |
0
| query.append(CasDBConstants.COL_USERGP_NAME).append(" from")
|
|
89 |
| .append(CasDBConstants.TABLE_USERGP); |
|
90 |
0
| return CasDBStorage.runListQuery(query.toString(),
|
|
91 |
| CasDBConstants.COL_USERGP_NAME); |
|
92 |
| } |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
0
| public static CasObjectData retrieveObject(String groupName)
|
|
99 |
| throws CasDBException { |
|
100 |
| |
|
101 |
0
| UserGroupData returnObject = null;
|
|
102 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
103 |
0
| Vector temp = null;
|
|
104 |
0
| String query = "select" + CasDBConstants.COL_USER_NICKNAME + " from"
|
|
105 |
| + CasDBConstants.TABLE_USERGP_ENTRY + " where " |
|
106 |
| + CasDBConstants.COL_USERGP_NAME + " ='" + groupName.trim() + "'"; |
|
107 |
0
| Statement statement = null;
|
|
108 |
0
| ResultSet resultSet = null;
|
|
109 |
0
| try {
|
|
110 |
0
| statement = connection.createStatement();
|
|
111 |
0
| resultSet = statement.executeQuery(query);
|
|
112 |
0
| if ((resultSet!=null) && (resultSet.next())) {
|
|
113 |
0
| temp = new Vector(resultSet.getFetchSize());
|
|
114 |
0
| do {
|
|
115 |
0
| temp.add(resultSet.getString(
|
|
116 |
| CasDBConstants.COL_USER_NICKNAME.trim())); |
|
117 |
0
| } while (resultSet.next());
|
|
118 |
| } |
|
119 |
| } |
|
120 |
| catch (SQLException exp) { |
|
121 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
122 |
0
| logger.error(i18n.getMessage("retrieveErr", new Object[] {
|
|
123 |
| "user group", query }), exp); |
|
124 |
0
| throw new CasDBException(i18n.getMessage("retrieveErr",
|
|
125 |
| new Object[] { "user group", exp.getMessage() }), exp); |
|
126 |
| } |
|
127 |
| finally { |
|
128 |
0
| try {
|
|
129 |
0
| if (resultSet != null)
|
|
130 |
0
| resultSet.close();
|
|
131 |
0
| if (statement != null)
|
|
132 |
0
| statement.close();
|
|
133 |
| } |
|
134 |
| catch (SQLException exp) { |
|
135 |
0
| logger.warn(i18n.getMessage("retrieveErrClose", "user group"),
|
|
136 |
| exp); |
|
137 |
| } |
|
138 |
| } |
|
139 |
| |
|
140 |
0
| ArrayOfString arrayOfUserNames = null;
|
|
141 |
0
| if (temp != null) {
|
|
142 |
0
| String userNamesString[] = new String[temp.size()];
|
|
143 |
0
| temp.toArray(userNamesString);
|
|
144 |
0
| arrayOfUserNames = new ArrayOfString(userNamesString);
|
|
145 |
| } |
|
146 |
0
| query = "select * from" + CasDBConstants.TABLE_USERGP + " where"
|
|
147 |
| + CasDBConstants.COL_USERGP_NAME + " = '" + groupName.trim() + "'"; |
|
148 |
| |
|
149 |
0
| try {
|
|
150 |
0
| statement = connection.createStatement();
|
|
151 |
0
| resultSet = statement.executeQuery(query);
|
|
152 |
0
| if (resultSet!=null) {
|
|
153 |
0
| if (resultSet.next()) {
|
|
154 |
0
| returnObject = new UserGroupData();
|
|
155 |
0
| returnObject.setGroupName(resultSet.getString(
|
|
156 |
| CasDBConstants.COL_USERGP_NAME.trim())); |
|
157 |
0
| returnObject
|
|
158 |
| .setUserNames(arrayOfUserNames); |
|
159 |
| } |
|
160 |
| } |
|
161 |
| } |
|
162 |
| catch (SQLException exp) { |
|
163 |
0
| logger.error(i18n.getMessage("retrieveErr", new Object[] {
|
|
164 |
| "user group", query }), exp); |
|
165 |
0
| throw new CasDBException(i18n.getMessage("retrieveErr",
|
|
166 |
| new Object[] { "user group", exp.getMessage() }), exp); |
|
167 |
| } |
|
168 |
| finally { |
|
169 |
| |
|
170 |
| |
|
171 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
172 |
0
| try {
|
|
173 |
0
| if (resultSet != null)
|
|
174 |
0
| resultSet.close();
|
|
175 |
0
| if (statement != null)
|
|
176 |
0
| statement.close();
|
|
177 |
| } |
|
178 |
| catch (SQLException exp) { |
|
179 |
0
| logger.warn(i18n.getMessage("retrieveErrClose", "user group"),
|
|
180 |
| exp); |
|
181 |
| } |
|
182 |
| } |
|
183 |
0
| return returnObject;
|
|
184 |
| } |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
0
| public static void deleteObject(String groupName) throws CasDBException {
|
|
191 |
| |
|
192 |
0
| logger.debug("delete object called");
|
|
193 |
| |
|
194 |
| |
|
195 |
0
| if (ObjectGroupDataHandler.isMember(groupName.trim(),
|
|
196 |
| CasConstants.USERGP_SPEC)) { |
|
197 |
0
| String errMesg = i18n.getMessage("memberOfGpErr", new Object[] {
|
|
198 |
| "User group ", groupName.trim(), "object" }); |
|
199 |
0
| logger.error(errMesg);
|
|
200 |
0
| throw new CasDBException(errMesg);
|
|
201 |
| } |
|
202 |
| |
|
203 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
204 |
0
| String query = "delete from" + CasDBConstants.TABLE_USERGP + " where"
|
|
205 |
| + CasDBConstants.COL_USERGP_NAME + " ='" + groupName.trim() + "'"; |
|
206 |
0
| Statement statement = null;
|
|
207 |
0
| int result = -1;
|
|
208 |
0
| try {
|
|
209 |
0
| statement = connection.createStatement();
|
|
210 |
0
| logger.debug("delete object query is " + query);
|
|
211 |
0
| result = statement.executeUpdate(query);
|
|
212 |
| } |
|
213 |
| catch (SQLException exp) { |
|
214 |
0
| logger.error(i18n.getMessage("delObjErr", new Object[] {
|
|
215 |
| "user group", query}), exp); |
|
216 |
0
| throw new CasDBException(i18n.getMessage("delObjErr",
|
|
217 |
| new Object[] { "user group", exp.getMessage()}), exp); |
|
218 |
| } |
|
219 |
| finally { |
|
220 |
| |
|
221 |
| |
|
222 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
223 |
0
| try {
|
|
224 |
0
| if (statement != null)
|
|
225 |
0
| statement.close();
|
|
226 |
| } |
|
227 |
| catch (SQLException exp) { |
|
228 |
0
| logger.warn(i18n.getMessage("delObjErrClose", "user group"),
|
|
229 |
| exp); |
|
230 |
| } |
|
231 |
| } |
|
232 |
0
| if (result <= 0) {
|
|
233 |
0
| String err = i18n.getMessage("userGpDelError", groupName);
|
|
234 |
0
| logger.error(err);
|
|
235 |
0
| throw new CasDBException(err);
|
|
236 |
| } |
|
237 |
| } |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
0
| public static void addGroupMember(String groupName, String userName)
|
|
244 |
| throws CasDBException { |
|
245 |
| |
|
246 |
0
| if ((groupName == null) || (userName == null)) {
|
|
247 |
0
| String err = i18n.getMessage("paramNull", "Groupname or Username");
|
|
248 |
0
| logger.error(err);
|
|
249 |
0
| throw new CasDBException(err);
|
|
250 |
| } |
|
251 |
| |
|
252 |
0
| String query = "insert into" + CasDBConstants.TABLE_USERGP_ENTRY
|
|
253 |
| + " ( " + CasDBConstants.COL_USERGP_NAME + "," |
|
254 |
| + CasDBConstants.COL_USER_NICKNAME + ") values ( '" |
|
255 |
| + groupName.trim() + "','" + userName.trim() + "')"; |
|
256 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
257 |
0
| Statement statement = null;
|
|
258 |
0
| try {
|
|
259 |
0
| statement = connection.createStatement();
|
|
260 |
0
| logger.debug(" query is " + query);
|
|
261 |
0
| statement.executeUpdate(query);
|
|
262 |
| } |
|
263 |
| catch (SQLException exp) { |
|
264 |
0
| logger.error(i18n.getMessage("addGpErr", "user"), exp);
|
|
265 |
0
| throw new CasDBException(i18n.getMessage("addGpErr", "user") +
|
|
266 |
| exp.getMessage(), exp); |
|
267 |
| } |
|
268 |
| finally { |
|
269 |
| |
|
270 |
| |
|
271 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
272 |
0
| try {
|
|
273 |
0
| if (statement != null)
|
|
274 |
0
| statement.close();
|
|
275 |
| } |
|
276 |
| catch (SQLException exp) { |
|
277 |
0
| logger.warn(i18n.getMessage("addGpErrClose", "user"), exp);
|
|
278 |
| } |
|
279 |
| } |
|
280 |
| } |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
0
| public static void removeGroupMember(String groupName, String userName)
|
|
287 |
| throws CasDBException { |
|
288 |
| |
|
289 |
0
| if ((groupName == null) || (userName == null)) {
|
|
290 |
0
| String err = i18n.getMessage("paramNull", "Groupname or Username");
|
|
291 |
0
| logger.error(err);
|
|
292 |
0
| throw new CasDBException(err);
|
|
293 |
| } |
|
294 |
| |
|
295 |
0
| String query = "delete from" + CasDBConstants.TABLE_USERGP_ENTRY
|
|
296 |
| + " where (" + CasDBConstants.COL_USERGP_NAME + "='" |
|
297 |
| + groupName.trim() + "') and (" + CasDBConstants.COL_USER_NICKNAME |
|
298 |
| + " ='" + userName.trim() + "')"; |
|
299 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
300 |
0
| Statement statement = null;
|
|
301 |
0
| int result = -1;
|
|
302 |
0
| try {
|
|
303 |
0
| statement = connection.createStatement();
|
|
304 |
0
| logger.debug(" query is " + query);
|
|
305 |
0
| result = statement.executeUpdate(query);
|
|
306 |
| } |
|
307 |
| catch (SQLException exp) { |
|
308 |
0
| String err = i18n.getMessage("removeGpErr",
|
|
309 |
| new Object[] { userName , groupName}); |
|
310 |
0
| logger.error(err, exp);
|
|
311 |
0
| throw new CasDBException(err, exp);
|
|
312 |
| } |
|
313 |
| finally { |
|
314 |
| |
|
315 |
| |
|
316 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
317 |
0
| try {
|
|
318 |
0
| if (statement != null)
|
|
319 |
0
| statement.close();
|
|
320 |
| } |
|
321 |
| catch (SQLException exp) { |
|
322 |
0
| logger.warn(i18n.getMessage("removeGpErrClose", "user"), exp);
|
|
323 |
| } |
|
324 |
| } |
|
325 |
0
| if (result <= 0) {
|
|
326 |
0
| String err = i18n.getMessage("userMemDelError", new Object[]
|
|
327 |
| { userName , groupName}); |
|
328 |
0
| logger.error(err);
|
|
329 |
0
| throw new CasDBException(err);
|
|
330 |
| } |
|
331 |
| } |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
0
| public static boolean groupExists(String groupName) throws CasDBException {
|
|
337 |
| |
|
338 |
0
| if (groupName == null) {
|
|
339 |
0
| logger.error(i18n.getMessage("paramNull", "Group name"));
|
|
340 |
0
| throw new CasDBException(i18n.getMessage("paramNull",
|
|
341 |
| "Group name")); |
|
342 |
| } |
|
343 |
0
| String query = "select * from" + CasDBConstants.TABLE_USERGP
|
|
344 |
| + " where" + CasDBConstants.COL_USERGP_NAME + "='" |
|
345 |
| + groupName.trim() + "'"; |
|
346 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
347 |
0
| Statement statement = null;
|
|
348 |
0
| ResultSet resultSet = null;
|
|
349 |
0
| try {
|
|
350 |
0
| statement = connection.createStatement();
|
|
351 |
0
| resultSet = statement.executeQuery(query);
|
|
352 |
0
| if ((resultSet!=null) && (resultSet.next())) {
|
|
353 |
0
| logger.debug("There is an entry for this user group ");
|
|
354 |
0
| return true;
|
|
355 |
| } |
|
356 |
| } |
|
357 |
| catch (SQLException exp) { |
|
358 |
0
| logger.error(i18n.getMessage("verifyGpPresence", new Object[] {
|
|
359 |
| groupName, query }), exp); |
|
360 |
0
| throw new CasDBException(i18n.getMessage("verifyGpPresence",
|
|
361 |
| new Object[] { groupName, query }), exp); |
|
362 |
| } |
|
363 |
| finally { |
|
364 |
| |
|
365 |
| |
|
366 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
367 |
0
| try {
|
|
368 |
0
| if (resultSet != null)
|
|
369 |
0
| resultSet.close();
|
|
370 |
0
| if (statement != null)
|
|
371 |
0
| statement.close();
|
|
372 |
| } |
|
373 |
| catch (SQLException exp) { |
|
374 |
0
| logger.warn(i18n.getMessage("verifyGpPresenceClose",
|
|
375 |
| groupName), exp); |
|
376 |
| } |
|
377 |
| } |
|
378 |
0
| return false;
|
|
379 |
| } |
|
380 |
| } |