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  package com.fatwire.gst.foundation.controller.action.support;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.logging.Log;
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.facade.logging.LogUtil;
27  
28  /**
29   * ActionLocator that loads actions based on a naming convention; if the action name is prefixed with <tt>class:</tt>
30   * and the rest of the action name is a java class that implements the Action interface , an Action from this class will be created.
31   
32   * 
33   * @author Dolf Dijkstra
34   *
35   */
36  public class ClassActionLocator extends AbstractActionLocator {
37      private static final Log LOG = LogUtil.getLog(ClassActionLocator.class);
38      private static final String CLASS_PREFIX = "class:";
39  
40      public ClassActionLocator(ActionLocator fallbackActionLocator, Injector injector) {
41          super(fallbackActionLocator, injector);
42  
43      }
44  
45      public ClassActionLocator() {
46          super();
47      }
48  
49      @Override
50      protected Action doFindAction(ICS ics, String name) {
51          if (StringUtils.startsWith(name, CLASS_PREFIX)) {
52              String c = StringUtils.trim(StringUtils.substringAfter(name, CLASS_PREFIX));
53              if (StringUtils.isEmpty(c)) {
54                  LOG.warn("Passed in classname with the " + CLASS_PREFIX + " is null or empty.");
55              } else {
56                  try {
57                      Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(c);
58                      if (Action.class.isAssignableFrom(clazz)) {
59                          LOG.debug("Creating Action for class " + clazz);
60                          Action action = (Action) clazz.newInstance();
61                          return action;
62                      } else {
63                          throw new RuntimeException("Class " + c + " is not an Action.");
64                      }
65  
66                  } catch (ClassNotFoundException e) {
67                      throw new RuntimeException("Class " + c + " cannot be found.", e);
68                  } catch (InstantiationException e) {
69                      throw new RuntimeException("Class " + c + " " + e.getMessage(), e);
70                  } catch (IllegalAccessException e) {
71                      throw new RuntimeException("Class " + c + " " + e.getMessage(), e);
72                  }
73              }
74  
75          }
76          return null;
77      }
78  
79  }