View Javadoc

1   /**
2    * This file Copyright (c) 2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.objectfactory.guice;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.context.WebContext;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpSession;
41  
42  import com.google.inject.Key;
43  import com.google.inject.OutOfScopeException;
44  import com.google.inject.Provider;
45  import com.google.inject.Scope;
46  import com.google.inject.Scopes;
47  
48  /**
49   * Servlet scopes that use WebContext to get request and session.
50   *
51   * @version $Id$
52   */
53  public class MagnoliaScopes {
54  
55      private MagnoliaScopes() {
56      }
57  
58      public static final Scope LAZY_SINGLETON = new LazySingletonScope();
59  
60      /**
61       * HTTP servlet request scope.
62       */
63      public static final Scope LOCAL = new LocalScope();
64  
65      /**
66       * HTTP session scope.
67       */
68      public static final Scope SESSION = new SessionScope();
69  
70      /**
71       * Scope for lazy singletons.
72       *
73       * @version $Id$
74       * @see info.magnolia.objectfactory.annotation.LazySingleton
75       */
76      public static class LazySingletonScope implements Scope {
77  
78          @Override
79          public <T> Provider<T> scope(Key<T> key, Provider<T> creator) {
80              return Scopes.SINGLETON.scope(key, creator);
81          }
82  
83          @Override
84          public String toString() {
85              return "MagnoliaScopes.LAZY_SINGLETON";
86          }
87      }
88  
89      /**
90       * A sentinel attribute value representing null.
91       */
92      enum NullObject {
93          INSTANCE
94      }
95  
96      /**
97       * Scope for object local to the current request.
98       *
99       * @version $Id$
100      * @see info.magnolia.objectfactory.annotation.LocalScoped
101      */
102     public static class LocalScope implements Scope {
103 
104         @Override
105         public <T> Provider<T> scope(Key<T> key, final Provider<T> creator) {
106             final String name = key.toString();
107             return new Provider<T>() {
108                 @Override
109                 public T get() {
110 
111                     HttpServletRequest request = getRequest();
112 
113                     if (request == null) {
114                         return null;
115                     }
116 
117                     synchronized (request) {
118                         Object obj = request.getAttribute(name);
119                         if (NullObject.INSTANCE == obj) {
120                             return null;
121                         }
122                         @SuppressWarnings("unchecked")
123                         T t = (T) obj;
124                         if (t == null) {
125                             t = creator.get();
126                             request.setAttribute(name, (t != null) ? t : NullObject.INSTANCE);
127                         }
128                         return t;
129                     }
130                 }
131 
132                 @Override
133                 public String toString() {
134                     return String.format("%s[%s]", creator, LOCAL);
135                 }
136             };
137         }
138 
139         @Override
140         public String toString() {
141             return "MagnoliaScopes.LOCAL";
142         }
143     }
144 
145     /**
146      * Scope for object local to the current session.
147      *
148      * @version $Id$
149      * @see info.magnolia.objectfactory.annotation.SessionScoped
150      */
151     public static class SessionScope implements Scope {
152 
153         @Override
154         public <T> Provider<T> scope(Key<T> key, final Provider<T> creator) {
155             final String name = key.toString();
156             return new Provider<T>() {
157                 @Override
158                 public T get() {
159 
160                     HttpSession session = getSession();
161 
162                     synchronized (session) {
163                         Object obj = session.getAttribute(name);
164                         if (NullObject.INSTANCE == obj) {
165                             return null;
166                         }
167                         @SuppressWarnings("unchecked")
168                         T t = (T) obj;
169                         if (t == null) {
170                             t = creator.get();
171                             session.setAttribute(name, (t != null) ? t : NullObject.INSTANCE);
172                         }
173                         return t;
174                     }
175                 }
176 
177                 @Override
178                 public String toString() {
179                     return String.format("%s[%s]", creator, SESSION);
180                 }
181             };
182         }
183 
184         @Override
185         public String toString() {
186             return "MagnoliaScopes.SESSION";
187         }
188     }
189 
190     private static HttpServletRequest getRequest() {
191         WebContext webContext;
192         try {
193             webContext = MgnlContext.getWebContextOrNull();
194             // when injecting request to objects outside of the request scope (e.g. on destroy)
195             if (webContext == null) {
196                 return null;
197             }
198         } catch (IllegalStateException e) {
199             throw new OutOfScopeException("Cannot access scoped object." +
200                     " MgnlContext does not have a WebContext set, this is most likely" +
201                     " because we are not currently processing a HTTP request.", e);
202         }
203         HttpServletRequest request = webContext.getRequest();
204         if (request == null) {
205             throw new OutOfScopeException("Cannot access scoped object." +
206                     " MgnlContext does not have a HttpServletRequest set, this is most likely" +
207             " because we are not currently processing a HTTP request.");
208         }
209         return request;
210     }
211 
212     private static HttpSession getSession() {
213         try {
214             return getRequest().getSession();
215         } catch (IllegalStateException e) {
216             throw new OutOfScopeException("Cannot access scoped object." +
217                     " A session is not available and a new one could not be created," +
218                     " likely because the response has already been committed.", e);
219         }
220     }
221 }