@Override public <T> T lookupByNameAndType(String name, Class<T> type) { Object answer; try { answer = applicationContext.getBean(name, type); } catch (NoSuchBeanDefinitionException e) { return null; } catch (BeanNotOfRequiredTypeException e) { return null; } // just to be safe if (answer == null) { return null; } try { return type.cast(answer); } catch (Throwable e) { String msg = "Found bean: " + name + " in ApplicationContext: " + applicationContext + " of type: " + answer.getClass().getName() + " expected type was: " + type; throw new NoSuchBeanException(name, msg, e); } }
@Override public <T> T lookupByNameAndType(String name, Class<T> type) { Object object = lookupByName(name); if (object == null) { return null; } try { return type.cast(object); } catch (Throwable e) { String msg = "Found bean: " + name + " in Consul Registry: " + this + " of type: " + object.getClass().getName() + "expected type was: " + type; throw new NoSuchBeanException(name, msg, e); } }
@Override public <T> T lookupByNameAndType(String name, Class<T> type) { Object answer; try { answer = blueprintContainer.getComponentInstance(name); } catch (NoSuchComponentException e) { return null; } // just to be safe if (answer == null) { return null; } try { return type.cast(answer); } catch (Throwable e) { String msg = "Found bean: " + name + " in BlueprintContainer: " + blueprintContainer + " of type: " + answer.getClass().getName() + " expected type was: " + type; throw new NoSuchBeanException(name, msg, e); } }
protected DirContext getDirContext() throws NamingException { // Obtain our ldap context. We do this by looking up the context in our registry. // Note though that a new context is expected each time. Therefore if spring is // being used then use prototype="scope". If you do not then you might experience // concurrency issues as InitialContext is not required to support concurrency. // On the other hand if you have a DirContext that is able to support concurrency // then using the default singleton scope is entirely sufficient. Most DirContext // classes will require prototype scope though. // if its a Map/Hashtable then we create a new context per time DirContext answer = null; Object context = getEndpoint().getCamelContext().getRegistry().lookupByName(remaining); if (context instanceof Hashtable) { answer = new InitialDirContext((Hashtable<?, ?>) context); } else if (context instanceof Map) { Hashtable hash = new Hashtable((Map) context); answer = new InitialDirContext(hash); } else if (context instanceof DirContext) { answer = (DirContext) context; } else if (context != null) { String msg = "Found bean: " + remaining + " in Registry of type: " + answer.getClass().getName() + " expected type was: " + DirContext.class.getName(); throw new NoSuchBeanException(msg); } return answer; }
@Override protected Object lookupBean() throws NoSuchBeanException { try { return registry.lookup(getName()); } catch (NotBoundException e) { throw new NoSuchBeanException(getName(), e); } catch (AccessException e) { throw new RuntimeCamelException(e); } catch (RemoteException e) { throw new RuntimeCamelException(e); } } }
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); }