View Javadoc

1   /**
2    * This file Copyright (c) 2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.groovy.task;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.io.StringWriter;
40  import java.net.URL;
41  
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import groovy.lang.Binding;
46  import groovy.lang.GroovyRuntimeException;
47  import info.magnolia.cms.util.ClasspathResourcesUtil;
48  import info.magnolia.module.InstallContext;
49  import info.magnolia.module.delta.AbstractTask;
50  import info.magnolia.module.delta.TaskExecutionException;
51  import info.magnolia.module.groovy.console.MgnlGroovyConsole;
52  
53  
54  /**
55   * This Task execute a groovy file against the groovy console. File is define as a String.
56   *
57   * @version $Id$
58   */
59  public class ExecuteGroovyFileTask extends AbstractTask {
60  
61      // Init Global
62      private static final Logger log = LoggerFactory.getLogger(ExecuteGroovyFileTask.class);
63  
64      private MgnlGroovyConsole console;
65  
66      private String inputFile;
67  
68      public ExecuteGroovyFileTask(String taskName, String taskDescription, String inputFile) {
69          super(taskName, taskDescription);
70          this.inputFile = inputFile;
71      }
72  
73      @Override
74      public void execute(InstallContext installContext) throws TaskExecutionException {
75          try {
76              // Init
77              console = new MgnlGroovyConsole(new Binding());
78              // Run the GroovyFile against the Groovy Console
79              executeGroovyScript(installContext);
80          }
81          catch (Exception e) {
82              throw new TaskExecutionException("Could not execute the groovy File '" + inputFile +"': " + e.getMessage(), e);
83          }
84      }
85  
86      private void executeGroovyScript(InstallContext installContext) throws Exception {
87  
88          // Check
89          if (this.inputFile == null) {
90              throw new IOException("Input File is Null");
91          }
92  
93          // First Check
94          URL inFile = ClasspathResourcesUtil.getResource(this.inputFile);
95          if (inFile == null) {
96              throw new IOException("Can't find resource file at " + this.inputFile);
97          }
98          // Get Input Stream
99          final InputStream inputStream = ClasspathResourcesUtil.getResource(this.inputFile).openStream();
100         if (inputStream == null) {
101             throw new IOException("Can't find resource file at " + inFile.getFile());
102         }
103 
104         Object lastResult = null;
105         StringWriter outputStream = new StringWriter();
106         try {
107             lastResult = console.evaluate(inputStream, (new File(inFile.getFile())).getName(), outputStream);
108             // Shows the result of the evaluated code
109             if (lastResult != null) {
110                 outputStream.getBuffer().append(lastResult);
111             }
112             outputStream.flush();
113             installContext.info(outputStream.toString());
114         }
115         catch (GroovyRuntimeException e) {
116             log.error("Got a Groovy Execution Error", e);
117             throw e;
118         }
119         finally {
120             outputStream.close();
121             inputStream.close();
122         }
123     }
124 }