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 java.util.WeakHashMap; 022 023import org.apache.wiki.WikiPage; 024 025/** 026 * Provides a factory for Permission objects. Since the Permissions are immutable, 027 * and creating them takes a bit of time, caching them makes sense. 028 * <p> 029 * This class stores the permissions in a static HashMap. 030 * @since 2.5.54 031 */ 032public final class PermissionFactory 033{ 034 /** 035 * Prevent instantiation. 036 */ 037 private PermissionFactory() {} 038 039 /** 040 * This is a WeakHashMap<Integer,PagePermission>, which stores the 041 * cached page permissions. 042 */ 043 private static WeakHashMap<Integer, PagePermission> c_cache = new WeakHashMap<Integer, PagePermission>(); 044 045 /** 046 * Get a permission object for a WikiPage and a set of actions. 047 * 048 * @param page The page object. 049 * @param actions A list of actions. 050 * @return A PagePermission object, presenting this page+actions combination. 051 */ 052 public static PagePermission getPagePermission( WikiPage page, String actions ) 053 { 054 return getPagePermission( page.getWiki(), page.getName(), actions ); 055 } 056 057 /** 058 * Get a permission object for a WikiPage and a set of actions. 059 * 060 * @param page The name of the page. 061 * @param actions A list of actions. 062 * @return A PagePermission object, presenting this page+actions combination. 063 */ 064 public static PagePermission getPagePermission( String page, String actions ) 065 { 066 return getPagePermission( "", page, actions ); 067 } 068 069 /** 070 * Get a page permission based on a wiki, page, and actions. 071 * 072 * @param wiki The name of the wiki. Can be an empty string, but must not be null. 073 * @param page The page name 074 * @param actions A list of actions. 075 * @return A PagePermission object. 076 */ 077 private static PagePermission getPagePermission( String wiki, String page, String actions ) 078 { 079 PagePermission perm; 080 // 081 // Since this is pretty speed-critical, we try to avoid the StringBuffer creation 082 // overhead by XORring the hashcodes. However, if page name length > 32 characters, 083 // this might result in two same hashCodes. 084 // FIXME: Make this work for page-name lengths > 32 characters (use the alt implementation 085 // if page.length() > 32?) 086 // Alternative implementation below, but it does create an extra StringBuffer. 087 //String key = wiki+":"+page+":"+actions; 088 089 Integer key = wiki.hashCode() ^ page.hashCode() ^ actions.hashCode(); 090 091 // 092 // It's fine if two threads update the cache, since the objects mean the same 093 // thing anyway. And this avoids nasty blocking effects. 094 // 095 synchronized( c_cache ) 096 { 097 perm = c_cache.get( key ); 098 } 099 100 if( perm == null ) 101 { 102 if( wiki.length() > 0 ) page = wiki+":"+page; 103 perm = new PagePermission( page, actions ); 104 105 synchronized( c_cache ) 106 { 107 c_cache.put( key, perm ); 108 } 109 } 110 111 return perm; 112 } 113 114}