View Javadoc
1   /**
2    * This file Copyright (c) 2019 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.context;
35  
36  import info.magnolia.cms.security.User;
37  import info.magnolia.repository.RepositoryManager;
38  
39  import java.util.Locale;
40  import java.util.concurrent.Callable;
41  
42  import javax.inject.Inject;
43  import javax.inject.Provider;
44  import javax.jcr.RepositoryException;
45  import javax.jcr.Session;
46  import javax.security.auth.Subject;
47  
48  import com.machinezoo.noexception.Exceptions;
49  
50  /**
51   * Context usable when original web context might be already released.
52   */
53  public class AsynchronousContext extends AbstractContext {
54  
55      private final User user;
56      private final Subject subject;
57      private final Locale locale;
58  
59      AsynchronousContext(RepositoryManager repositoryManager, Context originalContext) {
60          setAttributeStrategy(new MapAttributeStrategy());
61          setRepositoryStrategy(new DefaultRepositoryStrategy(repositoryManager, null));
62          user = originalContext.getUser();
63          subject = originalContext.getSubject();
64          locale = originalContext.getLocale();
65      }
66  
67      @Override
68      public Session getJCRSession(String workspaceName) throws RepositoryException {
69          return getRepositoryStrategy().getSession(workspaceName);
70      }
71  
72      @Override
73      public void release() {
74          getRepositoryStrategy().release();
75      }
76  
77      @Override
78      public Locale getLocale() {
79          return locale;
80      }
81  
82      @Override
83      public User getUser() {
84          return user;
85      }
86  
87      @Override
88      public Subject getSubject() {
89          return subject;
90      }
91  
92      /**
93       * Wraps {@link java.util.concurrent.Callable} so it can be executed in asynchronous context.
94       * This operation has to be called in a separate thread.
95       */
96      public static class OperationFactory {
97  
98          private final Provider<SystemContext> systemContextProvider;
99          private final RepositoryManager repositoryManager;
100         private final Context originalContext;
101 
102         @Inject
103         public OperationFactory(RepositoryManager repositoryManager, Context originalContext, Provider<SystemContext> systemContextProvider) {
104             this.repositoryManager = repositoryManager;
105             this.systemContextProvider = systemContextProvider;
106             this.originalContext = originalContext;
107         }
108 
109         /**
110          * @param operation operation to be executed asynchronously
111          * @throws java.lang.IllegalStateException if {@link info.magnolia.context.MgnlContext} is already set for this thread.
112          * @return result of the operation. Don't return JCR objects as their asynchronously created session is already released.
113          */
114         public <T> Callable<T> wrap(Callable<T> operation) {
115             return () -> {
116                 if (MgnlContext.hasInstance()) {
117                     throw new IllegalStateException("MgnlContext is already set for this thread. Please run result of info.magnolia.context.AsynchronousContext.OperationFactory#wrap in a separate thread");
118                 }
119                 final AsynchronousContextl#AsynchronousContext">AsynchronousContext context = new AsynchronousContext(repositoryManager, originalContext); //new instance as there could be more parallel threads
120                 MgnlContext.setInstance(context);
121                 try {
122                     return operation.call();
123                 } finally {
124                     systemContextProvider.get().release(); //operation might use system context which is created for thread, we need to release JCR session from this context as well, see info.magnolia.context.JCRSessionPerThreadSystemContext.getRepositoryStrategy
125                     context.release();
126                     MgnlContext.setInstance(null);
127                 }
128             };
129         }
130 
131         public Runnable wrap(Runnable operation) {
132             return Exceptions.wrap().runnable(() -> wrap(() -> {
133                 operation.run();
134                 return null;
135             }).call());
136         }
137     }
138 }