public virtual void TestConstructUrlsFromClasspath() { FilePath file = new FilePath(testDir, "file"); Assert.True("Create file", file.CreateNewFile()); FilePath dir = new FilePath(testDir, "dir"); Assert.True("Make dir", dir.Mkdir()); FilePath jarsDir = new FilePath(testDir, "jarsdir"); Assert.True("Make jarsDir", jarsDir.Mkdir()); FilePath nonJarFile = new FilePath(jarsDir, "nonjar"); Assert.True("Create non-jar file", nonJarFile.CreateNewFile()); FilePath jarFile = new FilePath(jarsDir, "a.jar"); Assert.True("Create jar file", jarFile.CreateNewFile()); FilePath nofile = new FilePath(testDir, "nofile"); // don't create nofile StringBuilder cp = new StringBuilder(); cp.Append(file.GetAbsolutePath()).Append(FilePath.pathSeparator).Append(dir.GetAbsolutePath ()).Append(FilePath.pathSeparator).Append(jarsDir.GetAbsolutePath() + "/*").Append (FilePath.pathSeparator).Append(nofile.GetAbsolutePath()).Append(FilePath.pathSeparator ).Append(nofile.GetAbsolutePath() + "/*").Append(FilePath.pathSeparator); Uri[] urls = ApplicationClassLoader.ConstructUrlsFromClasspath(cp.ToString()); Assert.Equal(3, urls.Length); Assert.Equal(file.ToURI().ToURL(), urls[0]); Assert.Equal(dir.ToURI().ToURL(), urls[1]); Assert.Equal(jarFile.ToURI().ToURL(), urls[2]); }
public virtual void TestGetResource() { Uri testJar = MakeTestJar().ToURI().ToURL(); ClassLoader currentClassLoader = GetType().GetClassLoader(); ClassLoader appClassloader = new ApplicationClassLoader(new Uri[] { testJar }, currentClassLoader , null); NUnit.Framework.Assert.IsNull("Resource should be null for current classloader", currentClassLoader.GetResourceAsStream("resource.txt")); InputStream @in = appClassloader.GetResourceAsStream("resource.txt"); NUnit.Framework.Assert.IsNotNull("Resource should not be null for app classloader" , @in); Assert.Equal("hello", IOUtils.ToString(@in)); }
/// <summary> /// Creates a classloader based on the environment that was specified by the /// user. /// </summary> /// <remarks> /// Creates a classloader based on the environment that was specified by the /// user. If HADOOP_USE_CLIENT_CLASSLOADER is specified, it creates an /// application classloader that provides the isolation of the user class space /// from the hadoop classes and their dependencies. It forms a class space for /// the user jar as well as the HADOOP_CLASSPATH. Otherwise, it creates a /// classloader that simply adds the user jar to the classpath. /// </remarks> /// <exception cref="System.UriFormatException"/> private ClassLoader CreateClassLoader(FilePath file, FilePath workDir) { ClassLoader loader; // see if the client classloader is enabled if (UseClientClassLoader()) { StringBuilder sb = new StringBuilder(); sb.Append(workDir + "/").Append(FilePath.pathSeparator).Append(file).Append(FilePath .pathSeparator).Append(workDir + "/classes/").Append(FilePath.pathSeparator).Append (workDir + "/lib/*"); // HADOOP_CLASSPATH is added to the client classpath string hadoopClasspath = GetHadoopClasspath(); if (hadoopClasspath != null && !hadoopClasspath.IsEmpty()) { sb.Append(FilePath.pathSeparator).Append(hadoopClasspath); } string clientClasspath = sb.ToString(); // get the system classes string systemClasses = GetSystemClasses(); IList <string> systemClassesList = systemClasses == null ? null : Arrays.AsList(StringUtils .GetTrimmedStrings(systemClasses)); // create an application classloader that isolates the user classes loader = new ApplicationClassLoader(clientClasspath, GetType().GetClassLoader(), systemClassesList); } else { IList <Uri> classPath = new AList <Uri>(); classPath.AddItem(new FilePath(workDir + "/").ToURI().ToURL()); classPath.AddItem(file.ToURI().ToURL()); classPath.AddItem(new FilePath(workDir, "classes/").ToURI().ToURL()); FilePath[] libs = new FilePath(workDir, "lib").ListFiles(); if (libs != null) { for (int i = 0; i < libs.Length; i++) { classPath.AddItem(libs[i].ToURI().ToURL()); } } // create a normal parent-delegating classloader loader = new URLClassLoader(Collections.ToArray(classPath, new Uri[0])); } return(loader); }
private void TestIsSystemClassInternal(string nestedClass) { NUnit.Framework.Assert.IsFalse(ApplicationClassLoader.IsSystemClass("org.example.Foo" + nestedClass, null)); Assert.True(ApplicationClassLoader.IsSystemClass("org.example.Foo" + nestedClass, Classes("org.example.Foo"))); Assert.True(ApplicationClassLoader.IsSystemClass("/org.example.Foo" + nestedClass, Classes("org.example.Foo"))); Assert.True(ApplicationClassLoader.IsSystemClass("org.example.Foo" + nestedClass, Classes("org.example."))); Assert.True(ApplicationClassLoader.IsSystemClass("net.example.Foo" + nestedClass, Classes("org.example.,net.example."))); NUnit.Framework.Assert.IsFalse(ApplicationClassLoader.IsSystemClass("org.example.Foo" + nestedClass, Classes("-org.example.Foo,org.example."))); Assert.True(ApplicationClassLoader.IsSystemClass("org.example.Bar" + nestedClass, Classes("-org.example.Foo.,org.example."))); NUnit.Framework.Assert.IsFalse(ApplicationClassLoader.IsSystemClass("org.example.Foo" + nestedClass, Classes("org.example.,-org.example.Foo"))); NUnit.Framework.Assert.IsFalse(ApplicationClassLoader.IsSystemClass("org.example.Foo" + nestedClass, Classes("org.example.Foo,-org.example.Foo"))); }