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

org.kie.api.internal.utils.ServiceRegistry Maven / Gradle / Ivy

/*
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * 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.kie.api.internal.utils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.kie.api.Service;

import static org.kie.api.internal.utils.ServiceUtil.instanceFromNames;

/**
 * Internal Interface
 *
 */
public interface ServiceRegistry extends Service {

    static  T getService(Class cls) {
        return getInstance().get( cls );
    }

    static ServiceRegistry getInstance() {
        return ServiceRegistryHolder.serviceRegistry;
    }

     T get(Class cls);

     List getAll( Class cls);

    class ServiceRegistryHolder {
        private static ServiceRegistry serviceRegistry = Impl.getServiceRegistry();
    }

    class Impl implements ServiceRegistry {

        private static final String DYNAMIC_IMPL = "org.drools.dynamic.DynamicServiceRegistrySupplier";
        private static final String STATIC_IMPL = "org.drools.statics.StaticServiceRegistrySupplier";

        private static Supplier supplier;

        private Map> registry;

        public Impl() {
            registry = ServiceDiscoveryImpl.getInstance().getServices();
        }

        public synchronized void reset() {
            ServiceDiscoveryImpl.getInstance().reset();
        }

        public synchronized void reload() {
            registry = ServiceDiscoveryImpl.getInstance().getServices();
        }

        public  T get(Class cls) {
            for ( Object service : getAll( cls ) ) {
                if ( cls.isInstance( service ) ) {
                    return (T) service;
                }
            }
            return null;
        }

        public  List getAll(Class cls) {
            return (List) this.registry.getOrDefault( cls.getCanonicalName(), Collections.emptyList() );
        }

        public static ServiceRegistry getServiceRegistry() {
            if (supplier == null) {
                supplier = instanceFromNames(DYNAMIC_IMPL, STATIC_IMPL);
            }
            return supplier.get();
        }

        public static void setSupplier( Supplier supplier ) {
            Impl.supplier = supplier;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy