Skip to content

Commit

Permalink
Added a class (JettyServer) to run the server as a standalone Java pr…
Browse files Browse the repository at this point in the history
…ocess. This makes it easy to develop & debug without having to bother wit a servlet container.
  • Loading branch information
ghsnd committed Nov 20, 2014
1 parent 27d48fd commit 09ede33
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
28 changes: 24 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<artifactId>LDF-Server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
<jettyVersion>9.2.5.v20141112</jettyVersion>
</properties>

<dependencies>
<dependency>
<groupId>org.rdfhdt</groupId>
Expand All @@ -24,18 +29,33 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<version>2.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
Expand Down
12 changes: 9 additions & 3 deletions src/org/linkeddatafragments/servlet/BasicLdfServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ public class BasicLdfServlet extends HttpServlet {
private final static long TRIPLESPERPAGE = 100;

private ConfigReader config;
private HashMap<String, DataSource> dataSources = new HashMap<String, DataSource>();
private HashMap<String, DataSource> dataSources = new HashMap<>();

@Override
public void init(ServletConfig servletConfig) throws ServletException {
try {
// find the configuration file
final File applicationPath = new File(servletConfig.getServletContext().getRealPath("/"));
String applicationPathStr = servletConfig.getServletContext().getRealPath("/");
if (applicationPathStr == null) { // this can happen when running standalone
applicationPathStr = System.getProperty("user.dir");
}
final File applicationPath = new File(applicationPathStr);
final File serverHome = applicationPath.getParentFile().getParentFile();
final File configFile = new File(serverHome, "conf/ldf-server.json");
if (!configFile.exists())
Expand All @@ -68,7 +72,9 @@ public void init(ServletConfig servletConfig) throws ServletException {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
try {
// find the data source
final String path = request.getRequestURI().substring(request.getContextPath().length());
final String contextPath = request.getContextPath();
final String requestURI = request.getRequestURI();
final String path = contextPath == null ? requestURI : requestURI.substring(contextPath.length());
final String query = request.getQueryString();
final String dataSourceName = path.substring(1);
final DataSource dataSource = dataSources.get(dataSourceName);
Expand Down
64 changes: 64 additions & 0 deletions src/org/linkeddatafragments/standalone/JettyServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.linkeddatafragments.standalone;

import org.apache.commons.cli.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.linkeddatafragments.servlet.BasicLdfServlet;

/**
* <p> Use this class to run as a standalone service. Since it runs the BasicLdfServlet, it is important to have a
* configuration file at [working dir]/../../conf/ldf-server.json. (That's the way it is right now)</p>
* <p>This class runs an embedded Jetty servlet container. This way there is no need for a separate servlet container
* such as Tomcat.</p>
*
* <p>
* Copyright 2014 MMLab, UGent
* </p?
*
* @author Gerald Haesendonck
*/
public class JettyServer {

public static void main(String[] args) throws Exception {
Options options = new Options();
options.addOption("h", "help", false, "Print this help message and then exit.");
options.addOption("p", "port", true, "The port the server listents to. The default is 8080.");
boolean printHelp = false;
CommandLineParser parser = new BasicParser();
try {
CommandLine commandLine = parser.parse(options, args);
if (commandLine.hasOption('h')) {
printHelp = true;
return;
}
int port;
if (commandLine.hasOption('p')) {
port = Integer.parseInt(commandLine.getOptionValue('p'));
} else {
port = 8080;
}

// create a new (Jetty) server, and add a servlet handler
Server server = new Server(port);
ServletHandler handler = new ServletHandler();
server.setHandler(handler);

// add the BasicLdfServlet to the handler
handler.addServletWithMapping(BasicLdfServlet.class, "/*");

// start the server
server.start();
System.out.println("Started server, listening at port " + port);

// The use of server.join() the will make the current thread join and wait until the server is done executing.
// See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
server.join();

} finally {
if (printHelp) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(JettyServer.class.getName() + " [<options>]", "Starts a standalone LDF Trpile Pattern server. Options:", options, "");
}
}
}
}

0 comments on commit 09ede33

Please # to comment.