# ResourceRegistry.java

    /*
    ** Module   : ResourceRegistry.java
    ** Abstract : Implements a registry entry for abstract resource plugins.
    **
    ** Copyright (c) 2004-2017, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --JPRM-- ------------------Description-------------------
    ** 001 NVS 20050318   @20431 Created the initial version. This class
    **                           represents the abstract resource plugins.
    ** 002 NVS 20050525   @21271 Resource plugins now provide scoped variables
    **                           and their libraries are registered in single
    **                           instance of SymbolResolver which has become
    **                           the property of the security cache.
    ** 003 CA  20091026   @44196 Changes related to building a consolidated set of
    **                           ACLs. This implies adding new permission search
    **                           methods, to determine the rights for a subject
    **                           other than the authenticated one.
    ** 004 CA  20110105          Fixed concurrency issues related to per-subject
    **                           searches.
    ** 005 EVL 20160224          Javadoc fixes to make compatible with Oracle Java 8 for Solaris 10.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.security;

    import java.util.*;

    /**
     * Implements a registry of all abstract resource plugins. The registry is
     * an array of entries, one entry per plugin.
     * <p>
     * Registry entry holds the following information about an abstract resource:
     * <ul>
     *   <li>plugin registration index;
     *   <li>reference to an instance of the plugin;
     *   <li>abstract resource type name;
     *   <li>reference to the array of Descriptions for the access rights;
     *   <li>reference to the array of access control lists;
     *   <li>reference to the map of open searches;
     *   <li>reference to the library of exports;
     *   <li>reference to the defined audit targets.
     * </ul>
     */
    class ResourceRegistry
    {
       /** plugin registration index */
       private int index = -1;

       /** reference to the instance of the abstract resource plugin */
       private AbstractResource plugin = null;

       /** abstract resource type name */
       private String typeName = null;

       /** array of descriptors of rights items */
       private Description[] rightsItems = null;

       /** array of ACLs defined for this resource */
       private AccessControlList[] acls = null;

       /** map of open searches */
       private Map openSearches = null;

       /** map of open searches when checking permissions for other users */
       private Map openSearchesBySubject = null;

       /** audit targets defined for this plugin */
       private AuditTarget target = null;

       /**
        * Package private constructor.
        *
        * @param index
        *        registration index assigned to this registry entry
        *
        * @param plugin
        *        instance of the abstract resource plugin
        * @throws NoSuchMethodException
        *         in Resolver
        */
       ResourceRegistry(int index, AbstractResource plugin)
       throws NoSuchMethodException
       {
          this.index = index;
          this.plugin = plugin;
          typeName = plugin.getTypeName();
          rightsItems = plugin.describeRights();
          openSearches = new HashMap();
          openSearchesBySubject = new HashMap();
          target = new AuditTarget();
       }

       /**
        * Gets the registration index.
        *
        * @return resource registration index
        */
       int getIndex()
       {
          return index;
       }

       /**
        * Gets the plugin instance.
        *
        * @return registered resource plugin instance
        */
       AbstractResource getPlugin()
       {
          return plugin;
       }

       /**
        * Gets the abstract resource type name.
        *
        * @return registered resource resource type name
        */
       String getTypeName()
       {
          return typeName;
       }

       /**
        * Gets the array of rights descriptors.
        *
        * @return array of rights descriptors
        */
       Description[] getDescription()
       {
          return rightsItems;
       }

       /**
        * Gets the array of ACLs.
        *
        * @return array of ACLs for this resource
        */
       AccessControlList[] getACLs()
       {
          return acls;
       }

       /**
        * Sets the array of ACLs.
        *
        * @param acls
        *        new array of ACLs to be set for this resource
        */
       void setACLs(AccessControlList[] acls)
       {
          this.acls = acls;
       }

       /**
        * Gets the map of open searches.
        *
        * @return map of open searches
        */
       Map getOpenSearches()
       {
          return openSearches;
       }

       /**
        * Gets the map of open searches for the specified subject. If no map
        * exists, it will create one only if the <code>create</code> flag is true.
        *
        * @param   subject
        *          The subject ID for which the open searches are retrieved.
        * @param   create
        *          flag indicating if an empty map should be created if none
        *          exists yet for the specified subject.
        *
        * @return  map of open searches, as for the specified subject
        */
       Map getOpenSearches(String subject, boolean create)
       {
          Map open = null;

          synchronized (openSearchesBySubject)
          {
             open = (Map) openSearchesBySubject.get(subject);

             if (create && open == null)
             {
                open = new HashMap();
                openSearchesBySubject.put(subject, open);
             }
          }

          return open;
       }

       /**
        * Remove the map of the open searches for the specified subject.
        *
        * @param   subject
        *          The subject ID which needs its open searches cleared.
        */
       void removeOpenSearches(String subject)
       {
          synchronized (openSearchesBySubject)
          {
             openSearchesBySubject.remove(subject);
          }
       }

       /**
        * Gets the audit target object.
        *
        * @return <code>AuditTarget</code> object
        */
       AuditTarget getTarget()
       {
          return target;
       }
    }