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.PermissionManager;
23  import org.apache.fulcrum.security.entity.Permission;
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: AbstractPermissionManager.java 1372918 2012-08-14 15:19:40Z tv
34   *          $
35   */
36  public abstract class AbstractPermissionManager extends AbstractEntityManager implements PermissionManager
37  {
38      /**
39       * 
40       */
41      private static final long serialVersionUID = 1L;
42  
43      protected abstract <T extends Permission> T persistNewPermission(T permission) throws DataBackendException;
44  
45      /**
46       * Construct a blank Permission object.
47       *
48       * This method calls getPermissionClass, and then creates a new object using
49       * the default constructor.
50       *
51       * @return an object implementing Permission interface.
52       * @throws DataBackendException
53       *             if the object could not be instantiated.
54       */
55      @Override
56  	public <T extends Permission> T getPermissionInstance() throws DataBackendException
57      {
58          try
59          {
60              @SuppressWarnings("unchecked")
61  			T permission = (T) Class.forName(getClassName()).newInstance();
62              return permission;
63          }
64          catch (Exception e)
65          {
66              throw new DataBackendException("Failed to instantiate a Permission implementation object", e);
67          }
68      }
69  
70      /**
71       * Construct a blank Permission object.
72       *
73       * This method calls getPermissionClass, and then creates a new object using
74       * the default constructor.
75       *
76       * @param permName
77       *            The name of the permission.
78       *
79       * @return an object implementing Permission interface.
80       * @throws DataBackendException
81       *             if the object could not be instantiated.
82       */
83      @Override
84  	public <T extends Permission> T getPermissionInstance(String permName) throws DataBackendException
85      {
86          T perm = getPermissionInstance();
87          perm.setName(permName);
88          return perm;
89      }
90  
91      /**
92       * Retrieve a Permission object with specified name.
93       *
94       * @param name
95       *            the name of the Permission.
96       * @return an object representing the Permission with specified name.
97       * @throws DataBackendException
98       *             if there was an error accessing the data backend.
99       * @throws UnknownEntityException
100      *             if the permission does not exist.
101      */
102     @Override
103 	public <T extends Permission> T getPermissionByName(String name) throws DataBackendException, UnknownEntityException
104     {
105         @SuppressWarnings("unchecked")
106 		T permission = (T) getAllPermissions().getByName(name);
107         if (permission == null)
108         {
109             throw new UnknownEntityException("The specified permission does not exist");
110         }
111         return permission;
112     }
113 
114     /**
115      * Retrieve a Permission object with specified Id.
116      *
117      * @param id
118      *            the ID of the Permission.
119      *
120      * @return an object representing the Permission 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 Permission> T getPermissionById(Object id) throws DataBackendException, UnknownEntityException
129     {
130         @SuppressWarnings("unchecked")
131 		T permission = (T) getAllPermissions().getById(id);
132         if (permission == null)
133         {
134             throw new UnknownEntityException("The specified permission does not exist");
135         }
136         return permission;
137     }
138 
139     /**
140      * Creates a new permission with specified attributes.
141      *
142      * @param permission
143      *            the object describing the permission to be created.
144      * @return a new Permission 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 permission already exists.
149      */
150     @Override
151 	public synchronized <T extends Permission> T addPermission(T permission) throws DataBackendException, EntityExistsException
152     {
153         boolean permissionExists = false;
154         if (StringUtils.isEmpty(permission.getName()))
155         {
156             throw new DataBackendException("Could not create a permission with empty name!");
157         }
158 //        if (permission.getId() == null)
159 //        {
160 //            throw new DataBackendException("Could not create a permission with an id of null!");
161 //        }
162         try
163         {
164             permissionExists = checkExists(permission);
165             if (!permissionExists)
166             {
167                 return persistNewPermission(permission);
168             }
169         }
170         catch (Exception e)
171         {
172             throw new DataBackendException("addPermission(Permission) failed", e);
173         }
174         // the only way we could get here without return/throw triggered
175         // is that the permissionExists was true.
176         throw new EntityExistsException("Permission '" + permission + "' already exists");
177     }
178 
179     /**
180      * Check whether a specified permission exists.
181      *
182      * The name is used for looking up the permission
183      *
184      * @param permission
185      *            The permission to be checked.
186      * @return true if the specified permission exists
187      * @throws DataBackendException
188      *             if there was an error accessing the data backend.
189      */
190     @Override
191 	public boolean checkExists(Permission permission) throws DataBackendException
192     {
193         return checkExists(permission.getName());
194     }
195 
196 }