Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.support;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.BeanRepository;
import org.apache.camel.spi.LocalBeanRepositoryAware;
import org.apache.camel.spi.Registry;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.function.Suppliers;
/**
* The default {@link Registry} which supports using a given first-choice repository to lookup the beans, such as
* Spring, JNDI, OSGi etc. And to use a secondary {@link SimpleRegistry} as the fallback repository to lookup and bind
* beans.
*
* Notice that beans in the fallback registry are not managed by the first-choice registry, so these beans may not
* support dependency injection and other features that the first-choice registry may offer.
*/
public class DefaultRegistry extends ServiceSupport implements Registry, LocalBeanRepositoryAware, CamelContextAware {
protected CamelContext camelContext;
protected final ThreadLocal localRepository = new ThreadLocal<>();
protected volatile boolean localRepositoryEnabled; // flag to keep track if local is in use or not
protected List repositories;
protected Registry fallbackRegistry = new SimpleRegistry();
protected Registry supplierRegistry = new SupplierRegistry();
/**
* Creates a default registry that uses {@link SimpleRegistry} as the fallback registry. The fallback registry can
* customized via {@link #setFallbackRegistry(Registry)}.
*/
public DefaultRegistry() {
// noop
}
/**
* Creates a registry that uses the given {@link BeanRepository} as first choice bean repository to lookup beans.
* Will fallback and use {@link SimpleRegistry} as fallback registry if the beans cannot be found in the first
* choice bean repository. The fallback registry can customized via {@link #setFallbackRegistry(Registry)}.
*
* @param repositories the first choice repositories such as Spring, JNDI, OSGi etc.
*/
public DefaultRegistry(BeanRepository... repositories) {
if (repositories != null) {
this.repositories = new ArrayList<>(Arrays.asList(repositories));
}
}
/**
* Creates a registry that uses the given {@link BeanRepository} as first choice bean repository to lookup beans.
* Will fallback and use {@link SimpleRegistry} as fallback registry if the beans cannot be found in the first
* choice bean repository. The fallback registry can customized via {@link #setFallbackRegistry(Registry)}.
*
* @param repositories the first choice repositories such as Spring, JNDI, OSGi etc.
*/
public DefaultRegistry(Collection repositories) {
if (repositories != null) {
this.repositories = new ArrayList<>(repositories);
}
}
/**
* Sets a special local bean repository (ie thread local) that take precedence and will use first, if a bean exists.
*/
public void setLocalBeanRepository(BeanRepository repository) {
if (repository != null) {
this.localRepository.set(repository);
this.localRepositoryEnabled = true;
} else {
BeanRepository old = this.localRepository.get();
if (old != null) {
ServiceHelper.stopService(old);
}
this.localRepository.remove();
this.localRepositoryEnabled = false;
}
}
@Override
public BeanRepository getLocalBeanRepository() {
return localRepositoryEnabled ? localRepository.get() : null;
}
/**
* Gets the fallback {@link Registry}
*/
public Registry getFallbackRegistry() {
return fallbackRegistry;
}
/**
* To use a custom {@link Registry} as fallback.
*/
public void setFallbackRegistry(Registry fallbackRegistry) {
this.fallbackRegistry = fallbackRegistry;
}
/**
* Gets the supplier {@link Registry}
*/
public Registry getSupplierRegistry() {
return supplierRegistry;
}
/**
* To use a custom {@link Registry} for suppliers.
*/
public void setSupplierRegistry(Registry supplierRegistry) {
this.supplierRegistry = supplierRegistry;
}
@Override
public CamelContext getCamelContext() {
return camelContext;
}
@Override
public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}
/**
* Gets the bean repositories.
*
* @return the bean repositories, or null if none are in use.
*/
public List getRepositories() {
if (repositories == null) {
return null;
} else {
return Collections.unmodifiableList(repositories);
}
}
@Override
public void bind(String id, Class type, Object bean) throws RuntimeCamelException {
if (bean != null) {
// automatic inject camel context in bean if its aware
CamelContextAware.trySetCamelContext(bean, camelContext);
fallbackRegistry.bind(id, type, bean);
}
}
@Override
public void bind(String id, Class type, Supplier