PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/jira3-script-plugin/src/main/java/com/google/code/jira/script/service/ScriptService.java

http://jira-script-plugin.googlecode.com/
Java | 98 lines | 73 code | 22 blank | 3 comment | 14 complexity | 1f99ecf9c25b28e778f1616fec8d8585 MD5 | raw file
  1. package com.google.code.jira.script.service;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.Reader;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import javax.script.ScriptEngine;
  8. import javax.script.ScriptEngineFactory;
  9. import javax.script.ScriptEngineManager;
  10. import javax.script.ScriptException;
  11. import org.apache.commons.io.IOUtils;
  12. import org.apache.log4j.Category;
  13. import com.atlassian.configurable.ObjectConfiguration;
  14. import com.atlassian.configurable.ObjectConfigurationException;
  15. import com.atlassian.jira.service.AbstractService;
  16. import com.opensymphony.module.propertyset.PropertySet;
  17. public class ScriptService extends AbstractService {
  18. private static final Category log = Category.getInstance(ScriptService.class);
  19. private static final String SERVICE_ID = ScriptService.class.getName();
  20. private static final String SERVICE_XML = "service/scriptservice.xml";
  21. private static final String INPUT_FILE_KEY = "FILE";
  22. private static final String ENGINE_NAME_KEY = "ENGINE";
  23. private String inputFile;
  24. private String engineName;
  25. public ObjectConfiguration getObjectConfiguration() throws ObjectConfigurationException {
  26. return getObjectConfiguration(SERVICE_ID, SERVICE_XML, null);
  27. }
  28. public void init(PropertySet ps) throws ObjectConfigurationException {
  29. super.init(ps);
  30. inputFile = getProperty(INPUT_FILE_KEY);
  31. engineName = getProperty(ENGINE_NAME_KEY);
  32. log.info("init: " + getName() + " - " + engineName + " - " + inputFile);
  33. }
  34. public void run() {
  35. log.info("run: " + getName() + " located at " + inputFile + " using " + engineName + " as engine.");
  36. // Validates "engineName" property
  37. if (engineName == null || "".equals(engineName)) {
  38. log.error("Skip running service because engine name is NULL!");
  39. return;
  40. }
  41. // Validates "inputFile" property
  42. if (inputFile == null || "".equals(inputFile)) {
  43. log.error("Skip running service because input file is NULL!");
  44. return;
  45. }
  46. // Loads the engine
  47. ScriptEngineManager manager = new ScriptEngineManager();
  48. List factories = manager.getEngineFactories();
  49. if (factories == null || factories.size() == 0) {
  50. log.error("No engines found! Please check your JVM version and configuration.");
  51. }
  52. ScriptEngine engine = null;
  53. for (Iterator iterator = factories.iterator(); iterator.hasNext();) {
  54. ScriptEngineFactory factory = (ScriptEngineFactory) iterator.next();
  55. if (engineName.equals(factory.getEngineName())) {
  56. engine = factory.getScriptEngine();
  57. break;
  58. }
  59. }
  60. if (engine == null) {
  61. log.error("No script engine found associate with the name " + engineName + ".");
  62. }
  63. Reader reader = null;
  64. try {
  65. engine.put("log", log);
  66. reader = new FileReader(inputFile);
  67. engine.eval(reader);
  68. } catch (ScriptException e) {
  69. log.error("Error evaluating script.", e);
  70. } catch (IOException e) {
  71. log.error("Error loading file.", e);
  72. } finally {
  73. IOUtils.closeQuietly(reader);
  74. }
  75. }
  76. }