View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.properties.spi.config.support;
17  
18  import java.io.IOException;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.util.Collections;
22  import java.util.Enumeration;
23  import java.util.LinkedList;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * Utiliy methody regarding Classloader URLs.
30   */
31  public final class UrlUtil
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    /**
38     * Reference to the logger for this class.
39     */
40    private static final Logger LOG = LoggerFactory.getLogger(UrlUtil.class);
41  
42    // --- members --------------------------------------------------------------
43  
44    // ****************************** Initializer *******************************
45  
46    // ****************************** Constructors ******************************
47  
48    // ****************************** Inner Classes *****************************
49  
50    // ********************************* Methods ********************************
51  
52    // --- init -----------------------------------------------------------------
53  
54    // --- get&set --------------------------------------------------------------
55  
56    // --- business -------------------------------------------------------------
57  
58    // --- object basics --------------------------------------------------------
59  
60    /**
61     * Get all resourceURLs of the given class loader.
62     *
63     * @param classLoader the class loader from which all resource URLs shall be
64     *          fetched.
65     *
66     * @return the enumeration of resource URLs of the given class loader.
67     * @throws IOException when the URL can not be retrieved.
68     */
69    public static Enumeration<URL> getResourceUrlsFromFolder(
70        final ClassLoader classLoader, final String resourceFolderName)
71      throws IOException
72    {
73      return getResourceUrls(classLoader, resourceFolderName.endsWith("/")
74          ? resourceFolderName : resourceFolderName + "/");
75    }
76  
77    /**
78     * Get all resourceURLs of the given class loader.
79     *
80     * @param classLoader the class loader from which all resource URLs shall be
81     *          fetched.
82     * @return the enumeration of resource URLs of the given class loader.
83     * @throws IOException when the URL can not be retrieved.
84     */
85    public static Enumeration<URL> getResourceUrls(final ClassLoader classLoader,
86        final String resourceName) throws IOException
87    {
88      final Enumeration<URL> rootUrls = classLoader.getResources(resourceName);
89      final LinkedList<URL> rootResourceUrls = new LinkedList<URL>();
90      while (rootUrls.hasMoreElements())
91      {
92        final URL current = rootUrls.nextElement();
93        rootResourceUrls.add(shortenUrl(current, resourceName));
94      }
95      return Collections.enumeration(rootResourceUrls);
96    }
97  
98    public static URL shortenUrl(final URL current, final String resourceName)
99      throws MalformedURLException
100   {
101     final String path = current.getPath();
102     String strippedPath =
103         path.substring(0, path.length() - resourceName.length());
104     strippedPath = ensureEndsWith(strippedPath, "/");
105     final URL stripped;
106     try
107     {
108       if ("jar".equals(current.getProtocol()))
109       {
110         stripped =
111             new URL("jar:" + new URL(current, strippedPath).toExternalForm());
112       }
113       else
114       {
115         stripped = new URL(current, strippedPath);
116       }
117     }
118     catch (final MalformedURLException e)
119     {
120       LOG.warn("Cannot use URL '{}' in its truncated form '{}'.", current,
121           strippedPath);
122       return current;
123     }
124     return stripped;
125   }
126 
127   private static String ensureEndsWith(final String inputString,
128       final String endsWith)
129   {
130     String ensuredString = inputString;
131     if (!ensuredString.endsWith(endsWith))
132     {
133       ensuredString += endsWith;
134     }
135     return ensuredString;
136   }
137 }