Package org.apache.imperius.spl.evaluator.internal.impl

Source Code of org.apache.imperius.spl.evaluator.internal.impl.PolicyEvaluatorImpl

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License. 
*/

/**
* @author Prashant Baliga <prabalig@in.ibm.com>
*
*/

package org.apache.imperius.spl.evaluator.internal.impl;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.imperius.spl.core.Actuator;
import org.apache.imperius.spl.core.DataCollector;
import org.apache.imperius.spl.evaluator.internal.PolicyEvaluator;
import org.apache.imperius.spl.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.statements.EvaluationResults;
import org.apache.imperius.spl.parser.statements.impl.SPLPolicy;
import org.apache.imperius.spl.parser.util.ActuatorFactory;
import org.apache.imperius.spl.parser.util.DataCollectorFactory;
import org.apache.imperius.util.Messages;
import org.apache.imperius.util.SPLLogger;

public class PolicyEvaluatorImpl implements PolicyEvaluator
{
  private static final String sourceClass = "PolicyEvaluatorImpl";
  private static Logger logger = SPLLogger.getSPLLogger().getLogger();
  private static Logger auditLogger = SPLLogger.getSPLLogger().getAuditLogger();
  private DataCollector _dataCollector;
  private Actuator _actuator;
  public static final int SUCCESS = 1;
  public static final int FAILURE = -1;
  public static final int NOT_EVALUATED = 0;
  private static PolicyEvaluatorImpl singletonObj = null;

  private PolicyEvaluatorImpl()
  {
    init();
  }

  public void init()
  {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "init");

    _dataCollector = DataCollectorFactory.getDataCollector();
    _actuator = ActuatorFactory.getActuator();

    logger.exiting(sourceClass, Thread.currentThread().getName() + " "
        + "init");
  }

  public static PolicyEvaluatorImpl getInstance()
  {
    if (singletonObj == null)
      singletonObj = new PolicyEvaluatorImpl();
    return singletonObj;
  }

  public void shutdown()
  {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "shutdown");

    logger.exiting(sourceClass, Thread.currentThread().getName() + " "
        + "shutdown");
  }

  public Object evaluatePolicy(SPLPolicy cp, Map instances)
      throws SPLException
  {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "evaluatePolicy");
    try
    {
      int result = FAILURE;
      if (instances != null)
      {
        logger.fine(Thread.currentThread().getName() + " instances passed are not null ");

        if (!instances.isEmpty() && instances.size() > 0)
        {
          logger.fine(Thread.currentThread().getName() + " Policy Evaluator calling evaluate method of SPLPolicy");
         
          EvaluationResults er = cp.evaluateForResults(_dataCollector, _actuator, instances);
          result = er.getStatusCode() == SPLPolicy.POLICY_EVALUATED_SUCCESSFULLY ? SUCCESS : FAILURE;
         
          auditLogger.fine(Thread.currentThread().getName() + "\n" + cp.getAuditLogString());
        }
        else
        {
          logger.log(Level.INFO, Messages.getString("SPL_NO_INSTANCES_PASSED_FOR_EVALUATION_MSG"));
        }
      }
      else
      {
        throw new SPLException(
            Messages
                .getString("SPL_NO_INSTANCES_PASSED_FOR_EVALUATION_MSG"));
      }

      logger.fine(Thread.currentThread().getName()
          + " policy evaluation completed");

      logger.exiting(sourceClass, Thread.currentThread().getName() + " "
          + "evaluatePolicy");

      if (result == SUCCESS)
      {
        if (!ReturnObjectStore.hasReturnValues())
        {
          return new Integer(1);
        }
        else
        {
          Object returnObjectList = ReturnObjectStore
              .getReturnValues();

          return returnObjectList;
        }
      }
      else
      {
        return new Integer(FAILURE);
      }
    }
    catch (SPLException e1)
    {
      logger.log(Level.SEVERE, Messages.SPLOF0008E, new Object[] { e1
          .getMessage() });
      throw e1;
    }
    catch (Exception e)
    {
      logger.log(Level.SEVERE, Messages.SPLOF0008E, new Object[]{e.getMessage()});
      if (e instanceof RuntimeException)
          throw (RuntimeException)e;
    }
    return null;
  }
}
TOP

Related Classes of org.apache.imperius.spl.evaluator.internal.impl.PolicyEvaluatorImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.