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 javax.servlet.ServletContext;
19  
20  import COM.FutureTense.Interfaces.ICS;
21  
22  import com.fatwire.gst.foundation.controller.AppContext;
23  import com.fatwire.gst.foundation.controller.action.ActionLocator;
24  import com.fatwire.gst.foundation.controller.action.ActionNameResolver;
25  import com.fatwire.gst.foundation.controller.action.Factory;
26  import com.fatwire.gst.foundation.controller.action.FactoryProducer;
27  import com.fatwire.gst.foundation.controller.action.Injector;
28  import com.fatwire.gst.foundation.controller.support.WebAppContext;
29  
30  /**
31   * This is the WebAppContext with accessors to the ActionLocator,
32   * ActionNameResolver and the Injector, with the companion FactoryProducer.
33   * <p/>
34   * Developer are expected to subclass this class for their own implementations.
35   * In most cases they would only like to override {@link #getFactory(ICS)} for
36   * their own Service factory.
37   * 
38   * @author Dolf Dijkstra
39   * 
40   */
41  public class DefaultWebAppContext extends WebAppContext implements FactoryProducer {
42  
43      public DefaultWebAppContext(final ServletContext context) {
44          super(context);
45  
46      }
47  
48      public DefaultWebAppContext(final ServletContext context, final AppContext parent) {
49          super(context, parent);
50  
51      }
52  
53      public ActionLocator createActionLocator() {
54          // this method is expected to be called only once during the lifecycle
55          // of the WebAppContext, though more often does not need to be a
56          // problem per se.
57          final Injector injector = createInjector();
58          final ActionLocator root = new RenderPageActionLocator(injector);
59          final ClassActionLocator cal = new ClassActionLocator(root, injector);
60          return cal;
61  
62      }
63  
64      public ActionNameResolver createActionNameResolver() {
65          return new CommandActionNameResolver("action");
66      }
67  
68      public Injector createInjector() {
69          FactoryProducer fp = getBean("factoryProducer", FactoryProducer.class);
70          return new DefaultAnnotationInjector(fp);
71      }
72  
73      public FactoryProducer createFactoryProducer() {
74          return this;
75      }
76  
77      @Override
78      public Factory getFactory(final ICS ics) {
79          // called very often; once per request/pagelet, scoped per ICS context
80          return new IcsBackedObjectFactoryTemplate(ics);
81      }
82  
83  }