001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.oozie.util; 019 020 import java.util.Enumeration; 021 import java.net.URL; 022 import java.net.URLDecoder; 023 import java.io.IOException; 024 025 /** 026 * Class utilities. 027 */ 028 public class ClassUtils { 029 030 /** 031 * Return the path to the JAR file in the classpath containing the specified class. <p/> This method has been 032 * canibalized from Hadoop's JobConf class. 033 * 034 * @param clazz class to find its JAR file. 035 * @return the JAR file of the class. 036 */ 037 public static String findContainingJar(Class clazz) { 038 ClassLoader loader = clazz.getClassLoader(); 039 String class_file = clazz.getName().replaceAll("\\.", "/") + ".class"; 040 try { 041 for (Enumeration itr = loader.getResources(class_file); itr.hasMoreElements();) { 042 URL url = (URL) itr.nextElement(); 043 if ("jar".equals(url.getProtocol())) { 044 String toReturn = url.getPath(); 045 if (toReturn.startsWith("file:")) { 046 toReturn = toReturn.substring("file:".length()); 047 } 048 toReturn = URLDecoder.decode(toReturn, "UTF-8"); 049 return toReturn.replaceAll("!.*$", ""); 050 } 051 } 052 } 053 catch (IOException e) { 054 throw new RuntimeException(e); 055 } 056 return null; 057 } 058 059 }