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

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

Introduction

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

Prototype

public static byte[] serialize(final Serializable obj) 

Source Link

Document

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

Usage

From source file:org.apache.openejb.assembler.classic.EntityManagerFactoryCallableSerializationTest.java

@Test
public void serializationRoundTrip() {
    final Object em = SerializationUtils
            .deserialize(SerializationUtils.serialize(Serializable.class.cast(this.em)));
    assertTrue(EntityManager.class.isInstance(em));
    final ReloadableEntityManagerFactory factory = ReloadableEntityManagerFactory.class
            .cast(Reflections.get(em, "entityManagerFactory"));
    assertNotNull(factory.getDelegate());
    assertNotNull(Reflections.get(factory, "entityManagerFactoryCallable"));
}

From source file:org.apache.openejb.assembler.classic.LazyValidatorTest.java

@Test
public void serialize() {
    final Serializable obj = Serializable.class.cast(Proxy.newProxyInstance(
            Thread.currentThread().getContextClassLoader(), new Class<?>[] { ValidatorFactory.class },
            new LazyValidator(Validation.buildDefaultValidatorFactory())));
    final LazyValidator deserialized = LazyValidator.class.cast(
            Proxy.getInvocationHandler(SerializationUtils.deserialize(SerializationUtils.serialize(obj))));
    final Validator validator = deserialized.getValidator();
    assertNotNull(validator);/*from w  w w  .ja  va 2s .  com*/
}

From source file:org.apache.openejb.resource.jdbc.DataSourceSerializationTest.java

@Test
public void run() {
    final DataSource d = SerializationUtils
            .deserialize(SerializationUtils.serialize(Serializable.class.cast(ds)));
    assertNotNull(d);/*from ww  w .j  a v  a 2s .co  m*/
}

From source file:org.asimba.engine.session.jgroups.JGroupsSessionFactoryTest.java

/**
 * Isolates problems related to serializability of JGroupsTGT
 * @throws Exception/*from  w w  w .  j  a v  a2  s  . co  m*/
 */
@Test
public void test01_JGroupsSessionSerializable() throws Exception {
    JGroupsSessionFactory oSessionFactory = createJGroupsSessionFactory(0, EXPIRATION_FOR_TEST,
            FILENAME_CONFIG_DEFAULT);
    JGroupsSession oSession = (JGroupsSession) oSessionFactory.createSession(REQUESTOR_ID);

    try {
        SerializationUtils.serialize(oSession);
    } catch (Exception e) {
        _oLogger.error("Object of class JGroupsTGT cannot be serialized", e);
        assertThat("Serialization of JGroupsSession failed", true, equalTo(false)); // or the universe implodes
    }
}

From source file:org.asimba.engine.tgt.jgroups.JGroupsTGTFactoryTest.java

/**
 * Isolates problems related to serializability of JGroupsTGT
 * @throws Exception//from   ww w . j a  va2s.  com
 */
@Test
public void test01_JGroupsTGTSerializable() throws Exception {
    JGroupsTGTFactory oTGTFactory = createJGroupsTGTFactory(0, EXPIRATION_FOR_TEST, FILENAME_CONFIG);
    JGroupsTGT oTGT = (JGroupsTGT) oTGTFactory.createTGT(mockedUser);

    try {
        SerializationUtils.serialize(oTGT);
    } catch (Exception e) {
        _oLogger.error("Object of class JGroupsTGT cannot be serialized", e);
        assertThat(true, equalTo(false)); // or the universe implodes
    }
}

From source file:org.camunda.bpm.extension.batch.core.CustomBatchConfigurationJsonConverter.java

@Override
public JSONObject toJsonObject(final CustomBatchConfiguration<T> customBatchConfiguration) {
    final JSONObject json = new JSONObject();

    JsonUtil.addField(json, EXCLUSIVE, customBatchConfiguration.isExclusive());
    JsonUtil.addField(json, DATA_SERIALIZED, Base64.getEncoder()
            .encodeToString(SerializationUtils.serialize((Serializable) customBatchConfiguration.getData())));

    return json;//w w w. ja  va2  s  .  co m
}

From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java

/**
 * Serializes an object as String. If the passed object is
 * <ul>/*from   w  ww  .  j  av  a  2  s  .  c o  m*/
 * <li>{@link String} - it is prefixed with "T::"</li>
 * <li>{@link Boolean} - it is prefixed with "B::"</li>
 * <li>{@link Integer} - it is prefixed with "N::"</li>
 * <li>{@link Integer} - it is prefixed with "L::"</li>
 * <li>{@link Float} - it is prefixed with "F::"</li>
 * <li>{@link URI} - it is prefixed with "U::"</li>
 * </ul>
 * @param value
 * @return 
 */
public String marshallValue(Object value) {
    String retVal = null;
    if (value == null) {
    } else if (value instanceof String) {
        retVal = "T::" + (String) value;
    } else if (value instanceof Boolean) {
        retVal = "B::" + value.toString();
    } else if (value instanceof Integer) {
        retVal = "N::" + value.toString();
    } else if (value instanceof Long) {
        retVal = "N::" + value.toString();
    } else if (value instanceof Double) {
        retVal = "F::" + value.toString();
    } else if (value instanceof Float) {
        retVal = "F::" + value.toString();
    } else if (value instanceof URI) {
        retVal = "U::" + value.toString();
    } else if (value instanceof Serializable) {
        byte[] rawBytes = SerializationUtils.serialize((Serializable) value);
        retVal = "O::" + BaseEncoding.base64().encode(rawBytes);
    }
    return (retVal);
}

From source file:org.deeplearning4j.util.ModelSerializerTest.java

@Test
public void testJavaSerde_1() throws Exception {
    int nIn = 5;//from  w  ww.j a v  a  2s . c o m
    int nOut = 6;

    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).l1(0.01)
            .graphBuilder().addInputs("in")
            .layer("0", new OutputLayer.Builder().nIn(nIn).nOut(nOut).build(), "in").setOutputs("0")
            .validateOutputLayerConfig(false).build();

    ComputationGraph net = new ComputationGraph(conf);
    net.init();

    DataSet dataSet = trivialDataSet();
    NormalizerStandardize norm = new NormalizerStandardize();
    norm.fit(dataSet);

    val b = SerializationUtils.serialize(net);

    ComputationGraph restored = SerializationUtils.deserialize(b);

    assertEquals(net, restored);
}

From source file:org.deeplearning4j.util.ModelSerializerTest.java

@Test
public void testJavaSerde_2() throws Exception {
    int nIn = 5;/*ww w .  j a v  a  2  s  .  c  o  m*/
    int nOut = 6;

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).l1(0.01).list()
            .layer(0, new OutputLayer.Builder().nIn(nIn).nOut(nOut).activation(Activation.SOFTMAX).build())
            .build();

    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();

    DataSet dataSet = trivialDataSet();
    NormalizerStandardize norm = new NormalizerStandardize();
    norm.fit(dataSet);

    val b = SerializationUtils.serialize(net);

    MultiLayerNetwork restored = SerializationUtils.deserialize(b);

    assertEquals(net, restored);
}

From source file:org.force66.beantester.tests.SerializableTest.java

@Override
public boolean testBeanClass(Class<?> klass, Object[] constructorArgs) {
    this.setFailureReason(null);
    Object bean = InstantiationUtils.safeNewInstance(klass, constructorArgs);
    if (bean instanceof Serializable) {
        InjectionUtils.injectValues(bean, valueGeneratorFactory, false);

        Serializable sBean = (Serializable) bean;
        Serializable sBeanReconstituted = null;
        byte[] serializedObj;
        try {//from   w w w .  j av  a 2  s  . c  om
            serializedObj = SerializationUtils.serialize(sBean);
            sBeanReconstituted = SerializationUtils.deserialize(serializedObj);
        } catch (Throwable e) {
            this.setFailureReason("Error serializing bean that implements serializable");
            throw new BeanTesterException("Error serializing bean that implements serializable", e)
                    .addContextValue("class", klass.getName());
        }

        /*
         * An equals() test is only valid if the bean isn't relying on Object.equals().
         */
        Method equalsMethod = MethodUtils.getAccessibleMethod(klass, "equals", Object.class);
        if (!equalsMethod.getDeclaringClass().equals(Object.class) && !sBean.equals(sBeanReconstituted)) {
            this.setFailureReason(
                    "Bean implements serializable, but the reconstituted bean doesn't equal it's original");
            throw new BeanTesterException(
                    "Bean implements serializable, but the reconstituted bean doesn't equal it's original")
                            .addContextValue("class", klass.getName());
        }
    }
    return true;
}