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.IPoint;
022    import org.apache.commons.inject.api.IProvider;
023    
024    public class BindingProxy<T> implements IBinding<T>, IInjectorAware {
025            private IBinding<T> binding;
026            private boolean initialized;
027            private boolean isResolvedLater;
028            private final String cause;
029            private final RuntimeException rte = new RuntimeException();
030    
031            public BindingProxy(String pCause) {
032                    cause = pCause;
033            }
034    
035            public boolean isResolvedLater() {
036                    return isResolvedLater;
037            }
038    
039            public void setResolvedLater(boolean pResolvedLater) {
040                    isResolvedLater = pResolvedLater;
041            }
042            
043            @Override
044            public synchronized IProvider<T> getProvider() {
045                    if (!initialized) {
046                            throw new IllegalStateException("This Binding hasn't been initialized.", rte);
047                    }
048                    return binding.getProvider();
049            }
050    
051            @Override
052            public IPoint<T> getPoint() {
053                    if (!initialized) {
054                            throw new IllegalStateException("This Binding hasn't been initialized.");
055                    }
056                    return binding.getPoint();
057            }
058    
059            public synchronized void setBinding(IBinding<T> pBinding) {
060                    if (pBinding == null) {
061                            throw new NullPointerException("The proxied binding must not be null.");
062                    }
063                    binding = pBinding;
064                    initialized = true;
065            }
066    
067            public String getCause() {
068                    return cause;
069            }
070    
071            @Override
072            public void init(IInjector pInjector) {
073                    if (binding != null  &&  binding instanceof IInjectorAware) {
074                            ((IInjectorAware) binding).init(pInjector);
075                    }
076            }
077    }