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:
020: package org.apache.axis2.jaxws.utility;
021:
022: import org.apache.axis2.jaxws.ExceptionFactory;
023: import org.apache.axis2.jaxws.i18n.Messages;
024:
025: import javax.xml.soap.MessageFactory;
026: import javax.xml.soap.SOAPException;
027: import javax.xml.soap.SOAPFactory;
028: import javax.xml.ws.WebServiceException;
029: import java.lang.reflect.Method;
030:
031: /**
032: * Provides convenience methods to construct a SOAP 1.1 or SOAP 1.2 SAAJ MessageFactory or
033: * SOAPFactory. The code uses reflection; thus, when Axis2 upgrades to SAAJ 1.3, no changes will be
034: * neded to this class.
035: */
036: public class SAAJFactory {
037:
038: private static final String SOAP11_ENV_NS = "http://schemas.xmlsoap.org/soap/envelope/";
039: private static final String SOAP12_ENV_NS = "http://www.w3.org/2003/05/soap-envelope";
040:
041: // Protocol Names per the SAAJ 1.3 specification.
042: public static final String SOAP_1_1_PROTOCOL = "SOAP 1.1 Protocol";
043: public static final String SOAP_1_2_PROTOCOL = "SOAP 1.2 Protocol";
044: public static final String DYNAMIC_PROTOCOL = "Dynamic Protocol";
045:
046: /**
047: * Create SOAPFactory using information from the envelope namespace
048: *
049: * @param namespace
050: * @return
051: */
052: public static SOAPFactory createSOAPFactory(String namespace)
053: throws WebServiceException, SOAPException {
054: Method m = getSOAPFactoryNewInstanceProtocolMethod();
055: SOAPFactory sf = null;
056: if (m == null) {
057: if (namespace.equals(SOAP11_ENV_NS)) {
058: sf = SOAPFactory.newInstance();
059: } else {
060: throw ExceptionFactory.makeWebServiceException(Messages
061: .getMessage("SOAP12WithSAAJ12Err"));
062: }
063: } else {
064: String protocol = DYNAMIC_PROTOCOL;
065: if (namespace.equals(SOAP11_ENV_NS)) {
066: protocol = SOAP_1_1_PROTOCOL;
067: } else if (namespace.equals(SOAP12_ENV_NS)) {
068: protocol = SOAP_1_2_PROTOCOL;
069: }
070: try {
071: sf = (SOAPFactory) m.invoke(null,
072: new Object[] { protocol });
073: } catch (Exception e) {
074: throw ExceptionFactory.makeWebServiceException(e);
075: }
076: }
077: return sf;
078: }
079:
080: /**
081: * Create MessageFactory using information from the envelope namespace
082: *
083: * @param namespace
084: * @return
085: */
086: public static MessageFactory createMessageFactory(String namespace)
087: throws WebServiceException, SOAPException {
088: Method m = getMessageFactoryNewInstanceProtocolMethod();
089: MessageFactory mf = null;
090: if (m == null) {
091: if (namespace.equals(SOAP11_ENV_NS)) {
092: mf = MessageFactory.newInstance();
093: } else {
094: throw ExceptionFactory.makeWebServiceException(Messages
095: .getMessage("SOAP12WithSAAJ12Err"));
096: }
097: } else {
098: String protocol = DYNAMIC_PROTOCOL;
099: if (namespace.equals(SOAP11_ENV_NS)) {
100: protocol = SOAP_1_1_PROTOCOL;
101: } else if (namespace.equals(SOAP12_ENV_NS)) {
102: protocol = SOAP_1_2_PROTOCOL;
103: }
104: try {
105: mf = (MessageFactory) m.invoke(null,
106: new Object[] { protocol });
107: } catch (Exception e) {
108: throw ExceptionFactory.makeWebServiceException(e);
109: }
110: }
111: return mf;
112: }
113:
114: private static Method messageFactoryNewInstanceProtocolMethod = null;
115:
116: /**
117: * SAAJ 1.3 has a newInstance method that has a protocol parameter.
118: *
119: * @return newInstance(String) method if available
120: */
121: private static Method getMessageFactoryNewInstanceProtocolMethod() {
122: if (messageFactoryNewInstanceProtocolMethod == null) {
123: try {
124: messageFactoryNewInstanceProtocolMethod = MessageFactory.class
125: .getMethod("newInstance",
126: new Class[] { String.class });
127: } catch (Exception e) {
128: // TODO Might want to log this.
129: // Flow to here indicates that the installed SAAJ model does not support version 1.3
130: messageFactoryNewInstanceProtocolMethod = null;
131: }
132: }
133: return messageFactoryNewInstanceProtocolMethod;
134: }
135:
136: private static Method soapFactoryNewInstanceProtocolMethod = null;
137:
138: /**
139: * SAAJ 1.3 has a newInstance method that has a protocol parameter.
140: *
141: * @return newInstance(String) method if available
142: */
143: private static Method getSOAPFactoryNewInstanceProtocolMethod() {
144: if (soapFactoryNewInstanceProtocolMethod == null) {
145: try {
146: soapFactoryNewInstanceProtocolMethod = SOAPFactory.class
147: .getMethod("newInstance",
148: new Class[] { String.class });
149: } catch (Exception e) {
150: // TODO Might want to log this.
151: // Flow to here indicates that the installed SAAJ model does not support version 1.3
152: soapFactoryNewInstanceProtocolMethod = null;
153: }
154: }
155: return soapFactoryNewInstanceProtocolMethod;
156: }
157:
158: }
|