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

org.fabric3.fabric.container.binding.BindingHandlerRegistryImpl Maven / Gradle / Ivy

The newest version!
/*
 * Fabric3
 * Copyright (c) 2009-2015 Metaform Systems
 *
 * 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.fabric3.fabric.container.binding;

import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.fabric3.spi.container.binding.BindingHandler;
import org.fabric3.spi.container.binding.BindingHandlerRegistry;
import org.fabric3.spi.container.binding.BindingHandlerRegistryCallback;
import org.fabric3.spi.container.component.ComponentManager;
import org.fabric3.spi.model.physical.PhysicalBindingHandler;
import org.oasisopen.sca.annotation.Reference;

/**
 *
 */
public class BindingHandlerRegistryImpl implements BindingHandlerRegistry {
    private ComponentManager componentManager;

    private Map> callbacks = new HashMap<>();
    private Map>> handlers = new HashMap<>();

    public BindingHandlerRegistryImpl(@Reference ComponentManager componentManager) {
        this.componentManager = componentManager;
    }

    public  BindingHandler createHandler(Class type, PhysicalBindingHandler physicalHandler) {
        return new BindingHandlerLazyLoadDecorator<>(physicalHandler.getHandlerUri(), componentManager);
    }

    @SuppressWarnings({"unchecked"})
    public synchronized void register(BindingHandlerRegistryCallback callback) {
        QName binding = callback.getType();
        if (callbacks.containsKey(binding)) {
            throw new IllegalStateException("Callback already registered for " + binding);
        }
        callbacks.put(binding, callback);
        List> list = handlers.get(binding);
        if (list != null) {
            // notify the callback of existing handlers
            callback.update(Collections.unmodifiableList(list));
        }
    }

    public synchronized void unregister(BindingHandlerRegistryCallback callback) {
        QName binding = callback.getType();
        if (callbacks.remove(binding) == null) {
            throw new IllegalStateException("Callback not registered for " + binding);
        }
    }

    @SuppressWarnings({"unchecked"})
    public synchronized void register(BindingHandler handler) {
        QName binding = handler.getType();
        List> list = handlers.get(binding);
        if (list == null) {
            list = new ArrayList<>();
            handlers.put(binding, list);
        }
        list.add(handler);
        BindingHandlerRegistryCallback callback = callbacks.get(binding);
        if (callback != null) {
            callback.update(Collections.unmodifiableList(list));
        }
    }

    @SuppressWarnings({"unchecked"})
    public synchronized void unregister(BindingHandler handler) {
        QName binding = handler.getType();
        List> list = handlers.get(binding);
        if (list == null || !list.remove(handler)) {
            throw new IllegalStateException("Handler not registered for binding " + binding);
        }
        if (list.isEmpty()) {
            handlers.remove(binding);
        }
        BindingHandlerRegistryCallback callback = callbacks.get(binding);
        if (callback != null) {
            callback.update(Collections.unmodifiableList(list));
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy