public class HaUrlRewriteFunctionProcessor implements UrlRewriteFunctionProcessor<HaUrlRewriteFunctionDescriptor> {
private HostMapperService hostMapperService;
private HostMapper hostMapper = null;
private String clusterName;
@Override
public String name() {
return HaUrlRewriteFunctionDescriptor.FUNCTION_NAME;
}
@Override
public void initialize(UrlRewriteEnvironment environment, HaUrlRewriteFunctionDescriptor descriptor) throws Exception {
hostMapper = new HaBaseStrategyHostMapper();
clusterName = environment.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE );
GatewayServices services = environment.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE );
if( clusterName != null && services != null ) {
hostMapperService = services.getService( GatewayServices.HOST_MAPPING_SERVICE );
if( hostMapperService != null ) {
hostMapperService.registerHostMapperForCluster( clusterName, hostMapper );
}
}
}
@Override
public void destroy() throws Exception {
if( hostMapperService != null && clusterName != null ) {
hostMapperService.removeHostMapperForCluster( clusterName );
}
}
@Override
public List<String> resolve(UrlRewriteContext context, List<String> parameters) throws Exception {
List<String> result = null;
if( parameters != null ) {
result = new ArrayList<String>( parameters.size() );
for( String parameter : parameters ) {
switch( context.getDirection() ) {
case IN:
parameter = hostMapper.resolveInboundHostName( parameter );
break;
case OUT:
parameter = hostMapper.resolveOutboundHostName( parameter );
break;
}
result.add( parameter );
}
}
return result;
}
} |