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.AppContext;
21  import com.fatwire.gst.foundation.controller.action.Factory;
22  import com.fatwire.gst.foundation.controller.action.FactoryProducer;
23  import com.fatwire.gst.foundation.controller.support.WebContextUtil;
24  
25  /**
26   * Helper class to access the ICS based services through the {@link Factory}.
27   * 
28   * @author Dolf Dijkstra
29   * @since 28 August 2012 (for some history: <a
30   *        href="http://en.wikipedia.org/wiki/Siege_of_Groningen">28 August</a>.)
31   * 
32   */
33  public class IcsFactoryUtil {
34  
35      /**
36       * Method to access the Factory.
37       * 
38       * @param ics the current ICS.
39       * @return the Factory as found on the configured AppContext.
40       */
41      public static Factory getFactory(ICS ics) {
42          final Object o = ics.GetObj(Factory.class.getName());
43          if (o instanceof Factory) {
44              return (Factory) o;
45          }
46          Factory factory = null;
47          @SuppressWarnings("deprecation")
48          AppContext ctx = WebContextUtil.getWebAppContext(ics.getIServlet().getServlet().getServletContext());
49  
50          FactoryProducer fp = ctx.getBean("factoryProducder", FactoryProducer.class);
51          if (fp != null) {
52              factory = fp.getFactory(ics);
53              ics.SetObj(Factory.class.getName(), factory);
54          }
55          if (factory == null)
56              throw new IllegalStateException(
57                      "Could not find a FactoryProducer. Is the WebAppContext correctly configured. We found a "
58                              + (ctx == null ? "null" : ctx.getClass().getName()) + " AppContext.");
59          return factory;
60  
61      }
62  }