View Javadoc
1   package org.apache.fulcrum.security.spi;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  import org.apache.commons.lang3.StringUtils;
22  import org.apache.fulcrum.security.GroupManager;
23  import org.apache.fulcrum.security.entity.Group;
24  import org.apache.fulcrum.security.util.DataBackendException;
25  import org.apache.fulcrum.security.util.EntityExistsException;
26  import org.apache.fulcrum.security.util.UnknownEntityException;
27  
28  /**
29   * This implementation keeps all objects in memory. This is mostly meant to help
30   * with testing and prototyping of ideas.
31   *
32   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
33   * @version $Id$
34   */
35  public abstract class AbstractGroupManager extends AbstractEntityManager implements GroupManager
36  {
37      /**
38  	 * serial id
39  	 */
40  	private static final long serialVersionUID = 1L;
41  
42  	protected abstract <T extends Group> T persistNewGroup(T group) throws DataBackendException;
43  
44      /**
45       * Construct a blank Group object.
46       *
47       * This method calls getGroupClass, and then creates a new object using the
48       * default constructor.
49       *
50       * @return an object implementing Group interface.
51       * @throws DataBackendException
52       *             if the object could not be instantiated.
53       */
54      @Override
55  	public <T extends Group> T getGroupInstance() throws DataBackendException
56      {
57          try
58          {
59              @SuppressWarnings("unchecked")
60  			T group = (T) Class.forName(getClassName()).newInstance();
61              return group;
62          }
63          catch (Exception e)
64          {
65              throw new DataBackendException("Problem creating instance of class " + getClassName(), e);
66          }
67      }
68  
69      /**
70       * Construct a blank Group object.
71       *
72       * This method calls getGroupClass, and then creates a new object using the
73       * default constructor.
74       *
75       * @param groupName
76       *            The name of the Group
77       *
78       * @return an object implementing Group interface.
79       *
80       * @throws DataBackendException
81       *             if the object could not be instantiated.
82       */
83      @Override
84  	public <T extends Group> T getGroupInstance(String groupName) throws DataBackendException
85      {
86          T group = getGroupInstance();
87          group.setName(groupName);
88          return group;
89      }
90  
91      /**
92       * Retrieve a Group object with specified name.
93       *
94       * @param name
95       *            the name of the Group.
96       * @return an object representing the Group with specified name.
97       * @throws DataBackendException
98       *             if there was an error accessing the data backend.
99       * @throws UnknownEntityException
100      *             if the group does not exist.
101      */
102     @Override
103 	public <T extends Group> T getGroupByName(String name) throws DataBackendException, UnknownEntityException
104     {
105         @SuppressWarnings("unchecked")
106 		T group = (T) getAllGroups().getByName(name);
107         if (group == null)
108         {
109             throw new UnknownEntityException("The specified group does not exist");
110         }
111         return group;
112     }
113 
114     /**
115      * Retrieve a Group object with specified Id.
116      *
117      * @param id
118      *            the ID of the Group.
119      *
120      * @return an object representing the Group with specified name.
121      *
122      * @throws UnknownEntityException
123      *             if the permission does not exist in the database.
124      * @throws DataBackendException
125      *             if there is a problem accessing the storage.
126      */
127     @Override
128 	public <T extends Group> T getGroupById(Object id) throws DataBackendException, UnknownEntityException
129     {
130         @SuppressWarnings("unchecked")
131 		T group = (T) getAllGroups().getById(id);
132         if (group == null)
133         {
134             throw new UnknownEntityException("The specified group does not exist");
135         }
136         return group;
137     }
138 
139     /**
140      * Creates a new group with specified attributes.
141      *
142      * @param group
143      *            the object describing the group to be created.
144      * @return a new Group object that has id set up properly.
145      * @throws DataBackendException
146      *             if there was an error accessing the data backend.
147      * @throws EntityExistsException
148      *             if the group already exists.
149      */
150     @Override
151 	public synchronized <T extends Group> T addGroup(T group) throws DataBackendException, EntityExistsException
152     {
153         boolean groupExists = false;
154         if (StringUtils.isEmpty(group.getName()))
155         {
156             throw new DataBackendException("Could not create a group with empty name!");
157         }
158 //        if (group.getId() == null)
159 //        {
160 //            throw new DataBackendException("Could not create a group with an id of null!");
161 //        }
162         groupExists = checkExists(group);
163         if (!groupExists)
164         {
165 
166             // return the object with correct id
167             return persistNewGroup(group);
168         }
169         else
170         {
171             throw new EntityExistsException("Group '" + group + "' already exists");
172         }
173     }
174 
175     /**
176      * Check whether a specified group exists.
177      *
178      * The name is used for looking up the group
179      *
180      * @param group
181      *            The group to be checked.
182      * @return true if the specified group exists
183      * @throws DataBackendException
184      *             if there was an error accessing the data backend.
185      */
186     @Override
187 	public boolean checkExists(Group group) throws DataBackendException
188     {
189         return checkExists(group.getName());
190     }
191 
192 }