head	1.17;
access;
symbols
	TIGRIS_1_1_0RC2:1.6
	TIGRIS_1_1_0RC1:1.6
	TIGRIS_1_1:1.6.0.4
	TIGRIS_1_0_8:1.6
	TIGRIS_1_0_8RC3:1.6
	TIGRIS_1_0_8RC2:1.6
	TIGRIS_1_0_8RC1:1.6
	TIGRIS_1_0_7:1.6
	TIGRIS_1_0_7RC3:1.6
	TIGRIS_1_0_7RC2:1.6
	TIGRIS_1_0_7RC1:1.6
	TIGRIS_1_0_6:1.6
	TIGRIS_1_0_6RC5:1.6
	TIGRIS_1_0_6RC4:1.6
	TIGRIS_1_0_6RC3:1.6
	TIGRIS_1_0_6RC2:1.6
	TIGRIS_1_0_6RC1:1.6
	TIGRIS_1_0_5:1.6
	TIGRIS_1_0_5RC6:1.6
	TIGRIS_1_0_5RC5:1.6
	TIGRIS_1_0_5RC4:1.6
	TIGRIS_1_0_5RC3:1.6
	TIGRIS_1_0_5RC2:1.6
	TIGRIS_1_0_5RC1:1.6
	TIGRIS_1_0_4:1.6
	TIGRIS_1_0_3:1.6
	TIGRIS_1_0_2:1.6
	TIGRIS_1_0_1:1.6
	TIGRIS_1_0:1.6.0.6
	TIGRIS_1_0_0:1.6
	TIGRIS_1_0_0_RC1:1.6.0.2;
locks; strict;
comment	@# @;


1.17
date	2001.08.11.09.08.03;	author dlr;	state Exp;
branches;
next	1.16;

1.16
date	2001.05.30.20.15.25;	author leonardr;	state Exp;
branches;
next	1.15;

1.15
date	2001.05.30.19.10.21;	author leonardr;	state Exp;
branches;
next	1.14;

1.14
date	2001.05.23.01.50.13;	author dlr;	state Exp;
branches;
next	1.13;

1.13
date	2001.05.23.00.35.00;	author leonardr;	state Exp;
branches;
next	1.12;

1.12
date	2001.04.20.21.19.40;	author leonardr;	state Exp;
branches;
next	1.11;

1.11
date	2001.04.20.15.30.21;	author dlr;	state Exp;
branches;
next	1.10;

1.10
date	2001.04.18.22.39.19;	author dlr;	state Exp;
branches;
next	1.9;

1.9
date	2001.04.12.18.49.00;	author leonardr;	state Exp;
branches;
next	1.8;

1.8
date	2001.04.12.04.41.05;	author leonardr;	state Exp;
branches;
next	1.7;

1.7
date	2001.03.30.20.10.51;	author dlr;	state Exp;
branches;
next	1.6;

1.6
date	2001.03.16.15.58.50;	author leonardr;	state Exp;
branches;
next	1.5;

1.5
date	2001.03.16.00.48.09;	author leonardr;	state Exp;
branches;
next	1.4;

1.4
date	2001.03.13.03.25.04;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2001.03.13.02.21.16;	author leonardr;	state Exp;
branches;
next	1.2;

1.2
date	2001.03.07.01.53.22;	author lucas;	state Exp;
branches;
next	1.1;

1.1
date	2001.03.07.01.13.15;	author lucas;	state Exp;
branches;
next	;


desc
@@


1.17
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
text
@/* ================================================================
 * Copyright (c) 2000-2001 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.om;

import java.util.*;
import java.rmi.RemoteException;

import org.apache.commons.util.BufferCache;
import org.apache.commons.util.exception.NestableException;

import org.apache.torque.om.NumberKey;
import org.apache.torque.util.Criteria;

import org.tigris.helm.tool.cache.SimpleRegisterableCache;
import org.apache.turbine.util.Log;

/**
 * Extension of generated Peer BaseRoleActionResourcePeer.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 */
public class RoleActionResourcePeer extends BaseRoleActionResourcePeer
{
    /**
     * Flag indicating whether in debug mode.
     */
    private static final boolean DEBUG = true;

    /**
     * Static container of the permissions held by roles.
     */
    private static RoleActionResourceCache definitions;

    static
    {
        definitions = new RoleActionResourceCache();
    }


    /**
     * Overridden BasePeer method to do a delete and then to send out
     * an appropriate CacheInvalidationEvent.
     */
    public static void doDelete(Criteria c)
        throws Exception
    {
        BaseRoleActionResourcePeer.doDelete(c);
        uncache(RoleActionResource.class);
    }

    /**
     * Returns a Map of Actions to Lists of Resources for the given Role.
     */
    public static Map getPermissionsForRole(Role role)
        throws Exception
    {
        Map cache = definitions.getCache();
        Map permissions = (Map)cache.get(role);
        if (permissions == null)
        {
            synchronized (cache)
            {
                // The cache does not yet contain Permissions for this Role
                permissions = new HashMap();
                Criteria c = new Criteria()
                    .addJoin(ResourcePeer.RESOURCE_ID, 
                             RoleActionResourcePeer.RESOURCE_ID)
                    .add(RoleActionResourcePeer.ROLE_ID, role.getRoleId());
                List mappings = doSelect(c);
                for (Iterator i = mappings.iterator(); i.hasNext();)
                {
                    RoleActionResource mapping =
                        (RoleActionResource) i.next();
                    Action action = mapping.getAction();
                    Resource resource = mapping.getResource();
                    List resourcesForAction = null;
                    if (permissions.containsKey(action))
                    {
                        resourcesForAction = (List)permissions.get(action);
                    } 
                    else 
                    {
                        resourcesForAction = new ArrayList();
                    }
                    resourcesForAction.add(resource);
                    permissions.put(action, resourcesForAction);
                }
                cache.put(role, permissions);
            }
        }
        return permissions;
    }

    static class RoleActionResourceCache extends SimpleRegisterableCache
    {
        /**
         * The HelmObject subclasses changes to which should trigger
         * an ACL cache invalidation.  
         */
        private static Class[] interestingDescriptors =
        {
            Role.class,
            RoleActionResource.class
        };

        /**
         * Passthrough constructor.
         */
        public RoleActionResourceCache()
        {
            super("Role definition cache", 
                  RoleActionResource.class, new BufferCache(50));
        }
    }
}
@


1.16
log
@Big cleanup of cache self-descriptions. Also added an admin tool to do manual selective cache invalidation.
@
text
@d52 5
a56 5
import org.apache.turbine.util.db.*;
import org.apache.turbine.util.BufferCache;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.TurbineException;
import org.apache.turbine.util.TurbineException;
d59 1
@


1.15
log
@You're interested in the BO, not the peer.
@
text
@d155 1
a155 1
            super("Role-Action-Resource cache", 
@


1.14
log
@Ported Helm from WebMacro to Velocity.
@
text
@d156 1
a156 1
                  RoleActionResourcePeer.class, new BufferCache(50));
@


1.13
log
@Cleanup of caching and cache invalidation.
@
text
@d55 2
@


1.12
log
@Send CacheInvalidationEvents on deletes, and on saves of objects which are not themselves cached but which invalidate other caches when they are changed.
@
text
@d78 1
a78 9
        try
        {
            definitions = new RoleActionResourceCache();
        }
        catch (RemoteException e)
        {
            throw new RuntimeException("Unable to create role mapping cache: " + 
                                       e.getMessage());
        }
d90 1
a90 1
        uncache(RoleActionResourcePeer.class);
d139 2
a140 1
         * Passthrough constructor.
d142 1
a142 2
        public RoleActionResourceCache()
            throws RemoteException
d144 3
a146 2
            super(RoleActionResourcePeer.class, new BufferCache(50));
        }
d149 1
a149 2
         * This cache is invalidated whenever a RoleActionResource or
         * a Role is changed.
d151 1
a151 2
        protected final boolean affectedByInvalidation
            (Object invalidationDescriptor)
d153 2
a154 15
            boolean affects = false;
            if (invalidationDescriptor == null)
            {
                affects = true;
            }
            else if (invalidationDescriptor instanceof String)
            {
                String objectClassName = (String)invalidationDescriptor;
                if (objectClassName.equals(Role.class.getName()) || 
                    objectClassName.equals(RoleActionResource.class.getName()))
                {
                    affects = true;
                }
            }
            return affects;
@


1.11
log
@Added semi-selective cache invalidation which invalidates particular caches on a write instead of all the caches.
@
text
@d89 12
@


1.10
log
@SequencedHashtable, BufferCache, and BaseUnicastRemoteService have been moved into Turbine.  Changed references accordingly.
@
text
@d74 1
a74 1
    private static SimpleRegisterableCache definitions;
d80 1
a80 2
            definitions = new SimpleRegisterableCache
                (RoleActionResourcePeer.class, new BufferCache(50));
d132 1
a132 4
    /**
     * Logs the mapping of roles to actions and extents.
     */
    /*    public static String dumpStatic()
d134 15
a148 3
        StringBuffer s = new StringBuffer();
        s.append("PermissionsByRole :\n");
        for (Iterator i = definitions.keySet().iterator(); i.hasNext();)
d150 2
a151 4
            String roleID = (String)i.next();
            s.append(" " + roleID + ":\n");
            Map permissionsForRole = getPermissionsForRole(roleID);
            for (Iterator j = permissionsForRole.keySet().iterator(); j.hasNext();)
d153 7
a159 4
                String actionName = (String)j.next();
                s.append("  " + actionName + ": ");
                List resourcesForAction = null;
                if (permissionsForRole.containsKey(actionName))
d161 1
a161 3
                    resourcesForAction = (List)permissionsForRole.get(actionName);
                } else {
                    resourcesForAction = new ArrayList();
a162 6
                for (Iterator k = resourcesForAction.iterator(); k.hasNext();)
                {
                    String resource = (String)k.next();
                    s.append(resource + ' ');
                }
                s.append("\n");                
d164 1
a164 1
            s.append("\n");
d166 1
a166 2
        return s.toString();
        }*/
@


1.9
log
@Caching fixes and additions.
@
text
@d53 1
d55 1
a55 1
import org.tigris.helm.tool.BufferCache;
@


1.8
log
@Added debugging code and moved RARPeer and ProjectPeer to the new caching system.
@
text
@d55 1
a55 1
import org.tigris.helm.tool.cache.RegisterableCache;
d73 1
a73 1
    private static RoleDefinitionCache definitions;
d79 2
a80 1
            definitions = new RoleDefinitionCache();
d95 2
a96 1
        Map permissions = (Map)definitions.definitionsByRole.get(role);
d99 1
a99 1
            synchronized (definitions)
d126 1
a126 1
                definitions.definitionsByRole.put(role, permissions);
a165 52

    static class RoleDefinitionCache extends RegisterableCache
    {
        Map definitionsByRole;
        
        public RoleDefinitionCache()
            throws RemoteException
        {
            super(RoleActionResourcePeer.class);
            definitionsByRole = new BufferCache(50);
        }


        /**
         * Invalidates some portion of the cache.
         *
         * @@param invalidationDescriptor Description of the invalidation that 
         *                               should occur, or <code>null</code> for
         *                               complete invalidation.
         */
        public final void invalidate(Object invalidationDescriptor)
        {
            if (invalidationDescriptor == null)
            {
                if (DEBUG)
                {
                    StringBuffer buf =
                        new StringBuffer("RoleActionResourcePeer invalidating cache: ");
                    Object[] keys = definitionsByRole.keySet().toArray();
                    for (int i = 0; i < keys.length; i++)
                    {
                        Role key = (Role) keys[i];
                        if (i > 0) buf.append(", ");
                        buf.append(key.getName());
                        definitionsByRole.remove(key);
                    }
                    Log.debug(buf.toString());
                }
                else
                {
                    definitionsByRole.clear();
                }
            }
            else
            {
                throw new UnsupportedOperationException
                    ("RoleDefinitionCache.invalidate() does not " + 
                     "currently support a descriptor");
            }
        }
        
    }
@


1.7
log
@Attempt to inline invalidateRole().
@
text
@d50 1
d54 2
d66 1
a66 1
     * Static container of the permissions held by roles.
d68 1
a68 1
    private static Map permissionsByRole = new HashMap();
d71 1
a71 1
     * Remove a role's mapping.
d73 3
a75 1
    public static void uncache(Role toInvalidate)
d77 1
a77 1
        if (permissionsByRole.containsKey(toInvalidate))
d79 6
a84 1
            permissionsByRole.remove(toInvalidate);
a87 5
    public static final void invalidateRole(Role toInvalidate)
    {
        permissionsByRole.remove(toInvalidate);
    }

d94 1
a94 1
        Map permissions = (Map) permissionsByRole.get(role);
d97 1
a97 1
            synchronized (permissionsByRole)
d99 8
a106 2
                permissions = (Map) permissionsByRole.get(role);
                if (permissions == null)
d108 10
a117 8
                    // The cache does not yet contain Permissions for this Role
                    permissions = new HashMap();
                    Criteria c = new Criteria()
                        .addJoin(ResourcePeer.RESOURCE_ID, 
                                 RoleActionResourcePeer.RESOURCE_ID)
                        .add(RoleActionResourcePeer.ROLE_ID, role.getRoleId());
                    List mappings = doSelect(c);
                    for (Iterator i = mappings.iterator(); i.hasNext();)
d119 1
a119 15
                        RoleActionResource mapping =
                            (RoleActionResource) i.next();
                        Action action = mapping.getAction();
                        Resource resource = mapping.getResource();
                        List resourcesForAction = null;
                        if (permissions.containsKey(action))
                        {
                            resourcesForAction = (List)permissions.get(action);
                        } 
                        else 
                        {
                            resourcesForAction = new ArrayList();
                        }
                        resourcesForAction.add(resource);
                        permissions.put(action, resourcesForAction);
d121 2
a122 1
                    permissionsByRole.put(role, permissions);
d124 1
d137 1
a137 1
        for (Iterator i = permissionsByRole.keySet().iterator(); i.hasNext();)
d164 52
@


1.6
log
@Disabled some more actions on project lock.
@
text
@d78 1
a78 1
    public static void invalidateRole(Role toInvalidate)
@


1.5
log
@Corrected ambiguous comment.
@
text
@d68 16
@


1.4
log
@Corrected handling of critical section in getPermissionsForRole().
@
text
@d81 1
a81 1
                    // Permissions for the Role in question aren't yet cached.
@


1.3
log
@Added more caching.
@
text
@d50 1
a53 1
import org.tigris.helm.om.*;
d58 1
d73 2
a74 6
        Map permissions = null;
        if (permissionsByRole.containsKey(role))
        { 
            permissions = (Map)permissionsByRole.get(role);
        } 
        else 
d76 1
a76 7
            permissions = new HashMap();
            Criteria c = new Criteria()
                .addJoin(ResourcePeer.RESOURCE_ID, 
                         RoleActionResourcePeer.RESOURCE_ID)
                .add(RoleActionResourcePeer.ROLE_ID, role.getRoleId());
            List mappings = doSelect(c);
            for (Iterator i = mappings.iterator(); i.hasNext();)
d78 2
a79 9
                RoleActionResource mapping = (RoleActionResource)i.next();
                Action action = mapping.getAction();
                Resource resource = mapping.getResource();
                List resourcesForAction = null;
                if (permissions.containsKey(action))
                {
                    resourcesForAction = (List)permissions.get(action);
                } 
                else 
d81 26
a106 1
                    resourcesForAction = new ArrayList();
a107 2
                resourcesForAction.add(resource);
                permissions.put(action, resourcesForAction);
a108 1
            permissionsByRole.put(role, permissions);
@


1.2
log
@Corrected regex madness for Torque upgrade.  -dlr
@
text
@d51 1
d66 5
a70 1
    public static Map getPermissionsForRole(String roleID)
d73 1
a73 1
        if (permissionsByRole.containsKey(roleID))
d75 4
a78 2
            permissions = (Map)permissionsByRole.get(roleID);
        } else {
d80 23
a106 39
    //
    // Methods for dealing with the static maps.
    //

    /**
     * Clears and populates the static mapping of projects to groups and
     * groups to projects.
     *
     * @@exception Exception
     */
    public static void rebuildStatic()
        throws Exception
    {        
        permissionsByRole.clear();

        List dbMappings = doSelect(new Criteria());
        for (Iterator i = dbMappings.iterator(); i.hasNext();)
        {
            RoleActionResource rar = (RoleActionResource)i.next();
            String actionName = 
                ActionPeer.getInstance(rar.getActionID()).getName();
            String pattern = 
                ResourcePeer.getInstance(rar.getResourceID()).getPattern();
            String roleID = rar.getRoleID();

            Map permissionsForRole = getPermissionsForRole(roleID);
            List resourcesForAction = null;
            if (permissionsForRole.containsKey(actionName))
            {
                resourcesForAction = (List)permissionsForRole.get(actionName);
            } else {
                resourcesForAction = new ArrayList();
            }
            resourcesForAction.add(pattern);
            permissionsForRole.put(actionName, resourcesForAction);
            permissionsByRole.put(roleID, permissionsForRole);
        }        
    }

d110 1
a110 1
    public static String dumpStatic()
d140 1
a140 1
    }
@


1.1
log
@Moved in from peer package to handle Torque changes. -dlr
@
text
@a52 1

@

