SHARE
TWEET

WasWebJarAssetLocator

giancarloparma Oct 13th, 2017 (edited) 134 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2. import java.net.URL;
  3. import java.util.Enumeration;
  4. import java.util.HashSet;
  5. import java.util.ServiceLoader;
  6. import java.util.Set;
  7. import java.util.SortedMap;
  8. import java.util.TreeMap;
  9. import java.util.regex.Pattern;
  10.  
  11. import org.webjars.WebJarAssetLocator;
  12. import org.webjars.urlprotocols.UrlProtocolHandler;
  13.  
  14. public class WasWebJarAssetLocator extends WebJarAssetLocator {
  15.     // WAS webjars protocol (normally is jar)
  16.     private static final String WEBJARS_PROTOCOL = "wsjar";
  17.    
  18.     // WAS wants resources path trailing slash
  19.     public static final String WEBJARS_PATH_PREFIX = "META-INF/resources/webjars/";
  20.    
  21.     public WasWebJarAssetLocator() {
  22.         super(getWasFullPathIndex(Pattern.compile(".*"),
  23.                 WasWebJarAssetLocator.class.getClassLoader()));
  24.     }
  25.    
  26.     public static SortedMap<String, String> getWasFullPathIndex(
  27.             final Pattern filterExpr, final ClassLoader... classLoaders) {
  28.  
  29.         final Set<String> assetPaths = getWasAssetPaths(filterExpr, classLoaders);
  30.  
  31.         final SortedMap<String, String> assetPathIndex = new TreeMap<String, String>();
  32.         for (final String assetPath : assetPaths) {
  33.             assetPathIndex.put(reverseWasPath(assetPath), assetPath);
  34.         }
  35.  
  36.         return assetPathIndex;
  37.     }
  38.    
  39.     private static Set<String> getWasAssetPaths(final Pattern filterExpr, final ClassLoader... classLoaders) {
  40.         final Set<String> assetPaths = new HashSet<String>();
  41.         final Set<URL> urls = listParentWasURLsWithResource(classLoaders, WEBJARS_PATH_PREFIX);
  42.  
  43.         ServiceLoader<UrlProtocolHandler> urlProtocolHandlers = ServiceLoader.load(UrlProtocolHandler.class);
  44.         for (final URL url : urls) {
  45.             for (UrlProtocolHandler urlProtocolHandler : urlProtocolHandlers) {
  46.                 if (WEBJARS_PROTOCOL.equals(url.getProtocol()) || urlProtocolHandler.accepts(url.getProtocol())) {
  47.                     Set<String> assetPathSet = urlProtocolHandler.getAssetPaths(url, filterExpr, classLoaders);
  48.                     if (assetPathSet != null) {
  49.                         assetPaths.addAll(assetPathSet);
  50.                         break;
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.  
  56.         return assetPaths;
  57.     }
  58.    
  59.     private static String reverseWasPath(String assetPath) {
  60.         final String[] assetPathComponents = assetPath.split("/");
  61.         final StringBuilder reversedAssetPath = new StringBuilder();
  62.         for (int i = assetPathComponents.length - 1; i >= 0; --i) {
  63.             reversedAssetPath.append(assetPathComponents[i]);
  64.             reversedAssetPath.append('/');
  65.         }
  66.        
  67.         return reversedAssetPath.toString();
  68.     }
  69.    
  70.     private static Set<URL> listParentWasURLsWithResource(final ClassLoader[] classLoaders, final String resource) {
  71.         final Set<URL> urls = new HashSet<URL>();
  72.         for (final ClassLoader classLoader : classLoaders) {
  73.             try {
  74.                 final Enumeration<URL> enumeration = classLoader.getResources(resource);
  75.                 while (enumeration.hasMoreElements()) {
  76.                     urls.add(enumeration.nextElement());
  77.                 }
  78.             } catch (IOException e) {
  79.                 throw new RuntimeException(e);
  80.             }
  81.         }
  82.         return urls;
  83.     }
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top