Example usage for org.apache.commons.lang SerializationUtils serialize

List of usage examples for org.apache.commons.lang SerializationUtils serialize

Introduction

In this page you can find the example usage for org.apache.commons.lang SerializationUtils serialize.

Prototype

public static byte[] serialize(Serializable obj) 

Source Link

Document

Serializes an Object to a byte array for storage/serialization.

Usage

From source file:org.optaplanner.core.impl.testdata.util.SerializationTestUtils.java

public static <T> T serializeAndDeserializeWithJavaSerialization(T input) {
    byte[] bytes = SerializationUtils.serialize((Serializable) input);
    return (T) SerializationUtils.deserialize(bytes);
}

From source file:org.raml.parser.RamlSerializationTestCase.java

@Test
public void roundtrip() {
    MimeType mimeType = new MimeType();
    mimeType.setFormParameters(buildMap(Collections.singletonList(new FormParameter())));

    Action action = new Action();
    action.setBody(buildMap(mimeType));/*  w ww  .j a  v  a2  s. com*/
    action.setResponses(buildMap(new Response()));

    Resource resource = new Resource();
    resource.setActions(buildMap(ActionType.GET, action));

    SecuritySchemeDescriptor describedBy = new SecuritySchemeDescriptor();
    describedBy.setHeaders(buildMap(new Header()));

    SecurityScheme securityScheme = new SecurityScheme();
    securityScheme.setDescribedBy(describedBy);
    securityScheme.setSettings(new SecuritySettings());

    Raml raml = new Raml();
    raml.setTitle("hi");
    raml.setBaseUriParameters(buildMap(new UriParameter()));
    raml.setDocumentation(Collections.singletonList(new DocumentationItem()));
    raml.setResources(buildMap(resource));
    raml.setProtocols(Collections.singletonList(Protocol.HTTP));
    raml.setResourceTypes(Collections.singletonList(buildMap(new Template())));
    raml.setTraits(Collections.singletonList(buildMap(new Template())));
    raml.setSecuritySchemes(Collections.singletonList(buildMap(securityScheme)));
    raml.setSecuredBy(Collections.singletonList(new SecurityReference("ref")));

    byte[] bytes = SerializationUtils.serialize(raml);
    Raml copy = (Raml) SerializationUtils.deserialize(bytes);
    assertThat(copy.getTitle(), is(raml.getTitle()));
}

From source file:org.springframework.batch.integration.chunk.ChunkRequestTests.java

@Test
public void testSerializable() throws Exception {
    @SuppressWarnings("unchecked")
    ChunkRequest<String> result = (ChunkRequest<String>) SerializationUtils
            .deserialize(SerializationUtils.serialize(request));
    assertNotNull(result.getStepContribution());
    assertEquals(111L, result.getJobId());
    assertEquals(2, result.getItems().size());
}

From source file:org.springframework.batch.integration.chunk.ChunkResponseTests.java

@Test
public void testSerializable() throws Exception {
    ChunkResponse result = (ChunkResponse) SerializationUtils
            .deserialize(SerializationUtils.serialize(response));
    assertNotNull(result.getStepContribution());
    assertEquals(new Long(111L), result.getJobId());
}

From source file:org.springframework.batch.sample.common.StagingItemWriter.java

/**
 * Serialize the item to the staging table, and add a NEW processed flag.
 * //from  w ww .  jav a2  s  .  co m
 * @see ItemWriter#write(java.util.List)
 */
public void write(final List<? extends T> items) {

    final ListIterator<? extends T> itemIterator = items.listIterator();
    getJdbcTemplate().batchUpdate("INSERT into BATCH_STAGING (ID, JOB_ID, VALUE, PROCESSED) values (?,?,?,?)",
            new BatchPreparedStatementSetter() {

                public int getBatchSize() {
                    return items.size();
                }

                public void setValues(PreparedStatement ps, int i) throws SQLException {

                    long id = incrementer.nextLongValue();
                    long jobId = stepExecution.getJobExecution().getJobId();

                    Assert.state(itemIterator.nextIndex() == i,
                            "Item ordering must be preserved in batch sql update");

                    byte[] blob = SerializationUtils.serialize((Serializable) itemIterator.next());

                    ps.setLong(1, id);
                    ps.setLong(2, jobId);
                    ps.setBytes(3, blob);
                    ps.setString(4, NEW);
                }
            });

}

From source file:org.wso2.andes.server.handler.DtxRecoverHandler.java

@Override
public void methodReceived(AMQStateManager stateManager, DtxRecoverBodyImpl body, int channelId)
        throws AMQException {
    AMQProtocolSession session = stateManager.getProtocolSession();

    AMQChannel channel = session.getChannel(channelId);

    if (channel == null) {
        throw body.getChannelNotFoundException(channelId);
    }/*w  w  w. j av a2  s . c  o m*/

    try {
        ArrayList<Xid> inDoubtXids = channel.recoverDtxTransactions();
        MethodRegistry_0_91 methodRegistry = (MethodRegistry_0_91) session.getMethodRegistry();

        byte[] serializedData = SerializationUtils.serialize(inDoubtXids);
        DtxRecoverOkBody dtxRecoverOkBody = methodRegistry.createDtxRecoverOkBody(serializedData);
        session.writeFrame(dtxRecoverOkBody.generateFrame(channelId));
    } catch (DtxNotSelectedException e) {
        throw body.getChannelException(AMQConstant.NOT_ALLOWED, "Not a distributed transacted session", e);
    }
}

From source file:pluginmanager.RequestManager.java

@Override
public void run() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");

    // RESPONSE QUEUE STUFF
    try {/*from w  w w .j  a va  2s  .  co m*/
        responseConnection = factory.newConnection();
        responseChannel = responseConnection.createChannel();
        responseChannel.queueDeclare(RESPONSE_QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    } catch (IOException | TimeoutException e2) {
        e2.printStackTrace();
    }

    Consumer consumer = new DefaultConsumer(responseChannel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            HttpResponse response = (HttpResponse) SerializationUtils.deserialize(body);
            try {
                response.write(sockDrawer.get(response.id).getOutputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    try {
        responseChannel.basicConsume(RESPONSE_QUEUE_NAME, true, consumer);
    } catch (IOException e2) {
        e2.printStackTrace();
    }

    //REQUEST QUEUE STUFF
    try {
        requestConnection = factory.newConnection();
        requestChannel = requestConnection.createChannel();
        requestChannel.queueDeclare(REQUEST_QUEUE_NAME, false, false, false, null);
    } catch (IOException | TimeoutException e1) {
        e1.printStackTrace();
    }

    System.out.println("Request Manger Running");
    while (true) {
        if (!queue.isEmpty()) {
            System.out.println("Processing");
            PriorityRequest request = queue.poll();
            byte[] data = SerializationUtils.serialize(request);
            try {
                requestChannel.basicPublish("", REQUEST_QUEUE_NAME, null, data);
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            //response.write(socket.getOutputStream());
        } else {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

From source file:retrieval.server.globaldatabase.RedisDatabase.java

public void putToPurge(String storage, Map<Long, Integer> toPurge) {
    try (Jedis redis = databasePurge.getResource()) {
        HashMap<Long, Integer> map;

        byte[] data = redis.hget(SerializationUtils.serialize(KEY_PURGE_STORE),
                SerializationUtils.serialize(storage));
        if (data != null) {
            map = (HashMap<Long, Integer>) SerializationUtils.deserialize(data);
        } else {//from w  ww  .ja v  a2 s . co  m
            map = new HashMap<Long, Integer>();
        }
        map.putAll(toPurge);
        redis.hset(SerializationUtils.serialize(KEY_PURGE_STORE), SerializationUtils.serialize(storage),
                SerializationUtils.serialize(map));
    }
    ;
}

From source file:retrieval.server.globaldatabase.RedisDatabase.java

public Map<Long, Integer> getPicturesToPurge(String storage) {
    HashMap<Long, Integer> map;
    try (Jedis redis = databasePurge.getResource()) {

        byte[] data = redis.hget(SerializationUtils.serialize(KEY_PURGE_STORE),
                SerializationUtils.serialize(storage));
        if (data != null) {
            map = (HashMap<Long, Integer>) SerializationUtils.deserialize(data);
        } else {/*from w w w . j a v a  2 s  .com*/
            map = new HashMap<Long, Integer>();
        }
    }
    return map;
}

From source file:retrieval.server.globaldatabase.RedisDatabase.java

public void clearPurge(String storage) {
    try (Jedis redis = databasePurge.getResource()) {
        redis.hset(SerializationUtils.serialize(KEY_PURGE_STORE), SerializationUtils.serialize(storage),
                SerializationUtils.serialize(new HashMap<Long, Integer>()));
    }//w ww .  ja va2s  .co m
}