001: package org.tigris.scarab.services.security;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: import java.util.ArrayList;
050: import java.util.List;
051:
052: import org.apache.fulcrum.security.impl.db.DBSecurityService;
053:
054: import org.apache.fulcrum.security.entity.Group;
055:
056: import org.apache.fulcrum.security.util.GroupSet;
057: import org.apache.fulcrum.security.util.DataBackendException;
058: import org.apache.fulcrum.security.util.EntityExistsException;
059: import org.apache.fulcrum.security.util.UnknownEntityException;
060:
061: import org.apache.torque.om.Persistent;
062: import org.apache.torque.util.Criteria;
063:
064: import org.tigris.scarab.om.ScarabModulePeer;
065: import org.tigris.scarab.om.Module;
066: import org.tigris.scarab.util.Log;
067:
068: /**
069: * Implementation of turbine's SecurityService to account for the ScarabModule
070: * being the Group implementation.
071: *
072: * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
073: * @version $Id: ScarabDBSecurityService.java 9104 2004-05-10 21:04:51Z dabbous $
074: */
075: public class ScarabDBSecurityService extends DBSecurityService {
076: /**
077: * Retrieve a set of Groups that meet the specified Criteria.
078: *
079: * @param a <code>Criteria</code> of Group selection.
080: * @return a set of Groups that meet the specified Criteria.
081: */
082: public GroupSet getGroups(Criteria criteria)
083: throws DataBackendException {
084: List groups = null;
085: try {
086: groups = ScarabModulePeer.doSelect(criteria);
087: } catch (Exception e) {
088: throw new DataBackendException(
089: "getGroups(Criteria) failed", e); //EXCEPTION
090: }
091: if (groups == null) {
092: groups = new ArrayList(0);
093: }
094: return new GroupSet(groups);
095: }
096:
097: /**
098: * Stores Group's attributes. The Groups is required to exist in the system.
099: *
100: * @param group The Group to be stored.
101: * @throws DataBackendException if there was an error accessing the data backend.
102: * @throws UnknownEntityException if the group does not exist.
103: */
104: public void saveGroup(Group group) throws DataBackendException,
105: UnknownEntityException {
106: try {
107: if (!((Persistent) group).isNew()) {
108: group.save();
109: }
110: } catch (Exception e) {
111: throw new DataBackendException("saveGroup(Group) failed", e); //EXCEPTION
112: }
113: throw new UnknownEntityException("Unknown group '" + group
114: + "'"); //EXCEPTION
115: }
116:
117: /**
118: * Creates a new group with specified attributes.
119: *
120: * @param group the object describing the group to be created.
121: * @return a new Group object that has id set up properly.
122: * @throws DataBackendException if there was an error accessing the data backend.
123: * @throws EntityExistsException if the group already exists.
124: */
125: public synchronized Group addGroup(Group group)
126: throws DataBackendException, EntityExistsException {
127: try {
128: lockExclusive();
129: if (((Persistent) group).isNew()) {
130: group.save();
131: // add the group to system-wide cache
132: getAllGroups().add(group);
133: return group;
134: }
135: } catch (Exception e) {
136: throw new DataBackendException("addGroup(Group) failed", e); //EXCEPTION
137: } finally {
138: unlockExclusive();
139: }
140: // the only way we could get here without return/throw tirggered
141: // is that the groupExists was true.
142: throw new EntityExistsException("Group '" + group
143: + "' already exists"); //EXCEPTION
144: }
145:
146: /**
147: * Removes a Group from the system.
148: *
149: * @param group the object describing group to be removed.
150: * @throws DataBackendException if there was an error accessing the
151: * data backend.
152: * @throws UnknownEntityException if the group does not exist.
153: */
154: public synchronized void removeGroup(Group group)
155: throws DataBackendException, UnknownEntityException {
156: try {
157: lockExclusive();
158: if (!((Persistent) group).isNew()) {
159: ((Module) group).setDeleted(true);
160: group.save();
161: getAllGroups().remove(group);
162: }
163: } catch (Exception e) {
164: Log.get().error("Failed to delete a Group");
165: Log.get().error(e);
166: throw new DataBackendException("removeGroup(Group) failed",
167: e); //EXCEPTION
168: } finally {
169: unlockExclusive();
170: }
171: throw new UnknownEntityException("Unknown group '" + group
172: + "'"); //EXCEPTION
173: }
174:
175: /**
176: * Renames an existing Group.
177: *
178: * @param group the object describing the group to be renamed.
179: * @param name the new name for the group.
180: * @throws DataBackendException if there was an error accessing the
181: * data backend.
182: * @throws UnknownEntityException if the group does not exist.
183: */
184: public synchronized void renameGroup(Group group, String name)
185: throws DataBackendException, UnknownEntityException {
186: throw new DataBackendException("rename is not supported"); //EXCEPTION
187:
188: /* this stuff is cut-n-paste
189: boolean groupExists = false;
190: try
191: {
192: lockExclusive();
193: groupExists = checkExists(group);
194: if(groupExists)
195: {
196: ((SecurityObject)group).setName(name);
197: Criteria criteria = GroupPeer.buildCriteria(group);
198: GroupPeer.doUpdate(criteria);
199: return;
200: }
201: }
202: catch(Exception e)
203: {
204: throw new DataBackendException("renameGroup(Group,String)" ,e);
205: }
206: finally
207: {
208: unlockExclusive();
209: }
210: throw new UnknownEntityException("Unknown group '" + group + "'");
211: */
212: }
213:
214: /**
215: * Determines if the <code>Group</code> exists in the security system.
216: *
217: * @param group a <code>Group</code> value
218: * @return true if the group exists in the system, false otherwise
219: * @throws DataBackendException when more than one Group with
220: * the same name exists.
221: * @throws Exception a generic exception.
222: */
223: protected boolean checkExists(Group group)
224: throws DataBackendException, Exception {
225: return ScarabModulePeer.checkExists(group);
226: }
227: }
|