You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2010/07/09 14:09:50 UTC
[Tapestry Wiki] Update of "Tapestry5HowToCreateAConfigureableService" by DanielCaldeweyher
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.
The "Tapestry5HowToCreateAConfigureableService" page has been changed by DanielCaldeweyher.
http://wiki.apache.org/tapestry/Tapestry5HowToCreateAConfigureableService
--------------------------------------------------
New page:
Although his example describes how to create a sandbox and production version of a service using symbols, this method can be used for various other scenarios.
Scenario: you are developing a shopping cart and want to test your app against the sandbox PayPal NVPServer, without having to hard-code the variables into your implementation:
Note: This examples assumes you can read symbols from properties files (see Tapestry5HowToReadSymbolsFromPropertiesFile). Also it uses the [[http://paypal-nvp.sourceforge.net|PayPal NVP library]]
{{{
public class AppModule {
public PropertiesFileSymbolProvider buildClasspathPropertiesFileSymbolProvider(Logger logger) {
return new PropertiesFileSymbolProvider(logger, "paypal.properties", true);
}
}
}}}
we have a properties file that stores our credentials for the different services ({{{src/main/resources/paypal.properties}}}):
{{{
#paypal.api.endpoint=live
paypal.api.endpoint=sandbox
paypal.api.sandbox.username=my_sandbox_username@email.com
paypal.api.sandbox.password=my_sandbox_password@email.com
paypal.api.sandbox.signature=SANDBOX_SIGNATURE
paypal.api.live.username=my_live_username@email.com
paypal.api.live.password=my_live_username@email.com
paypal.api.live.signature=LIVE_SIGNATURE
}}}
Our PayPalNVPService Implementation looks something like this:
{{{
public class PayPalNVPService {
/* set user - these are your credentials from paypal */
private Profile user;
private Environment environment;
public PayPalNVPService(Environment environment, String apiUsername, String apiPassword, String signature) {
this.environment = environment;
user = new BaseProfile.Builder(apiUsername, apiPassword).signature(signature).build();
}
public boolean SetExpressCheckout(String returnURL, String cancelURL, ... ) {
/* create new instance of paypal nvp */
PayPal pp = new PayPal(user, environment);
...
}
}
}}}
Ideally we want to be able to do something like {{{@Inject @Symbol("paypal.api.${paypal.api.endpoint}.username") String userName}}}. However this doesn't work, as only @Value and @InjectService allow Symbol substitution. Thus, we will have to to create a new symbol:
{{{
public class AppModule {
public static void contributeApplicationDefaults(MappedConfiguration<String, String> configuration) {
configuration.add("PayPalNVPService", "paypal.nvp.${paypal.api.endpoint}");
}
}
}}}
And now we'll just need to create two different services, based on the different credentials:
{{{
# Java code
public class AppModule {
@ServiceId("paypal.nvp.sandbox")
public static PayPalNVPService buildPayPalNVPSandboxService(
@Inject @Symbol(value="paypal.api.sandbox.username") String apiUsername,
@Inject @Symbol(value="paypal.api.sandbox.password") String apiPassword,
@Inject @Symbol(value="paypal.api.sandbox.signature") String signature) {
return new PayPalNVPService(Environment.SANDBOX, apiUsername, apiPassword, signature);
}
@ServiceId("paypal.nvp.live")
public static PayPalNVPService buildPayPalNVPLiveService(
@Inject @Symbol(value="paypal.api.live.username") String apiUsername,
@Inject @Symbol(value="paypal.api.live.password") String apiPassword,
@Inject @Symbol(value="paypal.api.live.signature") String signature) {
return new PayPalNVPService(Environment.LIVE, apiUsername, apiPassword, signature);
}
}
}}}
And lastly inject the service into our page. Notice how we have to evaluate the PayPalNVPService symbol to get the actual service id:
{{{
public class ViewCart {
@InjectService("${PayPalNVPService}")
private PayPalNVPService paypalService;
public void onActionFromCheckoutWithPayPal() {
boolean success = paypalService.SetExpressCheckout("http://localhost:8080/website/checkout/revieworder", "http://localhost:8080/website/viewcart", ...);
...
}
}
}}}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org