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 COM.FutureTense.Interfaces.ICS;
19  
20  import com.fatwire.gst.foundation.controller.action.Action;
21  import com.fatwire.gst.foundation.controller.action.Injector;
22  
23  /**
24   * Class that provides access to a single action, ie the name is ignored and it
25   * will always return the same new Action, from the actionClass field.
26   * 
27   * @author Dolf Dijkstra
28   * 
29   */
30  public class SingleActionLocator extends AbstractActionLocator {
31  
32      private Class<Action> actionClass;
33  
34      public SingleActionLocator() {
35          super();
36      }
37  
38      public SingleActionLocator(Injector injector, String actionClass) {
39          super(injector);
40          setActionClass(actionClass);
41      }
42  
43      @Override
44      protected Action doFindAction(ICS ics, String name) {
45  
46          try {
47              return actionClass.newInstance();
48          } catch (InstantiationException e) {
49              throw new RuntimeException(e);
50          } catch (IllegalAccessException e) {
51              throw new RuntimeException(e);
52          }
53      }
54  
55      public Class<Action> getActionClass() {
56          return actionClass;
57      }
58  
59      @SuppressWarnings("unchecked")
60      public void setActionClass(String actionClass) {
61          try {
62              this.actionClass = (Class<Action>) Thread.currentThread().getContextClassLoader().loadClass(actionClass);
63              this.actionClass.newInstance(); // test if this class can be
64                                              // instantiated
65          } catch (ClassNotFoundException e) {
66              throw new RuntimeException(e.getMessage());
67          } catch (InstantiationException e) {
68              throw new RuntimeException(e);
69          } catch (IllegalAccessException e) {
70              throw new RuntimeException(e);
71          }
72      }
73  
74  }