001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.message.databinding.impl;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axis2.jaxws.ExceptionFactory;
023: import org.apache.axis2.jaxws.i18n.Messages;
024: import org.apache.axis2.jaxws.message.Block;
025: import org.apache.axis2.jaxws.message.databinding.JAXBBlockContext;
026: import org.apache.axis2.jaxws.message.factory.JAXBBlockFactory;
027: import org.apache.axis2.jaxws.message.impl.BlockFactoryImpl;
028: import org.apache.axis2.jaxws.utility.XMLRootElementUtil;
029:
030: import javax.xml.bind.JAXBException;
031: import javax.xml.namespace.QName;
032: import javax.xml.stream.XMLStreamException;
033: import javax.xml.ws.WebServiceException;
034:
035: /** JAXBBlockFactoryImpl Creates a JAXBBlock */
036: public class JAXBBlockFactoryImpl extends BlockFactoryImpl implements
037: JAXBBlockFactory {
038:
039: /** Default Constructor required for Factory */
040: public JAXBBlockFactoryImpl() {
041: super ();
042: }
043:
044: /* (non-Javadoc)
045: * @see org.apache.axis2.jaxws.message.BlockFactory#createFrom(org.apache.axiom.om.OMElement, java.lang.Object, javax.xml.namespace.QName)
046: */
047: public Block createFrom(OMElement omElement, Object context,
048: QName qName) throws XMLStreamException, WebServiceException {
049: // The context for a JAXBFactory must be non-null and should be a JAXBBlockContext.
050: if (context == null) {
051: // JAXWS spec 4.3.4 conformance requires a WebServiceException whose cause is JAXBException
052: throw ExceptionFactory
053: .makeWebServiceException(new JAXBException(Messages
054: .getMessage("JAXBBlockFactoryErr1", "null")));
055: } else if (context instanceof JAXBBlockContext) {
056: ;
057: } else {
058: // JAXWS spec 4.3.4 conformance requires a WebServiceException whose cause is JAXBException
059: throw ExceptionFactory
060: .makeWebServiceException(new JAXBException(Messages
061: .getMessage("JAXBBlockFactoryErr1", context
062: .getClass().getName())));
063: }
064: if (qName == null) {
065: qName = omElement.getQName();
066: }
067: return new JAXBBlockImpl(omElement, (JAXBBlockContext) context,
068: qName, this );
069: }
070:
071: /* (non-Javadoc)
072: * @see org.apache.axis2.jaxws.message.BlockFactory#createFrom(java.lang.Object, java.lang.Object, javax.xml.namespace.QName)
073: */
074: public Block createFrom(Object businessObject, Object context,
075: QName qName) throws WebServiceException {
076:
077: // The context must be non-null and should be a JAXBBlockContext.
078: // For legacy reasons, a JAXBContext is also supported (and wrapped into a JAXBBlockContext)
079: if (context == null) {
080: // JAXWS spec 4.3.4 conformance requires a WebServiceException whose cause is JAXBException
081: throw ExceptionFactory
082: .makeWebServiceException(new JAXBException(Messages
083: .getMessage("JAXBBlockFactoryErr1", "null")));
084: } else if (context instanceof JAXBBlockContext) {
085: ;
086: } else {
087: // JAXWS spec 4.3.4 conformance requires a WebServiceException whose cause is JAXBException
088: throw ExceptionFactory
089: .makeWebServiceException(new JAXBException(Messages
090: .getMessage("JAXBBlockFactoryErr1", context
091: .getClass().getName())));
092: }
093:
094: // The business object must be either a JAXBElement or a block with an @XmlRootElement qname.
095: // (Checking this is expensive, so it is assumed)
096: // The input QName must be set otherwise we have to look it up, which kills performance.
097: if (qName == null) {
098: qName = XMLRootElementUtil
099: .getXmlRootElementQNameFromObject(businessObject);
100: }
101:
102: try {
103: return new JAXBBlockImpl(businessObject,
104: (JAXBBlockContext) context, qName, this );
105: } catch (JAXBException e) {
106: throw ExceptionFactory.makeWebServiceException(e);
107: }
108: }
109:
110: public boolean isElement() {
111: return true;
112: }
113:
114: }
|