All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.sisu.osgi.ServiceBinding Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-3
Show newest version
/*******************************************************************************
 * Copyright (c) 2010-present 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.osgi;

import org.eclipse.sisu.inject.BindingSubscriber;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;

import com.google.inject.Binder;
import com.google.inject.Binding;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.name.Names;
import com.google.inject.spi.BindingScopingVisitor;
import com.google.inject.spi.BindingTargetVisitor;
import com.google.inject.spi.ElementVisitor;

/**
 * Service {@link Binding} backed by an OSGi {@link ServiceReference}.
 */
final class ServiceBinding
    implements Binding, Provider
{
    // ----------------------------------------------------------------------
    // Implementation fields
    // ----------------------------------------------------------------------

    private final Key key;

    private final T instance;

    private final int rank;

    // ----------------------------------------------------------------------
    // Constructors
    // ----------------------------------------------------------------------

    ServiceBinding( final BundleContext context, final String clazzName, final int maxRank,
                    final ServiceReference reference )
        throws ClassNotFoundException
    {
        @SuppressWarnings( "unchecked" )
        final Class clazz = (Class) reference.getBundle().loadClass( clazzName );
        final Object name = reference.getProperty( "name" );
        if ( name instanceof String && ( (String) name ).length() > 0 )
        {
            key = Key.get( clazz, Names.named( (String) name ) );
        }
        else
        {
            key = Key.get( clazz );
        }

        instance = context.getService( reference );

        if ( maxRank > Integer.MIN_VALUE )
        {
            final int serviceRanking = getServiceRanking( reference );
            rank = serviceRanking < maxRank ? serviceRanking : maxRank;
        }
        else
        {
            rank = Integer.MIN_VALUE;
        }
    }

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

    public Key getKey()
    {
        return key;
    }

    public Provider getProvider()
    {
        return this;
    }

    public T get()
    {
        return instance;
    }

    public Object getSource()
    {
        return "OSGi service registry";
    }

    public void applyTo( final Binder binder )
    {
        // no-op
    }

    public  V acceptVisitor( final ElementVisitor visitor )
    {
        return visitor.visit( this );
    }

    public  V acceptTargetVisitor( final BindingTargetVisitor visitor )
    {
        return null;
    }

    public  V acceptScopingVisitor( final BindingScopingVisitor visitor )
    {
        return visitor.visitScopeAnnotation( Singleton.class );
    }

    // ----------------------------------------------------------------------
    // Local methods
    // ----------------------------------------------------------------------

    boolean isCompatibleWith( final BindingSubscriber subscriber )
    {
        return key.getTypeLiteral().getRawType().equals( subscriber.type().getRawType() );
    }

    int rank()
    {
        return rank;
    }

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

    private static int getServiceRanking( final ServiceReference reference )
    {
        final Object ranking = reference.getProperty( Constants.SERVICE_RANKING );
        return ranking instanceof Integer ? ( (Integer) ranking ).intValue() : 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy