View Javadoc
1   /**
2    * This file Copyright (c) 2009-2016 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.imaging.caching;
35  
36  import info.magnolia.cms.beans.config.MIMEMapping;
37  import info.magnolia.cms.beans.runtime.FileProperties;
38  import info.magnolia.cms.core.Content;
39  import info.magnolia.cms.core.HierarchyManager;
40  import info.magnolia.cms.core.NodeData;
41  import info.magnolia.cms.util.ContentUtil;
42  import info.magnolia.cms.util.NodeDataUtil;
43  import info.magnolia.context.MgnlContext;
44  import info.magnolia.imaging.ImageGenerator;
45  import info.magnolia.imaging.ImageStreamer;
46  import info.magnolia.imaging.ImagingException;
47  import info.magnolia.imaging.ParameterProvider;
48  
49  import java.io.ByteArrayInputStream;
50  import java.io.ByteArrayOutputStream;
51  import java.io.IOException;
52  import java.io.InputStream;
53  import java.io.OutputStream;
54  import java.util.Calendar;
55  import java.util.concurrent.ExecutionException;
56  import java.util.concurrent.TimeUnit;
57  import java.util.concurrent.locks.ReentrantLock;
58  
59  import javax.jcr.PropertyType;
60  import javax.jcr.RepositoryException;
61  
62  import org.apache.commons.io.IOUtils;
63  
64  import com.google.common.cache.CacheBuilder;
65  import com.google.common.cache.CacheLoader;
66  import com.google.common.cache.LoadingCache;
67  
68  /**
69   * An ImageStreamer which stores and serves generated images to/from a specific workspace.
70   *
71   * @param <P> type of ParameterProvider's parameter
72   */
73  
74  public class CachingImageStreamer<P> implements ImageStreamer<P> {
75      private static final String GENERATED_IMAGE_PROPERTY = "generated-image";
76  
77      private final HierarchyManager hm;
78      private final CachingStrategy<P> cachingStrategy;
79      private final ImageStreamer<P> delegate;
80  
81      /**
82       * This LoadingCache is the key to understanding how this class works.
83       * By using a LoadingCache, we are essentially locking all requests
84       * coming in for the same image (ImageGenerationJob) except the first one.
85       *
86       * CacheBuilder.build() returns a LoadingCache implemented as such that the
87       * first call to get(K) will generate the value (by calling <V> Function.apply(<K>).
88       * Further calls are blocked until the value is generated, and they all retrieve the same value.
89       *
90       * TODO: make static if we don't use the exact same instance for all threads ?
91       */
92      private final LoadingCache<ImageGenerationJob<P>, NodeData> currentJobs;
93  
94      /**
95       * Despite the currentJobs doing quite a good job at avoiding multiple requests
96       * for the same job, we still need to lock around JCR operations, otherwise multiple
97       * requests end up creating the same cachePath (or parts of it), thus yielding
98       * InvalidItemStateException: "Item cannot be saved because it has been modified externally".
99       * TODO - this is currently static because we *know* ImagingServlet uses a different instance
100      * of CachingImageStreamer for every request. This is not exactly the most elegant.
101      * TODO - see related TODO in currentJobs and info.magnolia.imaging.ImagingServlet#getStreamer
102      */
103     private static final ReentrantLock lock = new ReentrantLock();
104 
105     public CachingImageStreamer(HierarchyManager hm, CachingStrategy<P> cachingStrategy, ImageStreamer<P> delegate) {
106         this.hm = hm;
107         this.cachingStrategy = cachingStrategy;
108         this.delegate = delegate;
109 
110         CacheBuilder<Object, Object> cb = CacheBuilder.newBuilder();
111         this.currentJobs = cb
112                 // entries from the LoadingCache will be removed 500ms after their creation,
113                 // thus unblocking further requests for an equivalent job.
114                 .expireAfterWrite(500, TimeUnit.MILLISECONDS)
115 
116                 // We're (ab)using CacheLoader -- this is NOT the cache. We're merely using it to schedule concurrent image generation jobs.
117                 .build(new CacheLoader<ImageGenerationJob<P>, NodeData>() {
118 
119                     @Override
120                     public NodeData load(ImageGenerationJob<P> job) throws Exception {
121                         try {
122                             return generateAndStore(job.getGenerator(), job.getParams());
123                         } catch (IOException e) {
124                             // the LoadingCache will further wrap these in ExecutionExceptions, and we will, in turn, unwrap them ...
125                             throw new RuntimeException(e);
126                         } catch (ImagingException e) {
127                             // the LoadingCache will further wrap these in ExecutionExceptions, and we will, in turn, unwrap them ...
128                             throw new RuntimeException(e);
129                         }
130                     }
131 
132                 });
133     }
134 
135     @Override
136     public void serveImage(ImageGenerator<ParameterProvider<P>> generator, ParameterProvider<P> params, OutputStream out) throws IOException, ImagingException {
137         NodeData imgProp = fetchFromCache(generator, params);
138         if (imgProp == null) {
139             // image is not in cache or should be regenerated
140             try {
141                 imgProp = currentJobs.get(new ImageGenerationJob<P>(generator, params));
142             } catch (ExecutionException e) {
143                 // thrown if the LoadingCache's Function failed
144                 unwrapRuntimeException(e);
145             }
146         }
147         serve(imgProp, out);
148     }
149 
150     /**
151      * Gets the binary property (NodeData) for the appropriate image, ready to be served,
152      * or null if the image should be regenerated.
153      */
154     protected NodeData fetchFromCache(ImageGenerator<ParameterProvider<P>> generator, ParameterProvider<P> parameterProvider) {
155         final String cachePath = cachingStrategy.getCachePath(generator, parameterProvider);
156         if (cachePath == null) {
157             // the CachingStrategy decided it doesn't want us to cache :(
158             return null;
159         }
160         try {
161             if (!hm.isExist(cachePath)) {
162                 return null;
163             }
164             final Content imageNode = hm.getContent(cachePath);
165             final NodeData nodeData = imageNode.getNodeData(GENERATED_IMAGE_PROPERTY);
166             if (!nodeData.isExist()) {
167                 return null;
168             }
169             InputStream in = null;
170             try {
171                 in = nodeData.getStream();
172             } catch (Exception e) {
173                 // will happen, when stream is not yet stored properly (generateAndStore)
174                 // we prefer this handling over having to lock because of better performance especially with big images
175                 return null;
176             }
177             IOUtils.closeQuietly(in);
178 
179             if (cachingStrategy.shouldRegenerate(nodeData, parameterProvider)) {
180                 return null;
181             }
182             return nodeData;
183         } catch (RepositoryException e) {
184             throw new RuntimeException(e); // TODO
185         }
186     }
187 
188 
189     protected void serve(NodeData binary, OutputStream out) throws IOException {
190         final InputStream in = binary.getStream();
191         if (in == null) {
192             throw new IllegalStateException("Can't get InputStream from " + binary.getHandle());
193         }
194         IOUtils.copy(in, out);
195         IOUtils.closeQuietly(in);
196         IOUtils.closeQuietly(out);
197     }
198 
199     protected NodeData generateAndStore(final ImageGenerator<ParameterProvider<P>> generator, final ParameterProvider<P> parameterProvider) throws IOException, ImagingException {
200         // generate
201         final ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
202         delegate.serveImage(generator, parameterProvider, tempOut);
203 
204         // it's time to lock now, we can only save one node at a time, since we'll be working on the same nodes as other threads
205         lock.lock();
206         try {
207             return MgnlContext.doInSystemContext(new MgnlContext.Op<NodeData, RepositoryException>() {
208                 @Override
209                 public NodeData exec() throws RepositoryException {
210                     HierarchyManager systemHM = MgnlContext.getHierarchyManager(hm.getName());
211                     // create cachePath if needed
212                     final String cachePath = cachingStrategy.getCachePath(generator, parameterProvider);
213                     final Content cacheNode = ContentUtil.createPath(systemHM, cachePath, false);
214                     final NodeData imageData = NodeDataUtil.getOrCreate(cacheNode, GENERATED_IMAGE_PROPERTY, PropertyType.BINARY);
215 
216                     // store generated image
217                     final ByteArrayInputStream tempIn = new ByteArrayInputStream(tempOut.toByteArray());
218                     imageData.setValue(tempIn);
219 
220                     final String formatName = generator.getOutputFormat(parameterProvider).getFormatName();
221                     final String mimeType = MIMEMapping.getMIMEType(formatName);
222                     imageData.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, mimeType);
223                     imageData.setAttribute(FileProperties.PROPERTY_LASTMODIFIED, Calendar.getInstance());
224 
225                     // Update metadata of the cache *after* a succesfull image generation (creationDate has been set when creating
226                     // Since this might be called from a different thread than the actual request, we can't call cacheNode.updateMetaData(), which by default tries to set the authorId by using the current context
227                     cacheNode.getMetaData().setModificationDate();
228 
229                     // finally save it all
230                     systemHM.save();
231                     return imageData;
232                 }
233             });
234         } catch (RepositoryException e) {
235             throw new ImagingException("Can't store rendered image: " + e.getMessage(), e);
236         } finally {
237             lock.unlock();
238         }
239     }
240 
241     /**
242      * Unwrap ExecutionExceptions wrapping a RuntimeException wrapping an ImagingException or IOException,
243      * as thrown by the Function of the computing map.
244      *
245      * @see #currentJobs
246      */
247     private void unwrapRuntimeException(Exception e) throws ImagingException, IOException {
248         final Throwable cause = e.getCause();
249         if (cause instanceof ImagingException) {
250             throw (ImagingException) cause;
251         } else if (cause instanceof IOException) {
252             throw (IOException) cause;
253         } else if (cause instanceof RuntimeException) {
254             unwrapRuntimeException((RuntimeException) cause);
255         } else if (cause == null) {
256             // This really, really, should not happen... but we'll let this exception bubble up
257             throw new IllegalStateException("Unexpected and unhandled exception: " + (e.getMessage() != null ? e.getMessage() : ""), e);
258         } else {
259             // this shouldn't happen either, actually.
260             throw new ImagingException(e.getMessage(), cause);
261         }
262     }
263 }