001: package org.mandarax.jdbc.rpc;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.beans.*;
022: import java.io.*;
023:
024: /**
025: * Serializer/deserializer for calls and call results.
026: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
027: * @version 3.3.2 <29 December 2004>
028: * @since 3.0
029: */
030: public class XMLSerializer implements Serializer {
031:
032: static class PersistenceDelegate4SQLDates extends
033: PersistenceDelegate {
034: public Expression instantiate(Object oldInstance, Encoder out) {
035: java.sql.Date d = (java.sql.Date) oldInstance;
036: return new Expression(oldInstance, java.sql.Date.class,
037: "new", new Object[] { new Long(d.getTime()) });
038: }
039: }
040:
041: private static PersistenceDelegate4SQLDates persistenceDelegate4SQLDates = new PersistenceDelegate4SQLDates();
042:
043: static class PersistenceDelegate4SQLTimes extends
044: PersistenceDelegate {
045: public Expression instantiate(Object oldInstance, Encoder out) {
046: java.sql.Time d = (java.sql.Time) oldInstance;
047: return new Expression(oldInstance, java.sql.Time.class,
048: "new", new Object[] { new Long(d.getTime()) });
049: }
050: }
051:
052: private static PersistenceDelegate4SQLTimes persistenceDelegate4SQLTimes = new PersistenceDelegate4SQLTimes();
053:
054: static class PersistenceDelegate4SQLTimestamps extends
055: PersistenceDelegate {
056: public Expression instantiate(Object oldInstance, Encoder out) {
057: java.sql.Timestamp d = (java.sql.Timestamp) oldInstance;
058: return new Expression(oldInstance,
059: java.sql.Timestamp.class, "new",
060: new Object[] { new Long(d.getTime()) });
061: }
062: }
063:
064: private static PersistenceDelegate4SQLTimestamps persistenceDelegate4SQLTimestamps = new PersistenceDelegate4SQLTimestamps();
065:
066: // exception listener
067: static class LogExceptionListener implements ExceptionListener,
068: org.mandarax.util.logging.LogCategories {
069: public void exceptionThrown(Exception x) {
070: LOG_JDBC.error("XML serialization error", x);
071: }
072: }
073:
074: private static ExceptionListener logExceptionListener = new LogExceptionListener();
075:
076: /**
077: * Serialize an object (call or call result)
078: * @param obj an object
079: * @param out an output stream
080: */
081: public void write(Object obj, OutputStream out) throws IOException {
082: XMLEncoder encoder = new XMLEncoder(out);
083: encoder.setPersistenceDelegate(java.sql.Date.class,
084: persistenceDelegate4SQLDates);
085: encoder.setPersistenceDelegate(java.sql.Time.class,
086: persistenceDelegate4SQLTimes);
087: encoder.setPersistenceDelegate(java.sql.Timestamp.class,
088: persistenceDelegate4SQLTimestamps);
089: encoder.setExceptionListener(logExceptionListener);
090: encoder.writeObject(obj);
091: encoder.close();
092: }
093:
094: /**
095: * Deserialize an object (call or call result).
096: * @param in an input stream
097: * @return an object
098: */
099: public Object read(InputStream in) throws IOException {
100: XMLDecoder decoder = new XMLDecoder(in);
101: decoder.setExceptionListener(logExceptionListener);
102: Object obj = decoder.readObject();
103: decoder.close();
104: return obj;
105: }
106:
107: /**
108: * Get the content type of the serialized data.
109: * @return a content type.
110: */
111: public String getContentType() {
112: return "text/xml";
113: }
114:
115: }
|