Package bndtools.utils

Source Code of bndtools.utils.ServiceUtils

/*******************************************************************************
* Copyright (c) 2010 Neil Bartlett.
* 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:
*     Neil Bartlett - initial API and implementation
*******************************************************************************/
package bndtools.utils;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class ServiceUtils {
    public static final <R, S, E extends Throwable> R usingService(BundleContext context, Class<S> clazz, ServiceOperation<R,S,E> operation) throws E {
        ServiceReference reference = context.getServiceReference(clazz.getName());
        if (reference != null) {
            @SuppressWarnings("unchecked")
            S service = (S) context.getService(reference);
            if (service != null) {
                try {
                    return operation.execute(service);
                } finally {
                    context.ungetService(reference);
                }
            }
        }
        return null;
    }
}
TOP

Related Classes of bndtools.utils.ServiceUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.