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.common;
20  
21  import java.nio.file.Files;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  import java.util.Optional;
25  
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  import org.mycore.common.config.MCRConfiguration2;
29  
30  /**
31   * @author Sebastian Hofmann
32   */
33  public class MCRDeveloperTools {
34  
35      private static final Logger LOGGER = LogManager.getLogger();
36  
37      /**
38       * @return true if any override is defined
39       */
40      public static boolean overrideActive() {
41          return MCRConfiguration2.getString("MCR.Developer.Resource.Override").isPresent();
42      }
43  
44      /**
45       * Reads the property <code>MCR.Developer.Resource.Override</code> and checks if any of the containing paths
46       * contains the path parameter.
47       * @param path the resource to override
48       * @param webResource if true META-INF/resources will be appended to the paths in the property
49       * @return the path to new file
50       */
51      public static Optional<Path> getOverriddenFilePath(String path, boolean webResource) {
52          if (overrideActive()) {
53              final String[] pathParts = path.split("/");
54  
55              return MCRConfiguration2
56                  .getOrThrow("MCR.Developer.Resource.Override", MCRConfiguration2::splitValue)
57                  .map(Paths::get)
58                  .map(p -> webResource ? p.resolve("META-INF").resolve("resources") : p)
59                  .map(p -> MCRUtils.safeResolve(p, pathParts))
60                  .filter(Files::exists)
61                  .peek(p -> LOGGER.debug("Found overridden Resource: {}", p.toAbsolutePath().toString()))
62                  .findFirst();
63          }
64          return Optional.empty();
65      }
66  }