View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.rest.cxf;
20  
21  import java.io.File;
22  import java.lang.management.ManagementFactory;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.net.URLClassLoader;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.List;
29  import java.util.Objects;
30  import java.util.Optional;
31  import java.util.stream.Collectors;
32  import java.util.stream.Stream;
33  import org.apache.commons.lang3.StringUtils;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  import org.springframework.core.env.Environment;
37  import org.springframework.util.ClassUtils;
38  
39  public final class JavaDocUtils {
40  
41      private static final Logger LOG = LoggerFactory.getLogger(JavaDocUtils.class);
42  
43      private static URL toURL(final String classPathEntry) {
44          try {
45              return new File(classPathEntry).toURI().toURL();
46          } catch (MalformedURLException e) {
47              throw new IllegalArgumentException("URL could not be created from '" + classPathEntry + "'", e);
48          }
49      }
50  
51      /**
52       * Inspired by org.springframework.boot.devtools.restart.ChangeableUrls#urlsFromClassLoader
53       *
54       * @param classLoader class loader
55       * @return URLs from underlying class loader
56       */
57      private static URL[] urlsFromClassLoader(final ClassLoader classLoader) {
58          if (classLoader instanceof URLClassLoader) {
59              return ((URLClassLoader) classLoader).getURLs();
60          }
61  
62          return Stream.of(ManagementFactory.getRuntimeMXBean().getClassPath().
63                  split(File.pathSeparator)).
64                  map(JavaDocUtils::toURL).toArray(URL[]::new);
65      }
66  
67      public static Optional<URL[]> getJavaDocURLs() {
68          URL[] urls = urlsFromClassLoader(ClassUtils.getDefaultClassLoader());
69          if (urls == null) {
70              LOG.debug("No classpath URLs found");
71              return Optional.empty();
72          }
73  
74          List<URL> javaDocURLs = new ArrayList<>();
75          for (URL url : urls) {
76              LOG.debug("Processing {}", url.toExternalForm());
77  
78              String filename = StringUtils.substringAfterLast(url.toExternalForm(), "/");
79              if (filename.startsWith("syncope-") && filename.endsWith("-javadoc.jar")) {
80                  javaDocURLs.add(url);
81              }
82          }
83  
84          LOG.debug("JavaDoc Urls found: {}", javaDocURLs);
85          return Optional.of(javaDocURLs.toArray(URL[]::new));
86      }
87  
88      public static Optional<String[]> getJavaDocPaths(final Environment env) {
89          String[] result = null;
90  
91          if (env.containsProperty("javadocPaths")) {
92              result = Objects.requireNonNull(env.getProperty("javadocPaths")).split(",");
93          }
94  
95          LOG.debug("JavaDoc paths found: {}",
96                  result == null ? List.of() : Arrays.stream(result).collect(Collectors.toList()));
97          return Optional.ofNullable(result);
98      }
99  
100     private JavaDocUtils() {
101         // private constructor for static utility class
102     }
103 }