Search in sources :

Example 11 with ObjectOutput

use of java.io.ObjectOutput in project coprhd-controller by CoprHD.

the class DataCollectionJobSerializer method serialize.

/**
 * serialize
 *
 * @param DataCollectionJob
 * @return bytes
 */
@Override
public byte[] serialize(DataCollectionJob job) {
    byte[] Objbytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(job);
        Objbytes = bos.toByteArray();
    } catch (Exception e) {
        _logger.error("Serializing Object to byte Array Exception: ", e);
    } finally {
        try {
            out.close();
            bos.close();
        } catch (IOException e) {
            _logger.error("Error while closing Streams: ", e);
        }
    }
    return Objbytes;
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException)

Example 12 with ObjectOutput

use of java.io.ObjectOutput in project ddf by codice.

the class FileSystemPersistenceProvider method store.

@Override
public void store(String key, Object value) {
    OutputStream file = null;
    ObjectOutput output = null;
    LOGGER.trace("Entering: store - key: {}", key);
    try {
        File dir = new File(getMapStorePath());
        if (!dir.exists()) {
            boolean success = dir.mkdir();
            if (!success) {
                LOGGER.info("Could not make directory: {}", dir.getAbsolutePath());
            }
        }
        LOGGER.debug("file name: {}{}{}", getMapStorePath(), key, SER);
        file = new FileOutputStream(getMapStoreFile(key));
        OutputStream buffer = new BufferedOutputStream(file);
        output = new ObjectOutputStream(buffer);
        output.writeObject(value);
    } catch (IOException e) {
        LOGGER.info("IOException storing value in cache with key = {}", key, e);
    } finally {
        try {
            if (output != null) {
                output.close();
            }
        } catch (IOException e) {
        // Intentionally ignored
        }
        IOUtils.closeQuietly(file);
    }
    LOGGER.trace("Exiting: store");
}
Also used : ObjectOutput(java.io.ObjectOutput) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with ObjectOutput

use of java.io.ObjectOutput in project jdk8u_jdk by JetBrains.

the class UnicastRef method invoke.

/**
     * Invoke a method. This form of delegating method invocation
     * to the reference allows the reference to take care of
     * setting up the connection to the remote host, marshalling
     * some representation for the method and parameters, then
     * communicating the method invocation to the remote host.
     * This method either returns the result of a method invocation
     * on the remote object which resides on the remote host or
     * throws a RemoteException if the call failed or an
     * application-level exception if the remote invocation throws
     * an exception.
     *
     * @param obj the proxy for the remote object
     * @param method the method to be invoked
     * @param params the parameter list
     * @param opnum  a hash that may be used to represent the method
     * @since 1.2
     */
public Object invoke(Remote obj, Method method, Object[] params, long opnum) throws Exception {
    if (clientRefLog.isLoggable(Log.VERBOSE)) {
        clientRefLog.log(Log.VERBOSE, "method: " + method);
    }
    if (clientCallLog.isLoggable(Log.VERBOSE)) {
        logClientCall(obj, method);
    }
    Connection conn = ref.getChannel().newConnection();
    RemoteCall call = null;
    boolean reuse = true;
    /* If the call connection is "reused" early, remember not to
         * reuse again.
         */
    boolean alreadyFreed = false;
    try {
        if (clientRefLog.isLoggable(Log.VERBOSE)) {
            clientRefLog.log(Log.VERBOSE, "opnum = " + opnum);
        }
        // create call context
        call = new StreamRemoteCall(conn, ref.getObjID(), -1, opnum);
        // marshal parameters
        try {
            ObjectOutput out = call.getOutputStream();
            marshalCustomCallData(out);
            Class<?>[] types = method.getParameterTypes();
            for (int i = 0; i < types.length; i++) {
                marshalValue(types[i], params[i], out);
            }
        } catch (IOException e) {
            clientRefLog.log(Log.BRIEF, "IOException marshalling arguments: ", e);
            throw new MarshalException("error marshalling arguments", e);
        }
        // unmarshal return
        call.executeCall();
        try {
            Class<?> rtype = method.getReturnType();
            if (rtype == void.class)
                return null;
            ObjectInput in = call.getInputStream();
            /* StreamRemoteCall.done() does not actually make use
                 * of conn, therefore it is safe to reuse this
                 * connection before the dirty call is sent for
                 * registered refs.
                 */
            Object returnValue = unmarshalValue(rtype, in);
            /* we are freeing the connection now, do not free
                 * again or reuse.
                 */
            alreadyFreed = true;
            /* if we got to this point, reuse must have been true. */
            clientRefLog.log(Log.BRIEF, "free connection (reuse = true)");
            /* Free the call's connection early. */
            ref.getChannel().free(conn, true);
            return returnValue;
        } catch (IOException e) {
            clientRefLog.log(Log.BRIEF, "IOException unmarshalling return: ", e);
            throw new UnmarshalException("error unmarshalling return", e);
        } catch (ClassNotFoundException e) {
            clientRefLog.log(Log.BRIEF, "ClassNotFoundException unmarshalling return: ", e);
            throw new UnmarshalException("error unmarshalling return", e);
        } finally {
            try {
                call.done();
            } catch (IOException e) {
                /* WARNING: If the conn has been reused early,
                     * then it is too late to recover from thrown
                     * IOExceptions caught here. This code is relying
                     * on StreamRemoteCall.done() not actually
                     * throwing IOExceptions.
                     */
                reuse = false;
            }
        }
    } catch (RuntimeException e) {
        /*
             * Need to distinguish between client (generated by the
             * invoke method itself) and server RuntimeExceptions.
             * Client side RuntimeExceptions are likely to have
             * corrupted the call connection and those from the server
             * are not likely to have done so.  If the exception came
             * from the server the call connection should be reused.
             */
        if ((call == null) || (((StreamRemoteCall) call).getServerException() != e)) {
            reuse = false;
        }
        throw e;
    } catch (RemoteException e) {
        /*
             * Some failure during call; assume connection cannot
             * be reused.  Must assume failure even if ServerException
             * or ServerError occurs since these failures can happen
             * during parameter deserialization which would leave
             * the connection in a corrupted state.
             */
        reuse = false;
        throw e;
    } catch (Error e) {
        /* If errors occurred, the connection is most likely not
             *  reusable.
             */
        reuse = false;
        throw e;
    } finally {
        /* alreadyFreed ensures that we do not log a reuse that
             * may have already happened.
             */
        if (!alreadyFreed) {
            if (clientRefLog.isLoggable(Log.BRIEF)) {
                clientRefLog.log(Log.BRIEF, "free connection (reuse = " + reuse + ")");
            }
            ref.getChannel().free(conn, reuse);
        }
    }
}
Also used : MarshalException(java.rmi.MarshalException) ObjectOutput(java.io.ObjectOutput) Connection(sun.rmi.transport.Connection) IOException(java.io.IOException) StreamRemoteCall(sun.rmi.transport.StreamRemoteCall) UnmarshalException(java.rmi.UnmarshalException) RemoteObject(java.rmi.server.RemoteObject) ObjectInput(java.io.ObjectInput) RemoteException(java.rmi.RemoteException) StreamRemoteCall(sun.rmi.transport.StreamRemoteCall) RemoteCall(java.rmi.server.RemoteCall)

Example 14 with ObjectOutput

use of java.io.ObjectOutput in project jdk8u_jdk by JetBrains.

the class UnicastServerRef method oldDispatch.

/**
     * Handle server-side dispatch using the RMI 1.1 stub/skeleton
     * protocol, given a non-negative operation number that has
     * already been read from the call stream.
     *
     * @param obj the target remote object for the call
     * @param call the "remote call" from which operation and
     * method arguments can be obtained.
     * @param op the operation number
     * @exception IOException if unable to marshal return result or
     * release input or output streams
     */
public void oldDispatch(Remote obj, RemoteCall call, int op) throws IOException {
    // hash for matching stub with skeleton
    long hash;
    try {
        // read remote call header
        ObjectInput in;
        try {
            in = call.getInputStream();
            try {
                Class<?> clazz = Class.forName("sun.rmi.transport.DGCImpl_Skel");
                if (clazz.isAssignableFrom(skel.getClass())) {
                    ((MarshalInputStream) in).useCodebaseOnly();
                }
            } catch (ClassNotFoundException ignore) {
            }
            hash = in.readLong();
        } catch (Exception readEx) {
            throw new UnmarshalException("error unmarshalling call header", readEx);
        }
        // if calls are being logged, write out object id and operation
        logCall(obj, skel.getOperations()[op]);
        unmarshalCustomCallData(in);
        // dispatch to skeleton for remote object
        skel.dispatch(obj, call, op, hash);
    } catch (Throwable e) {
        logCallException(e);
        ObjectOutput out = call.getResultStream(false);
        if (e instanceof Error) {
            e = new ServerError("Error occurred in server thread", (Error) e);
        } else if (e instanceof RemoteException) {
            e = new ServerException("RemoteException occurred in server thread", (Exception) e);
        }
        if (suppressStackTraces) {
            clearStackTraces(e);
        }
        out.writeObject(e);
    } finally {
        // in case skeleton doesn't
        call.releaseInputStream();
        call.releaseOutputStream();
    }
}
Also used : ServerException(java.rmi.ServerException) ObjectOutput(java.io.ObjectOutput) ServerError(java.rmi.ServerError) ServerError(java.rmi.ServerError) UnmarshalException(java.rmi.UnmarshalException) ServerException(java.rmi.ServerException) ExportException(java.rmi.server.ExportException) SkeletonNotFoundException(java.rmi.server.SkeletonNotFoundException) ServerNotActiveException(java.rmi.server.ServerNotActiveException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) UnmarshalException(java.rmi.UnmarshalException) ObjectInput(java.io.ObjectInput) RemoteException(java.rmi.RemoteException)

Example 15 with ObjectOutput

use of java.io.ObjectOutput in project jdk8u_jdk by JetBrains.

the class UnicastServerRef method dispatch.

/**
     * Call to dispatch to the remote object (on the server side).
     * The up-call to the server and the marshalling of return result
     * (or exception) should be handled before returning from this
     * method.
     * @param obj the target remote object for the call
     * @param call the "remote call" from which operation and
     * method arguments can be obtained.
     * @exception IOException If unable to marshal return result or
     * release input or output streams
     */
public void dispatch(Remote obj, RemoteCall call) throws IOException {
    // positive operation number in 1.1 stubs;
    // negative version number in 1.2 stubs and beyond...
    int num;
    long op;
    try {
        // read remote call header
        ObjectInput in;
        try {
            in = call.getInputStream();
            num = in.readInt();
            if (num >= 0) {
                if (skel != null) {
                    oldDispatch(obj, call, num);
                    return;
                } else {
                    throw new UnmarshalException("skeleton class not found but required " + "for client version");
                }
            }
            op = in.readLong();
        } catch (Exception readEx) {
            throw new UnmarshalException("error unmarshalling call header", readEx);
        }
        /*
             * Since only system classes (with null class loaders) will be on
             * the execution stack during parameter unmarshalling for the 1.2
             * stub protocol, tell the MarshalInputStream not to bother trying
             * to resolve classes using its superclasses's default method of
             * consulting the first non-null class loader on the stack.
             */
        MarshalInputStream marshalStream = (MarshalInputStream) in;
        marshalStream.skipDefaultResolveClass();
        Method method = hashToMethod_Map.get(op);
        if (method == null) {
            throw new UnmarshalException("unrecognized method hash: " + "method not supported by remote object");
        }
        // if calls are being logged, write out object id and operation
        logCall(obj, method);
        // unmarshal parameters
        Object[] params = null;
        try {
            unmarshalCustomCallData(in);
            params = unmarshalParameters(obj, method, marshalStream);
        } catch (java.io.IOException e) {
            throw new UnmarshalException("error unmarshalling arguments", e);
        } catch (ClassNotFoundException e) {
            throw new UnmarshalException("error unmarshalling arguments", e);
        } finally {
            call.releaseInputStream();
        }
        // make upcall on remote object
        Object result;
        try {
            result = method.invoke(obj, params);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
        // marshal return value
        try {
            ObjectOutput out = call.getResultStream(true);
            Class<?> rtype = method.getReturnType();
            if (rtype != void.class) {
                marshalValue(rtype, result, out);
            }
        } catch (IOException ex) {
            throw new MarshalException("error marshalling return", ex);
        /*
                 * This throw is problematic because when it is caught below,
                 * we attempt to marshal it back to the client, but at this
                 * point, a "normal return" has already been indicated,
                 * so marshalling an exception will corrupt the stream.
                 * This was the case with skeletons as well; there is no
                 * immediately obvious solution without a protocol change.
                 */
        }
    } catch (Throwable e) {
        logCallException(e);
        ObjectOutput out = call.getResultStream(false);
        if (e instanceof Error) {
            e = new ServerError("Error occurred in server thread", (Error) e);
        } else if (e instanceof RemoteException) {
            e = new ServerException("RemoteException occurred in server thread", (Exception) e);
        }
        if (suppressStackTraces) {
            clearStackTraces(e);
        }
        out.writeObject(e);
    } finally {
        // in case skeleton doesn't
        call.releaseInputStream();
        call.releaseOutputStream();
    }
}
Also used : MarshalException(java.rmi.MarshalException) ServerException(java.rmi.ServerException) ObjectOutput(java.io.ObjectOutput) ServerError(java.rmi.ServerError) ServerError(java.rmi.ServerError) IOException(java.io.IOException) Method(java.lang.reflect.Method) IOException(java.io.IOException) UnmarshalException(java.rmi.UnmarshalException) ServerException(java.rmi.ServerException) ExportException(java.rmi.server.ExportException) SkeletonNotFoundException(java.rmi.server.SkeletonNotFoundException) ServerNotActiveException(java.rmi.server.ServerNotActiveException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnmarshalException(java.rmi.UnmarshalException) ObjectInput(java.io.ObjectInput) RemoteException(java.rmi.RemoteException)

Aggregations

ObjectOutput (java.io.ObjectOutput)105 ObjectOutputStream (java.io.ObjectOutputStream)78 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 IOException (java.io.IOException)53 ObjectInput (java.io.ObjectInput)26 Test (org.junit.Test)24 ObjectInputStream (java.io.ObjectInputStream)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 WorkingMemory (org.drools.core.WorkingMemory)13 FileOutputStream (java.io.FileOutputStream)12 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)12 Pattern (org.drools.core.rule.Pattern)12 Consequence (org.drools.core.spi.Consequence)12 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)12 OutputStream (java.io.OutputStream)11 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 Declaration (org.drools.core.rule.Declaration)8 IntrospectionException (java.beans.IntrospectionException)7 File (java.io.File)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7