| 
 1 | 
  
 |  | 
| 
 2 | 
  
 |  | 
| 
 3 | 
  
 |  | 
| 
 4 | 
  
 |  | 
| 
 5 | 
  
 |  | 
| 
 6 | 
  
 |  | 
| 
 7 | 
  
 |  | 
| 
 8 | 
  
 |  | 
| 
 9 | 
  
 |  | 
| 
 10 | 
  
 |  | 
| 
 11 | 
  
 |  | 
| 
 12 | 
  
 |  | 
| 
 13 | 
  
 |  | 
| 
 14 | 
  
 |  | 
| 
 15 | 
  
 | package org.apache.hivemind.lib.groovy; | 
| 
 16 | 
  
 |  | 
| 
 17 | 
  
 | import groovy.lang.Binding; | 
| 
 18 | 
  
 | import groovy.lang.GroovyCodeSource; | 
| 
 19 | 
  
 | import groovy.lang.GroovyShell; | 
| 
 20 | 
  
 | import groovy.lang.Script; | 
| 
 21 | 
  
 |  | 
| 
 22 | 
  
 | import java.io.IOException; | 
| 
 23 | 
  
 |  | 
| 
 24 | 
  
 | import javax.xml.parsers.SAXParser; | 
| 
 25 | 
  
 |  | 
| 
 26 | 
  
 | import org.apache.hivemind.ApplicationRuntimeException; | 
| 
 27 | 
  
 | import org.apache.hivemind.ClassResolver; | 
| 
 28 | 
  
 | import org.apache.hivemind.ErrorHandler; | 
| 
 29 | 
  
 | import org.apache.hivemind.Resource; | 
| 
 30 | 
  
 | import org.apache.hivemind.parse.DescriptorParser; | 
| 
 31 | 
  
 | import org.apache.hivemind.parse.ModuleDescriptor; | 
| 
 32 | 
  
 | import org.apache.hivemind.parse.XmlResourceProcessor; | 
| 
 33 | 
  
 | import org.xml.sax.SAXException; | 
| 
 34 | 
  
 |  | 
| 
 35 | 
  
 |  | 
| 
 36 | 
  
 |  | 
| 
 37 | 
  
 |  | 
| 
 38 | 
  
 |  | 
| 
 39 | 
  
 |  | 
| 
 40 | 
  
 |  | 
| 
 41 | 
  
 |  | 
| 
 42 | 
  
 |  | 
| 
 43 | 
  
 | class GroovyScriptProcessor extends XmlResourceProcessor | 
| 
 44 | 
  
 | { | 
| 
 45 | 
  
 |     private GroovyShell _groovyShell; | 
| 
 46 | 
  
 |  | 
| 
 47 | 
 2
 |     public GroovyScriptProcessor(ClassResolver resolver, ErrorHandler errorHandler)
 | 
| 
 48 | 
  
 |     { | 
| 
 49 | 
 2
 |         super(resolver, errorHandler);
 | 
| 
 50 | 
  
 |     } | 
| 
 51 | 
  
 |  | 
| 
 52 | 
 3
 |     protected ModuleDescriptor parseResource(Resource resource, SAXParser parser,
 | 
| 
 53 | 
  
 |             DescriptorParser contentHandler) throws SAXException, IOException | 
| 
 54 | 
  
 |     { | 
| 
 55 | 
 3
 |         HiveMindBuilder builder = new HiveMindBuilder(contentHandler);
 | 
| 
 56 | 
  
 |  | 
| 
 57 | 
 3
 |         GroovyCodeSource source = new GroovyCodeSource(resource.getResourceURL());
 | 
| 
 58 | 
 3
 |         Script script;
 | 
| 
 59 | 
  
 |  | 
| 
 60 | 
 3
 |         try
 | 
| 
 61 | 
  
 |         { | 
| 
 62 | 
 3
 |             script = getGroovyShell().parse(source);
 | 
| 
 63 | 
  
 |         } | 
| 
 64 | 
  
 |         catch (Exception e) | 
| 
 65 | 
  
 |         { | 
| 
 66 | 
 0
 |             throw new ApplicationRuntimeException(e);
 | 
| 
 67 | 
  
 |         } | 
| 
 68 | 
  
 |  | 
| 
 69 | 
 3
 |         Binding processorBinding = new Binding();
 | 
| 
 70 | 
 3
 |         processorBinding.setVariable("processor", builder);
 | 
| 
 71 | 
  
 |  | 
| 
 72 | 
 3
 |         script.setBinding(processorBinding);
 | 
| 
 73 | 
  
 |  | 
| 
 74 | 
 3
 |         script.run();
 | 
| 
 75 | 
  
 |  | 
| 
 76 | 
 3
 |         return contentHandler.getModuleDescriptor();
 | 
| 
 77 | 
  
 |     } | 
| 
 78 | 
  
 |  | 
| 
 79 | 
 3
 |     private GroovyShell getGroovyShell()
 | 
| 
 80 | 
  
 |     { | 
| 
 81 | 
 3
 |         if (_groovyShell == null)
 | 
| 
 82 | 
 2
 |             _groovyShell = new GroovyShell(_resolver.getClassLoader());
 | 
| 
 83 | 
  
 |  | 
| 
 84 | 
 3
 |         return _groovyShell;
 | 
| 
 85 | 
  
 |     } | 
| 
 86 | 
  
 | } |