View Javadoc

1   /*
2    * Copyright 2016-2016 Hippo B.V. (http://www.onehippo.com)
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.onehippo.forge.gallerymagick.cms.plugins.gallery.processor;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.hippoecm.frontend.plugin.IPluginContext;
23  import org.hippoecm.frontend.plugin.Plugin;
24  import org.hippoecm.frontend.plugin.config.IPluginConfig;
25  import org.hippoecm.frontend.plugins.gallery.imageutil.ScalingParameters;
26  import org.hippoecm.frontend.plugins.gallery.model.GalleryProcessor;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * CMS Plugin to register the {@link GalleryProcessor} service using Gallery Magick Forge library.
32   */
33  public class MagickCommandGalleryProcessorPlugin extends Plugin {
34  
35      private static final long serialVersionUID = 1L;
36  
37      private static final Logger log = LoggerFactory.getLogger(MagickCommandGalleryProcessorPlugin.class);
38  
39      public static final String MAGICK_IMAGE_PROCESSOR = "magick.image.processor";
40  
41      public MagickCommandGalleryProcessorPlugin(final IPluginContext context, final IPluginConfig config) {
42          super(context, config);
43          final GalleryProcessor processor = createGalleryProcessor(config);
44          final String id = config.getString(GalleryProcessor.GALLERY_PROCESSOR_ID,
45                  GalleryProcessor.DEFAULT_GALLERY_PROCESSOR_ID);
46          context.registerService(processor, id);
47      }
48  
49      protected GalleryProcessor createGalleryProcessor(IPluginConfig config) {
50          final String magickImageProcessor = StringUtils.trim(config.getString(MAGICK_IMAGE_PROCESSOR, null));
51  
52          final Map<String, ScalingParameters> initScalingParametersMap = new HashMap<>();
53  
54          for (IPluginConfig childConfig : config.getPluginConfigSet()) {
55              final String nodeName = StringUtils.substringAfterLast(childConfig.getName(), ".");
56  
57              if (StringUtils.isNotBlank(nodeName)) {
58                  int width = childConfig.getAsInteger("width", 0);
59                  int height = childConfig.getAsInteger("height", 0);
60                  final ScalingParameters parameters = new ScalingParameters(width, height, false);
61                  log.debug("Scaling parameters for {}: {}", nodeName, parameters);
62                  initScalingParametersMap.put(nodeName, parameters);
63              }
64          }
65  
66          return createGalleryProcessor(magickImageProcessor, initScalingParametersMap);
67      }
68  
69      protected GalleryProcessor createGalleryProcessor(final String magickImageProcessor, final Map<String, ScalingParameters> initScalingParametersMap) {
70          return new MagickCommandGalleryProcessor(magickImageProcessor, initScalingParametersMap);
71      }
72  }