/*=============================================================================*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*=============================================================================*/
package org.apache.ws.util.jndi.tools;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.wsfx.wsrf.jndi.config.JndiConfigDocument;
import org.apache.wsfx.wsrf.jndi.config.ServiceDocument;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
/**
* @author Sal Campana
*/
public class JndiConfigUpdater
extends MatchingTask
{
/**
* Below default can be overridden by setting the below sysprop.
*/
public static final String SYSPROP_JNDI_CONFIG = "wsdd";
/**
* Default to looking for jndi-config.xml in current dir or classpath.
*/
private static final String DEFAULT_JNDI_CONFIG = "jndi-config.xml";
private String m_jndiConfig;
private List m_jndiConfigFiles = new ArrayList();
/**
* Creates a new {@link JndiConfigUpdater} object.
*/
public JndiConfigUpdater( )
{
this( (String) null );
}
/**
* Creates a new {@link JndiConfigUpdater} object for updating the specified WSDD file.
*
* @param jndiConfig DOCUMENT_ME
*/
public JndiConfigUpdater( File jndiConfig )
{
this( jndiConfig.getPath( ) );
}
/**
* Creates a new {@link JndiConfigUpdater} object for updating the specified jndi-config.xml, which may be a a file path or a location in
* the classpath.
*
* @param jndiConfig DOCUMENT_ME
*/
public JndiConfigUpdater( String jndiConfig )
{
initContextClassLoader( );
if ( jndiConfig != null )
{
m_jndiConfig = jndiConfig;
}
else
{
m_jndiConfig =
( System.getProperty( SYSPROP_JNDI_CONFIG ) != null ) ? System.getProperty( SYSPROP_JNDI_CONFIG )
: DEFAULT_JNDI_CONFIG;
}
}
/**
* Sets the JndiConfig fragment to be added to the JNDIonfig file.
*
* @param deployJndiConfig the JNDIConfigfragment to be added to the JndiConfig file.
*/
public void setDeployJndiConfig( File deployJndiConfig )
{
m_jndiConfigFiles.add( deployJndiConfig );
}
public void addConfiguredJndiConfigPaths( FileSet jndiConfigPaths )
{
File baseDir = jndiConfigPaths.getDir( getProject() );
DirectoryScanner dirScanner = jndiConfigPaths.getDirectoryScanner( getProject() );
String[] includedFiles = dirScanner.getIncludedFiles();
for ( int i = 0; i < includedFiles.length; i++ )
{
m_jndiConfigFiles.add( new File( baseDir, includedFiles[i] ) );
}
}
/**
* Sets the WSDD to be updated.
*
* @param jndiConfig the Jndi-config.xml to be updated
*/
public void setJndiConfig( String jndiConfig )
{
m_jndiConfig = jndiConfig;
}
/**
* DOCUMENT_ME
*
* @param deployJndiConfig DOCUMENT_ME
* @throws Exception DOCUMENT_ME
*/
public void deploy( File deployJndiConfig )
throws Exception
{
setDeployJndiConfig( deployJndiConfig );
execute( );
}
/**
* DOCUMENT_ME
*
* @throws BuildException DOCUMENT_ME
*/
public void execute( )
throws BuildException
{
if ( m_jndiConfigFiles.isEmpty( ) )
{
throw new BuildException( "No deploy jndi-config's were specified!" );
}
try
{
for ( int i = 0; i < m_jndiConfigFiles.size( ); i++ )
{
deployJndiConfig( (File) m_jndiConfigFiles.get( i ) );
}
}
catch ( Exception e )
{
throw new BuildException( e );
}
}
/**
* Command-line invocation entry point.
*
* @param args command-line arguments
* @throws Exception on fatal error
*/
public static void main( String[] args )
throws Exception
{
//System.setProperty( SYSPROP_JNDI_CONFIG, "C:/jndi-config.xml" );
if ( args.length != 1 )
{
System.err.println( "Usage: " + JndiConfigUpdater.class.getName( ) + " deployJndiConfigFile" );
System.exit( 1 );
}
new JndiConfigUpdater( ).deploy( new File( args[0] ) );
}
private void deployJndiConfig( File deployJndiConfig )
throws Exception
{
log( "Deploying Jndi-Config " + deployJndiConfig + " to configuration Jndi-Config " + m_jndiConfig + "..." );
File jndiConfigFile = new File(m_jndiConfig);
JndiConfigDocument sourceConfig = JndiConfigDocument.Factory.parse(new File(m_jndiConfig));
JndiConfigDocument.JndiConfig jndiConfig = sourceConfig.getJndiConfig();
Map services = new HashMap();
ServiceDocument.Service[] serviceArray = jndiConfig.getServiceArray();
for (int i = 0; i < serviceArray.length; i++)
{
ServiceDocument.Service service = serviceArray[i];
services.put(service.getName(), service);
}
for (int i = 0; i < m_jndiConfigFiles.size(); i++)
{
File config = (File) m_jndiConfigFiles.get(i);
JndiConfigDocument newConfig = JndiConfigDocument.Factory.parse(config);
ServiceDocument.Service[] newServices = newConfig.getJndiConfig().getServiceArray();
for (int j = 0; j < newServices.length; j++)
{
ServiceDocument.Service newService = newServices[j];
String name = newService.getName();
if(services.containsKey(name))
{
System.out.println("The service named: " + name + "is being updated in the jndi-config file: " + m_jndiConfig);
}
services.put(newService.getName(), newService );
}
}
ServiceDocument.Service[] updatedServiceArray = (ServiceDocument.Service[]) services.values().toArray(new ServiceDocument.Service[0]);
jndiConfig.setServiceArray(updatedServiceArray);
sourceConfig.save(jndiConfigFile);
}
private void initContextClassLoader( )
{
// this is done because for some reason, when run using Maven,
// the ContextClassloader is null, which causes an issue with Axis.
if ( Thread.currentThread( ).getContextClassLoader( ) == null )
{
Thread.currentThread( ).setContextClassLoader( MatchingTask.class.getClassLoader( ) );
}
}
}