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

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

Introduction

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

Prototype

public static Object deserialize(byte[] objectData) 

Source Link

Document

Deserializes a single Object from an array of bytes.

Usage

From source file:org.xdi.service.ObjectSerializationService.java

public Object loadObject(String path) {
    File file = new File(path);
    if (!file.exists()) {
        log.trace("File '{0}' is not exist", path);
        return null;
    }//w  w  w. jav a 2 s .  c  o  m

    FileInputStream fis;
    try {
        fis = new FileInputStream(file);
    } catch (FileNotFoundException ex) {
        log.error("Faield to deserialize from file: '{0}'. Error: ", path, ex);

        return null;
    }

    BufferedInputStream bis = new BufferedInputStream(fis);
    Object obj = null;
    try {
        GZIPInputStream gis = new GZIPInputStream(bis);
        obj = SerializationUtils.deserialize(gis);
        IOUtils.closeQuietly(gis);
    } catch (IOException ex) {
        log.error("Faield to deserialize from file: '{0}'. Error: ", path, ex);
        IOUtils.closeQuietly(bis);

        return null;
    }

    return obj;
}

From source file:pluginmanager.RequestManager.java

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

    // RESPONSE QUEUE STUFF
    try {/* w w  w.  j av a2s.  c  o 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 va 2  s .  c  o 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 {//w  ww .  ja va 2 s .  c  o  m
            map = new HashMap<Long, Integer>();
        }
    }
    return map;
}

From source file:server.Worker.java

/**
 * @param args//from   w w  w  .j  av  a2s  .  co  m
 * @throws IOException
 * @throws TimeoutException
 */
public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection requestConnection = factory.newConnection();
    Channel requestChannel = requestConnection.createChannel();
    requestChannel.queueDeclare(REQUEST_QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Connection responseConnection = factory.newConnection();
    final Channel responseChannel = responseConnection.createChannel();
    responseChannel.queueDeclare(RESPONSE_QUEUE_NAME, false, false, false, null);

    Consumer consumer = new DefaultConsumer(requestChannel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            PriorityRequest request = (PriorityRequest) SerializationUtils.deserialize(body);
            System.out.println(" [x] Received");
            System.out.println(request.request.toString());
            System.out.println("*************************************");

            System.out.println(request.request.getMethod());
            HttpResponse response = PluginManager.getInstance().process(request.request, request.rootDir);
            if (response == null) {
                response = HttpResponseFactory.create400BadRequest(Protocol.CLOSE);
            }
            try {
                System.out.println(request.id);
            } catch (Exception e) {
                e.printStackTrace();
            }

            response.id = request.id;

            byte[] data = SerializationUtils.serialize(response);
            responseChannel.basicPublish("", RESPONSE_QUEUE_NAME, null, data);
        }
    };
    requestChannel.basicConsume(REQUEST_QUEUE_NAME, true, consumer);
}

From source file:stroom.statistics.common.TestTimeAgnosticStatisticEvent.java

@Test
public void serialiseTest() {
    final List<StatisticTag> tagList = new ArrayList<StatisticTag>();
    tagList.add(new StatisticTag("tag1", "val1"));
    tagList.add(new StatisticTag("tag2", "val2"));

    final TimeAgnosticStatisticEvent timeAgnosticStatisticEvent = new TimeAgnosticStatisticEvent("MtStatName",
            tagList, 42L);//from   www  .jav  a  2  s  .  c  o  m

    // if we can't serialise the object then we should get an exception here
    final byte[] serializedForm = SerializationUtils.serialize(timeAgnosticStatisticEvent);

    final TimeAgnosticStatisticEvent timeAgnosticStatisticEvent2 = (TimeAgnosticStatisticEvent) SerializationUtils
            .deserialize(serializedForm);

    Assert.assertEquals(timeAgnosticStatisticEvent, timeAgnosticStatisticEvent2);
}

From source file:stroom.statistics.server.sql.TestTimeAgnosticStatisticEvent.java

@Test
public void serialiseTest() {
    final List<StatisticTag> tagList = new ArrayList<>();
    tagList.add(new StatisticTag("tag1", "val1"));
    tagList.add(new StatisticTag("tag2", "val2"));

    final TimeAgnosticStatisticEvent timeAgnosticStatisticEvent = TimeAgnosticStatisticEvent
            .createCount("MtStatName", tagList, 42L);

    // if we can't serialise the object then we should get an exception here
    final byte[] serializedForm = SerializationUtils.serialize(timeAgnosticStatisticEvent);

    final TimeAgnosticStatisticEvent timeAgnosticStatisticEvent2 = (TimeAgnosticStatisticEvent) SerializationUtils
            .deserialize(serializedForm);

    Assert.assertEquals(timeAgnosticStatisticEvent, timeAgnosticStatisticEvent2);
}

From source file:uk.ac.ebi.interpro.scan.model.ProteinTest.java

/**
 * Tests the serialization and de-serialization works.
 *//*  w  w  w .  j a va 2  s . c o m*/
@Test
public void testSerialization() throws IOException {

    Protein original = new Protein(GOOD);
    original.addCrossReference(new ProteinXref("A0A000_9ACTO"));

    Set<Hmmer2Match.Hmmer2Location> locations = new HashSet<Hmmer2Match.Hmmer2Location>();
    locations.add(new Hmmer2Match.Hmmer2Location(3, 107, 3.0, 3.7e-9, 1, 104, HmmBounds.N_TERMINAL_COMPLETE));
    original.addMatch(new Hmmer2Match(new Signature("PF02310", "B12-binding"), 0.035, 3.7e-9, locations));

    Set<ProfileScanMatch.ProfileScanLocation> l = new HashSet<ProfileScanMatch.ProfileScanLocation>();
    // Sequence is 60 chars, so make up a CIGAR string that adds up to 60 (10+10+30):
    l.add(new ProfileScanMatch.ProfileScanLocation(1, 60, 15.158, "10M10D10I30M"));
    original.addMatch(new ProfileScanMatch(new Signature("PS50206"), l));

    byte[] data = SerializationUtils.serialize(original);
    String originalXML = marshal(original);
    Object retrieved = SerializationUtils.deserialize(data);
    String unserializedXML = marshal((Protein) retrieved);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(unserializedXML);
        LOGGER.debug("Data lengths serialized: " + data.length + " original xml: " + originalXML.length()
                + " unserialized xml: " + unserializedXML.length());
    }

    XMLUnit.setIgnoreComments(true);
    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setIgnoreAttributeOrder(true);
    XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
    XMLUnit.setNormalizeWhitespace(true);
    assertEquals(originalXML, unserializedXML);
}

From source file:uk.bl.wa.hadoop.indexer.WritableSolrRecord.java

@Override
public void readFields(DataInput input) throws IOException {
    int length = input.readInt();
    byte[] bytes = new byte[length];
    input.readFully(bytes);//w  ww  . j ava  2  s .  com
    if (this.sr == null) {
        this.sr = SolrRecordFactory.createFactory(null).createRecord();
    }
    this.sr.setSolrDocument((SolrInputDocument) SerializationUtils.deserialize(bytes));
}

From source file:ws.prager.camel.consul.ConsulRegistry.java

@Override
public Object lookupByName(String key) {
    // Substitute $ character in key
    key = key.replaceAll("\\$", "/");
    logger.debug("lookup by name: " + key);
    kvClient = consul.keyValueClient();/*ww w .  ja  va  2  s  .c o m*/
    Optional<String> result = kvClient.getValueAsString(key);
    if (result.isPresent()) {
        byte[] postDecodedValue = Base64.decodeBase64(result.get());
        logger.debug("got result: " + postDecodedValue);
        return SerializationUtils.deserialize(postDecodedValue);
    }
    return null;
}