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.config;
17  
18  import COM.FutureTense.Interfaces.ICS;
19  
20  import javax.servlet.ServletContext;
21  
22  /**
23   * Utility class for working with the factory producer and factories.
24   *
25   * @author Tony Field
26   * @since 2016-08-06
27   */
28  public final class FactoryLocator {
29      private FactoryLocator() {
30      }
31  
32      /**
33       * Convenience method for locating the factory producer that resides in the servlet context.
34       * Never returns null.
35       *
36       * @param servletContext the servlet context for this application
37       * @return the factory producer, never null
38       */
39      public static FactoryProducer locateFactoryProducer(ServletContext servletContext) {
40          Object o = servletContext.getAttribute(ServletContextLoader.GSF_FACTORY_PRODUCER);
41          if (o instanceof FactoryProducer) {
42              return (FactoryProducer) o;
43          } else {
44              throw new IllegalStateException("No factory producer found");
45          }
46      }
47  
48      /**
49       * Convenience method for locating the factory producer that resides in the servlet context.
50       * Never returns null.
51       *
52       * @param ics the ics object (which of course holds a pointer to the servlet context)
53       * @return the factory producer, never null
54       */
55      public static FactoryProducer locateFactoryProducer(ICS ics) {
56          ServletContext servletContext = ics.getIServlet().getServlet().getServletContext();
57          return locateFactoryProducer(servletContext);
58      }
59  
60      /**
61       * Convenience method to locate the factory for the ICS scope.
62       *
63       * @param ics ics context
64       * @return the factory for the ics scope. Never null
65       */
66      public static Factory locateFactory(ICS ics) {
67          return locateFactoryProducer(ics).getFactory(ics);
68      }
69  }