001package org.apache.fulcrum.security.model.basic;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021import org.apache.fulcrum.security.acl.AccessControlList;
022import org.apache.fulcrum.security.entity.User;
023import org.apache.fulcrum.security.model.ACLFactory;
024import org.apache.fulcrum.security.model.basic.entity.BasicUser;
025import org.apache.fulcrum.security.spi.AbstractManager;
026import org.apache.fulcrum.security.util.GroupSet;
027import org.apache.fulcrum.security.util.UnknownEntityException;
028
029/**
030 *
031 * This factory creates instance of the DynamicAccessControlList
032 *
033 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
034 * @version $Id$
035 */
036public class BasicACLFactory extends AbstractManager implements ACLFactory
037{
038    /**
039     * Construct a new ACL object.
040     *
041     * This constructs a new ACL object from the configured class and
042     * initializes it with the supplied roles and permissions.
043     *
044     * @param groupSet
045     *            The GroupSet that this ACL should contain
046     *
047     * @return an object implementing ACL interface.
048     * @throws UnknownEntityException
049     *             if the object could not be instantiated.
050     */
051    private BasicAccessControlListImpl getAclInstance(GroupSet groupSet) throws UnknownEntityException
052    {
053        BasicAccessControlListImpl accessControlList;
054
055        try
056        {
057            accessControlList = new BasicAccessControlListImpl(groupSet);
058        }
059        catch (Exception e)
060        {
061            throw new UnknownEntityException("Failed to instantiate an ACL implementation object", e);
062        }
063
064        return accessControlList;
065    }
066
067    /* (non-Javadoc)
068     * @see org.apache.fulcrum.security.model.ACLFactory#getAccessControlList(org.apache.fulcrum.security.entity.User)
069     */
070    public <T extends AccessControlList> T getAccessControlList(User user)
071    {
072        GroupSet groupSet = ((BasicUser) user).getGroups();
073
074        try
075        {
076            @SuppressWarnings("unchecked")
077                        T aclInstance = (T) getAclInstance(groupSet);
078                        return aclInstance;
079        }
080        catch (UnknownEntityException uue)
081        {
082            throw new RuntimeException(uue.getMessage(), uue);
083        }
084    }
085}