protected void addBinding()

in xbean-naming/src/main/java/org/apache/xbean/naming/context/WritableContext.java [104:144]


    protected void addBinding(AtomicReference<Map<String, Object>> bindingsRef, String name, String nameInNamespace, Object value, boolean rebind) throws NamingException {
        if (supportReferenceable && value instanceof Referenceable) {
            Reference ref = ((Referenceable)value).getReference();
            if (ref != null) {
                if (checkDereferenceDifferent) {
                    try {
                        Object o = NamingManager.getObjectInstance(ref, null, null, new Hashtable());
                        if (!value.equals(o)) {
                            value = ref;
                        }
                    } catch (Exception e) {
                        if (!assumeDereferenceBound) {
                            value = ref;
                        }
                    }
                } else {
                    value = ref;
                }
            }

        }
        if (cacheReferences) {
            value = CachingReference.wrapReference(name, value, this);
        }

        writeLock.lock();
        try {
            Map<String, Object> bindings = bindingsRef.get();

            if (!rebind && bindings.containsKey(name)) {
                throw new NameAlreadyBoundException(name);
            }
            Map<String, Object> newBindings = new HashMap<String, Object>(bindings);
            newBindings.put(name,value);
            bindingsRef.set(newBindings);

            addToIndex(nameInNamespace, value);
        } finally {
            writeLock.unlock();
        }
    }