# Resolver.java

    /*
    ** Module   : Resolver.java
    ** Abstract : Implements symbol resolver for the access rights expressions.
    **
    **
    ** Copyright (c) 2005-2023, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- -T- --JPRM-- ----------------Description-----------------
    ** 001 NVS 20050323 NEW   @20471 Created the initial version.  This class
    **                               implements a symbol (variables and functions)
    **                               resolver for the access rights expressions
    **                               evaluator.
    ** 002 NVS 20050524 CHG   @21270 Reimplemented to comply with the new require-
    **                               ments set forth by the new Expression Engine.
    ** 003 GES 20060305 CHG   @24895 Added constant resolution for debug levels.
    ** 004 TJD 20220504              Java 11 compatibility minor changes
    ** 005 GBB 20230512              Logging methods replaced by CentralLogger/ConversionStatus.
    */
    /*
    ** 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.*;
    import java.util.logging.*;
    import java.lang.reflect.*;
    import com.goldencode.expr.*;

    /**
     * This class implements a symbol (variables and functions) resolver
     * for the access rights expressions evaluator.
     * <p>
     * The resolver operates multiple scopes which are organized in two nesting
     * levels. The outer scope belongs to the Security Manager and is a singleton.
     * The inner scopes belong to the resource plugins. All inner scopes have the
     * common parent (are siblings). For any expression evaluation, there is only
     * one active inner scope for the specific plugin, plus the outer scope for
     * the Security Manager.
     * <p>
     * All scopes are named. This allows the full access to any symbol in either
     * plugin's or Security Manager's scope even if they are named identically.
     * The Security Manager's scope is named "security", whereas plugins scopes
     * are named after the resource type name they control: "system", "directory",
     * "net" etc.
     */
    class Resolver
    extends SymbolResolver
    {
       /** Constants pool. */
       private static final Map constants = new HashMap();

       /** The current expression's scope. */
       private Scope scope = null;

       static
       {
          constants.put("error",   Level.SEVERE);
          constants.put("warning", Level.WARNING);
          constants.put("stat",    Level.INFO);
          constants.put("list",    Level.FINE);
          constants.put("trace",   Level.FINER);
          constants.put("data",    Level.FINEST);
       }

       /**
        * Default constructor.
        * Registers the Security Manager's library automatically.
        */
       Resolver()
       {
          super();
          registerLibrary(SecurityManager.getInstance(),
                          "security",
                          new VariablePool());
       }

       /**
        * Resolves the specified string constant to a literal object. At this
        * time, only security manager debug levels are resolved.
        *
        * @param   qualifier
        *          An optional qualifier for concrete implementations which must
        *          support multiple namespaces. A qualifier is not mandatory for
        *          constants, so this parameter may be <code>null</code>.
        * @param   constant
        *          The string constant to be resolved to a literal.
        *
        * @return  The object represented by the constant <code>symbol</code>, or
        *          <code>null</code> if the symbol is not recognized.
        */
       protected Object resolveConstant(String qualifier, String constant)
       {
          Integer i   = null;
          Level   lvl = (Level) constants.get(constant);

          if (lvl != null)
          {
             i = Integer.valueOf(lvl.intValue());
          }

          return i;
       }

       /**
        * Makes decision about validity of a method call in an expression.
        * <p>
        * The logic implemented here is very simple: this method returns true
        * (allows method use) only if it was registered with a library.
        *
        * @param target
        *        instance of a class referenced in the expression
        * @param method
        *        method of the class referenced in the expression
        * @param registered
        *        <code>true</code> if the referenced target class has been
        *        registered as a library and method is exported from the library
        *
        * @return <code>true</code> if method use is allowed
        */
       public boolean checkPermission(Class target,
                                      Method method,
                                      boolean registered)
       {
          // TODO: not done yet!
          return registered;
       }

       /**
        * Gets the current scope.
        * <p>
        * The current scope is always the scope of the plugin who requested
        * expression evaluation. That scope is kept in the "scope" member.
        *
        * @return <code>Scope</code> of the plugin submitted the expression
        */
       public Scope getCurrentScope()
       {
          return scope;
       }

       /**
        * Sets the current scope.
        * <p>
        * Security Manager should call this method to set the current scope to
        * the plugin that submitted the expression.
        *
        * @param scope
        *        scope of the plugin evaluating the expression
        */
       void setCurrentScope(Scope scope)
       {
          this.scope = scope;
       }
    }