GhApplication.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package hello;
  2. import hello.home.entity.*;
  3. import hello.home.handler.*;
  4. import com.techempower.*;
  5. import com.techempower.cache.*;
  6. import com.techempower.data.*;
  7. import com.techempower.gemini.*;
  8. import com.techempower.gemini.cluster.client.*;
  9. import com.techempower.gemini.cluster.client.handler.*;
  10. import com.techempower.gemini.data.*;
  11. import com.techempower.gemini.exceptionhandler.*;
  12. import com.techempower.gemini.path.*;
  13. import com.techempower.gemini.pyxis.*;
  14. import com.techempower.js.*;
  15. /**
  16. * GeminiHello Application. As a subclass of GeminiApplication, this
  17. * class acts as the central "hub" of references to components such as
  18. * the Dispatcher and EntityStore.
  19. *
  20. * @see com.techempower.gemini.GeminiApplication
  21. *
  22. * Development history:
  23. * 2012-04-19 - mh - Created
  24. *
  25. * @author mhixson
  26. */
  27. public class GhApplication
  28. extends GeminiApplication
  29. implements PyxisApplication
  30. {
  31. //
  32. // Static variables.
  33. //
  34. private static final GhApplication instance = new GhApplication();
  35. //
  36. // Member methods.
  37. //
  38. /**
  39. * Constructor. This method can be extended to construct references
  40. * to custom components for the application.
  41. */
  42. public GhApplication()
  43. {
  44. super();
  45. // Add the GhInfrastructure as an Asynchronous resource
  46. // that should be stopped and started along with the application.
  47. addAsynchronous(getInfrastructure());
  48. }
  49. /**
  50. * Constructs an application-specific Context, for an inbound request.
  51. * This is overloaded to return a custom GhContext.
  52. */
  53. @Override
  54. public GhContext getContext(Request request)
  55. {
  56. return new GhContext(
  57. request,
  58. this
  59. );
  60. }
  61. /**
  62. * Returns an application-specific reference to the Security manager.
  63. */
  64. @Override
  65. public GhSecurity getSecurity()
  66. {
  67. return (GhSecurity)super.getSecurity();
  68. }
  69. /**
  70. * Returns an application-specific reference to the EntityStore.
  71. */
  72. @Override
  73. public GhStore getStore()
  74. {
  75. return (GhStore)super.getStore();
  76. }
  77. /**
  78. * Returns an application-specific reference to the Infrastructure.
  79. */
  80. @Override
  81. public GhInfrastructure getInfrastructure()
  82. {
  83. return (GhInfrastructure)super.getInfrastructure();
  84. }
  85. //
  86. // Protected component constructors.
  87. //
  88. /**
  89. * Constructs a Version reference. This is overloaded to return a
  90. * custom GhVersion object.
  91. */
  92. @Override
  93. protected Version constructVersion()
  94. {
  95. return new GhVersion();
  96. }
  97. /**
  98. * Construct a Security reference.
  99. */
  100. @Override
  101. protected PyxisSecurity constructSecurity()
  102. {
  103. return new GhSecurity(this);
  104. }
  105. /**
  106. * Constructs an EntityStore reference. Overload to return a custom
  107. * object.
  108. * <p>
  109. * Note: it is acceptable to return null if no object caching is used.
  110. * The default implementation does exactly that.
  111. */
  112. @Override
  113. protected EntityStore constructEntityStore()
  114. {
  115. return new GhStore(this, getConnectorFactory());
  116. }
  117. /**
  118. * Constructs the application's Cluster Client.
  119. */
  120. @Override
  121. protected ClusterClient constructClusterClient()
  122. {
  123. ClusterClient client = super.constructClusterClient();
  124. client.addHandler(new LogNoteHandler(this));
  125. return client;
  126. }
  127. /**
  128. * Constructs a Infrastructure reference. This is overloaded to return a
  129. * custom GhInfrastructure object.
  130. */
  131. @Override
  132. protected BasicInfrastructure constructInfrastructure()
  133. {
  134. return new GhInfrastructure(this);
  135. }
  136. /**
  137. * Creates a JavaScriptWriter for writing JSON.
  138. */
  139. @Override
  140. protected JavaScriptWriter constructJavaScriptWriter()
  141. {
  142. return JavaScriptWriter.custom()
  143. .addVisitorFactory(World.class, World.VISITOR_FACTORY)
  144. .build();
  145. }
  146. /**
  147. * Constructs a Dispatcher.
  148. */
  149. @Override
  150. protected Dispatcher constructDispatcher()
  151. {
  152. final PathDispatcher.Configuration<GhContext> config = new PathDispatcher.Configuration<>();
  153. config.setDefault(new HelloHandler(this))
  154. .add(new BasicExceptionHandler(this));
  155. return new PathDispatcher<>(this, config);
  156. }
  157. /**
  158. * Construct a DatabaseConnectionListener. If this returns non-null,
  159. * this listener object will be provided to the ConnectorFactory instance
  160. * upon its creation.
  161. */
  162. @Override
  163. protected DatabaseConnectionListener constructDatabaseConnectionListener()
  164. {
  165. return new BasicConnectorListener(this);
  166. }
  167. //
  168. // Static methods.
  169. //
  170. public static GhApplication getInstance()
  171. {
  172. return instance;
  173. }
  174. } // End GhApplication.