package org.sc.command;
import java.util.Iterator;
import org.sc.meta.EndpointMetadata;
import org.sc.epresolver.*;
import org.sc.command.clopts.CLOptions;
import org.sc.command.clopts.CLOptionsException;
public class Resolve
{
public static void main(String[] args)
{
Resolve r=new Resolve();
int ret;
if (!r.init(args))
ret=1;
else
ret=r.resolve();
System.exit(ret);
}
ResolveCLOptions opt;
String serviceName;
Resolve() {}
boolean init (String[] args)
{
boolean ok=true;
try
{
opt=new ResolveCLOptions(args);
if (opt.args.length!=1)
{
usage(null);
ok=false;
}
else
serviceName=opt.args[0];
}
catch (CLOptionsException e)
{
usage(e);
ok=false;
}
return ok;
}
int resolve()
{
if (!opt.isUseMap)
return resolveFac();
else
return resolveMap();
}
int resolveFac()
{
int ret;
try
{
EndpointResolver r=EndpointResolverFactory.getResolver();
if (!opt.isGetAll)
{
try
{
EndpointMetadata ep=r.resolve(serviceName);
out(ep);
ret=0;
}
catch (NoEndpointFoundException e)
{
System.out.println(e.getMessage());
ret=2;
}
catch (MoreThanOneEndpointException e)
{
System.out.println(e.getMessage());
ret=2;
}
}
else
{
System.out.println(CommandLineStrings.format("resultsHdr","Endpoints resolved for service {0}:",serviceName));
Iterator<EndpointMetadata> epi=r.resolveAll(serviceName);
if (!epi.hasNext())
{
System.out.println(new NoEndpointFoundException(serviceName).getMessage());
ret=2;
}
else
{
while (epi.hasNext())
out(epi.next());
ret=0;
}
}
r.done();
}
catch (EndpointResolverException e)
{
System.out.println(CommandLineStrings.get("error","Unexpected error"));
e.printStackTrace();
ret=2;
}
return ret;
}
int resolveMap()
{
int ret;
try
{
System.out.println("URL: "+new EndpointResolverBean().getResolverMap().get(serviceName));
ret=0;
}
catch (Exception e)
{
System.out.println(CommandLineStrings.get("error.map","Unexpected error"));
e.printStackTrace();
ret=2;
}
return ret;
}
class ResolveCLOptions extends CLOptions
{
boolean isGetAll,isUseMap;
ResolveCLOptions(String[] args)
throws CLOptionsException
{
super(args);
}
protected boolean takeSwitch(String switchText)
{
if ("all".equals(switchText))
{
isGetAll=true;
return true;
}
else if ("map".equals(switchText))
{
isUseMap=true;
return true;
}
return false;
}
} /* class ResolveCLOptions */
public static void usage(Throwable e)
{
if (e!=null)
System.out.println(e.getMessage());
System.out.println(CommandLineStrings.get("usage","Usage: resolve [-all] [-map] <serviceName>"));
CommandLineTools.usage();
}
static void out(EndpointMetadata ep)
{
System.out.println(CommandLineStrings.format("endpointInfo.1","Endpoint URL: {0}",ep.url));
}
}