KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > services > security > ScarabDBSecurityService


1 package org.tigris.scarab.services.security;
2
3
4 /* ================================================================
5  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement: "This product includes
20  * software developed by Collab.Net <http://www.Collab.Net/>."
21  * Alternately, this acknowlegement may appear in the software itself, if
22  * and wherever such third-party acknowlegements normally appear.
23  *
24  * 4. The hosted project names must not be used to endorse or promote
25  * products derived from this software without prior written
26  * permission. For written permission, please contact info@collab.net.
27  *
28  * 5. Products derived from this software may not use the "Tigris" or
29  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
30  * prior written permission of Collab.Net.
31  *
32  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
36  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
40  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  * ====================================================================
45  *
46  * This software consists of voluntary contributions made by many
47  * individuals on behalf of Collab.Net.
48  */

49
50 import java.util.ArrayList JavaDoc;
51 import java.util.List JavaDoc;
52
53 import org.apache.fulcrum.security.impl.db.DBSecurityService;
54
55 import org.apache.fulcrum.security.entity.Group;
56
57 import org.apache.fulcrum.security.util.GroupSet;
58 import org.apache.fulcrum.security.util.DataBackendException;
59 import org.apache.fulcrum.security.util.EntityExistsException;
60 import org.apache.fulcrum.security.util.UnknownEntityException;
61
62 import org.apache.torque.om.Persistent;
63 import org.apache.torque.util.Criteria;
64
65 import org.tigris.scarab.om.ScarabModulePeer;
66 import org.tigris.scarab.om.Module;
67 import org.tigris.scarab.util.Log;
68
69
70 /**
71  * Implementation of turbine's SecurityService to account for the ScarabModule
72  * being the Group implementation.
73  *
74  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
75  * @version $Id: ScarabDBSecurityService.java 9104 2004-05-10 21:04:51Z dabbous $
76  */

77 public class ScarabDBSecurityService extends DBSecurityService
78 {
79     /**
80      * Retrieve a set of Groups that meet the specified Criteria.
81      *
82      * @param a <code>Criteria</code> of Group selection.
83      * @return a set of Groups that meet the specified Criteria.
84      */

85     public GroupSet getGroups(Criteria criteria)
86         throws DataBackendException
87     {
88         List JavaDoc groups = null;
89         try
90         {
91             groups = ScarabModulePeer.doSelect(criteria);
92         }
93         catch(Exception JavaDoc e)
94         {
95             throw new DataBackendException("getGroups(Criteria) failed", e); //EXCEPTION
96
}
97         if (groups == null)
98         {
99             groups = new ArrayList JavaDoc(0);
100         }
101         return new GroupSet(groups);
102     }
103
104     /**
105      * Stores Group's attributes. The Groups is required to exist in the system.
106      *
107      * @param group The Group to be stored.
108      * @throws DataBackendException if there was an error accessing the data backend.
109      * @throws UnknownEntityException if the group does not exist.
110      */

111     public void saveGroup(Group group)
112         throws DataBackendException, UnknownEntityException
113     {
114         try
115         {
116             if (!((Persistent)group).isNew())
117             {
118                 group.save();
119             }
120         }
121         catch(Exception JavaDoc e)
122         {
123             throw new DataBackendException("saveGroup(Group) failed" ,e); //EXCEPTION
124
}
125         throw new UnknownEntityException("Unknown group '" + group + "'"); //EXCEPTION
126
}
127
128     /**
129      * Creates a new group with specified attributes.
130      *
131      * @param group the object describing the group to be created.
132      * @return a new Group object that has id set up properly.
133      * @throws DataBackendException if there was an error accessing the data backend.
134      * @throws EntityExistsException if the group already exists.
135      */

136     public synchronized Group addGroup(Group group)
137         throws DataBackendException, EntityExistsException
138     {
139         try
140         {
141             lockExclusive();
142             if (((Persistent)group).isNew())
143             {
144                 group.save();
145                 // add the group to system-wide cache
146
getAllGroups().add(group);
147                 return group;
148             }
149         }
150         catch(Exception JavaDoc e)
151         {
152             throw new DataBackendException("addGroup(Group) failed", e); //EXCEPTION
153
}
154         finally
155         {
156             unlockExclusive();
157         }
158         // the only way we could get here without return/throw tirggered
159
// is that the groupExists was true.
160
throw new EntityExistsException("Group '" + group +
161             "' already exists"); //EXCEPTION
162
}
163
164     /**
165      * Removes a Group from the system.
166      *
167      * @param group the object describing group to be removed.
168      * @throws DataBackendException if there was an error accessing the
169      * data backend.
170      * @throws UnknownEntityException if the group does not exist.
171      */

172     public synchronized void removeGroup(Group group)
173         throws DataBackendException, UnknownEntityException
174     {
175         try
176         {
177             lockExclusive();
178             if (!((Persistent)group).isNew())
179             {
180                 ((Module)group).setDeleted(true);
181                 group.save();
182                 getAllGroups().remove(group);
183             }
184         }
185         catch(Exception JavaDoc e)
186         {
187             Log.get().error("Failed to delete a Group");
188             Log.get().error(e);
189             throw new DataBackendException("removeGroup(Group) failed", e); //EXCEPTION
190
}
191         finally
192         {
193             unlockExclusive();
194         }
195         throw new UnknownEntityException("Unknown group '" + group + "'"); //EXCEPTION
196
}
197
198     /**
199      * Renames an existing Group.
200      *
201      * @param group the object describing the group to be renamed.
202      * @param name the new name for the group.
203      * @throws DataBackendException if there was an error accessing the
204      * data backend.
205      * @throws UnknownEntityException if the group does not exist.
206      */

207     public synchronized void renameGroup(Group group, String JavaDoc name)
208         throws DataBackendException, UnknownEntityException
209     {
210         throw new DataBackendException("rename is not supported"); //EXCEPTION
211

212         /* this stuff is cut-n-paste
213         boolean groupExists = false;
214         try
215         {
216             lockExclusive();
217             groupExists = checkExists(group);
218             if(groupExists)
219             {
220                 ((SecurityObject)group).setName(name);
221                 Criteria criteria = GroupPeer.buildCriteria(group);
222                 GroupPeer.doUpdate(criteria);
223                 return;
224             }
225         }
226         catch(Exception e)
227         {
228             throw new DataBackendException("renameGroup(Group,String)" ,e);
229         }
230         finally
231         {
232             unlockExclusive();
233         }
234         throw new UnknownEntityException("Unknown group '" + group + "'");
235         */

236     }
237
238     /**
239      * Determines if the <code>Group</code> exists in the security system.
240      *
241      * @param group a <code>Group</code> value
242      * @return true if the group exists in the system, false otherwise
243      * @throws DataBackendException when more than one Group with
244      * the same name exists.
245      * @throws Exception a generic exception.
246      */

247     protected boolean checkExists(Group group)
248         throws DataBackendException, Exception JavaDoc
249     {
250         return ScarabModulePeer.checkExists(group);
251     }
252 }
253
Popular Tags