ShimBridgingClassloader extends ClassLoader implements BundleReference { @Override protected Class<?> findClass( String name ) throws ClassNotFoundException { int lastIndexOfDot = name.lastIndexOf( '.' ); final String packageName; final String translatedPath; final String translatedName; if ( lastIndexOfDot >= 0 ) { packageName = name.substring( 0, lastIndexOfDot ); if ( getPackage( packageName ) == null ) { definePackage( packageName, null, null, null, null, null, null, null ); } translatedPath = "/" + packageName.replace( '.', '/' ); translatedName = name.substring( lastIndexOfDot + 1 ) + ".class"; } else { packageName = ""; translatedPath = "/"; translatedName = name; } if ( getPackage( packageName ) == null ) { definePackage( packageName, null, null, null, null, null, null, null ); } List<URL> entries = bundleWiring.findEntries( translatedPath, translatedName, 0 ); if ( entries.size() == 1 ) { byte[] bytes; try ( ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() ) { IOUtils.copy( entries.get( 0 ).openStream(), byteArrayOutputStream ); bytes = byteArrayOutputStream.toByteArray(); } catch ( IOException e ) { throw new ClassNotFoundException( "Unable to define class", e ); } return defineClass( name, bytes, 0, bytes.length ); } throw new ClassNotFoundException(); } ShimBridgingClassloader( ClassLoader parentClassLoader, BundleContext bundleContext ); static Object create( BundleContext bundleContext, String className, List<Object> arguments ); @Override URL getResource( String name ); @Override Class<?> loadClass( String name, boolean resolve ); @Override Bundle getBundle(); static final String HADOOP_SPOON_PLUGIN; }
@Test( expected = ClassNotFoundException.class ) public void testFindFailReading() throws ClassNotFoundException, IOException, KettleFileException { FileObject myFile = KettleVFS.getFileObject( "ram: try ( FileObject fileObject = myFile ) { when( bundleWiring.findEntries( "/" + ShimBridgingClassloader.class.getPackage().getName().replace( ".", "/" ), ShimBridgingClassloader.class.getSimpleName() + ".class", 0 ) ) .thenReturn( Arrays.asList( fileObject.getURL() ) ); shimBridgingClassloader.findClass( ShimBridgingClassloader.class.getCanonicalName() ); } finally { myFile.delete(); } }
@Test( expected = ClassNotFoundException.class ) public void testFindNotFound() throws ClassNotFoundException { when( bundleWiring.findEntries( "/" + ShimBridgingClassloader.class.getPackage().getName().replace( ".", "/" ), ShimBridgingClassloader.class.getSimpleName() + ".class", 0 ) ) .thenReturn( Arrays.<URL>asList() ); shimBridgingClassloader.findClass( ShimBridgingClassloader.class.getCanonicalName() ); }