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 org.netbeans.modules.identity.profile.api.configurator.ConfiguratorException;
047: import org.netbeans.modules.identity.profile.api.configurator.ServerProperties;
048: import org.netbeans.modules.identity.profile.api.configurator.TrustAuthorityConfigurator.Type;
049: import org.netbeans.modules.identity.profile.api.configurator.spi.TrustAuthorityConfig;
050:
051: /**
052: * Dynamic implementation class for the TrustAuthorityConfig interface.
053: *
054: * Created on August 2, 2006, 3:35 AM
055: *
056: * @author ptliu
057: */
058: public class TrustAuthorityConfigImpl implements TrustAuthorityConfig {
059: private static final String AM_TRUST_AUTHORITY_CONFIG_CLASS = "com.sun.identity.wss.provider.TrustAuthorityConfig"; //NOI18N
060:
061: private static final String AM_DISCOVERY_TRUST_AUTHORITY_FIELD = "DISCOVERY_TRUST_AUTHORITY"; //NOI18N
062:
063: private static final String AM_GET_CONFIG_METHOD = "getConfig"; //NOI18N
064:
065: private static final String AM_SET_ENDPOINT_METHOD = "setEndpoint"; //NOI18N
066:
067: private static final String AM_GET_ENDPOINT_METHOD = "getEndpoint"; //NOI18N
068:
069: private static final String AM_SAVE_CONFIG_METHOD = "saveConfig"; //NOI18N
070:
071: private String name;
072: private Type type;
073: private ServerProperties properties;
074: private Object trustAuthConfig;
075: private Class trustAuthConfigClass;
076: private ClassLoader classLoader;
077:
078: /** Creates a new instance of TrustAuthorityConfigImpl */
079: public TrustAuthorityConfigImpl(String name, Type type,
080: ServerProperties properties) {
081: this .name = name;
082: this .type = type;
083: this .properties = properties;
084: }
085:
086: protected ClassLoader getClassLoader() {
087: if (classLoader == null) {
088: classLoader = ClassLoaderManager.getDefault()
089: .getClassLoader(properties);
090: }
091:
092: return classLoader;
093: }
094:
095: protected Class getTrustAuthorityConfigClass() {
096: if (trustAuthConfigClass == null) {
097: try {
098: trustAuthConfigClass = getClassLoader().loadClass(
099: AM_TRUST_AUTHORITY_CONFIG_CLASS);
100: } catch (Exception ex) {
101: throw createConfiguratorException(ex);
102: }
103: }
104:
105: return trustAuthConfigClass;
106: }
107:
108: protected Object getTrustAuthorityConfig() {
109: if (trustAuthConfig == null) {
110: try {
111: Method method = getTrustAuthorityConfigClass()
112: .getMethod(AM_GET_CONFIG_METHOD, String.class,
113: String.class);
114: trustAuthConfig = method.invoke(null, name,
115: getTypeValue());
116:
117: //System.out.println("trustAuthConfig = " + trustAuthConfig); //NOI18N
118: } catch (Exception ex) {
119: throw createConfiguratorException(ex);
120: }
121: }
122:
123: return trustAuthConfig;
124: }
125:
126: public Type getType() {
127: return type;
128: }
129:
130: public String getName() {
131: return name;
132: }
133:
134: public ServerProperties getServerProperties() {
135: return properties;
136: }
137:
138: public void setEndpoint(String endpoint) {
139: try {
140: Method method = getTrustAuthorityConfigClass().getMethod(
141: AM_SET_ENDPOINT_METHOD);
142: method.invoke(getTrustAuthorityConfig(), endpoint);
143: } catch (Exception ex) {
144: throw createConfiguratorException(ex);
145: }
146: }
147:
148: public String getEndpoint() {
149: try {
150: Method method = getTrustAuthorityConfigClass().getMethod(
151: AM_GET_ENDPOINT_METHOD);
152: return (String) method.invoke(getTrustAuthorityConfig());
153: } catch (Exception ex) {
154: throw createConfiguratorException(ex);
155: }
156: }
157:
158: public void saveConfig() {
159: try {
160: Method method = getTrustAuthorityConfigClass().getMethod(
161: AM_SAVE_CONFIG_METHOD,
162: getTrustAuthorityConfigClass());
163: method.invoke(null, getTrustAuthorityConfig());
164: } catch (Exception ex) {
165: throw createConfiguratorException(ex);
166: }
167: }
168:
169: private String getTypeValue() {
170: try {
171: String fieldName = null;
172:
173: if (type == Type.DISCOVERY) {
174: fieldName = AM_DISCOVERY_TRUST_AUTHORITY_FIELD;
175: }
176:
177: Field field = getTrustAuthorityConfigClass().getField(
178: fieldName);
179: return (String) field.get(null);
180: } catch (Exception ex) {
181: throw createConfiguratorException(ex);
182: }
183: }
184:
185: protected ConfiguratorException createConfiguratorException(
186: Exception ex) {
187: Throwable cause = ex.getCause();
188:
189: return new ConfiguratorException((cause != null) ? cause : ex);
190: }
191: }
|