001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.acegisecurity.acls.jdbc;
016:
017: import org.acegisecurity.acls.Acl;
018: import org.acegisecurity.acls.AclService;
019: import org.acegisecurity.acls.NotFoundException;
020: import org.acegisecurity.acls.objectidentity.ObjectIdentity;
021: import org.acegisecurity.acls.objectidentity.ObjectIdentityImpl;
022: import org.acegisecurity.acls.sid.Sid;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import org.springframework.jdbc.core.JdbcTemplate;
028: import org.springframework.jdbc.core.RowMapper;
029:
030: import org.springframework.util.Assert;
031:
032: import java.sql.ResultSet;
033: import java.sql.SQLException;
034:
035: import java.util.List;
036: import java.util.Map;
037:
038: import javax.sql.DataSource;
039:
040: /**
041: * Simple JDBC-based implementation of <code>AclService</code>.<p>Requires the "dirty" flags in {@link
042: * org.acegisecurity.acls.domain.AclImpl} and {@link org.acegisecurity.acls.domain.AccessControlEntryImpl} to be set,
043: * so that the implementation can detect changed parameters easily.</p>
044: *
045: * @author Ben Alex
046: * @version $Id: JdbcAclService.java 1754 2006-11-17 02:01:21Z benalex $
047: */
048: public class JdbcAclService implements AclService {
049: //~ Static fields/initializers =====================================================================================
050:
051: protected static final Log log = LogFactory
052: .getLog(JdbcAclService.class);
053: private static final String selectAclObjectWithParent = "SELECT obj.object_id_identity obj_id, class.class class "
054: + "FROM acl_object_identity obj, acl_object_identity parent, acl_class class "
055: + "WHERE obj.parent_object = parent.id AND obj.object_id_class = class.id "
056: + "AND parent.object_id_identity = ? AND parent.object_id_class = ("
057: + "SELECT id FROM acl_class WHERE acl_class.class = ?)";
058:
059: //~ Instance fields ================================================================================================
060:
061: protected JdbcTemplate jdbcTemplate;
062: private LookupStrategy lookupStrategy;
063:
064: //~ Constructors ===================================================================================================
065:
066: public JdbcAclService(DataSource dataSource,
067: LookupStrategy lookupStrategy) {
068: Assert.notNull(dataSource, "DataSource required");
069: Assert.notNull(lookupStrategy, "LookupStrategy required");
070: this .jdbcTemplate = new JdbcTemplate(dataSource);
071: this .lookupStrategy = lookupStrategy;
072: }
073:
074: //~ Methods ========================================================================================================
075:
076: public ObjectIdentity[] findChildren(ObjectIdentity parentIdentity) {
077: Object[] args = { parentIdentity.getIdentifier(),
078: parentIdentity.getJavaType().getName() };
079: List objects = jdbcTemplate.query(selectAclObjectWithParent,
080: args, new RowMapper() {
081: public Object mapRow(ResultSet rs, int rowNum)
082: throws SQLException {
083: String javaType = rs.getString("class");
084: String identifier = rs.getString("obj_id");
085:
086: return new ObjectIdentityImpl(javaType,
087: identifier);
088: }
089: });
090:
091: return (ObjectIdentityImpl[]) objects
092: .toArray(new ObjectIdentityImpl[] {});
093: }
094:
095: public Acl readAclById(ObjectIdentity object, Sid[] sids)
096: throws NotFoundException {
097: Map map = readAclsById(new ObjectIdentity[] { object }, sids);
098:
099: if (map.size() == 0) {
100: throw new NotFoundException("Could not find ACL");
101: } else {
102: return (Acl) map.get(object);
103: }
104: }
105:
106: public Acl readAclById(ObjectIdentity object)
107: throws NotFoundException {
108: return readAclById(object, null);
109: }
110:
111: public Map readAclsById(ObjectIdentity[] objects) {
112: return readAclsById(objects, null);
113: }
114:
115: public Map readAclsById(ObjectIdentity[] objects, Sid[] sids)
116: throws NotFoundException {
117: return lookupStrategy.readAclsById(objects, sids);
118: }
119: }
|