|
| 1 | +package org.linkeddatafragments.standalone; |
| 2 | + |
| 3 | +import org.apache.commons.cli.*; |
| 4 | +import org.eclipse.jetty.server.Server; |
| 5 | +import org.eclipse.jetty.servlet.ServletHandler; |
| 6 | +import org.linkeddatafragments.servlet.BasicLdfServlet; |
| 7 | + |
| 8 | +/** |
| 9 | + * <p> Use this class to run as a standalone service. Since it runs the BasicLdfServlet, it is important to have a |
| 10 | + * configuration file at [working dir]/../../conf/ldf-server.json. (That's the way it is right now)</p> |
| 11 | + * <p>This class runs an embedded Jetty servlet container. This way there is no need for a separate servlet container |
| 12 | + * such as Tomcat.</p> |
| 13 | + * |
| 14 | + * <p> |
| 15 | + * Copyright 2014 MMLab, UGent |
| 16 | + * </p? |
| 17 | + * |
| 18 | + * @author Gerald Haesendonck |
| 19 | + */ |
| 20 | +public class JettyServer { |
| 21 | + |
| 22 | + public static void main(String[] args) throws Exception { |
| 23 | + Options options = new Options(); |
| 24 | + options.addOption("h", "help", false, "Print this help message and then exit."); |
| 25 | + options.addOption("p", "port", true, "The port the server listents to. The default is 8080."); |
| 26 | + boolean printHelp = false; |
| 27 | + CommandLineParser parser = new BasicParser(); |
| 28 | + try { |
| 29 | + CommandLine commandLine = parser.parse(options, args); |
| 30 | + if (commandLine.hasOption('h')) { |
| 31 | + printHelp = true; |
| 32 | + return; |
| 33 | + } |
| 34 | + int port; |
| 35 | + if (commandLine.hasOption('p')) { |
| 36 | + port = Integer.parseInt(commandLine.getOptionValue('p')); |
| 37 | + } else { |
| 38 | + port = 8080; |
| 39 | + } |
| 40 | + |
| 41 | + // create a new (Jetty) server, and add a servlet handler |
| 42 | + Server server = new Server(port); |
| 43 | + ServletHandler handler = new ServletHandler(); |
| 44 | + server.setHandler(handler); |
| 45 | + |
| 46 | + // add the BasicLdfServlet to the handler |
| 47 | + handler.addServletWithMapping(BasicLdfServlet.class, "/*"); |
| 48 | + |
| 49 | + // start the server |
| 50 | + server.start(); |
| 51 | + System.out.println("Started server, listening at port " + port); |
| 52 | + |
| 53 | + // The use of server.join() the will make the current thread join and wait until the server is done executing. |
| 54 | + // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join() |
| 55 | + server.join(); |
| 56 | + |
| 57 | + } finally { |
| 58 | + if (printHelp) { |
| 59 | + HelpFormatter formatter = new HelpFormatter(); |
| 60 | + formatter.printHelp(JettyServer.class.getName() + " [<options>]", "Starts a standalone LDF Trpile Pattern server. Options:", options, ""); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments