Skip to content

Commit 09ede33

Browse files
committed
Added a class (JettyServer) to run the server as a standalone Java process. This makes it easy to develop & debug without having to bother wit a servlet container.
1 parent 27d48fd commit 09ede33

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

pom.xml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
<artifactId>LDF-Server</artifactId>
66
<version>0.0.1-SNAPSHOT</version>
77
<packaging>war</packaging>
8+
9+
<properties>
10+
<jettyVersion>9.2.5.v20141112</jettyVersion>
11+
</properties>
12+
813
<dependencies>
914
<dependency>
1015
<groupId>org.rdfhdt</groupId>
@@ -24,18 +29,33 @@
2429
<dependency>
2530
<groupId>org.apache.httpcomponents</groupId>
2631
<artifactId>httpclient</artifactId>
27-
<version>4.3.3</version>
32+
<version>4.3.5</version>
2833
</dependency>
2934
<dependency>
3035
<groupId>com.google.code.gson</groupId>
3136
<artifactId>gson</artifactId>
32-
<version>2.2.4</version>
37+
<version>2.3</version>
3338
</dependency>
3439
<dependency>
3540
<groupId>javax.servlet</groupId>
3641
<artifactId>javax.servlet-api</artifactId>
37-
<version>3.0.1</version>
38-
<scope>provided</scope>
42+
<version>3.1.0</version>
43+
<scope>compile</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.eclipse.jetty</groupId>
47+
<artifactId>jetty-server</artifactId>
48+
<version>${jettyVersion}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.eclipse.jetty</groupId>
52+
<artifactId>jetty-servlet</artifactId>
53+
<version>${jettyVersion}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>commons-cli</groupId>
57+
<artifactId>commons-cli</artifactId>
58+
<version>1.2</version>
3959
</dependency>
4060
</dependencies>
4161
<build>

src/org/linkeddatafragments/servlet/BasicLdfServlet.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ public class BasicLdfServlet extends HttpServlet {
4242
private final static long TRIPLESPERPAGE = 100;
4343

4444
private ConfigReader config;
45-
private HashMap<String, DataSource> dataSources = new HashMap<String, DataSource>();
45+
private HashMap<String, DataSource> dataSources = new HashMap<>();
4646

4747
@Override
4848
public void init(ServletConfig servletConfig) throws ServletException {
4949
try {
5050
// find the configuration file
51-
final File applicationPath = new File(servletConfig.getServletContext().getRealPath("/"));
51+
String applicationPathStr = servletConfig.getServletContext().getRealPath("/");
52+
if (applicationPathStr == null) { // this can happen when running standalone
53+
applicationPathStr = System.getProperty("user.dir");
54+
}
55+
final File applicationPath = new File(applicationPathStr);
5256
final File serverHome = applicationPath.getParentFile().getParentFile();
5357
final File configFile = new File(serverHome, "conf/ldf-server.json");
5458
if (!configFile.exists())
@@ -68,7 +72,9 @@ public void init(ServletConfig servletConfig) throws ServletException {
6872
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
6973
try {
7074
// find the data source
71-
final String path = request.getRequestURI().substring(request.getContextPath().length());
75+
final String contextPath = request.getContextPath();
76+
final String requestURI = request.getRequestURI();
77+
final String path = contextPath == null ? requestURI : requestURI.substring(contextPath.length());
7278
final String query = request.getQueryString();
7379
final String dataSourceName = path.substring(1);
7480
final DataSource dataSource = dataSources.get(dataSourceName);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)