PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jira3-script-plugin/src/main/java/com/google/code/jira/script/workflow/ScriptWorkflowUtils.java

http://jira-script-plugin.googlecode.com/
Java | 121 lines | 91 code | 27 blank | 3 comment | 10 complexity | 416db3859651d4d8a3e35e26e4392693 MD5 | raw file
  1. package com.google.code.jira.script.workflow;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.script.Invocable;
  6. import javax.script.ScriptEngine;
  7. import javax.script.ScriptEngineFactory;
  8. import javax.script.ScriptEngineManager;
  9. import javax.script.ScriptException;
  10. import org.apache.log4j.Category;
  11. import com.atlassian.core.user.UserUtils;
  12. import com.atlassian.jira.issue.Issue;
  13. import com.opensymphony.user.EntityNotFoundException;
  14. import com.opensymphony.user.User;
  15. import com.opensymphony.workflow.WorkflowContext;
  16. import com.opensymphony.workflow.WorkflowException;
  17. public class ScriptWorkflowUtils {
  18. public static final String ISSUE_KEY = "issue";
  19. public static final String ORIGINAL_ISSUE_KEY = "originalissueobject";
  20. public static final String CONTEXT_KEY = "context";
  21. public static final String SCRIPT_NAME = "scriptName";
  22. public static final String ENGINE_NAME = "engineName";
  23. public static final String SCRIPT_CONTENT = "scriptContent";
  24. public static final Issue getIssue(Map transientArgs) {
  25. return (Issue) transientArgs.get(ISSUE_KEY);
  26. }
  27. public static final Issue getOriginalIssue(Map transientArgs) {
  28. return (Issue) transientArgs.get(ORIGINAL_ISSUE_KEY);
  29. }
  30. public static final User getCaller(Map transientArgs) {
  31. WorkflowContext context = (WorkflowContext) transientArgs.get(CONTEXT_KEY);
  32. String username = context.getCaller();
  33. try {
  34. return UserUtils.getUser(username);
  35. } catch (EntityNotFoundException e) {
  36. return null;
  37. }
  38. }
  39. public static String getScriptName(Map args) {
  40. return (String) args.get(SCRIPT_NAME);
  41. }
  42. public static String getEngineName(Map args) {
  43. return (String) args.get(ENGINE_NAME);
  44. }
  45. public static String getScriptContent(Map args) {
  46. return (String) args.get(SCRIPT_CONTENT);
  47. }
  48. public static ScriptEngine getEngine(Map args) throws WorkflowException {
  49. String engineName = getEngineName(args);
  50. if (engineName == null) {
  51. throw new WorkflowException("Missing parameter: engine name.");
  52. }
  53. // Loads the engine
  54. ScriptEngineManager manager = new ScriptEngineManager();
  55. List factories = manager.getEngineFactories();
  56. if (factories == null || factories.size() == 0) {
  57. throw new WorkflowException("No engines found! Please check your JVM version and configuration.");
  58. }
  59. ScriptEngine engine = null;
  60. for (Iterator iterator = factories.iterator(); iterator.hasNext();) {
  61. ScriptEngineFactory factory = (ScriptEngineFactory) iterator.next();
  62. if (engineName.equals(factory.getEngineName())) {
  63. engine = factory.getScriptEngine();
  64. break;
  65. }
  66. }
  67. if (engine == null) {
  68. throw new WorkflowException("No script engine found associate with the name " + engineName + ".");
  69. }
  70. return engine;
  71. }
  72. public static Object executeScript(Map transientVars, Map args, Category log) throws WorkflowException {
  73. // Obtains the issue and the caller
  74. Issue issue = getIssue(transientVars);
  75. Issue originalIssue = getOriginalIssue(transientVars);
  76. User caller = getCaller(transientVars);
  77. // Obtains the script engine and the script content
  78. ScriptEngine engine = getEngine(args);
  79. String scriptContent = getScriptContent(args);
  80. try {
  81. engine.eval(scriptContent);
  82. engine.put("issue", issue);
  83. engine.put("originalissue", originalIssue);
  84. engine.put("caller", caller);
  85. engine.put("log", log);
  86. Invocable invocable = (Invocable) engine;
  87. return invocable.invokeFunction("execute", null);
  88. } catch (ScriptException e) {
  89. throw new WorkflowException("Error evaluating script.", e);
  90. } catch (NoSuchMethodException e) {
  91. throw new WorkflowException("The method execute() has not been found.", e);
  92. }
  93. }
  94. }