com.pastdev.httpcomponents.configuration.InitParameterConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server Show documentation
Show all versions of server Show documentation
A set of interfaces and factories for building servers.
The newest version!
package com.pastdev.httpcomponents.configuration;
import java.util.Enumeration;
import javax.servlet.FilterConfig;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import com.pastdev.httpcomponents.factory.ValueOfValueFactory;
public class InitParameterConfiguration extends MapConfiguration {
public InitParameterConfiguration( ServletContext servletContext ) {
Enumeration names = servletContext.getInitParameterNames();
while ( names.hasMoreElements() ) {
String name = names.nextElement();
add( name, servletContext.getInitParameter( name ) );
}
}
public InitParameterConfiguration( ServletConfig servletConfig ) {
Enumeration names = servletConfig.getInitParameterNames();
while ( names.hasMoreElements() ) {
String name = names.nextElement();
add( name, servletConfig.getInitParameter( name ) );
}
}
public InitParameterConfiguration( FilterConfig filterConfig ) {
Enumeration names = filterConfig.getInitParameterNames();
while ( names.hasMoreElements() ) {
String name = names.nextElement();
add( name, filterConfig.getInitParameter( name ) );
}
}
@Override
public T get( Key key, Class type ) {
return get( key.key(), type );
}
@Override
public T get( String key, Class type ) {
String value = super.get( key, String.class );
if ( value == null || type == String.class ) {
return type.cast( value );
}
else if ( type == Class.class ) {
try {
return type.cast( Class.forName( value ) );
}
catch ( ClassNotFoundException e ) {
throw new IllegalArgumentException( e.getMessage(), e );
}
}
else {
return type.cast( ValueOfValueFactory.valueOf( value, type ) );
}
}
}