001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.identity.profile.api.configurator.impl.dynamic;
043:
044: import java.lang.reflect.Field;
045: import java.lang.reflect.Method;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.Iterator;
050: import java.util.List;
051: import org.netbeans.modules.identity.profile.api.configurator.ConfiguratorException;
052: import org.netbeans.modules.identity.profile.api.configurator.SecurityMechanism;
053: import org.netbeans.modules.identity.profile.api.configurator.ServerProperties;
054:
055: /**
056: * Helper class for retrieving the low-level SecurityMechanism instances from
057: * the AM client sdk.
058: *
059: * Created on July 12, 2006, 1:20 PM
060: *
061: * @author ptliu
062: */
063: public class SecurityMechanismRetriever {
064:
065: private static final String AM_PROVIDER_CONFIG_CLASS = "com.sun.identity.wss.provider.ProviderConfig"; //NOI18N
066:
067: private static final String AM_GET_ALL_SUPPORT_SECURITY_MECH_METHOD = "getAllSupportedSecurityMech"; //NOI18N
068:
069: private static final String AM_GET_ALL_MESSAGE_LEVEL_SECURITY_MECH_METHOD = "getAllMessageLevelSecurityMech"; //NOI18N
070:
071: private static final String AM_SECURITY_MECHANISM_CLASS = "com.sun.identity.wss.security.SecurityMechanism"; //NOI18N
072:
073: private static final String AM_GET_LIBERTY_SECURITY_MECHANISM_URIS_METHOD = "getLibertySecurityMechanismURIs"; //NOI18N
074:
075: private static final String AM_GET_SECURITY_MECHANISM_METHOD = "getSecurityMechanism"; //NOI18N
076:
077: private static final String AM_LIBERTY_DS_SECURITY_FIELD = "LIBERTY_DS_SECURITY"; //NOI18N
078:
079: private ClassLoader loader;
080:
081: public SecurityMechanismRetriever(String id) {
082: loader = ClassLoaderManager.getDefault().getClassLoader(
083: ServerProperties.getInstance(id));
084: }
085:
086: /**
087: * Returns all the non-Liberty security mechanisms.
088: *
089: */
090: public Collection<SecurityMechanism> getAllSupportedSecurityMechanisms() {
091: try {
092: Class clazz = loader.loadClass(AM_PROVIDER_CONFIG_CLASS);
093: Method method = clazz
094: .getMethod(AM_GET_ALL_SUPPORT_SECURITY_MECH_METHOD);
095: return convertToSecurityMechProxies((List) method
096: .invoke(null));
097: } catch (Exception ex) {
098: throw ConfiguratorException.create(ex);
099: }
100: }
101:
102: /**
103: * Returns all the non-Liberty message-level security mechanisms.
104: *
105: */
106: public Collection<SecurityMechanism> getAllMessageLevelSecurityMechanism() {
107: try {
108: Class clazz = loader.loadClass(AM_PROVIDER_CONFIG_CLASS);
109: Method method = clazz
110: .getMethod(AM_GET_ALL_MESSAGE_LEVEL_SECURITY_MECH_METHOD);
111:
112: return convertToSecurityMechProxies((List) method
113: .invoke(null));
114: } catch (Exception ex) {
115: throw ConfiguratorException.create(ex);
116: }
117: }
118:
119: /*
120: * Returns all the WSP Liberty security mechanism.
121: *
122: */
123: public Collection<SecurityMechanism> getAllWSPLibertySecurityMechanisms() {
124: try {
125: Class clazz = loader.loadClass(AM_SECURITY_MECHANISM_CLASS);
126: Method method = clazz
127: .getMethod(AM_GET_LIBERTY_SECURITY_MECHANISM_URIS_METHOD);
128: List uris = (List) method.invoke(null);
129:
130: method = clazz.getMethod(AM_GET_SECURITY_MECHANISM_METHOD,
131: String.class);
132: List secMechs = new ArrayList();
133:
134: for (int i = 0; i < uris.size(); i++) {
135: String uri = (String) uris.get(i);
136: secMechs.add(method.invoke(null, uri));
137: }
138:
139: return convertToSecurityMechProxies(secMechs);
140: } catch (Exception ex) {
141: throw ConfiguratorException.create(ex);
142: }
143: }
144:
145: /**
146: *
147: *
148: */
149: public Collection<SecurityMechanism> getAllWSCLibertySecurityMechanisms() {
150: ArrayList secMechs = new ArrayList();
151:
152: try {
153: Class clazz = loader.loadClass(AM_SECURITY_MECHANISM_CLASS);
154: Field field = clazz.getField(AM_LIBERTY_DS_SECURITY_FIELD);
155: secMechs.add(field.get(null));
156:
157: return convertToSecurityMechProxies(secMechs);
158: } catch (Exception ex) {
159: throw ConfiguratorException.create(ex);
160: }
161: }
162:
163: private Collection<SecurityMechanism> convertToSecurityMechProxies(
164: List secMechs) {
165: ArrayList<SecurityMechanism> secMechProxies = new ArrayList<SecurityMechanism>();
166: Iterator iter = secMechs.iterator();
167:
168: while (iter.hasNext()) {
169: SecurityMechanism proxy = new SecurityMechanismImpl(iter
170: .next());
171: secMechProxies.add(proxy);
172: }
173:
174: return Collections.unmodifiableCollection(secMechProxies);
175: }
176:
177: }
|