/*******************************************************************************
 * Copyright (c) 2010, 2013 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.sisu.inject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.eclipse.sisu.BeanEntry;
import org.eclipse.sisu.Mediator;

import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;

/**
 * Default {@link MutableBeanLocator} that locates qualified beans across a dynamic group of {@link BindingPublisher}s.
 */
@Singleton
@SuppressWarnings( { "rawtypes", "unchecked" } )
public final class DefaultBeanLocator
    implements MutableBeanLocator
{
    // ----------------------------------------------------------------------
    // Implementation fields
    // ----------------------------------------------------------------------

    private final RankedSequence<BindingPublisher> publishers = new RankedSequence<BindingPublisher>();

    private final ConcurrentMap<TypeLiteral, RankedBindings> cachedBindings = Soft.concurrentValues( 256, 8 );

    // reverse mapping; can't use watcher as key since it may not be unique
    private final Map<WatchedBeans, Object> cachedWatchers = Weak.values();

    private final ImplicitBindings implicitBindings = new ImplicitBindings( publishers );

    // ----------------------------------------------------------------------
    // Public methods
    // ----------------------------------------------------------------------

    public Iterable<BeanEntry> locate( final Key key )
    {
        final TypeLiteral type = key.getTypeLiteral();
        RankedBindings bindings = cachedBindings.get( type );
        if ( null == bindings )
        {
            synchronized ( this )
            {
                bindings = new RankedBindings( type, publishers );
                final RankedBindings oldBindings = cachedBindings.putIfAbsent( type, bindings );
                if ( null != oldBindings )
                {
                    bindings = oldBindings;
                }
            }
        }
        final boolean isImplicit = key.getAnnotationType() == null && TypeArguments.isImplicit( type );
        return new LocatedBeans( key, bindings, isImplicit ? implicitBindings : null );
    }

    public synchronized void watch( final Key key, final Mediator mediator, final Object watcher )
    {
        final WatchedBeans beans = new WatchedBeans( key, mediator, watcher );
        for ( final BindingPublisher p : this )
        {
            p.subscribe( beans );
        }
        cachedWatchers.put( beans, watcher );
    }

    public synchronized boolean add( final BindingPublisher publisher, final int rank )
    {
        if ( !publishers.contains( publisher ) )
        {
            Logs.trace( "Add publisher: {}", publisher, null );
            publishers.insert( publisher, rank );
            for ( final RankedBindings bindings : cachedBindings.values() )
            {
                bindings.add( publisher, rank );
            }
            // take defensive copy in case publisher.subscribe has side-effect that triggers 'watch'
            for ( final WatchedBeans beans : new ArrayList<WatchedBeans>( cachedWatchers.keySet() ) )
            {
                publisher.subscribe( beans );
            }
            return true;
        }
        return false;
    }

    public synchronized boolean remove( final BindingPublisher publisher )
    {
        final BindingPublisher oldPublisher = publishers.remove( publisher );
        if ( null != oldPublisher )
        {
            Logs.trace( "Remove publisher: {}", oldPublisher, null );
            for ( final RankedBindings bindings : cachedBindings.values() )
            {
                bindings.remove( oldPublisher );
            }
            for ( final WatchedBeans beans : cachedWatchers.keySet() )
            {
                oldPublisher.unsubscribe( beans );
            }
            return true;
        }
        return false;
    }

    public synchronized void clear()
    {
        for ( final BindingPublisher p : this )
        {
            remove( p );
        }
    }

    public Iterator<BindingPublisher> iterator()
    {
        return publishers.snapshot().iterator();
    }

    public void add( final Injector injector, final int rank )
    {
        add( new InjectorPublisher( injector, new DefaultRankingFunction( rank ) ), rank );
    }

    public void remove( final Injector injector )
    {
        remove( new InjectorPublisher( injector, null ) );
    }

    // ----------------------------------------------------------------------
    // Implementation methods
    // ----------------------------------------------------------------------

    /**
     * Automatically publishes any {@link Injector} that contains a binding to this {@link BeanLocator}.
     * 
     * @param injector The injector
     */
    @Inject
    void autoPublish( final Injector injector )
    {
        staticAutoPublish( this, injector );
    }

    @Inject
    static void staticAutoPublish( final MutableBeanLocator locator, final Injector injector )
    {
        final RankingFunction function = injector.getInstance( RankingFunction.class );
        locator.add( new InjectorPublisher( injector, function ), function.maxRank() );
    }
}

@@ -2262,22 +2262,54 @@
 nized ( 
-this )
+cachedBindings ) // perform new lookup
 %0A       
@@ -3373,32 +3373,140 @@
 lisher, null );%0A
+            synchronized ( cachedBindings ) // block new lookup while we update the cache%0A            %7B%0A    
             publ
@@ -3531,32 +3531,36 @@
 lisher, rank );%0A
+    
             for 
@@ -3623,34 +3623,42 @@
 ) )%0A            
+ 
-%7B%0A
+   %7B%0A    
                 
@@ -3682,32 +3682,50 @@
 lisher, rank );%0A
+                %7D%0A
             %7D%0A  
@@ -4175,16 +4175,138 @@
 blisher 
+oldPublisher;%0A        synchronized ( cachedBindings ) // block new lookup while we update the cache%0A        %7B%0A            
 oldPubli
@@ -4344,32 +4344,36 @@
 sher );%0A        
+  
+  
 if ( null != old
@@ -4384,34 +4384,42 @@
 isher )%0A        
-%7B%0A
+    %7B%0A    
             Logs
@@ -4464,32 +4464,36 @@
 lisher, null );%0A
+    
             for 
@@ -4556,34 +4556,42 @@
 ) )%0A            
-%7B%0A
+    %7B%0A    
                 
@@ -4627,33 +4627,107 @@
  );%0A            
-%7D
+    %7D%0A            %7D%0A        %7D%0A        if ( null != oldPublisher )%0A        %7B
 %0A            for

Tweak locator locking to reduce contention between locate and watch