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.commons.inject.impl;
018    
019    import org.apache.commons.inject.api.IBinding;
020    import org.apache.commons.inject.api.IInjector;
021    import org.apache.commons.inject.api.IKey;
022    import org.apache.commons.inject.api.IPoint;
023    import org.apache.commons.inject.api.IProvider;
024    import org.apache.commons.inject.api.NoSuchBindingException;
025    import org.apache.commons.inject.impl.AbstractBindingSet.MappedKey;
026    import org.apache.commons.inject.impl.AbstractBindingSet.ReducedKey;
027    import org.apache.commons.inject.impl.bind.DefaultBinding;
028    
029    /**
030     * Default implementation of an {@link IInjector injector}.
031     */
032    public class DefaultInjector extends AbstractInjector {
033            private final ImmutableBindingSet bindings;
034            
035            public DefaultInjector(ImmutableBindingSet pBindings) {
036                    bindings = pBindings;
037            }
038    
039            protected <T> IBinding<T> getBinding(IKey<T> pKey) {
040                    synchronized(bindings) {
041                            return bindings.getBinding(pKey);
042                    }
043            }
044            
045            protected <T> IBinding<T> requireBinding(IKey<T> pKey) {
046                    synchronized(bindings) {
047                            IBinding<T> binding;
048                            binding = bindings.getBinding(pKey);
049                            if (binding == null) {
050                                    final IMutableBindingSource bindingSource = getBindingSource();
051                                    final Class<T> cl = pKey.getType();
052                                    final IPoint<T> point = Introspector.getInstance().getPoint(cl, bindingSource);
053                                    final IProvider<T> provider = Introspector.getInstance().getProvider(cl, point, bindingSource);
054                                    binding = new DefaultBinding<T>(provider, point);
055                                    final ReducedKey<T> rkey = new ReducedKey<T>(cl, pKey.getName());
056                                    final MappedKey<T> key = new MappedKey<T>(cl, pKey.getName(), null, null);
057                                    bindings.add(rkey, key, binding);
058                            }
059                            return binding;
060                    }
061            }
062    
063            protected IMutableBindingSource getBindingSource() {
064                    return new IMutableBindingSource() {
065                            @Override
066                            public <T> IBinding<T> requireBinding(IKey<T> pKey, String pCause) {
067                                    final IBinding<T> binding = bindings.getBinding(pKey);
068                                    if (binding == null) {
069                                            throw new NoSuchBindingException("No binding registered for key: " + pKey);
070                                    }
071                                    return binding;
072                            }
073                            
074                    };
075            }
076            
077    }