/// <summary>Find a jar that contains a class of the same name, if any.</summary> /// <remarks> /// Find a jar that contains a class of the same name, if any. /// It will return a jar file, even if that is not the first thing /// on the class path that has a class with the same name. /// </remarks> /// <param name="clazz">the class to find.</param> /// <returns>a jar file that contains the class, or null.</returns> /// <exception cref="System.IO.IOException"/> public static string FindContainingJar(Type clazz) { ClassLoader loader = clazz.GetClassLoader(); string classFile = clazz.FullName.ReplaceAll("\\.", "/") + ".class"; try { for (Enumeration <Uri> itr = loader.GetResources(classFile); itr.MoveNext();) { Uri url = itr.Current; if ("jar".Equals(url.Scheme)) { string toReturn = url.AbsolutePath; if (toReturn.StartsWith("file:")) { toReturn = Runtime.Substring(toReturn, "file:".Length); } toReturn = URLDecoder.Decode(toReturn, "UTF-8"); return(toReturn.ReplaceAll("!.*$", string.Empty)); } } } catch (IOException e) { throw new RuntimeException(e); } return(null); }
/// <summary>Returns the full path to the Jar containing the class.</summary> /// <remarks> /// Returns the full path to the Jar containing the class. It always return a /// JAR. /// </remarks> /// <param name="klass">class.</param> /// <returns>path to the Jar containing the class.</returns> public static string GetJar(Type klass) { Preconditions.CheckNotNull(klass, "klass"); ClassLoader loader = klass.GetClassLoader(); if (loader != null) { string class_file = klass.FullName.ReplaceAll("\\.", "/") + ".class"; try { for (IEnumeration itr = loader.GetResources(class_file); itr.MoveNext();) { Uri url = (Uri)itr.Current; string path = url.AbsolutePath; if (path.StartsWith("file:")) { path = Runtime.Substring(path, "file:".Length); } path = URLDecoder.Decode(path, "UTF-8"); if ("jar".Equals(url.Scheme)) { path = URLDecoder.Decode(path, "UTF-8"); return(path.ReplaceAll("!.*$", string.Empty)); } else { if ("file".Equals(url.Scheme)) { string klassName = klass.FullName; klassName = klassName.Replace(".", "/") + ".class"; path = Runtime.Substring(path, 0, path.Length - klassName.Length); FilePath baseDir = new FilePath(path); FilePath testDir = new FilePath(Runtime.GetProperty("test.build.dir", "target/test-dir" )); testDir = testDir.GetAbsoluteFile(); if (!testDir.Exists()) { testDir.Mkdirs(); } FilePath tempJar = FilePath.CreateTempFile("hadoop-", string.Empty, testDir); tempJar = new FilePath(tempJar.GetAbsolutePath() + ".jar"); CreateJar(baseDir, tempJar); return(tempJar.GetAbsolutePath()); } } } } catch (IOException e) { throw new RuntimeException(e); } } return(null); }
internal virtual bool HasNextService() { if (NextName != null) { return(true); } if (Configs == null) { try { String fullName = PREFIX + Service.Name; if (Loader == null) { Configs = ClassLoader.GetSystemResources(fullName); } else { Configs = Loader.GetResources(fullName); } } catch (IOException x) { Fail(Service, "Error locating configuration files", x); } } while ((Pending == null) || !Pending.MoveNext()) { //JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops: if (!Configs.hasMoreElements()) { return(false); } //JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops: Pending = outerInstance.Parse(Service, Configs.nextElement()); } //JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops: NextName = Pending.next(); return(true); }