From 25761f48c666ca64b432b91a7d2c8a3388fcd247 Mon Sep 17 00:00:00 2001
From: gmantele <gmantele@ari.uni-heidelberg.de>
Date: Mon, 16 Feb 2015 11:30:26 +0100
Subject: [PATCH] [TAP] Add a TAP servlet which loads a TAP configuration file
 and start a TAP service automatically.

---
 src/tap/config/ConfigurableTAPServlet.java | 154 +++++++++++++++++++++
 src/tap/config/TAPConfiguration.java       |   3 +
 2 files changed, 157 insertions(+)
 create mode 100644 src/tap/config/ConfigurableTAPServlet.java

diff --git a/src/tap/config/ConfigurableTAPServlet.java b/src/tap/config/ConfigurableTAPServlet.java
new file mode 100644
index 0000000..ddb03fc
--- /dev/null
+++ b/src/tap/config/ConfigurableTAPServlet.java
@@ -0,0 +1,154 @@
+package tap.config;
+
+/*
+ * This file is part of TAPLibrary.
+ * 
+ * TAPLibrary is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * TAPLibrary is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with TAPLibrary.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ * Copyright 2014-2015 - Astronomisches Rechen Institut (ARI)
+ */
+
+import static tap.config.TAPConfiguration.DEFAULT_TAP_CONF_FILE;
+import static tap.config.TAPConfiguration.TAP_CONF_PARAMETER;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import tap.ServiceConnection;
+import tap.resource.TAP;
+
+public class ConfigurableTAPServlet extends HttpServlet {
+	private static final long serialVersionUID = 1L;
+
+	private TAP tap = null;
+
+	@Override
+	public void init(final ServletConfig config) throws ServletException{
+		// Nothing to do, if TAP is already initialized:
+		if (tap != null)
+			return;
+
+		/* 1. GET THE FILE PATH OF THE TAP CONFIGURATION FILE */
+		String tapConfPath = config.getInitParameter(TAP_CONF_PARAMETER);
+		if (tapConfPath == null || tapConfPath.trim().length() == 0)
+			tapConfPath = null;
+		//throw new ServletException("Configuration file path missing! You must set a servlet init parameter whose the name is \"" + TAP_CONF_PARAMETER + "\".");
+
+		/* 2. OPEN THE CONFIGURATION FILE */
+		InputStream input = null;
+		// CASE: No file specified => search in the classpath for a file having the default name "tap.properties".
+		if (tapConfPath == null)
+			input = searchFile(DEFAULT_TAP_CONF_FILE, config);
+		else{
+			File f = new File(tapConfPath);
+			// CASE: The given path matches to an existing local file.
+			if (f.exists()){
+				try{
+					input = new FileInputStream(f);
+				}catch(IOException ioe){
+					throw new ServletException("Impossible to read the TAP configuration file (" + tapConfPath + ")!", ioe);
+				}
+			}
+			// CASE: The given path seems to be relative to the servlet root directory.
+			else
+				input = searchFile(tapConfPath, config);
+		}
+		// If no file has been found, cancel the servlet loading:
+		if (input == null)
+			throw new ServletException("Configuration file not found with the path: \"" + tapConfPath + "\"! Please provide a correct file path for the TAP configuration file.");
+
+		/* 3. PARSE IT INTO A PROPERTIES SET */
+		Properties tapConf = new Properties();
+		try{
+			tapConf.load(input);
+		}catch(IOException ioe){
+			throw new ServletException("Impossible to read the TAP configuration file (" + tapConfPath + ")!", ioe);
+		}finally{
+			try{
+				input.close();
+			}catch(IOException ioe2){}
+		}
+
+		/* 4. CREATE THE TAP SERVICE */
+		try{
+			// Create the service connection:
+			ServiceConnection serviceConn = new ConfigurableServiceConnection(tapConf);
+			// Create all the TAP resources:
+			tap = new TAP(serviceConn);
+		}catch(Exception ex){
+			tap = null;
+			throw new ServletException("Impossible to initialize the TAP service!", ex);
+		}
+
+		// TODO Set the home page file, if any:
+
+		super.init(config);
+	}
+
+	protected final InputStream searchFile(final String filePath, final ServletConfig config){
+		InputStream input = null;
+
+		// Try to search in the classpath (with just a file name or a relative path):
+		input = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
+
+		// If not found, try searching in the WebContent directory (as this fileName is a file path relative to WebContent):
+		if (input == null)
+			input = config.getServletContext().getResourceAsStream(filePath);
+
+		// LAST CHANCE: Only if it is not a path...
+		if (input == null && filePath.indexOf(File.separatorChar) < 0){
+			// ...try at the root of WEB-INF:
+			input = config.getServletContext().getResourceAsStream("/WEB-INF/" + filePath);
+			// ...and at the root of META-INF:
+			if (input == null)
+				input = config.getServletContext().getResourceAsStream("/META-INF/" + filePath);
+		}
+
+		return input;
+	}
+
+	@Override
+	public void destroy(){
+		// Free all resources used by TAP:
+		if (tap != null){
+			tap.destroy();
+			tap = null;
+		}
+		super.destroy();
+	}
+
+	@Override
+	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
+		if (tap != null){
+			try{
+				tap.executeRequest(req, resp);
+			}catch(Throwable t){
+				System.err.println("Request aborted !");
+				t.printStackTrace(System.err);
+				resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.getMessage());
+			}
+		}else
+			resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "TAP service not yet initialized!");
+	}
+
+}
diff --git a/src/tap/config/TAPConfiguration.java b/src/tap/config/TAPConfiguration.java
index 826cc9f..74da57e 100644
--- a/src/tap/config/TAPConfiguration.java
+++ b/src/tap/config/TAPConfiguration.java
@@ -12,6 +12,9 @@ import tap.backup.DefaultTAPBackupManager;
 
 public final class TAPConfiguration {
 
+	public final static String TAP_CONF_PARAMETER = "tapconf";
+	public final static String DEFAULT_TAP_CONF_FILE = "tap.properties";
+
 	/* FILE MANAGER KEYS */
 	public final static String KEY_FILE_MANAGER = "file_manager";
 	public final static String VALUE_LOCAL = "local";
-- 
GitLab