ResourceBundleClassLoader extends ClassLoader { @Override public URL getResource(String name) { Enumeration<URL> resourceFiles = this.bundle.findEntries(this.path, this.filePattern, true); List<URL> allResources = new LinkedList<>(); if (resourceFiles != null) { while (resourceFiles.hasMoreElements()) { URL resourceURL = resourceFiles.nextElement(); String resourcePath = resourceURL.getFile(); File resourceFile = new File(resourcePath); String resourceFileName = resourceFile.getName(); if (resourceFileName.equals(name)) { allResources.add(resourceURL); } } } if (allResources.isEmpty()) { return null; } if (allResources.size() == 1) { return allResources.get(0); } for (URL url : allResources) { boolean isHostResource = bundle.getEntry(url.getPath()) != null && bundle.getEntry(url.getPath()).equals(url); if (isHostResource) { continue; } return url; } return null; } ResourceBundleClassLoader(Bundle bundle, String path, String filePattern); @Override URL getResource(String name); @Override InputStream getResourceAsStream(String name);  }
@Test public void testName() throws Exception { Path tmp = Files.createTempDirectory("tmp"); URL hostPropertiesURL = createTmpTestPropetiesFile(tmp, "host/OH-INF/i18n/test.properties"); URL fragmentPropertiesURL = createTmpTestPropetiesFile(tmp, "fragment/OH-INF/i18n/test.properties"); Bundle bundleMock = mock(Bundle.class); when(bundleMock.findEntries(any(), any(), anyBoolean())) .thenAnswer(answer -> Collections.enumeration(List.of(hostPropertiesURL, fragmentPropertiesURL))); when(bundleMock.getEntry(any())).thenReturn(hostPropertiesURL); ResourceBundleClassLoader classloader = new ResourceBundleClassLoader(bundleMock, "/OH-INF/i18n", "*.properties"); List<URL> propertiesURLs = Collections.list(bundleMock.findEntries("/OH-INF/i18n", "*.properties", true)); assertEquals(2, propertiesURLs.size()); assertTrue(propertiesURLs.contains(hostPropertiesURL)); assertTrue(propertiesURLs.contains(fragmentPropertiesURL)); assertEquals(hostPropertiesURL, bundleMock.getEntry(null)); assertEquals(hostPropertiesURL, bundleMock.getEntry("always-return-hostPropertiesURL")); URL resource = classloader.getResource("test.properties"); assertEquals(fragmentPropertiesURL, resource); }