001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.xbean.naming.reference;
018    
019    import org.apache.xbean.naming.context.ContextUtil;
020    
021    import javax.naming.Reference;
022    import javax.naming.NamingException;
023    import java.util.Map;
024    import java.util.LinkedHashMap;
025    import java.util.Iterator;
026    
027    /**
028     * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
029     */
030    public class CachingReference extends SimpleReference {
031        public static Object wrapReference(String fullName, Object value) {
032            if (value instanceof Reference && !(value instanceof CachingReference)) {
033                return new CachingReference(fullName, (Reference)value);
034            }
035            return value;
036        }
037    
038        public static Map wrapReferences(Map bindings) {
039            LinkedHashMap newBindings = new LinkedHashMap(bindings);
040            for (Iterator iterator = bindings.entrySet().iterator(); iterator.hasNext();) {
041                Map.Entry entry = (Map.Entry) iterator.next();
042                String name = (String) entry.getKey();
043                Object value = entry.getValue();
044                if (value instanceof Reference && !(value instanceof CachingReference)) {
045                    newBindings.put(name, new CachingReference(name, (Reference)value));
046                }
047            }
048            return newBindings;
049        }
050    
051        private final Object lock = new Object();
052        private final String fullName;
053        private final Reference reference;
054        private final String className;
055        private Object value;
056    
057        public CachingReference(String fullName, Reference reference) {
058            this.fullName = fullName;
059            this.reference = reference;
060            className = reference.getClassName();
061        }
062    
063        public Object getContent() throws NamingException {
064            synchronized(lock) {
065                if (value == null) {
066                    value = ContextUtil.resolve(fullName, reference);
067                }
068                return value;
069            }
070        }
071    
072        public String getClassName() {
073            return className;
074        }
075    }