1 /**********************************************************************
2 * JavaBeansXmlAdapter.java
3 * created on 10.03.2005 by netseeker
4 * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/adapter/JavaBeansXmlAdapter.java,v $
5 * $Date: 2007/05/27 22:13:09 $
6 * $Revision: 1.17 $
7 *
8 * ====================================================================
9 *
10 * Copyright 2005-2006 netseeker aka Michael Manske
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 * ====================================================================
24 *
25 * This file is part of the ejoe framework.
26 * For more information on the author, please see
27 * <http://www.manskes.de/>.
28 *
29 *********************************************************************/
30 package de.netseeker.ejoe.adapter;
31
32 import java.beans.ExceptionListener;
33 import java.beans.XMLDecoder;
34 import java.beans.XMLEncoder;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 import java.math.BigDecimal;
38 import java.math.BigInteger;
39 import java.util.logging.Level;
40 import java.util.logging.Logger;
41
42 /***
43 * Simple SerializeAdapter implementation using the XmlEncoder/XmlDecoder classes of the Java Beans API
44 *
45 * @author netseeker
46 */
47 public class JavaBeansXmlAdapter extends BaseAdapter implements ExceptionListener
48 {
49 private static final long serialVersionUID = 1L;
50
51 private static final Logger logger = Logger.getLogger( JavaBeansXmlAdapter.class.getName() );
52
53
54
55
56
57
58 public Object read( InputStream in ) throws Exception
59 {
60 XMLDecoder decoder = new XMLDecoder( in );
61 decoder.setExceptionListener( this );
62 Object result = null;
63 try
64 {
65
66
67 result = decoder.readObject();
68
69
70 }
71 finally
72 {
73 if ( decoder != null ) decoder.close();
74 }
75
76 return result;
77 }
78
79
80
81
82
83
84 public void write( Object obj, OutputStream out ) throws Exception
85 {
86 XMLEncoder encoder = new XMLEncoder( out );
87 encoder.setExceptionListener( this );
88
89 try
90 {
91
92 encoder.setPersistenceDelegate( BigInteger.class, encoder.getPersistenceDelegate( Integer.class ) );
93 encoder.setPersistenceDelegate( BigDecimal.class, encoder.getPersistenceDelegate( Integer.class ) );
94
95 encoder.writeObject( obj );
96 }
97 finally
98 {
99 encoder.close();
100 }
101 }
102
103
104
105
106
107
108 public void exceptionThrown( Exception e )
109 {
110 logger.log( Level.WARNING, e.getMessage(), e );
111 }
112
113
114
115
116 public boolean requiresCustomEOFHandling()
117 {
118 return false;
119 }
120
121
122
123
124 public boolean isSelfBuffered()
125 {
126 return true;
127 }
128
129
130
131
132
133
134 public String getContentType()
135 {
136 return "text/xml";
137 }
138 }