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.opendaylight.controller.cluster.datastore.CompositeModificationByteStringPayloadTest.java

@Test
public void testSerialization() {
    WriteModification writeModification = new WriteModification(TestModel.TEST_PATH,
            ImmutableNodes.containerNode(TestModel.TEST_QNAME));

    MutableCompositeModification compositeModification = new MutableCompositeModification();

    compositeModification.addModification(writeModification);

    CompositeModificationByteStringPayload compositeModificationByteStringPayload = new CompositeModificationByteStringPayload(
            compositeModification.toSerializable());

    byte[] bytes = SerializationUtils.serialize(compositeModificationByteStringPayload);

    Object deserialize = SerializationUtils.deserialize(bytes);

    assertTrue(deserialize instanceof CompositeModificationByteStringPayload);

}

From source file:org.opendaylight.controller.cluster.persistence.LocalSnapshotStoreTest.java

private static void createSnapshotFile(final String persistenceId, final String payload, final int seqNr,
        final int timestamp) throws IOException {
    String name = toSnapshotName(persistenceId, seqNr, timestamp);
    try (FileOutputStream fos = new FileOutputStream(new File(SNAPSHOT_DIR, name))) {
        if (payload != null) {
            fos.write(SerializationUtils.serialize(payload));
        }/*from w  w w. j  av  a  2  s .co m*/
    }
}

From source file:org.opendaylight.controller.cluster.raft.utils.InMemoryJournal.java

public static void addEntry(String persistenceId, long sequenceNr, Object data) {
    Map<Long, Object> journal = journals.get(persistenceId);
    if (journal == null) {
        journal = Maps.newLinkedHashMap();
        journals.put(persistenceId, journal);
    }//ww w. ja v a  2  s . c om

    synchronized (journal) {
        journal.put(sequenceNr,
                data instanceof Serializable ? SerializationUtils.serialize((Serializable) data) : data);
    }
}

From source file:org.openehr.designer.diff.ArchetypeDifferentiator.java

private boolean isArchetypeSlotSpecialized(ArchetypeSlot flatParent, ArchetypeSlot cobj) {
    return !Arrays.equals(SerializationUtils.serialize(flatParent), SerializationUtils.serialize(cobj));
}

From source file:org.openengsb.core.services.SecureJavaSerializePortTest.java

@Override
protected byte[] encodeAndEncrypt(MethodCallMessage secureRequest, SecretKey sessionKey) throws Exception {
    byte[] serialized = SerializationUtils.serialize(secureRequest);
    byte[] content = CipherUtils.encrypt(serialized, sessionKey);
    EncryptedMessage message = new EncryptedMessage();
    message.setEncryptedContent(content);
    message.setEncryptedKey(CipherUtils.encrypt(sessionKey.getEncoded(), serverPublicKey));
    return SerializationUtils.serialize(message);
}

From source file:org.openengsb.core.services.SecureJavaSerializePortTest.java

@Override
protected FilterAction getSecureRequestHandlerFilterChain() throws Exception {
    FilterChainElementFactory unpackerFactory = new FilterChainElementFactory() {
        @Override//from  w  ww  . j  ava2s.c  o m
        public FilterChainElement newInstance() throws FilterConfigurationException {
            return new AbstractFilterChainElement<byte[], byte[]>() {
                private FilterAction next;

                @Override
                protected byte[] doFilter(byte[] input, Map<String, Object> metaData) {
                    EncryptedMessage deserialize = (EncryptedMessage) SerializationUtils.deserialize(input);
                    byte[] result = (byte[]) next.filter(deserialize, metaData);
                    return result;
                }

                @Override
                public void setNext(FilterAction next) throws FilterConfigurationException {
                    this.next = next;
                }
            };
        }
    };
    FilterChainElementFactory decrypterFactory = new MessageCryptoFilterFactory(privateKeySource, "AES");
    FilterChainElementFactory parserFactory = new FilterChainElementFactory() {
        @Override
        public FilterChainElement newInstance() throws FilterConfigurationException {
            return new AbstractFilterChainElement<byte[], byte[]>() {
                private FilterAction next;

                @Override
                protected byte[] doFilter(byte[] input, Map<String, Object> metaData) {
                    MethodCallMessage deserialize;
                    try {
                        deserialize = (MethodCallMessage) SerializationUtils.deserialize(input);
                    } catch (SerializationException e) {
                        throw new FilterException(e);
                    }
                    MethodResultMessage result = (MethodResultMessage) next.filter(deserialize, metaData);
                    return SerializationUtils.serialize(result);
                }

                @Override
                public void setNext(FilterAction next) throws FilterConfigurationException {
                    this.next = next;
                }
            };
        }
    };

    FilterChainFactory<byte[], byte[]> factory = new FilterChainFactory<byte[], byte[]>(byte[].class,
            byte[].class);
    List<Object> asList = Arrays.asList(unpackerFactory, decrypterFactory, parserFactory, filterTop.create());
    factory.setFilters(asList);
    return factory.create();
}

From source file:org.openiot.lsm.manager.TriplesDataRetriever.java

public static String ticketToRDF(LSMServiceTicketImpl ticket) {
    // TODO Auto-generated method stub
    String triples = "";
    String prefix = propertyManagement.getOpeniotResourceNamespace();
    String id = ticket.getId();//from  w w w .j  a v a 2  s  .c o m
    triples += "<" + prefix + id
            + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://openiot.eu/ontology/ns/Ticket>.\n"
            + "<" + prefix + id + "> <http://openiot.eu/ontology/ns/timesUsed> \"" + ticket.getCountOfUses()
            + "\"^^<http://www.w3.org/2001/XMLSchema#integer>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/creationTime> \""
            + DateUtil.date2StandardString(new Date(ticket.getCreationTime()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#dateTime>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/lastTimeUsed> \""
            + DateUtil.date2StandardString(new Date(ticket.getLastTimeUsed()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#dateTime>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/prevLastTimeUsed> \""
            + DateUtil.date2StandardString(new Date(ticket.getPreviousTimeUsed()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#dateTime>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/grantedBy> <" + prefix + ticket.getGrantingTicket().getId()
            + ">.\n" + "<" + prefix + id + ">  <http://openiot.eu/ontology/ns/serviceBinary> \""
            + Hex.encodeHexString(SerializationUtils.serialize(ticket.getService()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#hexBinary>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/expirationPolicy> \""
            + Hex.encodeHexString(SerializationUtils.serialize(ticket.getExpirationPolicy()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#hexBinary>.\n";
    if (ticket.isFromNewLogin())
        triples += "<" + prefix + id
                + ">  <http://openiot.eu/ontology/ns/ticketFrom> <http://openiot.eu/ontology/ns/NewLogin>.\n";
    else
        triples += "<" + prefix + id
                + ">  <http://openiot.eu/ontology/ns/ticketFrom> <http://openiot.eu/ontology/ns/OldLogin>.\n";
    return triples;
}

From source file:org.openiot.lsm.manager.TriplesDataRetriever.java

public static String ticketSchedulerToRDF(LSMTicketGrantingTicketImpl ticketGrant) {
    // TODO Auto-generated method stub
    String triples = "";
    String id = ticketGrant.getId();
    String prefix = propertyManagement.getOpeniotResourceNamespace();
    triples += "<" + prefix + id
            + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://openiot.eu/ontology/ns/TicketScheduler>.\n"
            + "<" + prefix + id + "> <http://openiot.eu/ontology/ns/timesUsed> \""
            + ticketGrant.getCountOfUses() + "\"^^<http://www.w3.org/2001/XMLSchema#integer>.\n" + "<" + prefix
            + id + ">  <http://openiot.eu/ontology/ns/creationTime> \""
            + DateUtil.date2StandardString(new Date(ticketGrant.getCreationTime()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#dateTime>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/lastTimeUsed> \""
            + DateUtil.date2StandardString(new Date(ticketGrant.getLastTimeUsed()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#dateTime>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/prevLastTimeUsed> \""
            + DateUtil.date2StandardString(new Date(ticketGrant.getPreviousTimeUsed()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#dateTime>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/authenticatedBy> \""
            + Hex.encodeHexString(SerializationUtils.serialize(ticketGrant.getAuthentication()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#hexBinary>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/expirationPolicy> \""
            + Hex.encodeHexString(SerializationUtils.serialize(ticketGrant.getExpirationPolicy()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#hexBinary>.\n" + "<" + prefix + id
            + ">  <http://openiot.eu/ontology/ns/servicesGranted> \""
            + Hex.encodeHexString(SerializationUtils.serialize(ticketGrant.getServices()))
            + "\"^^<http://www.w3.org/2001/XMLSchema#hexBinary>.\n";
    //      System.out.println(Hex.encodeHexString(SerializationUtils.serialize(ticketGrant.getAuthentication())));
    if (ticketGrant.getGrantingTicket() != null) {
        triples += "<" + prefix + id + ">  <http://openiot.eu/ontology/ns/grants> <" + prefix
                + ticketGrant.getGrantingTicket().getId() + ">.\n";
        triples += ticketSchedulerToRDF((LSMTicketGrantingTicketImpl) ticketGrant.getGrantingTicket());
    }/*from ww  w.j  a va  2s  .c  o m*/
    if (ticketGrant.isExpired())
        triples += "<" + prefix + id
                + ">  <http://openiot.eu/ontology/ns/validity> <http://openiot.eu/ontology/ns/TicketGrantingExpired>.\n";
    else
        triples += "<" + prefix + id
                + ">  <http://openiot.eu/ontology/ns/validity> <http://openiot.eu/ontology/ns/TicketGrantingValid>.\n";
    return triples;
}

From source file:org.openmrs.serialization.JavaSerializationTest.java

@Test
public void shouldSerializeOpenMrsData() {

    Date date = new Date();
    User user = new User(1);

    Person originalPerson = new Person();
    originalPerson.setGender("M");
    originalPerson.setBirthdate(date);/*w  w w. j  av a2 s . co  m*/
    originalPerson.setBirthdateEstimated(false);
    originalPerson.setUuid("abc123");
    originalPerson.setDateCreated(date);
    originalPerson.setCreator(user);
    originalPerson.setDateChanged(date);
    originalPerson.setChangedBy(user);
    originalPerson.setVoided(true);
    originalPerson.setVoidedBy(user);
    originalPerson.setDateVoided(date);
    originalPerson.setVoidReason("test");

    byte[] serialized = SerializationUtils.serialize(originalPerson);
    Person copyPerson = (Person) SerializationUtils.deserialize(serialized);

    assertThat(copyPerson.getGender(), is(originalPerson.getGender()));
    assertThat(copyPerson.getBirthdate(), is(originalPerson.getBirthdate()));
    assertThat(copyPerson.getBirthdateEstimated(), is(originalPerson.getBirthdateEstimated()));
    assertThat(copyPerson.getDateCreated(), is(originalPerson.getDateCreated()));
    assertThat(copyPerson.getCreator(), is(originalPerson.getCreator()));
    assertThat(copyPerson.getDateChanged(), is(originalPerson.getDateChanged()));
    assertThat(copyPerson.getChangedBy(), is(originalPerson.getChangedBy()));
    assertThat(copyPerson.getVoided(), is(originalPerson.getVoided()));
    assertThat(copyPerson.getVoidedBy(), is(originalPerson.getVoidedBy()));
    assertThat(copyPerson.getDateVoided(), is(originalPerson.getDateVoided()));
    assertThat(copyPerson.getVoidReason(), is(originalPerson.getVoidReason()));
    assertThat(copyPerson.getUuid(), is(originalPerson.getUuid()));
}

From source file:org.openmrs.serialization.JavaSerializationTest.java

@Test
public void shouldSerializeOpenMrsMetadata() {

    Date date = new Date();
    User user = new User(1);

    Concept originalConcept = new Concept();
    originalConcept.setConceptClass(new ConceptClass(1));
    originalConcept.setDatatype(new ConceptDatatype(1));
    originalConcept.setUuid("abc123");
    originalConcept.setDateCreated(date);
    originalConcept.setCreator(user);//from   www  .  j  av  a  2 s .  c  o  m
    originalConcept.setDateChanged(date);
    originalConcept.setChangedBy(user);
    originalConcept.setRetired(true);
    originalConcept.setRetiredBy(user);
    originalConcept.setDateRetired(date);
    originalConcept.setRetireReason("test");

    byte[] serialized = SerializationUtils.serialize(originalConcept);
    Concept copyConcept = (Concept) SerializationUtils.deserialize(serialized);

    assertThat(copyConcept.getConceptClass(), is(originalConcept.getConceptClass()));
    assertThat(copyConcept.getDatatype(), is(originalConcept.getDatatype()));
    assertThat(copyConcept.getDateCreated(), is(originalConcept.getDateCreated()));
    assertThat(copyConcept.getCreator(), is(originalConcept.getCreator()));
    assertThat(copyConcept.getDateChanged(), is(originalConcept.getDateChanged()));
    assertThat(copyConcept.getChangedBy(), is(originalConcept.getChangedBy()));
    assertThat(copyConcept.getRetired(), is(originalConcept.getRetired()));
    assertThat(copyConcept.getRetiredBy(), is(originalConcept.getRetiredBy()));
    assertThat(copyConcept.getDateRetired(), is(originalConcept.getDateRetired()));
    assertThat(copyConcept.getRetireReason(), is(originalConcept.getRetireReason()));
    assertThat(copyConcept.getUuid(), is(originalConcept.getUuid()));
}