public void addInterceptor(Interceptor interceptor) { if (interceptorChainHead == null) { interceptorChainHead = interceptor; } else { interceptorChainTail.setNext(interceptor); } interceptorChainTail = interceptor; }
private Interceptor getInterceptor() { // lazy load the interceptor as it may not have been added when the instance was created in the wire attacher if (interceptor == null) { interceptor = wire.getInvocationChains().iterator().next().getHeadInterceptor(); } return interceptor; } }
public void onAttach(Wire wire) { for (InvocationChain chain : wire.getInvocationChains()) { SecurityContextInterceptor interceptor = new SecurityContextInterceptor(); chain.addInterceptor(interceptor); } } }
public void attach(JUnitSourceDefinition source, PhysicalTargetDefinition target, Wire wire) throws WiringException { String testName = source.getTestName(); ContextConfiguration configuration = source.getConfiguration(); if (configuration != null) { if (authenticationService == null) { throw new WiringException("Security information set for the test but a security extension has not been installed in the runtime"); } // configuration an authentication interceptor to set the subject on the work context for (InvocationChain chain : wire.getInvocationChains()) { Interceptor next = chain.getHeadInterceptor(); String username = configuration.getUsername(); String password = configuration.getPassword(); AuthenticatingInterceptor interceptor = new AuthenticatingInterceptor(username, password, authenticationService, next); chain.addInterceptor(0, interceptor); } } holder.add(testName, wire); }
public Message invoke(Message msg) { transformInput(msg); Message ret = next.invoke(msg); return transformOutput(ret); }
private void addTransformer(List<InvocationChain> chains, ClassLoader loader) throws BrokerException { for (InvocationChain chain : chains) { try { PhysicalOperationDefinition physicalOperation = chain.getPhysicalOperation(); List<DataType<?>> targetTypes = createTypes(physicalOperation, loader); Interceptor interceptor = interceptorFactory.createInterceptor(physicalOperation, TRANSPORT_TYPES, targetTypes, loader, loader); chain.addInterceptor(new WrappingInterceptor()); chain.addInterceptor(interceptor); } catch (InterceptorCreationException e) { throw new BrokerException(e); } } }
/** * Returns the invocation chains for a wire in their natural order. * * @param wire the wire * @return the invocation chains */ public static List<InvocationChain> sortChains(Wire wire) { TreeMap<PhysicalOperationDefinition, InvocationChain> map = new TreeMap<PhysicalOperationDefinition, InvocationChain>(); for (InvocationChain chain : wire.getInvocationChains()) { map.put(chain.getPhysicalOperation(), chain); } List<InvocationChain> sorted = new ArrayList<InvocationChain>(); sorted.addAll(map.values()); return sorted; }
Wire createWire(PhysicalWireDefinition definition) throws BuilderException { Wire wire = new WireImpl(); for (PhysicalOperationDefinition operation : definition.getOperations()) { InvocationChain chain = new InvocationChainImpl(operation); for (PhysicalInterceptorDefinition interceptorDefinition : operation.getInterceptors()) { InterceptorBuilder<? super PhysicalInterceptorDefinition> builder = getBuilder(interceptorDefinition); Interceptor interceptor = builder.build(interceptorDefinition); chain.addInterceptor(interceptor); } wire.addInvocationChain(chain); } processTransform(wire, definition); return wire; }
/** * Determines if the wire is one-way or request-reply. The first operation is used to determine if the contract is one-way as the binding does not * support mixing one-way and request-response operations on a service contract. * * @param chains the wire invocation chains * @param uri thr service URI. * @return true if the wire is one-way */ private boolean isOneWay(List<InvocationChain> chains, URI uri) { if (chains.size() < 1) { throw new AssertionError("Contract must have at least one operation: " + uri); } return chains.get(0).getPhysicalOperation().isOneWay(); }
/** * Constructor. * * @param manager the ZeroMQ Context manager * @param address the address to receive messages on * @param chains the invocation chains for dispatching invocations * @param socketType the socket type as defined by ZeroMQ * @param metadata metadata * @param executorService the executor for scheduling work * @param monitor the monitor */ public AbstractReceiver(ContextManager manager, SocketAddress address, List<InvocationChain> chains, int socketType, ZeroMQMetadata metadata, ExecutorService executorService, MessagingMonitor monitor) { this.manager = manager; this.address = address; this.executorService = executorService; this.interceptors = new Interceptor[chains.size()]; for (int i = 0, chainsSize = chains.size(); i < chainsSize; i++) { InvocationChain chain = chains.get(i); interceptors[i] = chain.getHeadInterceptor(); } this.socketType = socketType; this.metadata = metadata; this.monitor = monitor; }
/** * Performs the invocation on the target component instance. If a target classloader is configured for the interceptor, it will be set as the TCCL. * * @param event the event * @param instance the target component instance */ private void invoke(Object event, Object instance) { try { if (targetTCCLClassLoader == null) { invoker.invoke(instance, event); } else { ClassLoader old = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(targetTCCLClassLoader); invoker.invoke(instance, event); } finally { Thread.currentThread().setContextClassLoader(old); } } } catch (InvocationTargetException e) { throw new InvocationRuntimeException(e); } catch (IllegalAccessException e) { throw new InvocationRuntimeException(e); } }
public void addInterceptor(int index, Interceptor interceptor) { int i = 0; Interceptor next = interceptorChainHead; Interceptor prev = null; while (next != null && i < index) { prev = next; next = next.getNext(); i++; } if (i == index) { if (prev != null) { prev.setNext(interceptor); } else { interceptorChainHead = interceptor; } interceptor.setNext(next); if (next == null) { interceptorChainTail = interceptor; } } else { throw new ArrayIndexOutOfBoundsException(index); } }
/** * Loads the source-side parameter types in the contribution classloader associated with the source component. * * @param definition the physical operation definition * @param loader the contribution classloader * @return a collection of loaded parameter types * @throws InterceptorCreationException * if an error occurs loading the parameter types */ private List<Class<?>> loadSourceInputTypes(PhysicalOperationDefinition definition, ClassLoader loader) throws InterceptorCreationException { try { return ParameterTypeHelper.loadSourceInParameterTypes(definition, loader); } catch (ClassNotFoundException e) { throw new InterceptorCreationException(e); } }
public Message invoke(Message msg) { msg.setBody(new Object[]{msg.getBody()}); return next.invoke(msg); }
public Message invoke(Message msg) { Object instance; try { instance = component.getInstance(); } catch (InstanceLifecycleException e) { throw new InvocationRuntimeException(e); } try { return invoke(msg, instance); } finally { try { component.releaseInstance(instance); } catch (ComponentException e) { throw new InvocationRuntimeException(e); } } }
/** * Loads the target-side parameter types in the contribution classloader associated with the target service. * * @param definition the physical operation definition * @param loader the contribution classloader * @return a collection of loaded parameter types * @throws InterceptorCreationException * if an error occurs loading the parameter types */ private List<Class<?>> loadTargetInputTypes(PhysicalOperationDefinition definition, ClassLoader loader) throws InterceptorCreationException { try { return ParameterTypeHelper.loadTargetInParameterTypes(definition, loader); } catch (ClassNotFoundException e) { throw new InterceptorCreationException(e); } }
public Message invoke(Message msg) { Object body = msg.getBody(); if (body == null || !body.getClass().isArray()) { return next.invoke(msg); } Object[] payload = (Object[]) body; if (payload.length != 1) { throw new ServiceRuntimeException("Unexpected payload size: " + payload.length); } msg.setBody(payload[0]); return next.invoke(msg); }
public void handle(Object event, boolean endOfBatch) { WorkContextCache.getAndResetThreadWorkContext(); Object instance; try { instance = component.getInstance(); } catch (InstanceLifecycleException e) { throw new InvocationRuntimeException(e); } try { invoke(event, instance); } finally { try { component.releaseInstance(instance); } catch (InstanceDestructionException e) { throw new InvocationRuntimeException(e); } } }
public Message invoke(Message msg) { Message ret = next.invoke(msg); if (ret.isFault() && expected.equals(ret.getBody().getClass())) { ret.setBody(null); } return ret; }
public void run() { Message request = MessageCache.getAndResetMessage(); try { request.setBody(frames[0]); int methodIndex = ByteBuffer.wrap(frames[1]).getInt(); WorkContext context = setWorkContext(frames[2]); request.setWorkContext(context); Interceptor interceptor = interceptors[methodIndex]; interceptor.invoke(request); } finally { request.reset(); } } });