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

org.cattleframework.cloud.discovery.enhancement.plugin.DefaultPluginRunner Maven / Gradle / Ivy

There is a newer version: 1.0.1-M5
Show newest version
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * 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.cattleframework.cloud.discovery.enhancement.plugin;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

/**
 * 缺省的插件运行器程序
 * 
 * @author orange
 *
 */
public class DefaultPluginRunner implements PluginRunner {

    private static final Logger logger = LoggerFactory.getLogger(DefaultPluginRunner.class);

    private static final String INVOKER_SOURCE = "invoker";

    private static final String INVOKER_OWNER = "cattle";

    private final ServiceInstance localServiceInstance;

    private final Multimap pluginMap = ArrayListMultimap.create();

    public DefaultPluginRunner(List plugins, Registration registration) {
	if (CollectionUtils.isNotEmpty(plugins)) {
	    plugins.forEach(plugin -> pluginMap.put(plugin.getType(), plugin));
	}
	localServiceInstance = registration;
    }

    @Override
    public void run(PluginType pluginType, PluginContext context) {
	if (Arrays.stream(PluginType.Server.values()).anyMatch(value -> value == pluginType)
		&& context.getRequest().getHttpHeaders().containsKey(INVOKER_SOURCE)) {
	    if (INVOKER_OWNER.equalsIgnoreCase(context.getRequest().getHttpHeaders().getFirst(INVOKER_SOURCE))) {
		return;
	    }
	}
	for (Plugin plugin : pluginMap.get(pluginType)) {
	    try {
		logger.debug("执行插件,类型:{},类:{}", pluginType.toString(), plugin.getClass().getName());
		plugin.run(context);
	    } catch (Throwable e) {
		plugin.handlerThrowable(context, e);
	    }
	}
    }

    @Override
    public ServiceInstance getLocalServiceInstance() {
	return localServiceInstance;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy