use of org.apache.camel.NoSuchBeanException in project camel by apache.
the class ConsulRegistry method remove.
public void remove(String key) {
// create session to avoid conflicts (not sure if that is safe enough)
SessionClient sessionClient = consul.sessionClient();
String sessionName = "session_" + UUID.randomUUID().toString();
SessionCreatedResponse response = sessionClient.createSession(ImmutableSession.builder().name(sessionName).build());
String sessionId = response.getId();
kvClient = consul.keyValueClient();
String lockKey = "lock_" + key;
kvClient.acquireLock(lockKey, sessionName, sessionId);
Object object = lookupByName(key);
if (object == null) {
String msg = "Bean with key '" + key + "' did not exist in Consul Registry.";
throw new NoSuchBeanException(msg);
}
kvClient.deleteKey(key);
kvClient.deleteKey(object.getClass().getName() + "/" + key);
kvClient.releaseLock(lockKey, sessionId);
}
use of org.apache.camel.NoSuchBeanException in project camel by apache.
the class RestApiEndpoint method createConsumer.
@Override
public Consumer createConsumer(Processor processor) throws Exception {
RestApiConsumerFactory factory = null;
String cname = null;
// the API then uses the api component (eg usually camel-swagger-java) to build the API
if (getComponentName() != null) {
Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
if (comp != null && comp instanceof RestApiConsumerFactory) {
factory = (RestApiConsumerFactory) comp;
} else {
comp = getCamelContext().getComponent(getComponentName());
if (comp != null && comp instanceof RestApiConsumerFactory) {
factory = (RestApiConsumerFactory) comp;
}
}
if (factory == null) {
if (comp != null) {
throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestApiConsumerFactory");
} else {
throw new NoSuchBeanException(getComponentName(), RestApiConsumerFactory.class.getName());
}
}
cname = getComponentName();
}
// try all components
if (factory == null) {
for (String name : getCamelContext().getComponentNames()) {
Component comp = getCamelContext().getComponent(name);
if (comp != null && comp instanceof RestApiConsumerFactory) {
factory = (RestApiConsumerFactory) comp;
cname = name;
break;
}
}
}
// lookup in registry
if (factory == null) {
Set<RestApiConsumerFactory> factories = getCamelContext().getRegistry().findByType(RestApiConsumerFactory.class);
if (factories != null && factories.size() == 1) {
factory = factories.iterator().next();
}
}
if (factory != null) {
// calculate the url to the rest API service
RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
// calculate the url to the rest API service
String path = getPath();
if (path != null && !path.startsWith("/")) {
path = "/" + path;
}
Consumer consumer = factory.createApiConsumer(getCamelContext(), processor, path, config, getParameters());
configureConsumer(consumer);
return consumer;
} else {
throw new IllegalStateException("Cannot find RestApiConsumerFactory in Registry or as a Component to use");
}
}
use of org.apache.camel.NoSuchBeanException in project camel by apache.
the class JndiRegistryTest method testLookupByType.
public void testLookupByType() throws Exception {
JndiRegistry jndi = new JndiRegistry(JndiTest.createInitialContext());
jndi.bind("foo", new SimpleLanguage());
jndi.bind("bar", "Hello bar");
assertEquals("Hello bar", jndi.lookup("bar"));
assertEquals("Hello bar", jndi.lookupByName("bar"));
assertEquals("Hello bar", jndi.lookupByNameAndType("bar", String.class));
assertNull(jndi.lookup("unknown"));
assertNull(jndi.lookupByName("unknown"));
try {
assertNull(jndi.lookupByNameAndType("bar", Language.class));
fail("Should throw exception");
} catch (NoSuchBeanException e) {
// expected
}
assertNotNull(jndi.lookupByNameAndType("foo", Language.class));
assertNotNull(jndi.lookupByNameAndType("foo", SimpleLanguage.class));
assertSame(jndi.lookupByNameAndType("foo", Language.class), jndi.lookupByNameAndType("foo", SimpleLanguage.class));
Map<String, ?> set = jndi.lookupByType(Language.class);
assertNotNull(set);
assertEquals(1, set.size());
String key = set.keySet().iterator().next();
assertEquals("foo", key);
assertSame(jndi.lookupByName("foo"), set.values().iterator().next());
}
use of org.apache.camel.NoSuchBeanException in project camel by apache.
the class RestEndpoint method createConsumer.
@Override
public Consumer createConsumer(Processor processor) throws Exception {
RestConsumerFactory factory = null;
String cname = null;
if (getComponentName() != null) {
Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
if (comp != null && comp instanceof RestConsumerFactory) {
factory = (RestConsumerFactory) comp;
} else {
comp = getCamelContext().getComponent(getComponentName());
if (comp != null && comp instanceof RestConsumerFactory) {
factory = (RestConsumerFactory) comp;
}
}
if (factory == null) {
if (comp != null) {
throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestConsumerFactory");
} else {
throw new NoSuchBeanException(getComponentName(), RestConsumerFactory.class.getName());
}
}
cname = getComponentName();
}
// try all components
if (factory == null) {
for (String name : getCamelContext().getComponentNames()) {
Component comp = getCamelContext().getComponent(name);
if (comp != null && comp instanceof RestConsumerFactory) {
factory = (RestConsumerFactory) comp;
cname = name;
break;
}
}
}
// lookup in registry
if (factory == null) {
Set<RestConsumerFactory> factories = getCamelContext().getRegistry().findByType(RestConsumerFactory.class);
if (factories != null && factories.size() == 1) {
factory = factories.iterator().next();
}
}
// and there must only be exactly one so we safely can pick this one
if (factory == null) {
RestConsumerFactory found = null;
String foundName = null;
for (String name : DEFAULT_REST_CONSUMER_COMPONENTS) {
Object comp = getCamelContext().getComponent(name, true);
if (comp != null && comp instanceof RestConsumerFactory) {
if (found == null) {
found = (RestConsumerFactory) comp;
foundName = name;
} else {
throw new IllegalArgumentException("Multiple RestConsumerFactory found on classpath. Configure explicit which component to use");
}
}
}
if (found != null) {
LOG.debug("Auto discovered {} as RestConsumerFactory", foundName);
factory = found;
}
}
if (factory != null) {
// if no explicit port/host configured, then use port from rest configuration
String scheme = "http";
String host = "";
int port = 80;
RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
if (config.getScheme() != null) {
scheme = config.getScheme();
}
if (config.getHost() != null) {
host = config.getHost();
}
int num = config.getPort();
if (num > 0) {
port = num;
}
// if no explicit hostname set then resolve the hostname
if (ObjectHelper.isEmpty(host)) {
if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
host = "0.0.0.0";
} else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
host = HostUtils.getLocalHostName();
} else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
host = HostUtils.getLocalIp();
}
}
// calculate the url to the rest service
String path = getPath();
if (!path.startsWith("/")) {
path = "/" + path;
}
// there may be an optional context path configured to help Camel calculate the correct urls for the REST services
// this may be needed when using camel-servlet where we cannot get the actual context-path or port number of the servlet engine
// during init of the servlet
String contextPath = config.getContextPath();
if (contextPath != null) {
if (!contextPath.startsWith("/")) {
path = "/" + contextPath + path;
} else {
path = contextPath + path;
}
}
String baseUrl = scheme + "://" + host + (port != 80 ? ":" + port : "") + path;
String url = baseUrl;
if (uriTemplate != null) {
// make sure to avoid double slashes
if (uriTemplate.startsWith("/")) {
url = url + uriTemplate;
} else {
url = url + "/" + uriTemplate;
}
}
Consumer consumer = factory.createConsumer(getCamelContext(), processor, getMethod(), getPath(), getUriTemplate(), getConsumes(), getProduces(), config, getParameters());
configureConsumer(consumer);
// add to rest registry so we can keep track of them, we will remove from the registry when the consumer is removed
// the rest registry will automatic keep track when the consumer is removed,
// and un-register the REST service from the registry
getCamelContext().getRestRegistry().addRestService(consumer, url, baseUrl, getPath(), getUriTemplate(), getMethod(), getConsumes(), getProduces(), getInType(), getOutType(), getRouteId(), getDescription());
return consumer;
} else {
throw new IllegalStateException("Cannot find RestConsumerFactory in Registry or as a Component to use");
}
}
use of org.apache.camel.NoSuchBeanException in project camel by apache.
the class RestEndpoint method createProducer.
@Override
public Producer createProducer() throws Exception {
RestProducerFactory apiDocFactory = null;
RestProducerFactory factory = null;
if (apiDoc != null) {
LOG.debug("Discovering camel-swagger-java on classpath for using api-doc: {}", apiDoc);
// lookup on classpath using factory finder to automatic find it (just add camel-swagger-java to classpath etc)
try {
FactoryFinder finder = getCamelContext().getFactoryFinder(RESOURCE_PATH);
Object instance = finder.newInstance(DEFAULT_API_COMPONENT_NAME);
if (instance instanceof RestProducerFactory) {
// this factory from camel-swagger-java will facade the http component in use
apiDocFactory = (RestProducerFactory) instance;
}
parameters.put("apiDoc", apiDoc);
} catch (NoFactoryAvailableException e) {
throw new IllegalStateException("Cannot find camel-swagger-java on classpath to use with api-doc: " + apiDoc);
}
}
String cname = getComponentName();
if (cname != null) {
Object comp = getCamelContext().getRegistry().lookupByName(getComponentName());
if (comp != null && comp instanceof RestProducerFactory) {
factory = (RestProducerFactory) comp;
} else {
comp = getCamelContext().getComponent(getComponentName());
if (comp != null && comp instanceof RestProducerFactory) {
factory = (RestProducerFactory) comp;
}
}
if (factory == null) {
if (comp != null) {
throw new IllegalArgumentException("Component " + getComponentName() + " is not a RestProducerFactory");
} else {
throw new NoSuchBeanException(getComponentName(), RestProducerFactory.class.getName());
}
}
cname = getComponentName();
}
// try all components
if (factory == null) {
for (String name : getCamelContext().getComponentNames()) {
Component comp = getCamelContext().getComponent(name);
if (comp != null && comp instanceof RestProducerFactory) {
factory = (RestProducerFactory) comp;
cname = name;
break;
}
}
}
parameters.put("componentName", cname);
// lookup in registry
if (factory == null) {
Set<RestProducerFactory> factories = getCamelContext().getRegistry().findByType(RestProducerFactory.class);
if (factories != null && factories.size() == 1) {
factory = factories.iterator().next();
}
}
// and there must only be exactly one so we safely can pick this one
if (factory == null) {
RestProducerFactory found = null;
String foundName = null;
for (String name : DEFAULT_REST_PRODUCER_COMPONENTS) {
Object comp = getCamelContext().getComponent(name, true);
if (comp != null && comp instanceof RestProducerFactory) {
if (found == null) {
found = (RestProducerFactory) comp;
foundName = name;
} else {
throw new IllegalArgumentException("Multiple RestProducerFactory found on classpath. Configure explicit which component to use");
}
}
}
if (found != null) {
LOG.debug("Auto discovered {} as RestProducerFactory", foundName);
factory = found;
}
}
if (factory != null) {
LOG.debug("Using RestProducerFactory: {}", factory);
Producer producer;
if (apiDocFactory != null) {
// wrap the factory using the api doc factory which will use the factory
parameters.put("restProducerFactory", factory);
producer = apiDocFactory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
} else {
producer = factory.createProducer(getCamelContext(), host, method, path, uriTemplate, queryParameters, consumes, produces, parameters);
}
RestConfiguration config = getCamelContext().getRestConfiguration(cname, true);
RestProducer answer = new RestProducer(this, producer, config);
answer.setOutType(outType);
answer.setType(inType);
return answer;
} else {
throw new IllegalStateException("Cannot find RestProducerFactory in Registry or as a Component to use");
}
}
Aggregations