summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-1.0-incubating/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/SerializingDeserializingADataObject.java
blob: eaecf436bf0dce6fa708c5f0ffc896001131bab0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package org.apache.tuscany.samples.sdo.intermediate;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.tuscany.samples.sdo.SampleBase;
import org.apache.tuscany.samples.sdo.internal.SampleInfrastructure;

import commonj.sdo.DataObject;
import commonj.sdo.helper.HelperContext;

/**
 * Demonstrates serializing and deserializing a DataObject to disk
 * using java serialization.
 * <p>
 * <h3>Running this Sample</h3> See <A HREF="../../../../../../index.html" 
 * target="_top">the main overview</A> for instructions on how to run this 
 * sample.
 */
public class SerializingDeserializingADataObject  extends SampleBase {
    HelperContext scope;

    public SerializingDeserializingADataObject(Integer userLevel) {
      super(userLevel, SAMPLE_LEVEL_INTERMEDIATE);
    }


    public static void main(String[] args) {

      SerializingDeserializingADataObject sample =
    	  new SerializingDeserializingADataObject(COMMENTARY_FOR_INTERMEDIATE);
      sample.run();

    }

    /*
     *  metadata for the sample documenting the areas of SDO that are explored
     */
    public static int [] CORE_FUNCTION = {
      SDOFacets.JAVA_SERIALIZATION_OF_DATA_GRAPH
    };
    /*
     *  metadata for the sample documenting the areas of SDO that are explored
     */
    public static int [] SIGNIFICANT_FUNCTION = {
      SDOFacets.TESTING_FOR_GRAPH_EQUALITY
    };
    
    public void runSample () throws Exception {

        commentary(
        		"Demonstrates serializing and deserializing a DataObject\n" +
        		"to disk using Java serialization.");

        scope = useDefaultScopeForTypes();
        loadTypesFromXMLSchemaFile(scope, SampleInfrastructure.COMPANY_XSD);
        DataObject company = getDataObjectFromFile(scope, SampleInfrastructure.COMPANY_DATAOBJECT_XML);


        commentary("We've loaded a data graph 'company' from a file\n" +
        		"using XML schema for the model and XML for the graph in the usual manner\n");
        String fileName = "temporarySerializedDataObject.xml";
        commentary("We've loaded a data graph 'company' from a file\n" +
        "using XML schema for the model and XML for the graph in the usual manner\n" +
        "Now we are going to serialize it to, and read it from a temporary file: "+fileName);
        
        commentary(
        		"The following code,  which doesn't use any SDO APIs, demonstrates the\n" +
        		"underlying SDO function of performing Java serialization on SDO objects\n\n" +
        		"FileOutputStream fos = new FileOutputStream(fileName);\n" +
        		"ObjectOutputStream out = new ObjectOutputStream(fos);\n" +
        		"out.writeObject(company);\n" +
        		"out.close();");

        FileOutputStream fos = new FileOutputStream(fileName);
        ObjectOutputStream out = new ObjectOutputStream(fos);
        out.writeObject(company);
        out.close();            
        
        // read in DataObject
        commentary("Having written the data graph to the temporary file we\n" +
        		"can read it back\n\n" +
        		"FileInputStream fis = new FileInputStream(fileName);\n" +
        		"ObjectInputStream input = new ObjectInputStream(fis);\n" +
        		"DataObject newDataObject = (DataObject) input.readObject();\n" +
        		"input.close();");
        
        FileInputStream fis = new FileInputStream(fileName);
        ObjectInputStream input = new ObjectInputStream(fis);
        DataObject newDataObject = (DataObject) input.readObject();
        input.close();
        
        /**
         * Compare data graphs
         */
        
        commentary("We can use the SDO EqualityHelper to check that we have got\n" +
        		"back an equivalent graph to the one we had originally\n\n" +
        		"boolean equal = scope.getEqualityHelper().equal(company, newDataObject);");                       

        boolean equal = scope.getEqualityHelper().equal(company, newDataObject);
        System.out.println("DataObjects are equal: " + equal);
        
       //print out xml representation
        System.out.println();
        System.out.println("Original company DataObject:");
        System.out.println(scope.getXMLHelper().save(company, SampleInfrastructure.COMPANY_NAMESPACE, "company"));
        
        System.out.println();
        System.out.println("Deserialized company DataObject:");
        System.out.println(scope.getXMLHelper().save(newDataObject, SampleInfrastructure.COMPANY_NAMESPACE, "company"));                       
            

    }
}