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,
42  			Injector injector) {
43  		super(fallbackActionLocator, injector);
44  	}
45  
46  	protected Action doFindAction(final ICS ics, final String name) {
47  
48  		Action action = null;
49  		action = groovyAction(ics, name);
50  
51  		return action;
52  	}
53  
54  	/**
55  	 * Factory method for the Action
56  	 * 
57  	 * @param name
58  	 *            the name of the action
59  	 * @return the Action
60  	 * @throws RuntimeException
61  	 */
62  	private Action groovyAction(final ICS ics, final String name) {
63  
64  		if (StringUtils.isBlank(name))
65  			return null;
66  		Action action = null;
67  		try {
68  
69  			final Object o = getGroovyLoader().load(ics, name);
70  
71  			if (o instanceof Action) {
72  				action = (Action) o;
73  			} else {
74  				if (LOG.isDebugEnabled())
75  					LOG.debug(name + " is not a valid script.");
76  			}
77  		} catch (final RuntimeException e) {
78  			throw e;
79  		} catch (final Exception e) {
80  			throw new RuntimeException(e);
81  		}
82  		return action;
83  	}
84  
85  	/**
86  	 * @return the groovyLoader
87  	 */
88  	public final GroovyLoader getGroovyLoader() {
89  		return groovyLoader;
90  	}
91  
92  	/**
93  	 * @param groovyLoader
94  	 *            the groovyLoader to set
95  	 */
96  	public final void setGroovyLoader(final GroovyLoader groovyLoader) {
97  		this.groovyLoader = groovyLoader;
98  	}
99  
100 }