Example usage for org.apache.commons.io.input ClassLoaderObjectInputStream ClassLoaderObjectInputStream

List of usage examples for org.apache.commons.io.input ClassLoaderObjectInputStream ClassLoaderObjectInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.input ClassLoaderObjectInputStream ClassLoaderObjectInputStream.

Prototype

public ClassLoaderObjectInputStream(ClassLoader classLoader, InputStream inputStream)
        throws IOException, StreamCorruptedException 

Source Link

Document

Constructs a new ClassLoaderObjectInputStream.

Usage

From source file:org.jbpm.executor.impl.ExecutorRunnable.java

@SuppressWarnings("unchecked")
public void run() {
    logger.debug("Executor Thread {} Waking Up!!!", this.toString());
    try {//from w  w  w.  java2 s. c o  m
        RequestInfo request = (RequestInfo) queryService.getRequestForProcessing();
        if (request != null) {
            CommandContext ctx = null;
            List<CommandCallback> callbacks = null;
            try {

                logger.debug("Processing Request Id: {}, status {} command {}", request.getId(),
                        request.getStatus(), request.getCommandName());
                ClassLoader cl = getClassLoader(request.getDeploymentId());

                byte[] reqData = request.getRequestData();
                if (reqData != null) {
                    ObjectInputStream in = null;
                    try {
                        in = new ClassLoaderObjectInputStream(cl, new ByteArrayInputStream(reqData));
                        ctx = (CommandContext) in.readObject();
                    } catch (IOException e) {
                        logger.warn("Exception while serializing context data", e);
                        return;
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                }
                callbacks = classCacheManager.buildCommandCallback(ctx, cl);

                Command cmd = classCacheManager.findCommand(request.getCommandName(), cl);
                ExecutionResults results = cmd.execute(ctx);
                for (CommandCallback handler : callbacks) {

                    handler.onCommandDone(ctx, results);
                }

                if (results != null) {
                    try {
                        ByteArrayOutputStream bout = new ByteArrayOutputStream();
                        ObjectOutputStream out = new ObjectOutputStream(bout);
                        out.writeObject(results);
                        byte[] respData = bout.toByteArray();
                        request.setResponseData(respData);
                    } catch (IOException e) {
                        request.setResponseData(null);
                    }
                }

                request.setStatus(STATUS.DONE);
                commandService.execute(new MergeObjectCommand(request));

            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } catch (Throwable e) {
                logger.warn("Error during command {} execution {}", request.getCommandName(), e.getMessage());

                ErrorInfo errorInfo = new ErrorInfo(e.getMessage(),
                        ExceptionUtils.getFullStackTrace(e.fillInStackTrace()));
                errorInfo.setRequestInfo(request);

                ((List<ErrorInfo>) request.getErrorInfo()).add(errorInfo);
                logger.debug("Error Number: {}", request.getErrorInfo().size());
                if (request.getRetries() > 0) {
                    request.setStatus(STATUS.RETRYING);
                    request.setRetries(request.getRetries() - 1);
                    request.setExecutions(request.getExecutions() + 1);
                    logger.debug("Retrying ({}) still available!", request.getRetries());

                    commandService.execute(new MergeObjectCommand(request));
                } else {
                    logger.debug("Error no retries left!");
                    request.setStatus(STATUS.ERROR);
                    request.setExecutions(request.getExecutions() + 1);

                    commandService.execute(new MergeObjectCommand(request));

                    if (callbacks != null) {
                        for (CommandCallback handler : callbacks) {
                            handler.onCommandError(ctx, e);
                        }
                    }

                }

            }
        }
    } catch (Exception e) {
        logger.warn("Unexpected error while processin executor's job {}", e.getMessage(), e);
    }
}

From source file:org.jbpm.process.workitem.jms.JMSSignalReceiver.java

protected Object readData(BytesMessage message, ClassLoader cl) throws JMSException, Exception {
    Object data = null;/*  w  ww .ja v a 2  s. co m*/
    if (message.getBodyLength() > 0) {
        byte[] reqData = new byte[(int) message.getBodyLength()];

        message.readBytes(reqData);
        if (reqData != null) {
            ObjectInputStream in = null;
            try {
                in = new ClassLoaderObjectInputStream(cl, new ByteArrayInputStream(reqData));
                data = in.readObject();
            } catch (IOException e) {
                logger.warn("Exception while serializing context data", e);

            } finally {
                if (in != null) {
                    in.close();
                }
            }
        }
    }
    return data;
}

From source file:org.jbpm.services.ejb.remote.api.AbstractRemoteObject.java

protected Object deserialize(byte[] bytes) {
    Object result = null;/*from   www  . jav  a  2 s.co m*/
    ObjectInputStream in = null;
    try {
        in = new ClassLoaderObjectInputStream(classLoader, new ByteArrayInputStream(bytes));
        result = in.readObject();
    } catch (Exception e) {

        throw new RuntimeException("Unable to deserialize stream ", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return result;
}

From source file:org.kie.server.services.jbpm.ExecutorServiceBase.java

protected Map<String, Object> readContent(byte[] data, ClassLoader classLoader) {
    Object result = null;/*www . j ava 2 s .c om*/
    if (data != null) {
        ObjectInputStream in = null;
        try {
            in = new ClassLoaderObjectInputStream(classLoader, new ByteArrayInputStream(data));
            result = in.readObject();
        } catch (Exception e) {
            logger.warn("Exception while serializing context data", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {

                }
            }
        }
        if (result instanceof CommandContext) {
            return ((CommandContext) result).getData();
        } else if (result instanceof ExecutionResults) {
            return ((ExecutionResults) result).getData();
        }
    }

    return new HashMap<String, Object>();
}

From source file:org.microtitan.diffusive.diffuser.serializer.ObjectSerializer.java

@Override
public synchronized <T> T deserialize(final InputStream input, final Class<T> clazz) {
    // read the input stream into an object. we use the the (apache commons-io) ClassLoaderObjectInputStream
    // to read the object because we need to be able to use the same class loader that loaded the class in
    // the first place (for example, the RestfulClassLoader).
    T object = null;/*from  w  ww .j  a v a 2s.c  om*/
    try (final ClassLoaderObjectInputStream in = new ClassLoaderObjectInputStream(clazz.getClassLoader(),
            input)) {
        object = clazz.cast(in.readObject());
    } catch (IOException | ClassNotFoundException e) {
        final StringBuffer message = new StringBuffer();
        message.append("Unable to serialize object to output stream:" + Constants.NEW_LINE);
        message.append("  Input Stream Type: " + input.getClass().getName() + Constants.NEW_LINE);
        message.append("  Object Type: " + clazz.getName() + Constants.NEW_LINE);
        LOGGER.error(message.toString(), e);
        throw new IllegalArgumentException(message.toString(), e);
    }
    return object;
}

From source file:org.mule.transport.tcp.protocols.CustomClassLoadingLengthProtocol.java

@Override
public Object read(InputStream is) throws IOException {
    byte[] bytes = (byte[]) super.read(is);

    if (bytes == null) {
        return null;
    } else {/*from  www  .ja v a2  s .co m*/
        ClassLoaderObjectInputStream classLoaderIS = new ClassLoaderObjectInputStream(this.getClassLoader(),
                is);
        try {
            return classLoaderIS.readObject();
        } catch (ClassNotFoundException e) {
            logger.warn(e.getMessage());
            IOException iox = new IOException();
            iox.initCause(e);
            throw iox;
        } finally {
            classLoaderIS.close();
        }
    }
}

From source file:org.mule.util.SerializationUtils.java

/**
 * <p>Deserializes an <code>Object</code> from the specified stream.</p>
 * <p/>/*w w w.  jav a2  s  . c  om*/
 * <p>The stream will be closed once the object is written. This
 * avoids the need for a finally clause, and maybe also exception
 * handling, in the application code.</p>
 * <p/>
 * <p>The stream passed in is not buffered internally within this method.
 * This is the responsibility of your application if desired.</p>
 *
 * @param inputStream the serialized object input stream, must not be null
 * @param cl          classloader which can load custom classes from the stream
 * @return the deserialized object
 * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
 * @throws org.apache.commons.lang.SerializationException
 *                                  (runtime) if the serialization fails
 */
private static Object deserialize(InputStream inputStream, ClassLoader cl, MuleContext muleContext) {
    if (inputStream == null) {
        throw new IllegalArgumentException("The InputStream must not be null");
    }
    if (cl == null) {
        throw new IllegalArgumentException("The ClassLoader must not be null");
    }
    ObjectInputStream in = null;
    try {
        // stream closed in the finally
        in = new ClassLoaderObjectInputStream(cl, inputStream);
        Object obj = in.readObject();
        if (obj instanceof DeserializationPostInitialisable) {
            DeserializationPostInitialisable.Implementation.init(obj, muleContext);
        }
        return obj;
    } catch (ClassNotFoundException ex) {
        throw new SerializationException(ex);
    } catch (IOException ex) {
        throw new SerializationException(ex);
    } catch (Exception ex) {
        throw new SerializationException(ex);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
}

From source file:org.srlutils.Files.java

public static Object load(byte[] data, int offset, int length, ClassLoader loader) {
    if (length == -1)
        length = data.length - offset;//ww  w . j  a va2  s . co  m
    ByteArrayInputStream stream = null;
    ObjectInputStream in = null;
    Object obj = null;
    RuntimeException rte = null;
    try {
        stream = new ByteArrayInputStream(data, offset, length);
        in = (loader == null) ? new ObjectInputStream(stream)
                : new ClassLoaderObjectInputStream(loader, stream);
        obj = in.readObject();
    } catch (Exception ex) {
        rte = rte(ex, "could not instatiate object from data: %s", data);
    } finally {
        try {
            in.close();
        } catch (Exception ex) {
        }
        try {
            stream.close();
        } catch (Exception ex) {
        }
    }
    if (rte != null)
        throw rte;
    return obj;
}