1 /* 2 * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.IOException; 25 import java.io.OutputStream; 26 import java.net.Socket; 27 import java.net.URI; 28 import java.util.ConcurrentModificationException; 29 import java.util.Hashtable; 30 import javax.naming.Context; 31 import javax.naming.InitialContext; 32 import javax.naming.NamingException; 33 import javax.naming.event.EventContext; 34 import javax.naming.event.NamingEvent; 35 import javax.naming.event.NamingExceptionEvent; 36 import javax.naming.event.NamingListener; 37 import javax.naming.event.ObjectChangeListener; 38 import jdk.test.lib.net.URIBuilder; 39 40 /** 41 * @test 42 * @bug 8176192 8241130 43 * @summary Incorrect usage of Iterator in Java 8 In com.sun.jndi.ldap. 44 * EventSupport.removeNamingListener 45 * @modules java.naming 46 * @library lib/ /test/lib 47 * @run main RemoveNamingListenerTest 48 */ 49 public class RemoveNamingListenerTest { 50 51 private static volatile Exception exception; 52 53 public static void main(String args[]) throws Exception { 54 // start the LDAP server 55 TestLDAPServer server = new TestLDAPServer(); 56 server.start(); 57 58 URI providerURI = URIBuilder.newBuilder() 59 .scheme("ldap") 60 .loopback() 61 .port(server.getPort()) 62 .path("/o=example") 63 .build(); 64 65 // Set up environment for creating initial context 66 Hashtable<String, Object> env = new Hashtable<>(3); 67 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 68 env.put(Context.PROVIDER_URL, providerURI.toString()); 69 env.put("com.sun.jndi.ldap.connect.timeout", "2000"); 70 EventContext ctx = null; 71 72 try { 73 ctx = (EventContext) (new InitialContext(env).lookup("")); 74 String target = "cn=Vyom Tewari"; 75 76 // Create listeners 77 NamingListener oneListener = new SampleListener(); 78 NamingListener objListener = new SampleListener(); 79 NamingListener subListener = new SampleListener(); 80 81 // Register listeners using different scopes 82 ctx.addNamingListener(target, EventContext.ONELEVEL_SCOPE, oneListener); 83 ctx.addNamingListener(target, EventContext.OBJECT_SCOPE, objListener); 84 ctx.addNamingListener(target, EventContext.SUBTREE_SCOPE, subListener); 85 86 //remove a listener in different thread 87 Thread t = new Thread(new RemoveNamingListener(ctx, subListener)); 88 t.start(); 89 t.join(); 90 91 if (exception != null) { 92 throw exception; 93 } 94 System.out.println("Test run OK!!!"); 95 } finally { 96 if (ctx != null) { 97 ctx.close(); 98 } 99 server.close(); 100 } 101 } 102 103 /** 104 * Helper thread that removes the naming listener. 105 */ 106 static class RemoveNamingListener implements Runnable { 107 108 final EventContext ctx; 109 final NamingListener listener; 110 111 RemoveNamingListener(EventContext ctx, NamingListener listener) { 112 this.ctx = ctx; 113 this.listener = listener; 114 } 115 116 @Override 117 public void run() { 118 try { 119 ctx.removeNamingListener(listener); 120 } catch (NamingException | ConcurrentModificationException ex) { 121 exception = ex; 122 } 123 } 124 } 125 126 static class SampleListener implements ObjectChangeListener { 127 128 @Override 129 public void objectChanged(NamingEvent ne) { 130 //do nothing 131 } 132 133 @Override 134 public void namingExceptionThrown(NamingExceptionEvent nee) { 135 //do nothing 136 } 137 } 138 } 139 140 class TestLDAPServer extends BaseLdapServer { 141 142 private byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00}; 143 private byte[] searchResponse = {0x30, 0x0C, 0x02, 0x01, 0x02, 0x65, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00}; 144 145 public TestLDAPServer() throws IOException { 146 } 147 148 @Override 149 protected void handleRequest(Socket socket, LdapMessage request, 150 OutputStream out) throws IOException { 151 switch (request.getOperation()) { 152 case BIND_REQUEST: 153 // Write an LDAP BindResponse 154 out.write(bindResponse); 155 break; 156 case SEARCH_REQUEST: 157 // Write an LDAP SearchResponse 158 out.write(searchResponse); 159 break; 160 default: 161 break; 162 } 163 } 164 }