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   * @author erichechinger
58   * @version $Revision: $ ($Author: $)
59   */
60  public class ExecuteGroovyFileTask extends AbstractTask {
61  
62      // Init Global
63      private static final Logger log = LoggerFactory.getLogger(ExecuteGroovyFileTask.class);
64  
65      private MgnlGroovyConsole console;
66  
67      private String inputFile;
68  
69      public ExecuteGroovyFileTask(String taskName, String taskDescription, String inputFile) {
70          super(taskName, taskDescription);
71          this.inputFile = inputFile;
72      }
73  
74      // @Override
75      @Override
76      public void execute(InstallContext installContext) throws TaskExecutionException {
77          try {
78              // Init
79              console = new MgnlGroovyConsole(new Binding());
80              // Run the GroovyFile against the Groovy Console
81              executeGroovyScript(installContext);
82          }
83          catch (Exception e) {
84              throw new TaskExecutionException("Could not execute the groovy File: " + e.getMessage(), e);
85          }
86      }
87  
88      private void executeGroovyScript(InstallContext installContext) throws Exception {
89  
90          // Check
91          if (this.inputFile == null) {
92              throw new IOException("Input File is Null");
93          }
94  
95          // First Check
96          URL inFile = ClasspathResourcesUtil.getResource(this.inputFile);
97          if (inFile == null) {
98              throw new IOException("Can't find resource file at " + this.inputFile);
99          }
100         // Get Input Stream
101         final InputStream inputStream = ClasspathResourcesUtil.getResource(this.inputFile).openStream();
102         if (inputStream == null) {
103             throw new IOException("Can't find resource file at " + inFile.getFile());
104         }
105 
106         Object lastResult = null;
107         StringWriter outputStream = new StringWriter();
108         try {
109             lastResult = console.evaluate(inputStream, (new File(inFile.getFile())).getName(), outputStream);
110             // Shows the result of the evaluated code
111             if (lastResult != null) {
112                 outputStream.getBuffer().append(lastResult);
113             }
114             outputStream.flush();
115             installContext.info(outputStream.toString());
116         }
117         catch (GroovyRuntimeException e) {
118             log.error("Got a Groovy Execution Error", e);
119             throw e;
120         }
121         finally {
122             if (outputStream != null) {
123                 outputStream.close();
124             }
125             if (inputStream != null) {
126                 inputStream.close();
127             }
128         }
129 
130     }
131 
132 }