SHARE
TWEET

com.example.server.MainComponent

tpeierls Jan 23rd, 2012 232 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This code is placed in the public domain by its author, Tim Peierls.
  3.  */
  4. package com.example.server;
  5.  
  6. import com.google.inject.Stage;
  7.  
  8. import javax.inject.Inject;
  9. import javax.inject.Named;
  10. import javax.inject.Singleton;
  11.  
  12. import org.restlet.Component;
  13. import org.restlet.data.Protocol;
  14. //import org.restlet.ext.guice.FinderFactory;
  15. import org.restlet.routing.VirtualHost;
  16. import org.restlet.util.ClientList;
  17.  
  18. /**
  19.  * The main Restlet component
  20.  */
  21. @Singleton
  22. public class MainComponent extends Component {
  23.  
  24.     /**
  25.      * For binding a port number in standalone mode (e.g., {@code @Named(HTTP_PORT) int port}).
  26.      */
  27.     public static final String HTTP_PORT = "main.httpPort";
  28.  
  29.  
  30.     /**
  31.      * Component that contains Restlet applications, deployable in either
  32.      * standalone or servlet mode.
  33.      */
  34.     @Inject MainComponent() {
  35.         setName("Main component");
  36.         setDescription("The main component");
  37.         setOwner("My Organization");
  38.         setAuthor("Me");
  39.     }
  40.  
  41.     /**
  42.      * Configure connectors and wire application(s) to host(s).
  43.      */
  44.     @Inject void initialize(
  45.         // Not used in this example, but can be injected to provide
  46.         // behavior that depends on the Guice stage.
  47.         Stage                   stage,
  48.  
  49.         // Used here to decide whether to set up an HTTP server connector.
  50.         DeploymentMode          deploymentMode,
  51.  
  52.         // Not used in this example, but can be injected in applications for
  53.         // creating Finder instances that will inject resources, e.g.:
  54.         // router.attach("/path/to/my/resource", ff.finder(MyServerResource.class));
  55.         // Needs the Restlet-Guice extension; see the {@link Main} class, where the
  56.         // injector is created with Guice.createInjector, but could use instead
  57.         // RestletGuice.createInjector.
  58.         //FinderFactory           ff,
  59.  
  60.         // Other dependencies go here, e.g., applications and other restlets.
  61.         //
  62.         // MyApplication app1,
  63.         // MyOtherApplication app2,
  64.  
  65.         @Named(HTTP_PORT)       int httpPort
  66.     ) {
  67.         getLogger().info(String.format(
  68.             "Component running in %s mode in %s stage.",
  69.             deploymentMode, stage));
  70.  
  71.         switch (deploymentMode) {
  72.  
  73.             case SERVLET:
  74.                 // In servlet mode the HTTP listener is set up automatically.
  75.                 // No need to add to the server list.
  76.                 break;
  77.  
  78.             case STANDALONE:
  79.             default:
  80.                 // In standalone mode we have set to it up explictly.
  81.                 getServers().add(Protocol.HTTP, httpPort);
  82.                 break;
  83.         }
  84.  
  85.         ClientList clients = getClients();
  86.         clients.add(Protocol.RIAP);
  87.         clients.add(Protocol.CLAP);
  88.         clients.add(Protocol.HTTP);
  89.         //clients.add(Protocol.HTTPS);
  90.  
  91.         VirtualHost host = getDefaultHost();
  92.  
  93.         //host.attach("/path/to/app1", app1);
  94.         //host.attach("/path/to/app2", app2);
  95.  
  96.         // Only accessible from within this component.
  97.         //getInternalRouter().attach("/internal/path", internalApp);
  98.     }
  99. }
RAW Paste Data
Top