View Javadoc
1   /*
2    * Copyright 2016 Function1. 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 tools.gsf.controller;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import COM.FutureTense.Interfaces.DependenciesAwareModelAndView;
22  import com.fatwire.assetapi.data.BaseController;
23  import tools.gsf.config.Factory;
24  import tools.gsf.config.FactoryLocator;
25  import tools.gsf.config.inject.Injector;
26  import tools.gsf.time.Stopwatch;
27  
28  /**
29   * Extension of Oracle's <code>BaseController</code> that invokes the
30   * {@link Injector} to inject dependencies into itself. Injection is
31   * done in the <code>handleRequest()</code> method. As most implementing
32   * classes of <code>BaseController</code> are meant to override the
33   * <code>doWork(Models models)</code> method, objects will be injected by
34   * the time doWork is executed.
35   *
36   * The injector is configured in the {@link Factory}. Additional injection
37   * capabilities can therefore be added without having to alter this object.
38   *
39   * This class also times the execution of the handleRequest() method using
40   * the {@link Stopwatch} class.
41   */
42  public class InjectingController extends BaseController {
43  
44  	private static final Logger LOG = LoggerFactory.getLogger(InjectingController.class);
45  
46      public DependenciesAwareModelAndView handleRequest() {
47  
48          if (LOG.isDebugEnabled()) {
49          	LOG.debug("These are all the vars available inside InjectingController for the current ICS:");
50  	        java.util.Enumeration allVars = ics.GetVars();
51  	        while (allVars.hasMoreElements()) {
52  	        	String varName = (String) allVars.nextElement();
53  	        	LOG.debug("ICS variable " + varName + " = " + ics.GetVar(varName));
54  	        }
55          }
56  
57          Factory factory = FactoryLocator.locateFactory(ics);
58  
59          Stopwatch stopwatch = factory.getObject("stopwatch", Stopwatch.class);
60          stopwatch.start();
61  
62          Injector injector = factory.getObject("compositeInjector", Injector.class);
63          injector.inject(this);
64          stopwatch.split("InjectingController: injecting into controller {}", this.getClass().getSimpleName());
65  
66          DependenciesAwareModelAndView result = super.handleRequest();
67          stopwatch.elapsed("Executed controller {}", this.getClass().getSimpleName());
68  
69          return result;
70      }
71  }