001: /*
002: * $Id: WSTrustFactory.java,v 1.11 2007/08/09 06:09:44 shyam_rao Exp $
003: */
004:
005: /*
006: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
007: *
008: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
009: *
010: * The contents of this file are subject to the terms of either the GNU
011: * General Public License Version 2 only ("GPL") or the Common Development
012: * and Distribution License("CDDL") (collectively, the "License"). You
013: * may not use this file except in compliance with the License. You can obtain
014: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
015: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
016: * language governing permissions and limitations under the License.
017: *
018: * When distributing the software, include this License Header Notice in each
019: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
020: * Sun designates this particular file as subject to the "Classpath" exception
021: * as provided by Sun in the GPL Version 2 section of the License file that
022: * accompanied this code. If applicable, add the following below the License
023: * Header, with the fields enclosed by brackets [] replaced by your own
024: * identifying information: "Portions Copyrighted [year]
025: * [name of copyright owner]"
026: *
027: * Contributor(s):
028: *
029: * If you wish your version of this file to be governed by only the CDDL or
030: * only the GPL Version 2, indicate your decision by adding "[Contributor]
031: * elects to include this software in this distribution under the [CDDL or GPL
032: * Version 2] license." If you don't indicate a single choice of license, a
033: * recipient has the option to distribute your version of this file under
034: * either the CDDL, the GPL Version 2 or to extend the choice of license to
035: * its licensees as provided above. However, if you add GPL Version 2 code
036: * and therefore, elected the GPL Version 2 license, then the option applies
037: * only if the new code is made subject to such option by the copyright
038: * holder.
039: */
040:
041: package com.sun.xml.ws.security.trust;
042:
043: import com.sun.xml.ws.api.security.trust.STSAttributeProvider;
044: import com.sun.xml.ws.api.security.trust.STSAuthorizationProvider;
045: import com.sun.xml.ws.api.security.trust.WSTrustContract;
046: import com.sun.xml.ws.api.security.trust.WSTrustException;
047: import com.sun.xml.ws.api.security.trust.config.STSConfiguration;
048: import com.sun.xml.ws.api.security.trust.config.STSConfigurationProvider;
049: import com.sun.xml.ws.security.trust.elements.RequestSecurityToken;
050: import com.sun.xml.ws.security.trust.elements.RequestSecurityTokenResponse;
051:
052: import com.sun.xml.ws.security.trust.impl.DefaultSTSAttributeProvider;
053: import com.sun.xml.ws.security.trust.impl.DefaultSTSAuthorizationProvider;
054: import com.sun.xml.ws.security.trust.impl.WSTrustClientContractImpl;
055: import com.sun.xml.ws.security.trust.impl.TrustPluginImpl;
056:
057: import com.sun.xml.ws.util.ServiceFinder;
058:
059: import java.util.logging.Level;
060: import java.util.logging.Logger;
061: import com.sun.xml.ws.security.trust.logging.LogDomainConstants;
062: import com.sun.xml.ws.security.trust.logging.LogStringsMessages;
063:
064: /**
065: * A Factory for creating concrete WS-Trust contract instances
066: */
067: public class WSTrustFactory {
068:
069: private static final Logger log = Logger.getLogger(
070: LogDomainConstants.TRUST_IMPL_DOMAIN,
071: LogDomainConstants.TRUST_IMPL_DOMAIN_BUNDLE);
072:
073: /**
074: * return a concrete implementation for the TrustPlugin.
075: */
076: public static TrustPlugin newTrustPlugin(final Configuration config) {
077: return new TrustPluginImpl(config);
078: }
079:
080: /**
081: * Return a concrete implementor of WSTrustContract.
082: * <p>
083: * Note: This contract is based on JAXB Beans generated for ws-trust.xsd schema elements
084: * </p>
085: * @Exception UnsupportedOperationException if this factory does not support this contract
086: */
087: public static WSTrustContract<RequestSecurityToken, RequestSecurityTokenResponse> newWSTrustContract(
088: final STSConfiguration config, final String appliesTo)
089: throws WSTrustException {
090: //final STSConfiguration stsConfig = (STSConfiguration)config;
091: //TrustSPMetadata spMetadata = stsConfig.getTrustSPMetadata(appliesTo);
092: // if(spMetadata == null){
093: // spMetadata = stsConfig.getTrustSPMetadata(WSTrustConstants.DEFAULT_APPLIESTO);
094: // }
095: //if (config. == null){
096: // log.log(Level.SEVERE,
097: // LogStringsMessages.WST_0004_UNKNOWN_SERVICEPROVIDER(appliesTo));
098: // throw new WSTrustException(LogStringsMessages.WST_0004_UNKNOWN_SERVICEPROVIDER(appliesTo));
099: // }
100: String type = config.getType();
101: if (log.isLoggable(Level.FINE)) {
102: log.log(Level.FINE, LogStringsMessages
103: .WST_1002_PROVIDER_TYPE(type));
104: }
105: WSTrustContract<RequestSecurityToken, RequestSecurityTokenResponse> contract = null;
106: try {
107: Class clazz = null;
108: final ClassLoader loader = Thread.currentThread()
109: .getContextClassLoader();
110:
111: if (loader == null) {
112: clazz = Class.forName(type);
113: } else {
114: clazz = loader.loadClass(type);
115: }
116:
117: if (clazz != null) {
118: contract = (WSTrustContract<RequestSecurityToken, RequestSecurityTokenResponse>) clazz
119: .newInstance();
120: contract.init(config);
121: }
122: } catch (ClassNotFoundException ex) {
123: contract = null;
124: log.log(Level.SEVERE, LogStringsMessages
125: .WST_0005_CLASSNOTFOUND_NULL_CONTRACT(type), ex);
126: throw new WSTrustException(LogStringsMessages
127: .WST_0005_CLASSNOTFOUND_NULL_CONTRACT(type), ex);
128: } catch (Exception ex) {
129: log.log(Level.SEVERE, LogStringsMessages
130: .WST_0038_INIT_CONTRACT_FAIL(), ex);
131: throw new WSTrustException(LogStringsMessages
132: .WST_0038_INIT_CONTRACT_FAIL(), ex);
133: }
134:
135: return contract;
136: }
137:
138: /**
139: * return a concrete implementor for WS-Trust Client Contract
140: */
141: public static WSTrustClientContract createWSTrustClientContract(
142: final Configuration config) {
143: return new WSTrustClientContractImpl(config);
144: }
145:
146: /**
147: * Returns the single instance of STSAuthorizationProvider
148: * Use the usual services mechanism to find implementing class. If not
149: * found, use <code>com.sun.xml.ws.security.trust.impl.DefaultSTSAuthorizationProvider</code>
150: * by default.
151: *
152: */
153: public static STSAuthorizationProvider getSTSAuthorizationProvider() {
154:
155: STSAuthorizationProvider authzProvider = null;
156: final ServiceFinder<STSAuthorizationProvider> finder = ServiceFinder
157: .find(STSAuthorizationProvider.class);
158: java.util.Iterator it = finder.iterator();
159: if (it.hasNext()) {
160: authzProvider = (STSAuthorizationProvider) it.next();
161: } else {
162: authzProvider = new DefaultSTSAuthorizationProvider();
163: }
164: return authzProvider;
165: }
166:
167: /**
168: * Returns the single instance of STSAttributeProvider
169: * Use the usual services mechanism to find implementing class. If not
170: * found, use <code>com.sun.xml.ws.security.trust.impl.DefaultSTSAttributeProvider</code>
171: * by default.
172: *
173: */
174: public static STSAttributeProvider getSTSAttributeProvider() {
175:
176: STSAttributeProvider attrProvider = null;
177: final ServiceFinder<STSAttributeProvider> finder = ServiceFinder
178: .find(STSAttributeProvider.class);
179: java.util.Iterator it = finder.iterator();
180: if (it.hasNext()) {
181: attrProvider = (STSAttributeProvider) it.next();
182: } else {
183: attrProvider = new DefaultSTSAttributeProvider();
184: }
185: return attrProvider;
186: }
187:
188: public static STSConfiguration getRuntimeSTSConfiguration() {
189: STSConfigurationProvider configProvider = null;
190: final ServiceFinder<STSConfigurationProvider> finder = ServiceFinder
191: .find(STSConfigurationProvider.class);
192: java.util.Iterator it = finder.iterator();
193: if (it.hasNext()) {
194: configProvider = (STSConfigurationProvider) it.next();
195: }
196:
197: if (configProvider != null) {
198: return configProvider.getSTSConfiguration();
199: }
200:
201: return null;
202: }
203:
204: private WSTrustFactory() {
205: //private constructor
206: }
207: }
|