All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.util.GroovyUtil Maven / Gradle / Ivy

/**
 * Copyright (C) 2009  BonitaSoft S.A..
 * BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library 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 Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 **/
package org.ow2.bonita.util;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ow2.bonita.facade.uuid.ProcessDefinitionUUID;
import org.ow2.bonita.runtime.ClassDataLoader;

/**
 * 
 * @author Matthieu Chaffotte
 *
 */
public class GroovyUtil {

  public static String GROOVY_START_DELIMITER = "${";
  public static String GROOVY_END_DELIMITER = "}";

  public static boolean isGroovyExpression(String expression) {
    if (expression == null || "".equals(expression.trim())) {
      return false;
    } else {
      int begin = expression.indexOf(GROOVY_START_DELIMITER);
      int end = expression.indexOf(GROOVY_END_DELIMITER);
      if (begin == -1 || begin >= end) {
        return false;
      }
    }
    return true;
  }

  public static Object evaluate(String expression, Map variables) throws GroovyException {
	  return evaluate(expression, variables, null);
  }

  public static Object evaluate(String expression, Map variables, ProcessDefinitionUUID processUUID) throws GroovyException {
    if (expression == null || "".equals(expression.trim())) {
      throw new GroovyException("The expression is null or empty");
    } else {
      int begin = expression.indexOf(GROOVY_START_DELIMITER);
      int end = expression.indexOf(GROOVY_END_DELIMITER);
      if (begin >= end) {
        throw new GroovyException("The expression is not a Groovy one");
      }
    }
    ClassLoader ori = null;
    try {
    	if (processUUID != null) {
    	  ori = Thread.currentThread().getContextClassLoader();
    	  ClassLoader processClassLoader = ClassDataLoader.getProcessClassLoader(processUUID);
          Thread.currentThread().setContextClassLoader(processClassLoader);
    	}
      if (isJustAGroovyExpression(expression)) {
        expression = expression.replace(GROOVY_START_DELIMITER, "");
        expression = expression.replace(GROOVY_END_DELIMITER, "");
        return evaluateGroovyExpression(expression, variables); 
      } else {
        return evaluate(getExpressions(expression), variables);
      }
    } catch (Exception e) {
      throw new GroovyException(e.getMessage());
    } finally {
    	if (ori != null) {
    	  Thread.currentThread().setContextClassLoader(ori);
    	}
    }
  }

  private static int getExpressionEndIndex(String expression) {
  	int open = 0;
  	char[] characters = expression.toCharArray();
  	for (int i = 1; i < characters.length; i++) {
  		if (characters[i] == '{') {
  			open++;
  		} else if (characters[i] == '}') {
  			open --;
  		}
  	  if (open == 0) {
    		return i+1;
  		}
		}
  	return -1;
  }

  private static boolean isJustAGroovyExpression(String expression) {
  	return expression.startsWith(GROOVY_START_DELIMITER)
  	  && (getExpressionEndIndex(expression) == expression.length());
  }

  private static Object evaluateGroovyExpression(String expression, Map variables) {
    ClassLoader scriptClassLoader = Thread.currentThread().getContextClassLoader();
    Binding binding = new Binding(variables);
    GroovyShell shell = new GroovyShell(scriptClassLoader, binding);
    return shell.evaluate(expression);
  }
  
  private static String evaluate(List expressions, Map variables) {
    StringBuilder builder = new StringBuilder();
    int i = 0;
    while (i < expressions.size()) {
      String expression = expressions.get(i);
      if (expression.equals(GROOVY_START_DELIMITER)) {
        expression = expressions.get(++i);
        builder.append(evaluateGroovyExpression(expression, variables));
      } else {
        builder.append(expression);
      }
      i++;
    }
    return builder.toString();
  }

  private static List getExpressions(String expression) {
    List expressions = new ArrayList();
    String concat = new String(expression);
    while (concat.contains(GROOVY_START_DELIMITER)) {
      int index = concat.indexOf(GROOVY_START_DELIMITER);
      expressions.add(concat.substring(0, index));
      concat = concat.substring(index);
      expressions.add(GROOVY_START_DELIMITER);
      int endGroovy = getExpressionEndIndex(concat);
      expressions.add(concat.substring(2, endGroovy - 1));
      concat = concat.substring(endGroovy);
    }
    expressions.add(concat);
    return expressions;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy