Reputation: 411
Suppose, we don't have access to some needed server while debugging program on our machine. So we need a stub. But then every time i want make build without stub i need to comment my stub code and uncomment actual code, so it looks dirty. It would be nice if i can configure builds somehow to avoid this commenting/uncommenting. I haven't figured out any good decision.
For example, some code
public class SingeFormatServiceClient {
public static final QName SERVICE_NAME = new QName("http://creditregistry.ru/2010/webservice/SingleFormatService", "SingleFormatService");
public SingleFormatService Connect(){
URL wsdlURL = SingleFormatService_Service.WSDL_LOCATION;
SingleFormatService_Service ss = new SingleFormatService_Service(wsdlURL, SERVICE_NAME);
return ss.getSingleFormatServiceHttpPort();
}
public SingleFormatService Connect(){
return new SingleFormatServiceStub();
}
}
So first function is actual, second is stub. May be there is a way not to comment, but just saying to builder that now i want make build with first function, and now - with second? Thank you.
Upvotes: 0
Views: 82
Reputation: 2145
Use System.getProperty()
to instantiate an implementation. For example:
SingleFormatService service = (SingleFormatService) Class.forName(
System.getProperty("single_format_service_class",
"your.comp.SingleFormatServiceStub")).getConstructor().newInstance();
Your implementation must provide non-arg constructor. In your jvm argument, specify the working class, i.e.
-Dsingle_format_service_class=your.comp.SingleFormatServiceActual
In intellij idea, you could specify several run configurations using different jvm args.
NB. IMO many libraries use that way. Hibernate uses hibernate.cache.provider_class
to choose which cache provider implementation to use.
Upvotes: 1