|
16 | 16 | * limitations under the License.
|
17 | 17 | */
|
18 | 18 |
|
| 19 | +import javax.naming.Context; |
| 20 | +import javax.naming.directory.DirContext; |
| 21 | +import javax.naming.directory.InitialDirContext; |
| 22 | + |
19 | 23 | import java.net.URL;
|
20 | 24 | import java.net.URLClassLoader;
|
21 | 25 | import java.util.Collections;
|
| 26 | +import java.util.Hashtable; |
22 | 27 | import java.util.concurrent.CountDownLatch;
|
23 | 28 |
|
24 | 29 | import org.codehaus.classworlds.ClassRealmAdapter;
|
@@ -265,6 +270,39 @@ void testParallelDeadlockClassRealm() throws InterruptedException {
|
265 | 270 | }
|
266 | 271 | }
|
267 | 272 |
|
| 273 | + /** |
| 274 | + * Test that JDK internal classes from named modules (like com.sun.jndi.dns.DnsContextFactory) |
| 275 | + * can be properly accessed when loaded through ClassRealm. This is crucial for JNDI to work |
| 276 | + * correctly in Maven plugins. |
| 277 | + * |
| 278 | + * @see <a href="https://github.com/codehaus-plexus/plexus-classworlds/issues/XX">Issue XX</a> |
| 279 | + */ |
| 280 | + @Test |
| 281 | + void testLoadJdkModuleClassThroughJNDI() throws Exception { |
| 282 | + // Use system classloader as base to ensure JDK module classes can be loaded |
| 283 | + ClassRealm realm = new ClassRealm(new ClassWorld(), "test", ClassLoader.getSystemClassLoader()); |
| 284 | + |
| 285 | + // Set the thread's context classloader to the realm |
| 286 | + Thread currentThread = Thread.currentThread(); |
| 287 | + ClassLoader originalClassLoader = currentThread.getContextClassLoader(); |
| 288 | + try { |
| 289 | + currentThread.setContextClassLoader(realm); |
| 290 | + |
| 291 | + // Try to instantiate DnsContextFactory via JNDI with explicit factory class name |
| 292 | + // This is how Netty uses JNDI under Windows |
| 293 | + Hashtable<String, String> env = new Hashtable<>(); |
| 294 | + env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); |
| 295 | + env.put(Context.PROVIDER_URL, "dns:"); |
| 296 | + |
| 297 | + // This should succeed without IllegalAccessException |
| 298 | + DirContext ctx = new InitialDirContext(env); |
| 299 | + assertNotNull(ctx); |
| 300 | + ctx.close(); |
| 301 | + } finally { |
| 302 | + currentThread.setContextClassLoader(originalClassLoader); |
| 303 | + } |
| 304 | + } |
| 305 | + |
268 | 306 | private void doOneDeadlockAttempt() throws InterruptedException {
|
269 | 307 | // Deadlock sample graciously ripped from http://docs.oracle.com/javase/7/docs/technotes/guides/lang/cl-mt.html
|
270 | 308 | final ClassRealm cl1 = new ClassRealm(new ClassWorld(), "cl1", null);
|
|
0 commit comments