View Javadoc

1   /*
2    * Copyright 2011 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.fatwire.gst.foundation.groovy.action;
18  
19  import org.apache.commons.lang.StringUtils;
20  
21  import COM.FutureTense.Interfaces.ICS;
22  
23  import com.fatwire.gst.foundation.controller.action.Action;
24  import com.fatwire.gst.foundation.controller.action.ActionLocator;
25  import com.fatwire.gst.foundation.controller.action.Injector;
26  import com.fatwire.gst.foundation.controller.action.support.AbstractActionLocator;
27  import com.fatwire.gst.foundation.groovy.GroovyLoader;
28  
29  /**
30   * @author Dolf Dijkstra
31   * @since Mar 28, 2011
32   */
33  public class GroovyActionLocator extends AbstractActionLocator {
34      private GroovyLoader groovyLoader;
35  
36      public GroovyActionLocator() {
37          super();
38  
39      }
40  
41      public GroovyActionLocator(ActionLocator fallbackActionLocator, Injector injector) {
42          super(fallbackActionLocator, injector);
43      }
44  
45      protected Action doFindAction(final ICS ics, final String name) {
46  
47          Action action = null;
48          action = groovyAction(name);
49  
50          return action;
51      }
52  
53      /**
54       * Factory method for the Action
55       * 
56       * @param name the name of the action
57       * @return the Action
58       * @throws RuntimeException
59       */
60      private Action groovyAction(final String name) {
61  
62          if (StringUtils.isBlank(name))
63              return null;
64          Action action = null;
65          try {
66  
67              final Object o = getGroovyLoader().load(name);
68  
69              if (o instanceof Action) {
70                  action = (Action) o;
71              } else {
72                  if (LOG.isDebugEnabled())
73                      LOG.debug(name + " is not a valid script.");
74              }
75          } catch (final RuntimeException e) {
76              throw e;
77          } catch (final Exception e) {
78              throw new RuntimeException(e);
79          }
80          return action;
81      }
82  
83      /**
84       * @return the groovyLoader
85       */
86      public final GroovyLoader getGroovyLoader() {
87          return groovyLoader;
88      }
89  
90      /**
91       * @param groovyLoader the groovyLoader to set
92       */
93      public final void setGroovyLoader(final GroovyLoader groovyLoader) {
94          this.groovyLoader = groovyLoader;
95      }
96  
97  }