Example usage for java.lang StringBuilder writeObject

List of usage examples for java.lang StringBuilder writeObject

Introduction

In this page you can find the example usage for java.lang StringBuilder writeObject.

Prototype

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException 

Source Link

Document

Save the state of the StringBuilder instance to a stream (that is, serialize it).

Usage

From source file:com.univocity.examples.Tutorial004Advanced.java

@Test(dependsOnMethods = "example001DataSetProducer")
public void example002PersistentScope() {
    final StringBuilder out = new StringBuilder();
    //##CODE_START
    EngineConfiguration config = new EngineConfiguration("Persistent", getCsvDataStore(),
            getFixedWidthDataStore());/*from w ww. ja  v a2 s  . co m*/

    //The persistent scope retains values persisted in variables other elements that should be made available
    //after the engine is stopped and subsequently started. It depends on a storage provider defined by the user.
    //The storage can be a file, database, distributed cache or anything else that will outlive the engine.
    config.setPersistentStorageProvider(new ScopeStorageProvider() {

        //We will keep the state in a byte array.
        private byte[] persistentState;

        //This is where our data will be kept. It will be serialized to the byte array
        //When the persisted scope is deactivated.
        private Map<Object, Object> persistentMap;

        @Override
        public Object setValue(Object key, Object value) {
            return persistentMap.put(key, value);
        }

        @SuppressWarnings("unchecked")
        @Override
        public void initialize() {
            //Here we restore the contents stored in our "persistent" byte array.
            if (persistentState == null) {
                //No byte array yet, let's just create an empty map
                println(out, "Creating new persistent state");
                persistentMap = new HashMap<Object, Object>();
            } else {
                // Deserialize map from the byte array
                try {
                    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(persistentState));
                    persistentMap = ((Map<Object, Object>) in.readObject());
                    println(out, "Restored previous persistent state: " + persistentMap);
                } catch (Exception ex) {
                    throw new IllegalStateException("Unable to restore state of persistent scope", ex);
                }
            }
        }

        @Override
        public Object getValue(Object key) {
            return persistentMap.get(key);
        }

        @Override
        public void deactivate() {
            //This method is called when the persistent scope is deactivated (i.e. the engine is being stopped)
            try {
                println(out, "Persistent scope deactivated. Saving its state for later recovery.");
                ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();

                ObjectOutputStream out = new ObjectOutputStream(byteOutput);
                //let's serialize our map
                out.writeObject(persistentMap);
                out.flush();
                out.close();

                //update the byte array. In a "real" application that would be a file, database,
                //distributed cache or anything you need to use to persist these values.
                persistentState = byteOutput.toByteArray();
            } catch (Exception ex) {
                throw new IllegalStateException("Unable to save data in persistent scope", ex);
            }
        }

        @Override
        public boolean contains(Object key) {
            return persistentMap.containsKey(key);
        }
    });

    //Here we register the engine our configuration using a persistent scope.
    Univocity.registerEngine(config);

    //Let's create a mapping to move some data around
    DataIntegrationEngine engine = Univocity.getEngine("Persistent");
    DataStoreMapping mapping = engine.map("csvDataStore", "fixedWidthDestination");

    EntityMapping foodGroupMapping = mapping.map("FD_GROUP", "food_group");
    foodGroupMapping.identity().associate("FdGrp_CD").to("id");
    foodGroupMapping.value().copy("FdGrp_Desc").to("name");

    //Here we define the persistent variable "processedCodes". It will retain values read from "FdGrp_CD"
    //into a set in the persistent scope.
    engine.setPersistentVariable("processedCodes", new TreeSet<String>());

    //This row reader will discard input rows where the value of "FdGrp_CD" has been already mapped.
    //It uses the "processedCodes" variable to ensure each row is mapped only once.
    foodGroupMapping.addInputRowReader(new RowReader() {
        @SuppressWarnings("unchecked")
        @Override
        public void processRow(Object[] inputRow, Object[] outputRow, RowMappingContext context) {
            //gets the value of FdGrp_CD in the input
            String code = context.getInputValue("FdGrp_CD", String.class);

            //gets the set of processed codes stored in the persistent variable "processedCodes"
            Set<String> processedCodes = (Set<String>) context.readVariable("processedCodes");

            //verifies whether a row with this code has been mapped already
            if (processedCodes.contains(code)) {
                //discards row if it has been mapped
                context.discardRow();
            } else {
                //this is a new row. Let's store it's code so it does not get mapped again.
                processedCodes.add(code);
                //Let's also increment the "rowCount" variable to inform us how many new rows were mapped in this cycle.
                Integer rowCount = (Integer) context.readVariable("rowCount");
                context.setVariable("rowCount", rowCount + 1);
            }
        }
    });

    //executes a cycle and returns the number of new rows mapped in this cycle
    println(out, "Row count after first cycle: " + executeAndReturnRowCount());
    println(out, "Shutting down...");
    //shuts down the engine so we can see the persistence provider's messages while stopping and restarting
    Univocity.shutdown("Persistent");
    println(out, "...Starting engine again");
    //starts the engine and executes another cycle, then returns the number of new rows mapped
    println(out, "Row count after shutting down and executing again: " + executeAndReturnRowCount());
    //##CODE_END
    printAndValidate(out);

    Univocity.shutdown("Persistent");
}