Package org.apache.imperius.spl.parser.statements.actions.impl

Source Code of org.apache.imperius.spl.parser.statements.actions.impl.InvokePolicyAction

/*
* 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.parser.statements.actions.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.imperius.spl.core.Actuator;
import org.apache.imperius.spl.core.Expression;
import org.apache.imperius.spl.core.InternalClient;
import org.apache.imperius.spl.core.TypeConstants;
import org.apache.imperius.spl.core.TypeInfo;
import org.apache.imperius.spl.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.statements.BasicActionBlock;
import org.apache.imperius.spl.parser.util.InternalClientFactory;
import org.apache.imperius.util.Messages;
import org.apache.imperius.util.SPLLogger;

public class InvokePolicyAction implements BasicActionBlock {

  private List _paramList;

  private String _className;

  private static Logger logger = SPLLogger.getSPLLogger().getLogger();

  public static final int POLICY_EVALUATED_SUCCESSFULLY = 0;
  public static final int POLICY_NOT_EVALUATED = 1;
  public static final int POLICY_EVALUATION_FAILED = -1;

  private static final String sourceClass = "InvokePolicyAction";

  public InvokePolicyAction(List paramList)
  {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "InvokePolicyAction");
    //System.out.println("creating InvokePolicyAction ");

    //      _className = className;
    _paramList = paramList;

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

  }

  public boolean execute(Actuator ac) throws SPLException
  {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "execute");

    Expression _lhsExp = null;
    Expression _rhsExp = null;
    TypeInfo lType = null;

//    try
//    {
      InternalClient internalClientObj = null;
      // System.out.println("parameters List " + _paramList);
      if (_paramList.size() == 0)
      {
        logger
            .severe("number of parameters passed to a InvokePolicyAction should be 2 or more");
        throw new SPLException(Messages.getString(
            "SPL_NO_OF_ARGUMENTS_PASSED_ERROR_MSG", new Object[] {
                "2 or more", "0" }));
      }
      else
      {
        internalClientObj = InternalClientFactory.getInternalClient();
        _lhsExp = (Expression) _paramList.get(0);
        lType = _lhsExp.getType();
        if (lType.getType() != TypeConstants.stringType)
        {
          logger
              .severe("first Expression should be a string describing the name of the policy to invoke");
          throw new SPLException(
              Messages
                  .getString("SPL_INVOKE_POLICY_ACTION_EXCEPTION1_MSG"));
        }
        String policyName = (String) _lhsExp.evaluate();

        if (_paramList.size() > 1)
        {
          // get second parameter
          _rhsExp = (Expression) _paramList.get(1);
          Object rhsResult = _rhsExp.evaluate();
          if ((rhsResult instanceof java.util.List))
          {
            List instanceInfoList = (List) rhsResult;
            internalClientObj.invokePolicy(policyName,
                instanceInfoList, true);
          }
          else
          {
            Map instanceInfoMap = new HashMap();
            Expression tempExpr = null;
            Object keyObj = null;
            Object valueObj = null;
            if (((_paramList.size() - 1) % 2) == 0)
            {
              for (int i = 1; i < _paramList.size(); i++)
              {
                tempExpr = (Expression) _paramList.get(i);
                keyObj = tempExpr.evaluate();
                if (++i < _paramList.size())
                {
                  tempExpr = (Expression) _paramList.get(i);
                  valueObj = tempExpr.evaluate();
                }
                else
                {
                  logger
                      .severe("instance names and instances passed are not same");
                  throw new SPLException(
                      "instance names and instances passed are not same");
                }
                instanceInfoMap.put(keyObj, valueObj);
              }
              internalClientObj.invokePolicy(policyName,
                  instanceInfoMap);
            } else {
              throw new SPLException(
                  "number of parameters passed is insufficient to invoke the policy");
            }
          }
        } else {
          internalClientObj.invokePolicy(policyName, null, true);
        }
      }
      logger.exiting(sourceClass, Thread.currentThread().getName() + " "
          + "execute");
      return true;
//    } catch (SPLException e) {
//      logger.severe(e.getMessage());
//      logger.exiting(sourceClass, Thread.currentThread().getName() + " "
//          + "execute");
//      return false;
//    }
  }

  public String getClassName() {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "getClassName");

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

    return _className;
  }

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

    _className = className;
    logger.exiting(sourceClass, Thread.currentThread().getName() + " "
        + "setClassName");

  }

  public List getParamList() {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "getParamList");

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

    return _paramList;
  }

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

    _paramList = paramList;

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

  }

}
TOP

Related Classes of org.apache.imperius.spl.parser.statements.actions.impl.InvokePolicyAction

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.