Skip to content

Commit

Permalink
Move configuration file.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Feb 17, 2014
1 parent a6a782e commit 6a495a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.settings
build
WebContent/WEB-INF/config.json
ldf-server.json
File renamed without changes.
18 changes: 4 additions & 14 deletions src/org/linkeddatafragments/config/ConfigReader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.linkeddatafragments.config;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -20,19 +19,10 @@ public class ConfigReader {

/**
* Creates a new configuration reader.
* @param configFile the name of the configuration file
* @throws Exception if the configuration cannot be loaded
* @param configReader the configuration
*/
public ConfigReader(String configFile) throws Exception {
// check the configuration file
if (configFile == null || configFile.length() == 0)
throw new Exception("No configuration file name specified.");
final File config = new File(configFile);
if (!config.exists())
throw new Exception("Configuration file " + configFile + " does not exist.");

// read the configuration file
final JsonObject root = new JsonParser().parse(new FileReader(config)).getAsJsonObject();
public ConfigReader(Reader configReader) {
final JsonObject root = new JsonParser().parse(configReader).getAsJsonObject();
for (final Entry<String, JsonElement> entry : root.getAsJsonObject("datasources").entrySet()) {
final JsonObject dataSource = entry.getValue().getAsJsonObject();
this.dataSources.put(entry.getKey(), dataSource.getAsJsonPrimitive("path").getAsString());
Expand Down
14 changes: 12 additions & 2 deletions src/org/linkeddatafragments/servlet/BasicLdfServlet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.linkeddatafragments.servlet;

import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -41,7 +43,15 @@ public class BasicLdfServlet extends HttpServlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
try {
config = new ConfigReader(servletConfig.getInitParameter("configFile"));
// find the configuration file
final File applicationPath = new File(servletConfig.getServletContext().getRealPath("/"));
final File serverHome = applicationPath.getParentFile().getParentFile();
final File configFile = new File(serverHome, "conf/ldf-server.json");
if (!configFile.exists())
throw new Exception("Configuration file " + configFile + " not found.");

// load the configuration
config = new ConfigReader(new FileReader(configFile));
for (Entry<String, String> dataSource : config.getDataSources().entrySet())
dataSources.put(dataSource.getKey(), new HdtDataSource(dataSource.getValue()));
}
Expand All @@ -59,7 +69,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
final String dataSourceName = path.substring(1);
final DataSource dataSource = dataSources.get(dataSourceName);
if (dataSource == null)
throw new Exception("data source not found");
throw new Exception("Data source not found.");

// query the fragment
final Resource subject = parseAsResource(request.getParameter("subject"));
Expand Down

0 comments on commit 6a495a1

Please sign in to comment.