001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.webservices.saaj;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import javax.xml.soap.SOAPException;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: class SAAJFactoryFinder {
027:
028: private static final Log LOG = LogFactory
029: .getLog(SAAJFactoryFinder.class);
030:
031: private static final String SAAJ_PROVIDER_PROPERTY = "org.apache.geronimo.saaj.provider";
032:
033: private static final Map<String, Map<String, String>> SAAJ_FACTORIES = new HashMap<String, Map<String, String>>();
034:
035: private static SAAJUniverse.Type DEFAULT_SAAJ_UNIVERSE = null;
036:
037: static {
038: SAAJ_FACTORIES
039: .put(
040: SAAJUniverse.Type.AXIS1.toString(),
041: createSAAJInfo(
042: "org.apache.axis.soap.MessageFactoryImpl",
043: "org.apache.axis.soap.SOAPFactoryImpl",
044: "org.apache.axis.soap.SOAPConnectionFactoryImpl",
045: "org.apache.axis.soap.SAAJMetaFactoryImpl"));
046: SAAJ_FACTORIES
047: .put(
048: SAAJUniverse.Type.AXIS2.toString(),
049: createSAAJInfo(
050: "org.apache.axis2.saaj.MessageFactoryImpl",
051: "org.apache.axis2.saaj.SOAPFactoryImpl",
052: "org.apache.axis2.saaj.SOAPConnectionFactoryImpl",
053: "org.apache.axis2.saaj.SAAJMetaFactoryImpl"));
054: SAAJ_FACTORIES
055: .put(
056: SAAJUniverse.Type.SUN.toString(),
057: createSAAJInfo(
058: "com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl",
059: "com.sun.xml.messaging.saaj.soap.ver1_1.SOAPFactory1_1Impl",
060: "com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnectionFactory",
061: "com.sun.xml.messaging.saaj.soap.SAAJMetaFactoryImpl"));
062:
063: initDefaultSAAJProvider();
064: }
065:
066: private static void initDefaultSAAJProvider() {
067: String provider = System.getProperty(SAAJ_PROVIDER_PROPERTY);
068: if (provider != null) {
069: if (provider.equalsIgnoreCase("axis2")) {
070: DEFAULT_SAAJ_UNIVERSE = SAAJUniverse.Type.AXIS2;
071: } else if (provider.equalsIgnoreCase("sun")) {
072: DEFAULT_SAAJ_UNIVERSE = SAAJUniverse.Type.SUN;
073: } else {
074: throw new RuntimeException(
075: "Invalid SAAJ universe specified: " + provider);
076: }
077:
078: LOG.info("Default SAAJ universe: " + DEFAULT_SAAJ_UNIVERSE);
079: } else {
080: LOG.info("Default SAAJ universe not set");
081: }
082: }
083:
084: private static Map<String, String> createSAAJInfo(
085: String messageFactory, String soapFactory,
086: String soapConnectionFactory, String metaFactory) {
087: Map<String, String> map = new HashMap<String, String>();
088: map.put("javax.xml.soap.MessageFactory", messageFactory);
089: map.put("javax.xml.soap.SOAPFactory", soapFactory);
090: map.put("javax.xml.soap.SOAPConnectionFactory",
091: soapConnectionFactory);
092: map.put("javax.xml.soap.MetaFactory", metaFactory);
093: return map;
094: }
095:
096: static Object find(String factoryPropertyName) throws SOAPException {
097: String factoryClassName = getFactoryClass(factoryPropertyName);
098: if (factoryClassName == null) {
099: throw new SOAPException("Provider for "
100: + factoryPropertyName + " cannot be found", null);
101: } else {
102: return newInstance(factoryClassName);
103: }
104: }
105:
106: private static String getFactoryClass(String factoryName) {
107: SAAJUniverse.Type universe = SAAJUniverse.getCurrentUniverse();
108: if (universe == null || universe == SAAJUniverse.Type.DEFAULT) {
109: if (DEFAULT_SAAJ_UNIVERSE == null) {
110: // Default SAAJ universe not set.
111: // Prefer Axis2 SAAJ if it is in class loader, otherwise use Sun's
112: if (isAxis2InClassLoader()) {
113: universe = SAAJUniverse.Type.AXIS2;
114: } else {
115: universe = SAAJUniverse.Type.SUN;
116: }
117: } else {
118: // Use default SAAJ universe
119: universe = DEFAULT_SAAJ_UNIVERSE;
120: }
121: }
122:
123: return SAAJ_FACTORIES.get(universe.toString()).get(factoryName);
124: }
125:
126: private static boolean isAxis2InClassLoader() {
127: try {
128: loadClass("org.apache.axis2.saaj.MessageFactoryImpl");
129: return true;
130: } catch (ClassNotFoundException e) {
131: return false;
132: }
133: }
134:
135: private static Class loadClass(String className)
136: throws ClassNotFoundException {
137: ClassLoader classLoader = Thread.currentThread()
138: .getContextClassLoader();
139: if (classLoader == null) {
140: return Class.forName(className);
141: } else {
142: return classLoader.loadClass(className);
143: }
144: }
145:
146: private static Object newInstance(String factoryClassName)
147: throws SOAPException {
148: try {
149: Class factory = null;
150: try {
151: factory = loadClass(factoryClassName);
152: } catch (ClassNotFoundException cnfe) {
153: factory = SAAJFactoryFinder.class.getClassLoader()
154: .loadClass(factoryClassName);
155: }
156: return factory.newInstance();
157: } catch (ClassNotFoundException e) {
158: throw new SOAPException("Provider " + factoryClassName
159: + " not found", e);
160: } catch (Exception e) {
161: throw new SOAPException("Provider " + factoryClassName
162: + " could not be instantiated: " + e.getMessage(),
163: e);
164: }
165: }
166:
167: }
|