001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.xbean.naming.context;
022    
023    import java.util.Collections;
024    import java.util.Map;
025    import java.util.Set;
026    import java.util.concurrent.atomic.AtomicReference;
027    
028    import javax.naming.Context;
029    import javax.naming.NamingException;
030    import javax.naming.OperationNotSupportedException;
031    
032    /**
033     * @version $Rev: 901481 $ $Date: 2010-01-21 02:14:08 +0100 (Thu, 21 Jan 2010) $
034     */
035    public class ImmutableFederatedContext extends AbstractFederatedContext {
036    
037        public ImmutableFederatedContext(String nameInNamespace, Set<Context> federatedContexts) {
038            super(nameInNamespace, ContextAccess.UNMODIFIABLE, federatedContexts);
039        }
040    
041        public void federateContext(Context context) throws NamingException {
042            addFederatedContext(this, context);
043        }
044    
045        public void unfederateContext(Context context) throws NamingException {
046            removeFederatedContext(this, context);
047        }
048    
049        @Override
050        protected Map<String, Object> getWrapperBindings() throws NamingException {
051            return Collections.emptyMap();
052        }
053    
054        public Context createNestedSubcontext(String path, Map<String, Object> bindings) throws NamingException {
055            return new NestedImmutableFederatedContext(path, bindings);
056        }
057    
058        /**
059          * Nested context which shares the absolute index map in MapContext.
060          */
061         public class NestedImmutableFederatedContext extends AbstractFederatedContext {
062             private final AtomicReference<Map<String, Object>> bindingsRef;
063             private final String pathWithSlash;
064    
065             public NestedImmutableFederatedContext(String path, Map<String, Object> bindings) throws NamingException {
066                 super(ImmutableFederatedContext.this, path);
067    
068                 path = getNameInNamespace();
069                 if (!path.endsWith("/")) path += "/";
070                 this.pathWithSlash = path;
071    
072                 this.bindingsRef = new AtomicReference<Map<String, Object>>(Collections.unmodifiableMap(bindings));
073             }
074    
075             public Context createNestedSubcontext(String path, Map<String, Object> bindings) throws NamingException {
076                 return new NestedImmutableFederatedContext(getNameInNamespace(path), bindings);
077             }
078    
079             protected Object getDeepBinding(String name) {
080                 String absoluteName = pathWithSlash + name;
081                 return ImmutableFederatedContext.this.getDeepBinding(absoluteName);
082             }
083    
084             protected Map<String, Object> getWrapperBindings() throws NamingException {
085                 return bindingsRef.get();
086             }
087    
088             protected boolean addBinding(String name, Object value, boolean rebind) throws NamingException {
089                 throw new OperationNotSupportedException("Context is immutable");
090             }
091    
092             protected boolean removeBinding(String name, boolean removeNotEmptyContext) throws NamingException {
093                 throw new OperationNotSupportedException("Context is immutable");
094             }
095         }
096    
097    }