Search in sources :

Example 1 with ClassLoaderAwareObjectInputStream

use of com.oracle.bedrock.runtime.java.io.ClassLoaderAwareObjectInputStream in project oracle-bedrock by coherence-community.

the class AbstractRemoteChannel method open.

/**
 * Opens the {@link AbstractRemoteChannel} to accept and submit {@link Callable}s.
 */
public synchronized void open() {
    if (!isOpen()) {
        setOpen(true);
        // determine the ClassLoader to use for reading requests
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        // establish the input stream to read requests
        try {
            this.input = new ClassLoaderAwareObjectInputStream(classLoader, underlyingInput);
        } catch (IOException e) {
            isReadable.set(false);
            setOpen(false);
            LOGGER.warning(this.getClass().getName() + ".open: unexpected IOException: " + e.getLocalizedMessage());
            LOGGER.info("netstats info: " + getNetStatsInfo());
            LOGGER.log(Level.FINE, "stack trace", e);
            return;
        }
        requestAcceptorThread = new Thread(new Runnable() {

            @Override
            public void run() {
                while (isReadable.get() && isWritable.get()) {
                    try {
                        // read the operation to perform
                        String operationType = input.readUTF();
                        // read the allocated sequence number for the operation
                        long sequence = input.readLong();
                        // read the serialized operation from the stream
                        int length = input.readInt();
                        byte[] bytes = new byte[length];
                        input.readFully(bytes, 0, length);
                        // attempt to instantiate, deserialize and schedule the operation for execution
                        try {
                            ByteArrayInputStream buffer = new ByteArrayInputStream(bytes);
                            ObjectInputStream stream = new ClassLoaderAwareObjectInputStream(classLoader, buffer);
                            // instantiate the operation and initialize its state
                            Class<? extends Operation> operationClass = protocol.get(operationType);
                            Constructor<? extends AbstractRemoteChannel.Operation> constructor = operationClass.getConstructor(AbstractRemoteChannel.class);
                            Operation operation = constructor.newInstance(AbstractRemoteChannel.this);
                            operation.read(stream);
                            // submit the operation for execution based on the
                            // operational stream
                            StreamName streamName = operation.getStreamName();
                            if (streamName == null) {
                                // when there's no stream name, execute the operation concurrently
                                concurrentExecutionService.submit(new Executor(sequence, operation));
                            } else {
                                // when there's stream name, execute the operation sequentially
                                sequentialExecutionService.submit(new Executor(sequence, operation));
                            }
                        } catch (Exception e) {
                            // when we can't execute the operation we notify the sender of the exception
                            sequentialExecutionService.submit(new Sender(sequence, new ResponseOperation(e)));
                        }
                    } catch (Exception e) {
                        // the stream has become corrupted or was closed
                        // (either way there's nothing else we can read or do)
                        isReadable.set(false);
                        LOGGER.log(Level.FINE, "termination of RemoteChannel:RequestAcceptor thread", e);
                    }
                }
                close();
            }
        });
        requestAcceptorThread.setName("RemoteChannel:RequestAcceptor");
        requestAcceptorThread.setDaemon(true);
        requestAcceptorThread.start();
        for (RemoteChannelListener listener : channelListeners) {
            try {
                listener.onOpened(this);
            } catch (Exception e) {
            // we ignore exceptions thrown by listeners
            }
        }
    }
}
Also used : ClassLoaderAwareObjectInputStream(com.oracle.bedrock.runtime.java.io.ClassLoaderAwareObjectInputStream) IOException(java.io.IOException) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StreamName(com.oracle.bedrock.runtime.concurrent.options.StreamName) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) ClassLoaderAwareObjectInputStream(com.oracle.bedrock.runtime.java.io.ClassLoaderAwareObjectInputStream)

Aggregations

StreamName (com.oracle.bedrock.runtime.concurrent.options.StreamName)1 ClassLoaderAwareObjectInputStream (com.oracle.bedrock.runtime.java.io.ClassLoaderAwareObjectInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 NotSerializableException (java.io.NotSerializableException)1 ObjectInputStream (java.io.ObjectInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1