public static String findContainingJar(Class<?> clazz, String fileNamePattern) {
    ClassLoader loader = clazz.getClassLoader();
    String classFile = clazz.getName().replaceAll("\\.", "/") + ".class";
    try {
      for(final Enumeration<URL> itr = loader.getResources(classFile);
          itr.hasMoreElements();) {
        final URL url = itr.nextElement();
        if ("jar".equals(url.getProtocol())) {
          String toReturn = url.getPath();
          if (fileNamePattern == null || toReturn.matches(fileNamePattern)) {
            toReturn = URLDecoder.decode(toReturn, "UTF-8");
            return toReturn.replaceAll("!.*$", "");
          }
        }
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return null;
  }
@Test
  public void testFindContainingJar() throws Exception {
    String result = TempletonUtils.findContainingJar(Configuration.class, ".*hadoop.*\\.jar.*");
    Assert.assertNotNull(result);
    result = TempletonUtils.findContainingJar(FileSystem.class, ".*hadoop.*\\.jar.*");
    Assert.assertNotNull(result);
    result = TempletonUtils.findContainingJar(HadoopShimsSecure.class, ".*unknownjar.*");
    Assert.assertNull("unexpectedly found jar for HadoopShimsSecure class: " + result, result);
  }