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