View Javadoc

1   package net.trajano.twiff.internal;
2   
3   import static net.trajano.twiff.Twiff.CONTAINER_BUILDER_KEY;
4   import static net.trajano.twiff.Twiff.PACKAGE_CONTEXT_KEY;
5   import static net.trajano.twiff.Twiff.TOKEN_FIELD_NAME;
6   import static net.trajano.twiff.internal.TwiffInternal.APP_CONTAINER_KEY;
7   import static net.trajano.twiff.internal.TwiffInternal.SESSION_FACTORY_KEY;
8   import javax.servlet.ServletContext;
9   import net.trajano.twiff.adapter.ServletContextAdapter;
10  import net.trajano.twiff.container.ContainerBuilder;
11  import net.trajano.twiff.internal.util.InitParamUtil;
12  import org.hibernate.SessionFactory;
13  import org.picocontainer.MutablePicoContainer;
14  import org.picocontainer.PicoContainer;
15  
16  /***
17   * This provides access to the servlet context data. Adapters like this are
18   * meant to remove the implicit casting performed by those that use it.
19   * 
20   * @author Archimedes Trajano
21   */
22  public class ServletContextAdapterImpl implements ServletContextAdapter {
23      /***
24       * The application level container.
25       */
26      private final MutablePicoContainer container;
27  
28      /***
29       * The container builder.
30       */
31      private final ContainerBuilder containerBuilder;
32  
33      /***
34       * Hibernate session factory.
35       */
36      private final SessionFactory sessionFactory;
37  
38      /***
39       * Creates the adapter.
40       * 
41       * @param servletContext
42       *                   the context to adapt
43       */
44      public ServletContextAdapterImpl(final ServletContext servletContext) {
45          this.containerBuilder = (ContainerBuilder) servletContext.getAttribute(CONTAINER_BUILDER_KEY);
46          this.container = (MutablePicoContainer) servletContext.getAttribute(APP_CONTAINER_KEY);
47          this.sessionFactory = (SessionFactory) servletContext.getAttribute(SESSION_FACTORY_KEY);
48          this.tokenFieldName = InitParamUtil.getInitParam(servletContext, TOKEN_FIELD_NAME);
49          this.packageContext = InitParamUtil.getInitParam(servletContext, PACKAGE_CONTEXT_KEY);
50      }
51  
52      /***
53       * Token field name.
54       */
55      private final String tokenFieldName;
56  
57      /***
58       * Package context.
59       */
60      privateong> final String packageContext;
61  
62      /***
63       * Returns the {@link PicoContainer} stored in the servlet context. The
64       * container is stored in the attribute
65       * {@link net.trajano.twiff.internal.TwiffInternal#APP_CONTAINER_KEY}.
66       * 
67       * @return the application level container.
68       */
69      public final MutablePicoContainer getContainer() {
70          return container;
71      }
72  
73      /***
74       * Returns the {@link ContainerBuilder} stored in the servlet context. The
75       * builder is stored in the attribute
76       * {@link net.trajano.twiff.Twiff#CONTAINER_BUILDER_KEY}.
77       * 
78       * @return the container builder.
79       */
80      public final ContainerBuilder getContainerBuilder() {
81          return containerBuilder;
82      }
83  
84      /***
85       * This returns the Hibernate session factory. This is stored in the
86       * attribute
87       * {@link net.trajano.twiff.internal.TwiffInternal#SESSION_FACTORY_KEY}.
88       * 
89       * @return the session factory.
90       */
91      public final SessionFactory getSessionFactory() {
92          return sessionFactory;
93      }
94  }