public void test_removeNamingListener() throws Exception { MockLdapClient client = new MockLdapClient(); Hashtable<Object, Object> env = new Hashtable<Object, Object>(); context = new LdapContextImpl(client, env, "cn=test"); // remove not registered listener, do nothing context.removeNamingListener(new TestNamingListener()); NamingListener listener = new TestNamingListener(); context.addNamingListener("test=listener", EventContext.OBJECT_SCOPE, listener); context.removeNamingListener(listener); listener = new TestNamingListener(); context.addNamingListener("test=listener", EventContext.OBJECT_SCOPE, listener); context.addNamingListener("test=listener", EventContext.ONELEVEL_SCOPE, listener); context.removeNamingListener(listener); }
/** * Create InitialDirContext and NamingEventListener objects, register event * listener. */ public void setUp() { try { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.harmony.test.func.api.javax." + "naming.event.EventDirCtxFactory"); ctx = new InitialDirContext(env); l = new NamingEventListenerSample(); ((EventDirContext) ctx.lookup("")).addNamingListener(ctxName, EventContext.ONELEVEL_SCOPE, l); } catch (Exception e) { e.printStackTrace(); System.exit(fail(e.toString())); } }
@Override public void addListener(String jndiName, NamespaceChangeListener listener) { final InitialContext initialContext = buildInitialContext(); final Name name = parseName( jndiName, initialContext ); try { ( (EventContext) initialContext ).addNamingListener( name, EventContext.OBJECT_SCOPE, listener ); } catch (Exception e) { throw new JndiException( "Unable to bind listener to namespace [" + name + "]", e ); } finally { cleanUp( initialContext ); } }
public static void addInstance(String uid, String name, SessionFactory instance, Properties properties) { log.debug("registered: " + uid + " (" + ( (name==null) ? "unnamed" : name ) + ')'); INSTANCES.put(uid, instance); if (name!=null) NAMED_INSTANCES.put(name, instance); //must add to JNDI _after_ adding to HashMaps, because some JNDI servers use serialization if (name==null) { log.info("Not binding factory to JNDI, no JNDI name configured"); } else { log.info("Factory name: " + name); try { Context ctx = NamingHelper.getInitialContext(properties); NamingHelper.bind(ctx, name, instance); log.info("Bound factory to JNDI name: " + name); ( (EventContext) ctx ).addNamingListener(name, EventContext.OBJECT_SCOPE, LISTENER); } catch (InvalidNameException ine) { log.error("Invalid JNDI name: " + name, ine); } catch (NamingException ne) { log.warn("Could not bind factory to JNDI", ne); } catch(ClassCastException cce) { log.warn("InitialContext did not implement EventContext"); } } }
RemoveNamingListener(EventContext ctx, NamingListener listener) { this.ctx = ctx; this.listener = listener; }
public void addNamingListener(Name name, int scope, NamingListener namingListener) throws NamingException { checkName(name); if (namingListener == null) { return; } // only ldap name is supportted if (!(name instanceof LdapName)) { if (name instanceof CompositeName && name.size() == 1) { name = name.getPrefix(0); } else { // ldap.32=Target cannot span multiple namespaces: {0} throw new InvalidNameException(Messages.getString("ldap.32", //$NON-NLS-1$ new Object[] { name.toString() })); } } if (namingListener instanceof UnsolicitedNotificationListener) { if (unls == null) { unls = new ArrayList<UnsolicitedNotificationListener>(); addUnsolicitedListener(); } unls.add((UnsolicitedNotificationListener) namingListener); if (!(namingListener instanceof NamespaceChangeListener) && !(namingListener instanceof ObjectChangeListener)) { return; } } // ri is silent in this case if (scope != EventContext.OBJECT_SCOPE && scope != EventContext.ONELEVEL_SCOPE && scope != EventContext.SUBTREE_SCOPE) { // ldap.33=Scope should be one of 'OBJECT_SCOPE', 'ONELEVEL_SCOPE' // or 'SUBTREE_SCOPE' throw new IllegalArgumentException(Messages.getString("ldap.33")); //$NON-NLS-1$ } String targetDN = getTargetDN(name, contextDn); Filter filter = new Filter(Filter.PRESENT_FILTER); filter.setValue("objectClass"); SearchControls controls = new SearchControls(); controls.setSearchScope(scope); Name tempName = new LdapName(contextDn.toString()); tempName.addAll(name); String baseDN = tempName.toString(); int messageId = doPersistentSearch(targetDN, baseDN, filter, controls, namingListener); if (listeners == null) { listeners = new HashMap<NamingListener, List<Integer>>(); } List<Integer> idList = listeners.get(namingListener); if (idList == null) { idList = new ArrayList<Integer>(); listeners.put(namingListener, idList); } idList.add(Integer.valueOf(messageId)); }