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: package org.netbeans.modules.identity.ant;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import org.apache.tools.ant.BuildException;
046: import org.apache.tools.ant.Task;
047: import org.netbeans.modules.identity.profile.api.configurator.ConfiguratorException;
048: import org.netbeans.modules.identity.profile.api.configurator.ProviderConfigurator.Configurable;
049: import org.netbeans.modules.identity.profile.api.configurator.ProviderConfigurator;
050: import org.netbeans.modules.identity.profile.api.configurator.ProviderConfigurator.Type;
051: import org.netbeans.modules.identity.profile.api.configurator.Configurator.AccessMethod;
052: import org.netbeans.modules.identity.profile.api.configurator.DiscoveryConfigurator;
053: import org.netbeans.modules.identity.profile.api.configurator.ServerProperties;
054: import org.netbeans.modules.identity.profile.api.configurator.SecurityMechanism;
055: import org.netbeans.modules.identity.profile.api.configurator.TrustAuthorityConfigurator;
056: import org.netbeans.modules.identity.server.manager.api.ServerInstance;
057: import org.netbeans.modules.identity.server.manager.api.ServerManager;
058:
059: /**
060: * Ant task for deploying configuration data to the AM server.
061: *
062: * Created on June 26, 2006, 11:00 PM
063: *
064: * @author ptliu
065: */
066: public class AMDeploy extends Task {
067: private static final String PROP_AM_CONFIG_FILE = "AM_CONFIG_FILE"; //NOI18N
068:
069: // // This is temporarily until we have the appserver fix for FCS
070: // private static final String WSC = "wsc"; //NOI18N
071:
072: private static final String LOCAL_DISCO = "LocalDisco"; //NOI18N
073:
074: private String amConfigXmlDir;
075: private String amAsUrl;
076: private DiscoveryConfigurator discoveryConfig;
077:
078: private static final Configurable[] wscConfigurables = {
079: Configurable.SECURITY_MECH, Configurable.SIGN_RESPONSE,
080: Configurable.USERNAME_PASSWORD_PAIRS,
081: Configurable.USE_DEFAULT_KEYSTORE,
082: Configurable.SERVICE_TYPE };
083:
084: private static final Configurable[] wspConfigurables = { Configurable.SERVICE_TYPE };
085:
086: private static final Configurable[] keystoreConfigurables = {
087: Configurable.KEYSTORE_LOCATION,
088: Configurable.KEYSTORE_PASSWORD, Configurable.KEY_ALIAS,
089: Configurable.KEY_PASSWORD };
090:
091: /** Creates a new instance of AMDeploy */
092: public AMDeploy() {
093: }
094:
095: public void setAmconfigxmldir(String amConfigXmlDir) {
096: this .amConfigXmlDir = amConfigXmlDir;
097: log("amConfigXmlDir: " + amConfigXmlDir); //NOI18N
098: }
099:
100: public void setAmasurl(String url) {
101: this .amAsUrl = url;
102: log("amAsUrl: " + amAsUrl); //NOI18N
103: }
104:
105: public void execute() throws BuildException {
106: try {
107: ServerProperties.getInstance(amAsUrl);
108: } catch (ConfiguratorException ex) {
109: log("skip am deploy");
110: return;
111: }
112:
113: try {
114: deployWSCProviders();
115: deployWSPProviders();
116: } catch (ConfiguratorException ex) {
117: //ex.printStackTrace();
118: ex.getCause().printStackTrace();
119: } catch (Exception ex) {
120: ex.printStackTrace();
121: }
122: }
123:
124: private void deployWSCProviders() {
125: Collection<ProviderConfigurator> fileConfigs = ProviderConfigurator
126: .getAllConfigurators(Type.WSC, AccessMethod.FILE,
127: amConfigXmlDir, amAsUrl);
128:
129: for (ProviderConfigurator fileConfig : fileConfigs) {
130: log("Deploying wsc provider "
131: + fileConfig.getProviderName()); //NOI18N
132:
133: ServerInstance instance = ServerManager.getDefault()
134: .getServerInstance(amAsUrl);
135: ServerProperties properties = instance
136: .getServerProperties();
137:
138: log("ServerProperties: " + properties); //NOI18N
139:
140: ProviderConfigurator dynamicConfig = ProviderConfigurator
141: .getConfigurator(fileConfig.getProviderName(),
142: fileConfig.getType(), AccessMethod.DYNAMIC,
143: properties, amAsUrl);
144:
145: //
146: // This is temporary until we have the appserver fix for FCS. For
147: // now we use "wsc" as the provider name.
148: //
149: // ProviderConfigurator dynamicConfig =
150: // ProviderConfigurator.getConfigurator(WSC,
151: // fileConfig.getType(), AccessMethod.DYNAMIC,
152: // (ServerProperties) fileConfig.getValue(Configurable.SERVER_PROPERTIES));
153:
154: transferConfigurationData(fileConfig, dynamicConfig,
155: wscConfigurables);
156:
157: Boolean value = (Boolean) fileConfig
158: .getValue(Configurable.USE_DEFAULT_KEYSTORE);
159: if (value == Boolean.FALSE) {
160: transferConfigurationData(fileConfig, dynamicConfig,
161: keystoreConfigurables);
162: }
163:
164: if (isLiberty(fileConfig)) {
165: ArrayList list = new ArrayList();
166: list.add(getDiscoveryConfigurator(properties));
167: dynamicConfig.setValue(
168: Configurable.TRUST_AUTHORITY_CONFIG_LIST, list);
169: }
170:
171: dynamicConfig.save();
172: }
173:
174: }
175:
176: private void deployWSPProviders() {
177: Collection<ProviderConfigurator> fileConfigs = ProviderConfigurator
178: .getAllConfigurators(Type.WSP, AccessMethod.FILE,
179: amConfigXmlDir, amAsUrl);
180:
181: for (ProviderConfigurator fileConfig : fileConfigs) {
182: log("Deploying wsp provider "
183: + fileConfig.getProviderName()); //NOI18N
184:
185: ServerInstance instance = ServerManager.getDefault()
186: .getServerInstance(amAsUrl);
187: ServerProperties properties = instance
188: .getServerProperties();
189:
190: log("ServerProperties: " + properties); //NOI18N
191:
192: SecurityMechanism secMech = (SecurityMechanism) fileConfig
193: .getValue(Configurable.SECURITY_MECH);
194:
195: ProviderConfigurator dynamicConfig = ProviderConfigurator
196: .getConfigurator(secMech.getName(), Type.WSP,
197: AccessMethod.DYNAMIC, properties, amAsUrl);
198:
199: transferConfigurationData(fileConfig, dynamicConfig,
200: wspConfigurables);
201:
202: if (isLiberty(fileConfig)) {
203: //
204: // Need to reset the security mechanism for the preconfigured
205: // liberty providers which are invalid.
206: //
207: dynamicConfig.setValue(Configurable.SECURITY_MECH,
208: secMech);
209:
210: // wsp endpoint cannot be null and cannot be empty
211: dynamicConfig.setValue(Configurable.WSP_ENDPOINT,
212: "http://wsc.com"); //NOI18N
213:
214: dynamicConfig.save();
215:
216: DiscoveryConfigurator discoveryConfig = getDiscoveryConfigurator(properties);
217:
218: log("Registering provider with Discovery Service"); //NOI18N
219: discoveryConfig.unregisterProvider(dynamicConfig);
220: discoveryConfig.registerProvider(dynamicConfig);
221: } else {
222: dynamicConfig.save();
223: }
224: }
225: }
226:
227: private void transferConfigurationData(
228: ProviderConfigurator fromConfig,
229: ProviderConfigurator toConfig, Configurable[] configurables) {
230: for (Configurable configurable : configurables) {
231: log(configurable + ": " + fromConfig.getValue(configurable)); //NOI18N
232: toConfig.setValue(configurable, fromConfig
233: .getValue(configurable));
234: }
235: }
236:
237: private boolean isLiberty(ProviderConfigurator config) {
238: SecurityMechanism secMech = (SecurityMechanism) config
239: .getValue(Configurable.SECURITY_MECH);
240:
241: return secMech.isLiberty();
242: }
243:
244: private DiscoveryConfigurator getDiscoveryConfigurator(
245: ServerProperties properties) {
246: if (discoveryConfig == null) {
247: discoveryConfig = (DiscoveryConfigurator) TrustAuthorityConfigurator
248: .getConfigurator(LOCAL_DISCO,
249: TrustAuthorityConfigurator.Type.DISCOVERY,
250: AccessMethod.DYNAMIC, properties);
251:
252: discoveryConfig
253: .setValue(
254: TrustAuthorityConfigurator.Configurable.ENDPOINT,
255: properties
256: .getProperty(ServerProperties.PROP_LIBERTY_DISCO_SERVICE_URL));
257: discoveryConfig.save();
258: }
259:
260: return discoveryConfig;
261: }
262: }
|