001: /* Copyright 2001, 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.security.provider;
007:
008: import java.sql.Connection;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012: import java.sql.Statement;
013: import java.sql.Timestamp;
014: import java.sql.Types;
015: import java.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.Date;
018: import java.util.List;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.jasig.portal.AuthorizationException;
023: import org.jasig.portal.RDBMServices;
024: import org.jasig.portal.security.IPermission;
025: import org.jasig.portal.security.IPermissionStore;
026:
027: /**
028: * Reference implementation of IPermissionStore. Performs CRUD operations
029: * on the UP_Permission table.
030: * @author Dan Ellentuck (de3@columbia.edu)
031: * @version $Revision: 42156 $
032: */
033: public class RDBMPermissionImpl implements IPermissionStore {
034:
035: private static final Log log = LogFactory
036: .getLog(RDBMPermissionImpl.class);
037:
038: private static RDBMPermissionImpl singleton;
039:
040: // Prior to jdk 1.4, java.sql.Timestamp.getTime() truncated milliseconds.
041: private static boolean timestampHasMillis;
042:
043: // sql Strings:
044: private static String PERMISSION_TABLE = "UP_PERMISSION";
045: private static String OWNER_COLUMN = "OWNER";
046: private static String PRINCIPAL_TYPE_COLUMN = "PRINCIPAL_TYPE";
047: private static String PRINCIPAL_KEY_COLUMN = "PRINCIPAL_KEY";
048: private static String ACTIVITY_COLUMN = "ACTIVITY";
049: private static String TARGET_COLUMN = "TARGET";
050: private static String TYPE_COLUMN = "PERMISSION_TYPE";
051: private static String EFFECTIVE_COLUMN = "EFFECTIVE";
052: private static String EXPIRES_COLUMN = "EXPIRES";
053: private static String allPermissionColumnsSql;
054: private static String deletePermissionSql;
055: private static String findPermissionSql;
056: private static String insertPermissionSql;
057: private static String selectPermissionSql;
058: private static String updatePermissionSql;
059:
060: private static String PRINCIPAL_SEPARATOR = ".";
061:
062: /**
063: * RDBMReferencePermission constructor comment.
064: */
065: public RDBMPermissionImpl() {
066: super ();
067: Date testDate = new Date();
068: Timestamp testTimestamp = new Timestamp(testDate.getTime());
069: timestampHasMillis = (testDate.getTime() == testTimestamp
070: .getTime());
071: }
072:
073: /**
074: * Add the IPermissions to the store.
075: * @param perms org.jasig.portal.security.IPermission[]
076: * @exception org.jasig.portal.AuthorizationException - wraps an Exception specific to the store.
077: */
078: public void add(IPermission[] perms) throws AuthorizationException {
079: if (perms.length > 0) {
080: try {
081: primAdd(perms);
082: } catch (Exception ex) {
083: log.error("Exception adding permissions " + perms, ex);
084: throw new AuthorizationException(ex);
085: }
086: }
087: }
088:
089: /**
090: * Add the IPermission to the store.
091: * @param perm org.jasig.portal.security.IPermission
092: * @exception org.jasig.portal.AuthorizationException - wraps an Exception specific to the store.
093: */
094: public void add(IPermission perm) throws AuthorizationException
095:
096: {
097: Connection conn = null;
098: int rc = 0;
099:
100: try {
101: conn = RDBMServices.getConnection();
102: String sQuery = getInsertPermissionSql();
103: PreparedStatement ps = conn.prepareStatement(sQuery);
104: try {
105: primAdd(perm, ps);
106: if (log.isDebugEnabled())
107: log.debug("RDBMPermissionImpl.add(): " + ps);
108: rc = ps.executeUpdate();
109: if (rc != 1) {
110: throw new AuthorizationException(
111: "Problem adding Permission " + perm);
112: }
113: } finally {
114: ps.close();
115: }
116: } catch (Exception ex) {
117: log.error("Exception adding permission [" + perm + "]", ex);
118: throw new AuthorizationException(
119: "Problem adding Permission " + perm);
120: } finally {
121: RDBMServices.releaseConnection(conn);
122: }
123: }
124:
125: /**
126: * Delete the IPermissions from the store.
127: * @param perms org.jasig.portal.security.IPermission[]
128: * @exception org.jasig.portal.AuthorizationException - wraps an Exception specific to the store.
129: */
130: public void delete(IPermission[] perms)
131: throws AuthorizationException {
132: if (perms.length > 0) {
133: try {
134: primDelete(perms);
135: } catch (Exception ex) {
136: log.error("Exception deleting permissions "
137: + Arrays.toString(perms), ex);
138: throw new AuthorizationException(
139: "Exception deleting permissions "
140: + Arrays.toString(perms), ex);
141: }
142: }
143: }
144:
145: /**
146: * Delete a single IPermission from the store.
147: * @param perm org.jasig.portal.security.IPermission
148: * @exception org.jasig.portal.AuthorizationException - wraps an Exception specific to the store.
149: */
150: public void delete(IPermission perm) throws AuthorizationException
151:
152: {
153: Connection conn = null;
154: try {
155: conn = RDBMServices.getConnection();
156: String sQuery = getDeletePermissionSql();
157: PreparedStatement ps = conn.prepareStatement(sQuery);
158: try {
159: primDelete(perm, ps);
160: } finally {
161: ps.close();
162: }
163: } catch (Exception ex) {
164: log.error("Exception deleting permission [" + perm + "]",
165: ex);
166: throw new AuthorizationException(
167: "Problem deleting Permission " + perm, ex);
168: } finally {
169: RDBMServices.releaseConnection(conn);
170: }
171: }
172:
173: /**
174: * Answer if this entity exists in the database.
175: * @return boolean
176: * @param perm org.jasig.portal.security.IPermission
177: * @exception java.sql.SQLException
178: */
179: public boolean existsInDatabase(IPermission perm)
180: throws AuthorizationException, SQLException {
181: Connection conn = null;
182: try {
183: conn = RDBMServices.getConnection();
184: String sQuery = getFindPermissionSql();
185: PreparedStatement ps = conn.prepareStatement(sQuery);
186: try {
187: ps.setString(1, perm.getOwner());
188: ps.setInt(2, getPrincipalType(perm));
189: ps.setString(3, getPrincipalKey(perm));
190: ps.setString(4, perm.getActivity());
191: ps.setString(5, perm.getTarget());
192: if (log.isDebugEnabled())
193: log.debug("RDBMPermissionImpl.existsInDatabase(): "
194: + ps);
195: ResultSet rs = ps.executeQuery();
196: try {
197: return (rs.next());
198: } finally {
199: rs.close();
200: }
201: } finally {
202: ps.close();
203: }
204: } catch (Exception ex) {
205: log.error("Exception determining whether " + "permission ["
206: + perm + "] exists in database.", ex);
207: throw new AuthorizationException(
208: "RDBMPermissionImpl.existsInDatabase(): " + ex);
209: } finally {
210: RDBMServices.releaseConnection(conn);
211: }
212: }
213:
214: /**
215: * @return java.lang.String
216: */
217: private static String getAllPermissionColumnsSql() {
218: if (allPermissionColumnsSql == null) {
219: StringBuffer sqlBuff = new StringBuffer(200);
220: sqlBuff.append(OWNER_COLUMN);
221: sqlBuff.append(", ");
222: sqlBuff.append(PRINCIPAL_TYPE_COLUMN);
223: sqlBuff.append(", ");
224: sqlBuff.append(PRINCIPAL_KEY_COLUMN);
225: sqlBuff.append(", ");
226: sqlBuff.append(ACTIVITY_COLUMN);
227: sqlBuff.append(", ");
228: sqlBuff.append(TARGET_COLUMN);
229: sqlBuff.append(", ");
230: sqlBuff.append(TYPE_COLUMN);
231: sqlBuff.append(", ");
232: sqlBuff.append(EFFECTIVE_COLUMN);
233: sqlBuff.append(", ");
234: sqlBuff.append(EXPIRES_COLUMN);
235: allPermissionColumnsSql = sqlBuff.toString();
236: }
237: return allPermissionColumnsSql;
238: }
239:
240: /**
241: * @return java.lang.String
242: */
243: private static String getDeletePermissionSql() {
244: if (deletePermissionSql == null) {
245: StringBuffer sqlBuff = new StringBuffer(200);
246: sqlBuff.append("DELETE FROM ");
247: sqlBuff.append(PERMISSION_TABLE);
248: sqlBuff.append(" WHERE ");
249: sqlBuff.append(OWNER_COLUMN);
250: sqlBuff.append(" = ? AND ");
251: sqlBuff.append(PRINCIPAL_TYPE_COLUMN);
252: sqlBuff.append(" = ? AND ");
253: sqlBuff.append(PRINCIPAL_KEY_COLUMN);
254: sqlBuff.append(" = ? AND ");
255: sqlBuff.append(ACTIVITY_COLUMN);
256: sqlBuff.append(" = ? AND ");
257: sqlBuff.append(TARGET_COLUMN);
258: sqlBuff.append(" = ? ");
259: deletePermissionSql = sqlBuff.toString();
260: }
261: return deletePermissionSql;
262: }
263:
264: /**
265: * Insert the method's description here.
266: * Creation date: (11/6/01 5:19:57 PM)
267: * @return java.lang.String
268: */
269: private static java.lang.String getFindPermissionSql() {
270: if (findPermissionSql == null) {
271: StringBuffer sqlBuff = new StringBuffer(
272: getSelectPermissionSql());
273: sqlBuff.append("WHERE ");
274: sqlBuff.append(OWNER_COLUMN);
275: sqlBuff.append(" = ? AND ");
276: sqlBuff.append(PRINCIPAL_TYPE_COLUMN);
277: sqlBuff.append(" = ? AND ");
278: sqlBuff.append(PRINCIPAL_KEY_COLUMN);
279: sqlBuff.append(" = ? AND ");
280: sqlBuff.append(ACTIVITY_COLUMN);
281: sqlBuff.append(" = ? AND ");
282: sqlBuff.append(TARGET_COLUMN);
283: sqlBuff.append(" = ? ");
284: sqlBuff.append(TYPE_COLUMN);
285: sqlBuff.append(" = ? ");
286: findPermissionSql = sqlBuff.toString();
287: }
288: return findPermissionSql;
289: }
290:
291: /**
292: * @return java.lang.String
293: */
294: private static String getInsertPermissionSql() {
295: if (insertPermissionSql == null) {
296: StringBuffer sqlBuff = new StringBuffer(200);
297: sqlBuff.append("INSERT INTO ");
298: sqlBuff.append(PERMISSION_TABLE);
299: sqlBuff.append(" (");
300: sqlBuff.append(getAllPermissionColumnsSql());
301: sqlBuff.append(") VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
302: insertPermissionSql = sqlBuff.toString();
303: }
304: return insertPermissionSql;
305: }
306:
307: /**
308: * Returns the principal key portion of the IPermission principal.
309: * @return String
310: * @param principalString
311: */
312: private String getPrincipalKey(String principalString) {
313: return principalString.substring(principalString
314: .indexOf(PRINCIPAL_SEPARATOR) + 1);
315: }
316:
317: /**
318: * Returns the principal key portion of the IPermission principal.
319: * @return String
320: * @param perm org.jasig.portal.security.IPermission
321: * @exception AuthorizationException
322: */
323: private String getPrincipalKey(IPermission perm)
324: throws AuthorizationException {
325: return getPrincipalKey(perm.getPrincipal());
326: }
327:
328: /**
329: * Returns the principal type portion of the principal.
330: * @return int
331: * @param principalString
332: */
333: private int getPrincipalType(String principalString) {
334: return Integer.parseInt(principalString.substring(0,
335: principalString.indexOf(PRINCIPAL_SEPARATOR)));
336: }
337:
338: /**
339: * Returns the principal type portion of the IPermission principal.
340: * @return int
341: * @param perm org.jasig.portal.security.IPermission
342: * @exception AuthorizationException
343: */
344: private int getPrincipalType(IPermission perm)
345: throws AuthorizationException {
346: return getPrincipalType(perm.getPrincipal());
347: }
348:
349: /**
350: * @return java.lang.String
351: */
352: private static String getSelectPermissionSql() {
353: if (selectPermissionSql == null) {
354: StringBuffer sqlBuff = new StringBuffer(200);
355: sqlBuff.append("SELECT ");
356: sqlBuff.append(getAllPermissionColumnsSql());
357: sqlBuff.append(" FROM ");
358: sqlBuff.append(PERMISSION_TABLE);
359: sqlBuff.append(" ");
360: selectPermissionSql = sqlBuff.toString();
361: }
362: return selectPermissionSql;
363: }
364:
365: /**
366: * @return java.lang.String
367: */
368: private static String getUpdatePermissionSql() {
369: if (updatePermissionSql == null) {
370: StringBuffer sqlBuff = new StringBuffer(300);
371: sqlBuff.append("UPDATE ");
372: sqlBuff.append(PERMISSION_TABLE);
373: sqlBuff.append(" SET ");
374: sqlBuff.append(TYPE_COLUMN);
375: sqlBuff.append(" = ?, ");
376: sqlBuff.append(EFFECTIVE_COLUMN);
377: sqlBuff.append(" = ?, ");
378: sqlBuff.append(EXPIRES_COLUMN);
379: sqlBuff.append(" = ? WHERE ");
380: sqlBuff.append(OWNER_COLUMN);
381: sqlBuff.append(" = ? AND ");
382: sqlBuff.append(PRINCIPAL_TYPE_COLUMN);
383: sqlBuff.append(" = ? AND ");
384: sqlBuff.append(PRINCIPAL_KEY_COLUMN);
385: sqlBuff.append(" = ? AND ");
386: sqlBuff.append(ACTIVITY_COLUMN);
387: sqlBuff.append(" = ? AND ");
388: sqlBuff.append(TARGET_COLUMN);
389: sqlBuff.append(" = ? ");
390: updatePermissionSql = sqlBuff.toString();
391: }
392: return updatePermissionSql;
393: }
394:
395: /**
396: * @return org.jasig.portal.security.IPermission
397: * @param rs java.sql.ResultSet
398: */
399: private IPermission instanceFromResultSet(ResultSet rs)
400: throws SQLException {
401: Timestamp ts = null;
402:
403: IPermission perm = newInstance(rs.getString(OWNER_COLUMN));
404: perm.setPrincipal(rs.getString(PRINCIPAL_TYPE_COLUMN) + "."
405: + rs.getString(PRINCIPAL_KEY_COLUMN));
406: perm.setActivity(rs.getString(ACTIVITY_COLUMN));
407: perm.setTarget(rs.getString(TARGET_COLUMN));
408: perm.setType(rs.getString(TYPE_COLUMN));
409:
410: ts = rs.getTimestamp(EFFECTIVE_COLUMN);
411: if (ts != null) {
412: perm.setEffective(new Date(getTimestampMillis(ts)));
413: }
414:
415: ts = rs.getTimestamp(EXPIRES_COLUMN);
416: if (ts != null) {
417: perm.setExpires(new Date(getTimestampMillis(ts)));
418: }
419:
420: return perm;
421: }
422:
423: /**
424: * Factory method for IPermissions
425: */
426: public IPermission newInstance(String owner) {
427: return new PermissionImpl(owner);
428: }
429:
430: /**
431: * Add the IPermissions to the store.
432: * @param perms org.jasig.portal.security.IPermission[]
433: * @exception Exception
434: */
435: private void primAdd(IPermission[] perms) throws Exception {
436: Connection conn = null;
437: int rc = 0;
438:
439: try {
440: conn = RDBMServices.getConnection();
441: String sQuery = getInsertPermissionSql();
442: PreparedStatement ps = conn.prepareStatement(sQuery);
443: try {
444: RDBMServices.setAutoCommit(conn, false);
445:
446: for (int i = 0; i < perms.length; i++) {
447: primAdd(perms[i], ps);
448: if (log.isDebugEnabled())
449: log
450: .debug("RDBMPermissionImpl.primAdd(): "
451: + ps);
452: rc = ps.executeUpdate();
453:
454: if (rc != 1) {
455: String errMsg = "Problem adding " + perms[i]
456: + " RC: " + rc;
457: log.error(errMsg);
458: RDBMServices.rollback(conn);
459: throw new AuthorizationException(errMsg);
460: }
461: }
462: } finally {
463: ps.close();
464: }
465:
466: RDBMServices.commit(conn);
467:
468: } catch (Exception ex) {
469: log.error("Exception adding permissions " + perms, ex);
470: RDBMServices.rollback(conn);
471: throw ex;
472: } finally {
473: try {
474: RDBMServices.setAutoCommit(conn, true);
475: } finally {
476: RDBMServices.releaseConnection(conn);
477: }
478: }
479: }
480:
481: /**
482: * Set the params on the PreparedStatement and execute the insert.
483: * @param perm org.jasig.portal.security.IPermission
484: * @param ps java.sql.PreparedStatement - the PreparedStatement for inserting a Permission row.
485: * @exception Exception
486: */
487: private void primAdd(IPermission perm, PreparedStatement ps)
488: throws Exception {
489: java.sql.Timestamp ts = null;
490:
491: // NON-NULL COLUMNS:
492: ps.clearParameters();
493: ps.setString(1, perm.getOwner());
494: ps.setInt(2, getPrincipalType(perm));
495: ps.setString(3, getPrincipalKey(perm));
496: ps.setString(4, perm.getActivity());
497: ps.setString(5, perm.getTarget());
498: // TYPE:
499: if (perm.getType() == null) {
500: ps.setNull(6, Types.VARCHAR);
501: } else {
502: ps.setString(6, perm.getType());
503: }
504: // EFFECTIVE:
505: if (perm.getEffective() == null) {
506: ps.setNull(7, Types.TIMESTAMP);
507: } else {
508: ts = new java.sql.Timestamp(perm.getEffective().getTime());
509: ps.setTimestamp(7, ts);
510: }
511: // EXPIRES:
512: if (perm.getExpires() == null) {
513: ps.setNull(8, Types.TIMESTAMP);
514: } else {
515: ts = new java.sql.Timestamp(perm.getExpires().getTime());
516: ps.setTimestamp(8, ts);
517: }
518: }
519:
520: /**
521: * Delete the IPermissions from the store.
522: * @param perms org.jasig.portal.security.IPermission[]
523: * @exception Exception
524: */
525: private void primDelete(IPermission[] perms) throws Exception {
526: Connection conn = null;
527:
528: try {
529: conn = RDBMServices.getConnection();
530: String sQuery = getDeletePermissionSql();
531: PreparedStatement ps = conn.prepareStatement(sQuery);
532: try {
533: RDBMServices.setAutoCommit(conn, false);
534:
535: for (int i = 0; i < perms.length; i++) {
536: primDelete(perms[i], ps);
537: }
538: } finally {
539: ps.close();
540: }
541:
542: RDBMServices.commit(conn);
543:
544: } catch (Exception ex) {
545: log.error("Exception deleting permissions [" + perms + "]",
546: ex);
547: RDBMServices.rollback(conn);
548: throw ex;
549: } finally {
550: try {
551: RDBMServices.setAutoCommit(conn, true);
552: } finally {
553: RDBMServices.releaseConnection(conn);
554: }
555: }
556: }
557:
558: /**
559: * Set the params on the PreparedStatement and execute the delete.
560: * @param perm org.jasig.portal.security.IPermission
561: * @param ps java.sql.PreparedStatement - the PreparedStatement for deleting a Permission row.
562: * @return int - the return code from the PreparedStatement
563: * @exception Exception
564: */
565: private int primDelete(IPermission perm, PreparedStatement ps)
566: throws Exception {
567: ps.clearParameters();
568: ps.setString(1, perm.getOwner());
569: ps.setInt(2, getPrincipalType(perm));
570: ps.setString(3, getPrincipalKey(perm));
571: ps.setString(4, perm.getActivity());
572: ps.setString(5, perm.getTarget());
573: if (log.isDebugEnabled())
574: log.debug("RDBMPermissionImpl.primDelete(): " + ps);
575:
576: return ps.executeUpdate();
577: }
578:
579: /**
580: * Update the IPermissions in the store.
581: * @param perms org.jasig.portal.security.IPermission[]
582: * @exception Exception
583: */
584: private void primUpdate(IPermission[] perms) throws Exception {
585: Connection conn = null;
586:
587: try {
588: conn = RDBMServices.getConnection();
589: String sQuery = getUpdatePermissionSql();
590: PreparedStatement ps = conn.prepareStatement(sQuery);
591: try {
592: RDBMServices.setAutoCommit(conn, false);
593:
594: for (int i = 0; i < perms.length; i++) {
595: primUpdate(perms[i], ps);
596: }
597: } finally {
598: ps.close();
599: }
600:
601: RDBMServices.commit(conn);
602:
603: } catch (Exception ex) {
604: log.error("Exception updating permissions " + perms, ex);
605: RDBMServices.rollback(conn);
606: throw ex;
607: } finally {
608: try {
609: RDBMServices.setAutoCommit(conn, true);
610: } finally {
611: RDBMServices.releaseConnection(conn);
612: }
613: }
614: }
615:
616: /**
617: * Set the params on the PreparedStatement and execute the update.
618: * @param perm org.jasig.portal.security.IPermission
619: * @param ps java.sql.PreparedStatement - the PreparedStatement for updating a Permission row.
620: * @return int - the return code from the PreparedStatement
621: * @exception Exception
622: */
623: private int primUpdate(IPermission perm, PreparedStatement ps)
624: throws Exception {
625: java.sql.Timestamp ts = null;
626:
627: // UPDATE COLUMNS:
628:
629: ps.clearParameters();
630: // TYPE:
631: if (perm.getType() == null) {
632: ps.setNull(1, Types.VARCHAR);
633: } else {
634: ps.setString(1, perm.getType());
635: }
636: // EFFECTIVE:
637: if (perm.getEffective() == null) {
638: ps.setNull(2, Types.TIMESTAMP);
639: } else {
640: ts = new java.sql.Timestamp(perm.getEffective().getTime());
641: ps.setTimestamp(2, ts);
642: }
643: // EXPIRES:
644: if (perm.getExpires() == null) {
645: ps.setNull(3, Types.TIMESTAMP);
646: } else {
647: ts = new java.sql.Timestamp(perm.getExpires().getTime());
648: ps.setTimestamp(3, ts);
649: }
650: // WHERE COLUMNS:
651: ps.setString(4, perm.getOwner());
652: ps.setInt(5, getPrincipalType(perm));
653: ps.setString(6, getPrincipalKey(perm));
654: ps.setString(7, perm.getActivity());
655: ps.setString(8, perm.getTarget());
656: if (log.isDebugEnabled())
657: log.debug("RDBMPermissionImpl.primUpdate(): " + ps);
658:
659: return ps.executeUpdate();
660: }
661:
662: private void prepareSelectQuery(PreparedStatement stmt,
663: String owner, String principal, String activity,
664: String target, String type) throws SQLException {
665: int i = 1;
666:
667: if (owner != null) {
668: stmt.setString(i++, owner);
669: }
670:
671: if (principal != null) {
672: stmt.setInt(i++, getPrincipalType(principal));
673: stmt.setString(i++, getPrincipalKey(principal));
674: }
675:
676: if (activity != null) {
677: stmt.setString(i++, activity);
678: }
679:
680: if (target != null) {
681: stmt.setString(i++, target);
682: }
683:
684: if (type != null) {
685: stmt.setString(i++, type);
686: }
687: }
688:
689: private String getSelectQuery(String owner, String principal,
690: String activity, String target, String type) {
691: StringBuffer sqlQuery = new StringBuffer(
692: getSelectPermissionSql());
693: sqlQuery.append(" WHERE ");
694:
695: if (owner != null) {
696: sqlQuery.append(OWNER_COLUMN);
697: sqlQuery.append(" = ? ");
698: } else {
699: sqlQuery.append("1 = 1 ");
700: }
701:
702: if (principal != null) {
703: sqlQuery.append("AND ");
704: sqlQuery.append(PRINCIPAL_TYPE_COLUMN);
705: sqlQuery.append(" = ? AND ");
706: sqlQuery.append(PRINCIPAL_KEY_COLUMN);
707: sqlQuery.append(" = ? ");
708: }
709:
710: if (activity != null) {
711: sqlQuery.append("AND ");
712: sqlQuery.append(ACTIVITY_COLUMN);
713: sqlQuery.append(" = ? ");
714: }
715:
716: if (target != null) {
717: sqlQuery.append("AND ");
718: sqlQuery.append(TARGET_COLUMN);
719: sqlQuery.append(" = ? ");
720: }
721:
722: if (type != null) {
723: sqlQuery.append("AND ");
724: sqlQuery.append(TYPE_COLUMN);
725: sqlQuery.append(" = ? ");
726: }
727:
728: if (log.isTraceEnabled()) {
729: log.trace("Computed SQL query [" + sqlQuery
730: + "] for owner=[" + owner + "] and principal=["
731: + principal + "] and activity=[" + activity
732: + "] and target=[" + target + "] and type=[" + type
733: + "]");
734: }
735:
736: return sqlQuery.toString();
737: }
738:
739: /**
740: * Select the Permissions from the store.
741: * @param owner String - the Permission owner
742: * @param principal String - the Permission principal
743: * @param activity String - the Permission activity
744: * @exception org.jasig.portal.AuthorizationException - wraps an Exception specific to the store.
745: */
746: public IPermission[] select(String owner, String principal,
747: String activity, String target, String type)
748: throws AuthorizationException {
749: Connection conn = null;
750: PreparedStatement stmt = null;
751: ResultSet rs = null;
752: List<IPermission> perms = new ArrayList<IPermission>();
753:
754: String query = getSelectQuery(owner, principal, activity,
755: target, type);
756:
757: try {
758: conn = RDBMServices.getConnection();
759: stmt = conn.prepareStatement(query);
760: prepareSelectQuery(stmt, owner, principal, activity,
761: target, type);
762: try {
763: rs = stmt.executeQuery();
764: try {
765: while (rs.next()) {
766: perms.add(instanceFromResultSet(rs));
767: }
768: } finally {
769: rs.close();
770: }
771: } finally {
772: stmt.close();
773: }
774: } catch (SQLException sqle) {
775: log.error("Problem retrieving permissions", sqle);
776: throw new AuthorizationException(
777: "Problem retrieving Permissions ["
778: + sqle.getMessage() + "] for query=["
779: + query + "] for owner=[" + owner
780: + "] and principal=[" + principal
781: + "] and activity=[" + activity
782: + "] and target=[" + target
783: + "] and type=[" + type + "]", sqle);
784: } finally {
785: RDBMServices.releaseConnection(conn);
786: }
787:
788: if (log.isTraceEnabled()) {
789: log.trace("RDBMPermissionImpl.select(): [" + query
790: + "] for owner=[" + owner + "] and principal=["
791: + principal + "] and activity=[" + activity
792: + "] and target=[" + target + "] and type=[" + type
793: + "] returned permissions [" + perms + "]");
794: }
795:
796: return ((IPermission[]) perms.toArray(new IPermission[perms
797: .size()]));
798:
799: }
800:
801: /**
802: * @return org.jasig.portal.security.provider.RDBMPermissionImpl
803: */
804: public static synchronized RDBMPermissionImpl singleton() {
805: if (singleton == null) {
806: singleton = new RDBMPermissionImpl();
807: }
808: return singleton;
809: }
810:
811: /**
812: * Update the IPermissions in the store.
813: * @param perms org.jasig.portal.security.IPermission[]
814: * @exception org.jasig.portal.AuthorizationException - wraps an Exception specific to the store.
815: */
816: public void update(IPermission[] perms)
817: throws AuthorizationException {
818: if (perms.length > 0) {
819: try {
820: primUpdate(perms);
821: } catch (Exception ex) {
822: log
823: .error("Exception updating permissions "
824: + perms, ex);
825: throw new AuthorizationException(ex);
826: }
827: }
828: }
829:
830: /**
831: * Update a single IPermission in the store.
832: * @param perm org.jasig.portal.security.IPermission
833: * @exception org.jasig.portal.AuthorizationException - wraps an Exception specific to the store.
834: */
835: public void update(IPermission perm) throws AuthorizationException
836:
837: {
838: Connection conn = null;
839: try {
840: conn = RDBMServices.getConnection();
841: String sQuery = getUpdatePermissionSql();
842: if (log.isDebugEnabled())
843: log.debug("RDBMPermissionImpl.update(): " + sQuery);
844: PreparedStatement ps = conn.prepareStatement(sQuery);
845: try {
846: primUpdate(perm, ps);
847: } finally {
848: ps.close();
849: }
850: } catch (Exception ex) {
851: log.error("Exception updating permission [" + perm + "]",
852: ex);
853: throw new AuthorizationException(
854: "Problem updating Permission " + perm);
855: } finally {
856: RDBMServices.releaseConnection(conn);
857: }
858: }
859:
860: /**
861: * @return long
862: */
863: private static long getTimestampMillis(Timestamp ts) {
864: if (timestampHasMillis) {
865: return ts.getTime();
866: } else {
867: return (ts.getTime() + ts.getNanos() / 1000000);
868: }
869: }
870: }
|