View Javadoc

1   package org.apache.torque.generator.template.groovy;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import groovy.lang.Binding;
23  import groovy.lang.GroovyShell;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.torque.generator.GeneratorException;
28  import org.apache.torque.generator.configuration.ConfigurationException;
29  import org.apache.torque.generator.configuration.ConfigurationProvider;
30  import org.apache.torque.generator.control.ControllerState;
31  import org.apache.torque.generator.outlet.OutletResult;
32  import org.apache.torque.generator.qname.QualifiedName;
33  import org.apache.torque.generator.template.TemplateOutletImpl;
34  
35  
36  /**
37   * A Outlet which uses a groovy script for generation.
38   */
39  public class GroovyOutlet extends TemplateOutletImpl
40  {
41      /**
42       * The name under which the Torque generator interface will be put
43       * into the context.
44       */
45      public static final String TORQUE_GEN_CONTEXT_NAME = "torqueGen";
46  
47      /** The log. */
48      private static Log log = LogFactory.getLog(GroovyOutlet.class);
49  
50      /**
51       * Constructs a new GroovyOutlet.
52       *
53       * @param name the name of this outlet, not null.
54       * @param configurationProvider the provider for reading the templates,
55       *        not null.
56       * @param path the path to the templates, not null.
57       *
58       * @throws NullPointerException if name, path or directories are null.
59       * @throws ConfigurationException if an error occurs while reading the
60       *         template.
61       */
62      public GroovyOutlet(
63              QualifiedName name,
64              ConfigurationProvider configurationProvider,
65              String path)
66          throws ConfigurationException
67      {
68          super(name,
69                configurationProvider,
70                path,
71                null,
72                null);
73      }
74  
75      /**
76       * Executes the generation process; the result is returned.
77       *
78       * @param controllerState the current controller state.
79       *
80       * @return the result of the generation, not null.
81       *
82       * @see org.apache.torque.generator.outlet.Outlet#execute(ControllerState)
83       */
84      @Override
85      public OutletResult execute(ControllerState controllerState)
86          throws GeneratorException
87  
88      {
89          if (log.isDebugEnabled())
90          {
91              log.debug("Start executing GroovyOutlet " + getName());
92          }
93  
94          try
95          {
96              Binding binding = new Binding();
97              GroovyShell shell = new GroovyShell(binding);
98  
99              String result = (String) shell.evaluate(
100                     getContent(controllerState));
101             return new OutletResult(result);
102         }
103         finally
104         {
105             if (log.isDebugEnabled())
106             {
107                 log.debug("End executing GroovyOutlet " + getName());
108             }
109         }
110     }
111 }