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.openejb.server.webservices.saaj;
017:
018: import org.apache.openejb.util.Logger;
019: import org.apache.openejb.util.LogCategory;
020:
021: import javax.xml.soap.SOAPException;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: class SaajFactoryFinder {
026: private static final Logger logger = Logger.getInstance(
027: LogCategory.OPENEJB_WS, SaajFactoryFinder.class);
028: private static final String SAAJ_PROVIDER_PROPERTY = "org.apache.openejb.server.webservices.saaj.provider";
029:
030: private static SaajUniverse.Type DEFAULT_SAAJ_UNIVERSE = null;
031:
032: private static final Map<String, Map<String, String>> SAAJ_FACTORIES = new HashMap<String, Map<String, String>>();
033: static {
034: SAAJ_FACTORIES
035: .put(
036: SaajUniverse.Type.AXIS1.toString(),
037: createSAAJInfo(
038: "org.apache.axis.soap.MessageFactoryImpl",
039: "org.apache.axis.soap.SOAPFactoryImpl",
040: "org.apache.axis.soap.SOAPConnectionFactoryImpl",
041: "org.apache.axis.soap.SAAJMetaFactoryImpl"));
042: SAAJ_FACTORIES
043: .put(
044: SaajUniverse.Type.AXIS2.toString(),
045: createSAAJInfo(
046: "org.apache.axis2.saaj.MessageFactoryImpl",
047: "org.apache.axis2.saaj.SOAPFactoryImpl",
048: "org.apache.axis2.saaj.SOAPConnectionFactoryImpl",
049: "org.apache.axis2.saaj.SAAJMetaFactoryImpl"));
050: SAAJ_FACTORIES
051: .put(
052: SaajUniverse.Type.SUN.toString(),
053: createSAAJInfo(
054: "com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl",
055: "com.sun.xml.messaging.saaj.soap.ver1_1.SOAPFactory1_1Impl",
056: "com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnectionFactory",
057: "com.sun.xml.messaging.saaj.soap.SAAJMetaFactoryImpl"));
058:
059: initDefaultSAAJProvider();
060: }
061:
062: private static void initDefaultSAAJProvider() {
063: String provider = System.getProperty(SAAJ_PROVIDER_PROPERTY);
064: if (provider != null) {
065: if (provider.equalsIgnoreCase("axis2")) {
066: DEFAULT_SAAJ_UNIVERSE = SaajUniverse.Type.AXIS2;
067: } else if (provider.equalsIgnoreCase("sun")) {
068: DEFAULT_SAAJ_UNIVERSE = SaajUniverse.Type.SUN;
069: } else {
070: throw new RuntimeException(
071: "Invalid SAAJ universe specified: " + provider);
072: }
073:
074: logger.info("Default SAAJ universe: "
075: + DEFAULT_SAAJ_UNIVERSE);
076: } else {
077: logger.info("Default SAAJ universe not set");
078: }
079: }
080:
081: private static Map<String, String> createSAAJInfo(
082: String messageFactory, String soapFactory,
083: String soapConnectionFactory, String metaFactory) {
084: Map<String, String> map = new HashMap<String, String>();
085: map.put("javax.xml.soap.MessageFactory", messageFactory);
086: map.put("javax.xml.soap.SOAPFactory", soapFactory);
087: map.put("javax.xml.soap.SOAPConnectionFactory",
088: soapConnectionFactory);
089: map.put("javax.xml.soap.MetaFactory", metaFactory);
090: return map;
091: }
092:
093: static Object find(String factoryPropertyName) throws SOAPException {
094: String factoryClassName = getFactoryClass(factoryPropertyName);
095: if (factoryClassName == null) {
096: throw new SOAPException("Provider for "
097: + factoryPropertyName + " cannot be found", null);
098: } else {
099: return newInstance(factoryClassName);
100: }
101: }
102:
103: private static String getFactoryClass(String factoryName) {
104: SaajUniverse.Type universe = SaajUniverse.getCurrentUniverse();
105: if (universe == null || universe == SaajUniverse.Type.DEFAULT) {
106: if (DEFAULT_SAAJ_UNIVERSE == null) {
107: // Default SAAJ universe not set.
108: // Prefer Axis2 SAAJ if it is in class loader, otherwise use Sun's
109: if (isAxis2InClassLoader()) {
110: universe = SaajUniverse.Type.AXIS2;
111: } else {
112: universe = SaajUniverse.Type.SUN;
113: }
114: } else {
115: // Use default SAAJ universe
116: universe = DEFAULT_SAAJ_UNIVERSE;
117: }
118: }
119:
120: return SAAJ_FACTORIES.get(universe.toString()).get(factoryName);
121: }
122:
123: private static boolean isAxis2InClassLoader() {
124: try {
125: loadClass("org.apache.axis2.saaj.MessageFactoryImpl");
126: return true;
127: } catch (ClassNotFoundException e) {
128: return false;
129: }
130: }
131:
132: private static Class loadClass(String className)
133: throws ClassNotFoundException {
134: ClassLoader classLoader = Thread.currentThread()
135: .getContextClassLoader();
136: if (classLoader == null) {
137: return Class.forName(className);
138: } else {
139: return classLoader.loadClass(className);
140: }
141: }
142:
143: private static Object newInstance(String factoryClassName)
144: throws SOAPException {
145: try {
146: Class factory = null;
147: try {
148: factory = loadClass(factoryClassName);
149: } catch (ClassNotFoundException cnfe) {
150: factory = SaajFactoryFinder.class.getClassLoader()
151: .loadClass(factoryClassName);
152: }
153: return factory.newInstance();
154: } catch (ClassNotFoundException e) {
155: throw new SOAPException("Provider " + factoryClassName
156: + " not found", e);
157: } catch (Exception e) {
158: throw new SOAPException("Provider " + factoryClassName
159: + " could not be instantiated: " + e.getMessage(),
160: e);
161: }
162: }
163: }
|