Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 134 135 136 137 138 139 < 140 > 141 142 143 144 145 146 .. 166 >> Next

Listing 46.1 serializeBean.java
JDO and Data Persistence 387
049: errorsFound=true;
050: }
051:
052: return errorsFound;
053: }
054:
055: public String getErrorMsg(String s) {
056: String errorMsg =(String)errors.get(s.trim());
057: return (errorMsg == null) ? "":errorMsg;
058: }
059:
060: public serializeBean() {
061:
062: try 063 : {
064: URL url = null;
065: Properties props = new Properties();
066:
067: url =
this.getClass().getClassLoader().getResource ("serialization.properties");
068: props.load( new FileInputStream(url.getFile()) );
069: // Get properties
070: filename = props.getProperty("filename");
071: setFilename(filename);
072: logger.info("[serializeBean:(constructor)] Filename is: ^
" + getFilename());
073:
074: BufferedlnputStream in = new BufferedInputStream(new ^
FilelnputStream(filename));
075 XMLDecoder decoder = new XMLDecoder(in);
076 Object f = decoder.readObject(); // firstName
077 Object l = decoder.readObject(); // lastName
078 Object s = decoder.readObject(); // SSN
079 Object c = decoder.readObject(); // comments
080
081 lastname = (String)l;
082 firstname = (String)f;
083 ssn = (String)s;
084 comments = (String)c;
085
086 setLastname(lastname);
087 setFirstname(firstname);
088 setSsn(ssn);
089 setComments(comments);
090
091 decoder.close();
092
093 }
094 catch(Exception e) {
Listing 46.1 (continued)
tJtJ
388 Item 46
095
096 System.out.println("ERROR " + e.toString());
097 g n i s U n( l t n i r .p t. u o m. e t s y S default values.");
098
099 // use default values
100 firstname="";
101 lastname="";
102 // department[];
103 ssn=" " ;
104 comments=" " ;
105
106 }
Listing 46.1 (continued)
The serialization process described in lines 109 to 210 can be a problem with different versions of class libraries or Java Runtime Environments. The latest Merlin release has addressed this with the introduction of XML serialization. Since the data is saved in XML files, it is possible to serialize data, manually modify the serialized file by hand, and have it deserialized without losing a step. Previously, this would not have been possible because the data was saved in binary byte streams.
108 /*
109 try
110 {
111 URL url = null;
112 Properties props = new Properties();
113 url = this.getClass().getClassLoader().
114 getResource("serialization.properties );
115 props.load( new FileInputStream(url.getFile )) );
116 // Get properties
117 filename2 = props.getProperty("filename2");
118 setFilename2(filename2);
119 logger.info("Filename2 is: " + getFilename2 ));
120 FileInputStream inputFile =
121 new FileInputStream(getFilename2());
122 ObjectInputStream inputStream =
123 new ObjectInputStream(inputFile);
124 // Read data
125 lastname2 = (String)inputStream.readObject() ;
126 firstname2 = (String)inputStream.readObject );
127 ssn2 = (String)inputStream.readObject();
128 comments2 = (String)inputStream.readObject() ;
129
130 inputStream.close();
131
132 }
133 catch(Exception e) {
134
135 System.out.println("ERROR: " + e.toString())
Listing 46.1 (continued)
JDO and Data Persistence 389
136 137 System.out.println("Using default values.");
138 // use default values
139 firstname2="";
140 lastname2="";
141 ssn2="";
142 143 comments2="";
144 }
145 */
146 errors = new Hashtable();
147 logger.info("lastname, firstname, ssn, comments= " +
148 lastname + ", " + firstname + ", " + ssn + ", " + comments);
149 // logger.info("lastname2, firstname2, ssn2, comments2 = " +
150 // lastname2 + ", " + firstname2 + ", " + ssn2 + ", " + comments2);
151 }
152 public void write()
153 {
154 try
155 {
156 157 logger.info("Filename is: " + getFilename());
158 System.out.println("Firstname= " + getFirstname()) ;
159 System.out.println("Lastname= " + getLastname());
160 System.out.println("SSN= " + getSsn());
161 162 System.out.println("Comments= " + getComments());
Listing 46.1 (continued)
The code below demonstrates a new serialization mechanism that saves the states of internal methods in an XML format.
163 // try XMLEncode
164 BufferedOutputStream out =
165 new BufferedOutputStream(new FileOutputStream(getFilename()));
166 XMLEncoder encoder = new XMLEncoder(out);
167 encoder.writeObject(getFirstname());
168 encoder.writeObject(getLastname());
169 encoder.writeObject(getSsn());
170 encoder.writeObject(getComments());
171 encoder.close();
172
173 }
174 catch(IOException ioe) {
175 System.err.println("IOERROR: " + ioe);
176 }
177 catch(Exception e) {
178 System.err.println("ERROR: " + e);
179 }
180
Listing 46.1 (continued)
390 Item 46
181 /*
182 try
183 {
184 logger.info("Filename is: " + getFilename2());
185 // avoid XMLEncode
186 FileOutputStream outputFile2 =
187 new FileOutputStream(getFilename2());
188 ObjectOutputStream outputStream2 =
189 new ObjectOutputStream(outputFile2);
Previous << 1 .. 134 135 136 137 138 139 < 140 > 141 142 143 144 145 146 .. 166 >> Next