View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts.processor;
20  
21  import groovy.lang.Binding;
22  import groovy.lang.GroovyShell;
23  import groovy.lang.Script;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.InputStream;
27  import java.io.Writer;
28  import java.util.Map;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.springframework.extensions.surf.core.scripts.ScriptException;
33  import org.springframework.extensions.webscripts.ScriptContent;
34  import org.springframework.extensions.webscripts.processor.AbstractScriptProcessor;
35  
36  /**
37   * @author muzquiano
38   */
39  public class GroovyScriptProcessor extends AbstractScriptProcessor
40  {
41  	private static final Log logger = LogFactory.getLog(GroovyScriptProcessor.class);
42  	    
43      /* (non-Javadoc)
44       * @see org.alfresco.processor.Processor#getExtension()
45       */
46      public String getExtension()
47      {
48          return "groovy";
49      }
50  
51      /* (non-Javadoc)
52       * @see org.alfresco.processor.Processor#getName()
53       */
54      public String getName()
55      {
56          return "groovy";
57      }
58      
59      /**
60       * Executes the Groovy script
61       * 
62       * @param is        the input stream
63       * @param out       the writer.  This can be null if no output is required.
64       * @param model     the context model for the script
65       * @return Object   the return result of the executed script
66       */
67      private Object executeGroovyScript(InputStream is, Writer out, Map<String, Object> model)
68      {
69          try
70          {
71  			GroovyShell shell = new GroovyShell();
72  			Script script = shell.parse(is);
73  			
74  			this.addProcessorModelExtensions(model);
75  			
76  			Binding binding = new Binding(model);
77  			for(String name : processorExtensions.keySet())
78  			{
79  				binding.setProperty(name, processorExtensions.get(name));
80  			}			
81  			binding.setProperty("out", out);
82  			script.setBinding(binding);
83  			
84  			return script.run();
85          }
86          catch (Exception exception)
87          {
88              throw new ScriptException("Error executing groovy script", exception);
89          }
90      }
91  
92      /**
93       * @see org.alfresco.service.cmr.repository.ScriptProcessor#executeString(java.lang.String, java.util.Map)
94       */
95      public Object executeGroovyString(String script, Map<String, Object> model)
96      {
97          return executeGroovyScript(new ByteArrayInputStream(script.getBytes()), null, model);
98      }
99      
100         
101     /* (non-Javadoc)
102      * @see org.alfresco.web.scripts.processor.AbstractScriptProcessor#init()
103      */
104     public void init()
105     {
106         super.init();
107         
108         // TODO: other init?
109     }
110     
111     /* (non-Javadoc)
112      * @see org.alfresco.web.scripts.ScriptProcessor#findScript(java.lang.String)
113      */
114     public ScriptContent findScript(String path)
115     {
116         return getScriptLoader().getScript(path);
117     }
118 
119     /* (non-Javadoc)
120      * @see org.alfresco.web.scripts.ScriptProcessor#executeScript(java.lang.String, java.util.Map)
121      */
122     public Object executeScript(String path, Map<String, Object> model)
123     {
124     	ScriptContent scriptContent = findScript(path);
125     	if (scriptContent == null)
126     	{
127     		throw new ScriptException("Unable to locate: " + path);
128     	}
129     	
130     	return executeScript(scriptContent, model);
131     }
132 
133     /* (non-Javadoc)
134      * @see org.alfresco.web.scripts.ScriptProcessor#executeScript(org.alfresco.web.scripts.ScriptContent, java.util.Map)
135      */
136     public Object executeScript(ScriptContent scriptContent, Map<String, Object> model)
137     {
138     	return executeGroovyScript(scriptContent.getInputStream(), null, model);
139     }
140     
141     /* (non-Javadoc)
142      * @see org.alfresco.web.scripts.ScriptProcessor#unwrapValue(java.lang.Object)
143      */
144     public Object unwrapValue(Object value)
145     {
146     	return value;
147     }
148 
149     /* (non-Javadoc)
150      * @see org.alfresco.web.scripts.ScriptProcessor#reset()
151      */
152     public void reset()
153     {
154         init();
155     }
156 }