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

org.fusesource.fabric.fab.osgi.util.Services Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fusesource.fabric.fab.osgi.util;

import org.fusesource.common.util.Strings;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;

import java.util.*;

/**
 * A set of helper methods for working with OSGi services
 */
public class Services {

    /**
     * Parse an OSGi Export-Service header into a set of {@link Service} representations
     *
     * @param header the header value
     * @return the set of services
     */
    public static Set parseHeader(String header) {
        Set services = new HashSet();
        if (Strings.notEmpty(header)) {
            Scanner scanner = new Scanner(header).useDelimiter(",");
            while (scanner.hasNext()) {
                services.add(Service.parse(scanner.next()));
            }
        }
        return services;
    }

    public static boolean isAvailable(BundleContext context, String className) throws InvalidSyntaxException {
        return isAvailable(context, new Service(className));
    }

    public static boolean isAvailable(BundleContext context, String className, Map properties) throws InvalidSyntaxException {
        return isAvailable(context, new Service(className, properties));
    }

    public static boolean isAvailable(BundleContext context, Service service) throws InvalidSyntaxException {
        return service.isAvailable(context);
    }

    public static Map createProperties(String... elements) {
        if (elements.length % 2 != 0) {
            throw new IllegalArgumentException("Expected an even number of elements");
        }
        Map properties = new LinkedHashMap();
        for (int i = 0; i < elements.length; i = i + 2) {
            properties.put(elements[i], elements[i + 1]);
        }
        return properties;
    }

    public static boolean isAvailable(BundleContext context, Set services) throws InvalidSyntaxException {
        for (Service service : services) {
            if (!service.isAvailable(context)) {
                return false;
            }
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy