001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.xml.internal.ws.spi.runtime;
026:
027: import javax.xml.ws.WebServiceException;
028: import javax.xml.namespace.QName;
029: import static java.lang.Class.forName;
030: import static java.lang.Thread.currentThread;
031: import java.util.HashMap;
032:
033: public abstract class SystemHandlerDelegateFactory {
034:
035: private final static String DEFAULT_FACTORY_NAME = "com.sun.xml.internal.xwss.SystemHandlerDelegateFactory";
036:
037: private static String factoryName;
038:
039: private static HashMap factoryMap;
040:
041: static {
042: init();
043: }
044:
045: private static synchronized void init() {
046: factoryName = DEFAULT_FACTORY_NAME;
047: factoryMap = new HashMap();
048: };
049:
050: // foctory implementations that maintain a map of serviceName to
051: // would override this method
052: /**
053: * Used by the Appserver on client and server sides
054: * factory implementations that maintain a map of serviceName to
055: * factory
056: * @param serviceName when called by the SOAPBindingImpl to
057: * create the SystemHandlerDelegate. serviceName must be
058: * a QName
059: * @return com.sun.xml.internal.ws.spi.runtime.SystemHandlerDelegate
060: * @throws java.lang.Exception when the create failed.
061: */
062: public SystemHandlerDelegate getDelegate(QName serviceName) {
063: return create();
064: }
065:
066: /**
067: * Used by the Appserver and xws-security on client and server sides
068: * factory implementations that maintain a map of serviceName to
069: * factory
070: * @return com.sun.xml.internal.ws.spi.runtime.SystemHandlerDelegate
071: * @throws java.lang.Exception when the create failed.
072: */
073: public abstract SystemHandlerDelegate create();
074:
075: //currently not used
076: public abstract boolean isEnabled(MessageContext messageContext);
077:
078: // factory name can be set to null, in which case,
079: // the default factory will be disabled.
080: /**
081: * Used by the Appserver on client and server sides
082: * factoryName can be set to null, in which case the defaultFactory will be
083: * disabled
084: * @param name when called by the SOAPBindingImpl to
085: * create the SystemHandlerDelegate. serviceName must be
086: * a String
087: */
088: public static synchronized void setFactoryName(String name) {
089: factoryName = name;
090: }
091:
092: /**
093: * Used by the Appserver on client and server sides
094: * factoryName can be set to null, in which case the defaultFactory will be
095: * disabled and will be null on return
096: * @return java.lang.String - name of factory
097: */
098: public static synchronized String getFactoryName() {
099: return factoryName;
100: }
101:
102: /**
103: * Used by the JAX-WS implementation on client and server sides
104: * to load the SystemHandlerDelegateFactory
105: * @return com.sun.xml.internal.ws.spi.runtime.SystemHandlerDelegateFactory
106: * @throws javax.xml.ws.WebServiceException when the load fails.
107: */
108: public static synchronized SystemHandlerDelegateFactory getFactory() {
109:
110: SystemHandlerDelegateFactory factory = (SystemHandlerDelegateFactory) factoryMap
111: .get(factoryName);
112:
113: if (factory != null || factoryMap.containsKey(factoryName)) {
114: return factory;
115: } else {
116:
117: Class clazz = null;
118: try {
119: ClassLoader loader = currentThread()
120: .getContextClassLoader();
121:
122: if (loader == null) {
123: clazz = forName(factoryName);
124: } else {
125: clazz = loader.loadClass(factoryName);
126: }
127:
128: if (clazz != null) {
129: factory = (SystemHandlerDelegateFactory) clazz
130: .newInstance();
131: }
132: } catch (ClassNotFoundException e) {
133: factory = null;
134: // e.printStackTrace(); //todo:need to add log
135: } catch (Exception x) {
136: throw new WebServiceException(x);
137: } finally {
138: // stores null factory values in map to prevent
139: // repeated class loading and instantiation errors.
140: factoryMap.put(factoryName, factory);
141: }
142: }
143: return factory;
144: }
145: }
|