PageRenderTime 827ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/org.restlet.example/src/main/java/org/restlet/example/ext/guice/Main.java

http://github.com/restlet/restlet-framework-java
Java | 184 lines | 99 code | 37 blank | 48 comment | 6 complexity | 71df4aade6a198bef16d51eb296a869d MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, CPL-1.0, LGPL-2.1
  1. /**
  2. * Copyright 2005-2020 Talend
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can
  6. * select the license that you prefer but you may not use this file except in
  7. * compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the Apache 2.0 license at
  10. * http://www.opensource.org/licenses/apache-2.0
  11. *
  12. * You can obtain a copy of the EPL 1.0 license at
  13. * http://www.opensource.org/licenses/eclipse-1.0
  14. *
  15. * See the Licenses for the specific language governing permissions and
  16. * limitations under the Licenses.
  17. *
  18. * Alternatively, you can obtain a royalty free commercial license with less
  19. * limitations, transferable or non-transferable, directly at
  20. * https://restlet.talend.com/
  21. *
  22. * Restlet is a registered trademark of Talend S.A.
  23. */
  24. package org.restlet.example.ext.guice;
  25. import static com.google.inject.name.Names.named;
  26. import java.util.concurrent.atomic.AtomicInteger;
  27. import javax.inject.Inject;
  28. import javax.inject.Named;
  29. import org.restlet.Application;
  30. import org.restlet.Component;
  31. import org.restlet.Restlet;
  32. import org.restlet.data.Protocol;
  33. import org.restlet.ext.guice.FinderFactory;
  34. import org.restlet.ext.guice.RestletGuice;
  35. import org.restlet.resource.Get;
  36. import org.restlet.resource.ServerResource;
  37. import org.restlet.routing.Router;
  38. import com.google.inject.AbstractModule;
  39. import com.google.inject.Injector;
  40. import com.google.inject.Module;
  41. /**
  42. * Demonstrates trivial use of FinderFactory and RestletGuice by starting a
  43. * component on port 8182 with a single application that routes to Finder
  44. * instances obtained from a FinderFactory. A command line argument controls
  45. * whether the Injector behind the FinderFactory is created explicitly with
  46. * RestletGuice or implicitly from the first use of RestletGuice.Module as a
  47. * FinderFactory.
  48. */
  49. public class Main extends Application {
  50. public static class DefaultResource extends ServerResource {
  51. private final String path;
  52. @Inject
  53. DefaultResource(@Named(PATH_QUALIFIER)
  54. String path) {
  55. this.path = path;
  56. }
  57. @Get
  58. public String represent() {
  59. return String.format("Default resource, try %s -- mode is %s",
  60. path, mode);
  61. }
  62. }
  63. public static class HelloServerResource extends ServerResource {
  64. private static final AtomicInteger count = new AtomicInteger();
  65. private final String msg;
  66. @Inject
  67. public HelloServerResource(@Named(MSG_QUALIFIER)
  68. String msg) {
  69. this.msg = msg;
  70. }
  71. @Get
  72. public String asString() {
  73. return String.format("%d: %s", count.incrementAndGet(), msg);
  74. }
  75. }
  76. enum Mode {
  77. /**
  78. * Injector is created implicitly by first use of RestletGuice.Module as
  79. * FinderFactory.
  80. */
  81. AUTO_INJECTOR,
  82. /**
  83. * Injector is created explicitly with RestletGuice.
  84. */
  85. EXPLICIT_INJECTOR
  86. }
  87. static final String HELLO_MSG = "Hello, Restlet 2.0 - Guice 2.0!";
  88. static final String HELLO_PATH = "/hello";
  89. /**
  90. * Whether to create Injector explicitly or automatically.
  91. */
  92. static volatile Mode mode = Mode.AUTO_INJECTOR;
  93. static final String MSG_QUALIFIER = "hello.message";
  94. static final String PATH_QUALIFIER = "hello.path";
  95. /**
  96. * Creates and starts component. Pass 'auto' or 'explicit' (or a prefix of
  97. * either) to control how the Injector is created. Default is auto.
  98. */
  99. public static void main(String[] args) throws Exception {
  100. if (args.length > 0) {
  101. if ("explicit".startsWith(args[0])) {
  102. mode = Mode.EXPLICIT_INJECTOR;
  103. } else if (!"auto".startsWith(args[0])) {
  104. System.out
  105. .println("Call with prefix of 'auto' (default) or 'explicit'");
  106. System.exit(1);
  107. }
  108. }
  109. Component component = new Component();
  110. component.getServers().add(Protocol.HTTP, 8182);
  111. component.getDefaultHost().attach(new Main());
  112. component.start();
  113. }
  114. @Override
  115. public Restlet createInboundRoot() {
  116. Module bindings = new AbstractModule() {
  117. protected void configure() {
  118. bind(ServerResource.class).annotatedWith(HelloWorld.class).to(
  119. HelloServerResource.class);
  120. bindConstant().annotatedWith(named(MSG_QUALIFIER))
  121. .to(HELLO_MSG);
  122. bindConstant().annotatedWith(named(PATH_QUALIFIER)).to(
  123. HELLO_PATH);
  124. }
  125. };
  126. FinderFactory ff = null;
  127. switch (mode) {
  128. case EXPLICIT_INJECTOR:
  129. Injector injector = RestletGuice.createInjector(bindings);
  130. ff = injector.getInstance(FinderFactory.class);
  131. break;
  132. case AUTO_INJECTOR:
  133. default:
  134. ff = new RestletGuice.Module(bindings);
  135. break;
  136. }
  137. assert ff != null : "Must specify Injector creation mode.";
  138. Router router = new Router(getContext());
  139. // Route HELLO_PATH to whatever is bound to ServerResource
  140. // annotated with @HelloWorld.
  141. router.attach(HELLO_PATH,
  142. ff.finder(ServerResource.class, HelloWorld.class));
  143. // Everything else goes to DefaultResource.
  144. router.attachDefault(ff.finder(DefaultResource.class));
  145. return router;
  146. }
  147. }