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.groovy.context;
17  
18  import groovy.lang.GroovyClassLoader;
19  
20  import javax.servlet.ServletContext;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  
24  import com.fatwire.gst.foundation.controller.AppContext;
25  import com.fatwire.gst.foundation.controller.action.ActionLocator;
26  import com.fatwire.gst.foundation.controller.action.Factory;
27  import com.fatwire.gst.foundation.controller.action.Injector;
28  import com.fatwire.gst.foundation.controller.action.support.ClassActionLocator;
29  import com.fatwire.gst.foundation.controller.action.support.DefaultWebAppContext;
30  import com.fatwire.gst.foundation.controller.action.support.RenderPageActionLocator;
31  import com.fatwire.gst.foundation.controller.support.WebAppContextLoader;
32  import com.fatwire.gst.foundation.groovy.GroovyElementCatalogLoader;
33  import com.fatwire.gst.foundation.groovy.GroovyLoader;
34  import com.fatwire.gst.foundation.groovy.action.GroovyActionLocator;
35  
36  /**
37   * WebAppContext that is using Groovy to load Actions.
38   * 
39   * @author Dolf Dijkstra
40   * @since 11 mei 2012
41   * 
42   */
43  public class GroovyWebContext extends DefaultWebAppContext {
44      private GroovyClassLoader classLoader;
45  
46      /**
47       * This constructor is needed for the {@link WebAppContextLoader}.
48       * 
49       * @param context
50       * @param app
51       */
52      public GroovyWebContext(ServletContext context, AppContext app) {
53          super(context, app);
54          classLoader = new GroovyClassLoader();
55          String path = getServletContext().getRealPath("/WEB-INF/gsf-groovy");
56          classLoader.addClasspath(path);
57  
58      }
59  
60      /* (non-Javadoc)
61       * @see com.fatwire.gst.foundation.controller.action.support.DefaultWebAppContext#createActionLocator()
62       */
63      public ActionLocator createActionLocator() {
64          // this method is expected to be called only once during the lifecycle
65          // of the web app context, though more often does not need to be a
66          // problem per se.
67  
68          // set up a chain of action locators
69          // at the root level (if everything else fails), return a ActionLocator
70          // that returns a RenderPage
71          Injector injector = getBean("injector", Injector.class);
72          ActionLocator root = getRootActionLocator(injector);
73  
74          GroovyLoader loader = getGroovyLoader();
75          // next, set the groovy action loader
76          GroovyActionLocator groovyLocator = new GroovyActionLocator(root, injector);
77          groovyLocator.setGroovyLoader(loader);
78          // and at last the class:<classname> loader
79          final ClassActionLocator cal = new ClassActionLocator(groovyLocator, injector);
80          return cal;
81  
82      }
83  
84      /**
85       * @return GroovyLoader that looks at elementcatalog and /WEB-INF/gsf-groovy for groovy classes.
86       */
87      protected GroovyLoader getGroovyLoader() {
88          return new GroovyElementCatalogLoader(getServletContext());
89      }
90  
91      protected ActionLocator getRootActionLocator(Injector injector) {
92          return new RenderPageActionLocator(injector);
93      }
94  
95      @Override
96      public Factory getFactory(ICS ics) {
97          Factory base = super.getFactory(ics);
98          GroovyFactory fg = new GroovyFactory(ics, getClassLoader(), base);
99          return fg;
100     }
101 
102     public ClassLoader getClassLoader() {
103         return classLoader;
104     }
105 
106 }