View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.iiif.image.impl;
20  
21  import java.awt.image.BufferedImage;
22  import java.lang.reflect.Constructor;
23  import java.lang.reflect.InvocationTargetException;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.mycore.access.MCRAccessException;
28  import org.mycore.common.MCRException;
29  import org.mycore.common.config.MCRConfiguration2;
30  import org.mycore.common.config.MCRConfigurationException;
31  import org.mycore.iiif.image.model.MCRIIIFImageInformation;
32  import org.mycore.iiif.image.model.MCRIIIFImageProfile;
33  import org.mycore.iiif.image.model.MCRIIIFImageQuality;
34  import org.mycore.iiif.image.model.MCRIIIFImageSourceRegion;
35  import org.mycore.iiif.image.model.MCRIIIFImageTargetRotation;
36  import org.mycore.iiif.image.model.MCRIIIFImageTargetSize;
37  
38  public abstract class MCRIIIFImageImpl {
39  
40      private static final String MCR_IIIF_IMAGE_CONFIG_PREFIX = "MCR.IIIFImage.";
41  
42      private static final Map<String, MCRIIIFImageImpl> IMPLHOLDER = new HashMap<>();
43  
44      private final String implName;
45  
46      public MCRIIIFImageImpl(final String implName) {
47          this.implName = implName;
48      }
49  
50      public static synchronized MCRIIIFImageImpl getInstance(String implNameParameter) {
51          String implName = (implNameParameter == null || implNameParameter.isBlank())
52              ? MCRConfiguration2.getStringOrThrow("MCR.IIIFImage.Default")
53              : implNameParameter;
54  
55          if (IMPLHOLDER.containsKey(implName)) {
56              return IMPLHOLDER.get(implName);
57          }
58  
59          String classPropertyName = MCR_IIIF_IMAGE_CONFIG_PREFIX + implName;
60          Class<? extends MCRIIIFImageImpl> classObject = MCRConfiguration2.<MCRIIIFImageImpl>getClass(classPropertyName)
61              .orElseThrow(() -> MCRConfiguration2.createConfigurationException(classPropertyName));
62  
63          try {
64              Constructor<? extends MCRIIIFImageImpl> constructor = classObject.getConstructor(String.class);
65              MCRIIIFImageImpl imageImpl = constructor.newInstance(implName);
66              IMPLHOLDER.put(implName, imageImpl);
67              return imageImpl;
68          } catch (NoSuchMethodException e) {
69              throw new MCRConfigurationException(
70                  "Configurated class (" + classObject.getName() + ") needs a string constructor: " + classPropertyName);
71          } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
72              throw new MCRException(e);
73          }
74      }
75  
76      public String getImplName() {
77          return implName;
78      }
79  
80      protected final Map<String, String> getProperties() {
81          return MCRConfiguration2.getSubPropertiesMap(getConfigPrefix());
82      }
83  
84      protected String getConfigPrefix() {
85          return MCR_IIIF_IMAGE_CONFIG_PREFIX + implName + ".";
86      }
87  
88      public abstract BufferedImage provide(String identifier,
89          MCRIIIFImageSourceRegion region,
90          MCRIIIFImageTargetSize targetSize,
91          MCRIIIFImageTargetRotation rotation,
92          MCRIIIFImageQuality imageQuality,
93          String format)
94          throws MCRIIIFImageNotFoundException, MCRIIIFImageProvidingException, MCRIIIFUnsupportedFormatException,
95          MCRAccessException;
96  
97      public abstract MCRIIIFImageInformation getInformation(String identifier)
98          throws MCRIIIFImageNotFoundException, MCRIIIFImageProvidingException, MCRAccessException;
99  
100     public abstract MCRIIIFImageProfile getProfile();
101 
102 }