/*=============================================================================*
* 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;
import org.apache.axis.AxisEngine;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.naming.ResourceRef;
import org.apache.ws.util.jndi.tools.ConfigContext;
import org.apache.ws.util.jndi.tools.Environment;
import org.apache.ws.util.jndi.tools.Resource;
import org.apache.ws.util.jndi.tools.ResourceLink;
import org.apache.ws.util.jndi.tools.ResourceParameters;
import javax.naming.Context;
import javax.naming.LinkRef;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import java.util.Iterator;
import java.util.Set;
/**
* DOCUMENT_ME
*
* @author $author$
* @version $Revision: 1.4 $
*/
public class NamingContext
{
private static Log LOG = LogFactory.getLog( NamingContext.class.getName( ) );
private Context context;
private AxisEngine engine;
/**
* Creates a new {@link NamingContext} object.
*
* @param context DOCUMENT_ME
* @param engine DOCUMENT_ME
*/
public NamingContext( Context context,
AxisEngine engine )
{
this.context = context;
this.engine = engine;
}
/**
* DOCUMENT_ME
*
* @param environment DOCUMENT_ME
*
* @throws IllegalArgumentException DOCUMENT_ME
* @throws NamingException DOCUMENT_ME
*/
public void addEnvironment( Environment environment )
throws IllegalArgumentException,
NamingException
{
Object value = null;
// Instantiate a new instance of the correct object type, and
// initialize it.
String type = environment.getType( );
LOG.debug("Attempting to add environment for type: " + type);
try
{
if ( type.equals( "java.lang.String" ) )
{
value = environment.getValue( );
}
else if ( type.equals( "java.lang.Byte" ) )
{
if ( environment.getValue( ) == null )
{
value = new Byte( (byte) 0 );
}
else
{
value = Byte.decode( environment.getValue( ) );
}
}
else if ( type.equals( "java.lang.Short" ) )
{
if ( environment.getValue( ) == null )
{
value = new Short( (short) 0 );
}
else
{
value = Short.decode( environment.getValue( ) );
}
}
else if ( type.equals( "java.lang.Integer" ) )
{
if ( environment.getValue( ) == null )
{
value = new Integer( 0 );
}
else
{
value = Integer.decode( environment.getValue( ) );
}
}
else if ( type.equals( "java.lang.Long" ) )
{
if ( environment.getValue( ) == null )
{
value = new Long( 0 );
}
else
{
value = Long.decode( environment.getValue( ) );
}
}
else if ( type.equals( "java.lang.Boolean" ) )
{
value = Boolean.valueOf( environment.getValue( ) );
}
else if ( type.equals( "java.lang.Double" ) )
{
if ( environment.getValue( ) == null )
{
value = new Double( 0 );
}
else
{
value = Double.valueOf( environment.getValue( ) );
}
}
else if ( type.equals( "java.lang.Float" ) )
{
if ( environment.getValue( ) == null )
{
value = new Float( 0 );
}
else
{
value = Float.valueOf( environment.getValue( ) );
}
}
else if ( type.equals( "java.lang.Character" ) )
{
if ( environment.getValue( ) == null )
{
value = new Character( (char) 0 );
}
else
{
if ( environment.getValue( ).length( ) == 1 )
{
value = new Character( environment.getValue( ).charAt( 0 ) );
}
else
{
throw new IllegalArgumentException( );
}
}
}
else
{
throw new IllegalArgumentException( "invalidType : " + type );
}
}
catch ( NumberFormatException e )
{
String msg = "invalidValueForType : " + environment.getValue( ) + " : " + type;
LOG.error( msg, e );
throw new IllegalArgumentException( msg );
}
// Bind the object to the appropriate name
createSubcontexts( environment.getName( ) );
this.context.bind( environment.getName( ),
value );
if ( LOG.isDebugEnabled( ) )
{
LOG.debug( "Added environment entry with name: " + environment.getName( ) );
LOG.debug( "value: " + value + " and type: " + environment.getType( ) );
}
}
/**
* DOCUMENT_ME
*
* @param resource DOCUMENT_ME
*
* @throws NamingException DOCUMENT_ME
*/
public void addResource( Resource resource )
throws NamingException
{
Reference reference =
new ResourceRef( resource.getType( ),
resource.getDescription( ),
resource.getScope( ),
resource.getAuth( ) );
this.addParameters( reference,
resource.getParameters( ) );
this.createSubcontexts( resource.getName( ) );
this.context.bind( resource.getName( ),
reference );
LOG.debug( "Added resource entry with name: " + resource.getName( ) );
}
/**
* DOCUMENT_ME
*
* @param resourceLink DOCUMENT_ME
*
* @throws NamingException DOCUMENT_ME
*/
public void addResourceLink( ResourceLink resourceLink )
throws NamingException
{
LinkRef link = new LinkRef( resourceLink.getTarget( ) );
this.createSubcontexts( resourceLink.getName( ) );
this.context.bind( resourceLink.getName( ),
link );
if ( LOG.isDebugEnabled( ) )
{
LOG.debug( "Added resource link with name: " + resourceLink.getName( ) );
LOG.debug( "Pointing to: " + resourceLink.getTarget( ) );
}
}
/**
* DOCUMENT_ME
*
* @param subContext DOCUMENT_ME
*
* @throws NamingException DOCUMENT_ME
*/
public void addService( ConfigContext subContext )
throws NamingException
{
String serviceName = subContext.getName( );
Set names;
Iterator nameIterator;
NamingContext newContext;
Context servicesContext = null;
try
{
servicesContext = (Context) this.context.lookup( "services" );
}
catch ( NameNotFoundException e )
{
servicesContext = this.context.createSubcontext( "services" );
}
JNDIUtils.createSubcontexts( servicesContext, serviceName );
try
{
newContext = new NamingContext( servicesContext.createSubcontext( serviceName ),
this.engine );
LOG.debug( "Created new subcontext with name: " + serviceName );
}
catch ( Exception e )
{
newContext = new NamingContext( (Context) servicesContext.lookup( serviceName ), this.engine );
LOG.debug( "Adding entries to existing subcontext with name: " + serviceName );
}
names = subContext.getEnvironmentNames( );
nameIterator = names.iterator( );
while ( nameIterator.hasNext( ) )
{
newContext.addEnvironment( subContext.getEnvironment( (String) nameIterator.next( ) ) );
}
names = subContext.getResourceNames( );
nameIterator = names.iterator( );
while ( nameIterator.hasNext( ) )
{
newContext.addServiceResource( subContext.getResource( (String) nameIterator.next( ) ),
serviceName );
}
names = subContext.getResourceLinkNames( );
nameIterator = names.iterator( );
while ( nameIterator.hasNext( ) )
{
newContext.addResourceLink( subContext.getResourceLink( (String) nameIterator.next( ) ) );
}
}
/**
* DOCUMENT_ME
*
* @param resource DOCUMENT_ME
* @param serviceName DOCUMENT_ME
*
* @throws NamingException DOCUMENT_ME
*/
public void addServiceResource( Resource resource,
String serviceName )
throws NamingException
{
Reference reference =
new ServiceResourceRef( resource.getType( ),
resource.getDescription( ),
resource.getScope( ),
resource.getAuth( ), this.engine, serviceName );
this.addParameters( reference,
resource.getParameters( ) );
this.createSubcontexts( resource.getName( ) );
this.context.bind( resource.getName( ),
reference );
LOG.debug( "Added service resource entry with name: " + resource.getName( ) );
}
/**
* DOCUMENT_ME
*
* @param subContext DOCUMENT_ME
*
* @throws NamingException DOCUMENT_ME
*/
public void addSubContext( ConfigContext subContext )
throws NamingException
{
addService( subContext );
}
private void addParameters( Reference reference,
ResourceParameters parameters )
{
if ( parameters != null )
{
Set names = parameters.getParameterNames( );
Iterator nameIterator = names.iterator( );
RefAddr parameter;
String parameterName;
while ( nameIterator.hasNext( ) )
{
parameterName = (String) nameIterator.next( );
parameter = new StringRefAddr( parameterName,
parameters.getParameter( parameterName ) );
reference.add( parameter );
}
}
}
private void createSubcontexts( String name )
throws NamingException
{
JNDIUtils.createSubcontexts( this.context, name );
}
}