001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.auth.permissions;
020
021import org.apache.wiki.api.core.Page;
022import org.apache.wiki.util.Synchronizer;
023
024import java.util.WeakHashMap;
025import java.util.concurrent.atomic.AtomicReference;
026import java.util.concurrent.locks.ReentrantLock;
027
028
029/**
030 *  Provides a factory for Permission objects.  Since the Permissions are immutable,
031 *  and creating them takes a bit of time, caching them makes sense.
032 *  <p>
033 *  This class stores the permissions in a static HashMap.
034 *  @since 2.5.54
035 */
036public final class PermissionFactory
037{
038    /**
039     *  Prevent instantiation.
040     */
041    private PermissionFactory() {}
042    
043    /**
044     *  This is a WeakHashMap<String,PagePermission>, which stores the
045     *  cached page permissions.
046     */
047    private static final WeakHashMap<String, PagePermission> c_cache = new WeakHashMap<>();
048
049    /**
050     * A lock used to ensure thread safety when accessing shared resources.
051     * This lock provides more flexibility and capabilities than the intrinsic locking mechanism,
052     * such as the ability to attempt to acquire a lock with a timeout, or to interrupt a thread
053     * waiting to acquire a lock.
054     *
055     * @see java.util.concurrent.locks.ReentrantLock
056     */
057    private static final ReentrantLock lock = new ReentrantLock();
058    
059    /**
060     *  Get a permission object for a WikiPage and a set of actions.
061     *  
062     *  @param page The page object.
063     *  @param actions A list of actions.
064     *  @return A PagePermission object, presenting this page+actions combination.
065     */
066    public static PagePermission getPagePermission( final Page page, final String actions )
067    {
068        return getPagePermission( page.getWiki(), page.getName(), actions );
069    }
070    
071    /**
072     *  Get a permission object for a WikiPage and a set of actions.
073     *  
074     *  @param page The name of the page.
075     *  @param actions A list of actions.
076     *  @return A PagePermission object, presenting this page+actions combination.
077     */
078    public static PagePermission getPagePermission( final String page, final String actions )
079    {
080        return getPagePermission( "", page, actions );
081    }
082
083    /**
084     *  Get a page permission based on a wiki, page, and actions.
085     *  
086     *  @param wiki The name of the wiki. Can be an empty string, but must not be null.
087     *  @param page The page name
088     *  @param actions A list of actions.
089     *  @return A PagePermission object.
090     */
091    private static PagePermission getPagePermission( final String wiki, String page, final String actions )
092    {
093        //
094        //  The cache key must uniquely identify the (wiki, page, actions) triple.  The previous
095        //  XOR-of-hashCodes key allowed two different pages to collide onto the same 32-bit
096        //  value, so an access check for one page could silently be evaluated against another
097        //  page's cached permission (and hence the wrong ACL).  The concatenated string key is
098        //  collision-free for any attacker-choosable page name.
099        //
100        final String key = wiki + ":" + page + ":" + actions;
101        final AtomicReference<PagePermission> permRef = new AtomicReference<>();
102
103        //
104        //  It's fine if two threads update the cache, since the objects mean the same
105        //  thing anyway.  And this avoids nasty blocking effects.
106        //
107        Synchronizer.synchronize( lock, () -> permRef.set( c_cache.get( key ) ) );
108
109        if( permRef.get() == null )
110        {
111            if( !wiki.isEmpty() ) page = wiki + ":" + page;
112            final PagePermission newPerm = new PagePermission( page, actions );
113
114            Synchronizer.synchronize( lock, () -> c_cache.put( key, newPerm ) );
115
116            return newPerm;
117        }
118
119        return permRef.get();
120    }
121
122
123}