View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.remoting.protocol.jaxws;
18  
19  import javax.xml.bind.JAXBContext;
20  import javax.xml.bind.JAXBException;
21  import javax.xml.bind.Marshaller;
22  import javax.xml.bind.Unmarshaller;
23  import javax.xml.ws.handler.soap.SOAPHandler;
24  import javax.xml.ws.handler.soap.SOAPMessageContext;
25  
26  import org.jdom.Namespace;
27  import org.slf4j.Logger;
28  
29  /**
30   *
31   * Baseclass for the JAXB context handlers, implementing some
32   * common functionalities.
33   *
34   * @svnLink $Revision: 4077 $;$Date: 2010-01-06 16:18:36 +0100 (Mi, 06. Jan 2010) $;$Author: swisswheel $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/remoting_jaxws/src/main/java/ch/elca/el4j/services/remoting/protocol/jaxws/AbstractJaxwsJaxbContextHandler.java $
35   *
36   * @author Philippe Jacot (PJA)
37   * @author Stefan Wismer (SWI)
38   */
39  public abstract class AbstractJaxwsJaxbContextHandler
40  	implements SOAPHandler<SOAPMessageContext> {
41  	
42  	/**
43  	 * Name of the element in the header containing the context.
44  	 */
45  	protected static final String CONTEXT_ELEMENT_NAME = "context";
46  	
47  	/**
48  	 * Name of the namespace of the element containing the context.
49  	 */
50  	protected static final Namespace CONTEXT_NAMESPACE = Namespace.getNamespace(
51  		"ch.elca.el4j.services.remoting.protocol.jaxws");
52  	
53  	/**
54  	 * The context used to serialize the implicit context.
55  	 */
56  	private JAXBContext m_jaxbContext;
57  	
58  	/**
59  	 * Creates a new hander with a context.
60  	 * @param jaxbContext The <code>JAXBContext</code> to serialize the context
61  	 */
62  	public AbstractJaxwsJaxbContextHandler(JAXBContext jaxbContext) {
63  		if (jaxbContext == null) {
64  			try {
65  				m_jaxbContext = JAXBContext.newInstance(new Class[]{});
66  			} catch (JAXBException e) {
67  				throw new NullPointerException("No JAXBContext passed and "
68  					+ "unable to create an empty one: " + e.getMessage());
69  			}
70  		} else {
71  			m_jaxbContext = jaxbContext;
72  		}
73  	}
74  	
75  	/**
76  	 * Get a JaxbContext.
77  	 * @return A JaxbContext
78  	 */
79  	protected JAXBContext getJaxbContext() {
80  		return m_jaxbContext;
81  	}
82  	
83  	/**
84  	 * Convenience method to get a marshaller.
85  	 * @return A marshaller
86  	 */
87  	protected Marshaller getMarshaller() {
88  		try {
89  			return m_jaxbContext.createMarshaller();
90  		} catch (JAXBException e) {
91  			getLogger().error(
92  				"Unable to create marshaller for context passing", e);
93  			return null;
94  		}
95  	}
96  	
97  	/**
98  	 * Convenience method to get an unmarshaller.
99  	 * @return A marshaller
100 	 */
101 	protected Unmarshaller getUnmarshaller() {
102 		try {
103 			return m_jaxbContext.createUnmarshaller();
104 		} catch (JAXBException e) {
105 			getLogger().error(
106 				"Unable to create unmarshaller for context passing", e);
107 			return null;
108 		}
109 	}
110 	
111 	/**
112 	 * Get the logger for this class.
113 	 * @return The Logger
114 	 */
115 	protected abstract Logger getLogger();
116 	
117 }