Skip to content
Snippets Groups Projects
Commit 25761f48 authored by gmantele's avatar gmantele
Browse files

[TAP] Add a TAP servlet which loads a TAP configuration file and start a TAP service automatically.

parent 34e04bf4
No related branches found
No related tags found
No related merge requests found
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!");
}
}
......@@ -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";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment