View Javadoc

1   /**********************************************************************
2    * Jaxb2Adapter.java
3    * created on 29.03.2006 by netseeker
4    * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/adapter/Jaxb2Adapter.java,v $
5    * $Date: 2007/03/22 21:01:28 $
6    * $Revision: 1.7 $
7    *
8    * ====================================================================
9    *
10   *  Copyright 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 de.netseeker.ejoe.adapter framework.
26   * For more information on the author, please see
27   * <http://www.manskes.de/>.
28   *
29   *********************************************************************/
30  
31  package de.netseeker.ejoe.adapter;
32  
33  import java.io.InputStream;
34  import java.io.OutputStream;
35  import java.util.logging.Level;
36  import java.util.logging.Logger;
37  
38  import javax.xml.bind.JAXBContext;
39  import javax.xml.bind.JAXBException;
40  import javax.xml.bind.Marshaller;
41  import javax.xml.bind.Unmarshaller;
42  
43  import de.netseeker.ejoe.EJConstants;
44  import de.netseeker.ejoe.request.ClassloaderRequest;
45  import de.netseeker.ejoe.request.RemotingRequest;
46  
47  /***
48   * @author netseeker
49   * @since 0.4.0
50   */
51  public class Jaxb2Adapter extends BaseAdapter
52  {
53      private static final long      serialVersionUID = 1L;
54  
55      private static final Logger    log              = Logger.getLogger( Jaxb2Adapter.class.getName() );
56  
57      private transient JAXBContext  _jcontext;
58  
59      private transient Marshaller   _marshaller;
60  
61      private transient Unmarshaller _unmarschaller;
62  
63      private String                 _packagePath;
64  
65      /***
66       * Creates a instance of this adapter with an empty binding using jaxb.properties and Configuration.xml from
67       * de.netseeker.ejoe.adapter.jaxb
68       */
69      public Jaxb2Adapter()
70      {
71          try
72          {
73              this._jcontext = JAXBContext.newInstance( new Class[] { RemotingRequest.class, ClassloaderRequest.class } );
74              this._marshaller = _jcontext.createMarshaller();
75              this._marshaller.setProperty( Marshaller.JAXB_ENCODING, EJConstants.EJOE_DEFAULT_CHARSET );
76              this._unmarschaller = _jcontext.createUnmarshaller();
77          }
78          catch ( JAXBException e )
79          {
80              log.log( Level.SEVERE, e.getMessage(), e );
81          }
82      }
83  
84      /***
85       * @param packagePath A colon separated path of package names where to look for jaxb.properties files. The package
86       *            names must match the generated classes which you are going to use in your application.
87       */
88      public Jaxb2Adapter(String packagePath)
89      {
90          try
91          {
92              this._packagePath = packagePath;
93              this._jcontext = JAXBContext.newInstance( packagePath );
94              this._marshaller = _jcontext.createMarshaller();
95              this._marshaller.setProperty( Marshaller.JAXB_ENCODING, EJConstants.EJOE_DEFAULT_CHARSET );
96              this._unmarschaller = _jcontext.createUnmarshaller();
97          }
98          catch ( JAXBException e )
99          {
100             log.log( Level.SEVERE, e.getMessage(), e );
101         }
102     }
103 
104     /*
105      * (non-Javadoc)
106      * 
107      * @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
108      */
109     public Object read( InputStream in ) throws Exception
110     {
111         return this._unmarschaller.unmarshal( in );
112     }
113 
114     /*
115      * (non-Javadoc)
116      * 
117      * @see de.netseeker.ejoe.adapter.SerializeAdapter#write(java.lang.Object, java.io.OutputStream)
118      */
119     public void write( Object obj, OutputStream out ) throws Exception
120     {
121         this._marshaller.marshal( obj, out );
122     }
123 
124     /*
125      * (non-Javadoc)
126      * 
127      * @see de.netseeker.ejoe.adapter.SerializeAdapter#handleClassLoaderChange(java.lang.ClassLoader)
128      */
129     public void handleClassLoaderChange( ClassLoader classLoader )
130     {
131         try
132         {
133             this._jcontext = JAXBContext.newInstance( this._packagePath, classLoader );
134             this._marshaller = _jcontext.createMarshaller();
135             this._marshaller.setProperty( Marshaller.JAXB_ENCODING, EJConstants.EJOE_DEFAULT_CHARSET );
136             this._unmarschaller = _jcontext.createUnmarshaller();
137         }
138         catch ( JAXBException e )
139         {
140             log.log( Level.WARNING, "Classloader change failed!", e );
141         }
142     }
143 
144     /*
145      * (non-Javadoc)
146      * 
147      * @see de.netseeker.ejoe.adapter.BaseAdapter#getContentType()
148      */
149     public String getContentType()
150     {
151         return "text/xml";
152     }
153 }