WicketApplication.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package hellowicket;
  2. import javax.naming.InitialContext;
  3. import javax.sql.DataSource;
  4. import org.apache.wicket.protocol.http.WebApplication;
  5. import org.apache.wicket.settings.RequestCycleSettings;
  6. import com.zaxxer.hikari.HikariDataSource;
  7. import hellowicket.dbupdates.HelloDbUpdatesReference;
  8. import hellowicket.fortune.FortunePage;
  9. import hellowicket.plaintext.HelloTextReference;
  10. /**
  11. * Application object for your web application..
  12. */
  13. public class WicketApplication extends WebApplication
  14. {
  15. private DataSource db;
  16. @Override
  17. public Class<HomePage> getHomePage()
  18. {
  19. return HomePage.class;
  20. }
  21. @Override
  22. public void init()
  23. {
  24. super.init();
  25. db = newDataSource();
  26. // mount the resources under test
  27. mountResource("/json", new HelloJsonReference());
  28. mountResource("/db", new HelloDbReference());
  29. mountResource("/updates", new HelloDbUpdatesReference());
  30. mountResource("/plaintext", new HelloTextReference());
  31. mountPage("/fortunes", FortunePage.class);
  32. // disable response caching to be more close to other
  33. // test applications' behavior
  34. RequestCycleSettings requestCycleSettings = getRequestCycleSettings();
  35. requestCycleSettings.setBufferResponse(false);
  36. // set UTF-8 for /fortunes test
  37. requestCycleSettings.setResponseRequestEncoding("UTF-8");
  38. }
  39. @Override
  40. protected void onDestroy()
  41. {
  42. if (db instanceof HikariDataSource) {
  43. ((HikariDataSource)db).close();
  44. }
  45. super.onDestroy();
  46. }
  47. private boolean useResinDataSource()
  48. {
  49. return false;
  50. }
  51. public static WicketApplication get()
  52. {
  53. return (WicketApplication) WebApplication.get();
  54. }
  55. public DataSource getDataSource()
  56. {
  57. return db;
  58. }
  59. private DataSource newDataSource()
  60. {
  61. DataSource dataSource;
  62. try
  63. {
  64. Class.forName("com.mysql.jdbc.Driver");
  65. if (useResinDataSource())
  66. {
  67. InitialContext jndiContext = new InitialContext();
  68. dataSource = (DataSource) jndiContext.lookup("java:comp/env/jdbc/hello_world");
  69. }
  70. else
  71. {
  72. // allocating a resource for future use should not (auto) close the resource
  73. @SuppressWarnings("resource")
  74. HikariDataSource ds = new HikariDataSource();
  75. // use faster DataSource impl
  76. ds.setJdbcUrl("jdbc:mysql://localhost:3306/hello_world?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useUnbufferedInput=false&useReadAheadInput=false&maintainTimeStats=false&useServerPrepStmts&cacheRSMetadata=true");
  77. ds.setDriverClassName("com.mysql.jdbc.Driver");
  78. ds.setUsername("benchmarkdbuser");
  79. ds.setPassword("benchmarkdbpass");
  80. dataSource = ds;
  81. }
  82. }
  83. catch (Exception x)
  84. {
  85. throw new RuntimeException("Cannot create the data source", x);
  86. }
  87. return dataSource;
  88. }
  89. }