package org.sc.epresolver.file;
import java.util.Properties;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Map;
import org.w3c.dom.*;
import org.sc.meta.ServiceMetadata;
import org.sc.meta.EndpointMetadata;
import org.sc.epresolver.*;
/**
* File-based endpoint resolver
*/
public class FileEndpointResolver implements EndpointResolver
{
protected Properties endpoints;
protected String endpointFileURL;
public FileEndpointResolver()
throws EndpointResolverException
{
}
public EndpointMetadata resolve(String serviceName)
throws EndpointResolverException
{
boolean found;
String endpoint=endpoints.getProperty(serviceName);
if (endpoint==null)
found=false;
else
{
endpoint=endpoint.trim();
if (endpoint.length()==0)
found=false;
else
found=true;
}
if (!found)
throw new NoEndpointFoundException(
FileEndpointResolverStrings.format("notfound","No endpoint found for service \"{0}\" in endpoint file \"{1}\"",serviceName,endpointFileURL)
);
return new EndpointMetadata(new ServiceMetadata(serviceName,serviceName),endpoint);
}
/**
* Retrieves ALL endpoints that can be resolved for a service name
* @return list of endpoints, which may be empty
*/
public Iterator<EndpointMetadata> resolveAll(String serviceName)
throws EndpointResolverException
{
// There can be only one!
EndpointMetadata endpoint=resolve(serviceName);
ArrayList<EndpointMetadata> epFound=new ArrayList<EndpointMetadata>(1);
epFound.add(endpoint);
return epFound.iterator();
}
/**
* Releases resolver resources
*/
public void done()
{
EndpointResolverFactory.done(this);
}
public void takeConfig(Element resolverConfig)
throws EndpointResolverException
{
/*
<resolver class="file">
[ <endpointFileInClasspath>endpoints.properties</endpointFileInClasspath> ]
</resolver>
*/
String resourceName;
Element e=EndpointResolverFactory.getFirstChildElement(resolverConfig);
if (e==null)
resourceName="endpoints.properties";
else
resourceName=EndpointResolverFactory.getElText(EndpointResolverFactory.checkConfigElement(e,"endpointFileInClasspath"));
endpoints=new Properties();
try
{
endpointFileURL=getClass().getClassLoader().getResource(resourceName).toString();
endpoints.load(getClass().getClassLoader().getResourceAsStream(resourceName));
}
catch (Exception ex)
{
throw new UnableToLoadEndpointFileException(
FileEndpointResolverStrings.format("unableToLoad","Unable to load endpoint file \"{0}\" from classpath",resourceName),
ex);
}
}
public Iterator<EndpointMetadata> listCache()
{
return null;
}
public void flushCache() {}
}