<commit_before>/*
 * Copyright 2015, 2015 IBM
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.ibm.util.merge.web;

import com.ibm.idmu.api.JsonProxy;
import com.ibm.idmu.api.PoolManagerConfiguration;
import com.ibm.util.merge.TemplateFactory;
import com.ibm.util.merge.db.ConnectionPoolManager;
import com.ibm.util.merge.json.DefaultJsonProxy;
import com.ibm.util.merge.json.PrettyJsonProxy;
import com.ibm.util.merge.persistence.*;
import com.ibm.util.merge.web.rest.servlet.RequestHandler;
import com.ibm.util.merge.web.rest.servlet.handler.*;
import com.ibm.util.merge.web.rest.servlet.writer.TextResponseWriter;

import org.apache.log4j.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.PrintWriter;

public class InitializeServlet extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = -6542461667547308985L;
	private Logger log = Logger.getLogger(InitializeServlet.class);
    private final List<RequestHandler> handlerChain = new ArrayList<>();

    public InitializeServlet() {
    }

    /**
     */
    @Override
    public void init(ServletConfig cfg) {
        initializeApp(cfg.getServletContext());
    }

    private void initializeApp(ServletContext servletContext) {
        handlerChain.addAll(createHandlerInstances());
        ConnectionPoolManager poolManager = new ConnectionPoolManager();
        if(poolsPropertiesPath.exists()){
            PoolManagerConfiguration config = PoolManagerConfiguration.fromPropertiesFile(poolsPropertiesPath);
            poolManager.applyConfig(config);
        }else{
            log.error("Could not load databasePools properties file from non-existant path: " + poolsPropertiesPath);
            log.error("No database config will be applied");
        }

        final TemplatePersistence persist;
            JdbcTemplatePersistence jdbcPersistence = new JdbcTemplatePersistence(poolManager);
            persist = jdbcPersistence;
        }else{
            FilesystemPersistence filesystemPersistence = new FilesystemPersistence(templatesDirPath, jsonProxy);
            persist = filesystemPersistence;
        }
        TemplateFactory tf = new TemplateFactory(persist, jsonProxy, outputDirPath, poolManager);
        servletContext.setAttribute("TemplateFactory", tf);
        for (RequestHandler handler : handlerChain) {
            log.info("Initializing handler " + handler.getClass().getName());
        }
        servletContext.setAttribute("handlerChain", handlerChain);
    }

    private ArrayList<RequestHandler> createHandlerInstances() {
    	ArrayList<RequestHandler> thelist = new ArrayList<RequestHandler>();
		thelist.add(new GetCollectionsResourceHandler());
		thelist.add(new GetDirectivesResourceHandler());
		thelist.add(new GetTemplateNamesForCollectionResourceHandler());
		thelist.add(new GetTemplateNamesForNameResourceHandler());
		thelist.add(new GetTemplatePackageResourceHandler());
		thelist.add(new GetTemplateResourceHandler());
		thelist.add(new PerformMergeResourceHandler());
		thelist.add(new RemoveArchiveResourceHandler());
		thelist.add(new GetStatusResourceHandler());
		if (!this.secure) {
			thelist.add(new PutTemplateResourceHandler());
			thelist.add(new PutTemplatePackageResourceHandler());
	    	thelist.add(new DelTemplateResourceHandler());
			thelist.add(new DelTemplatePackageResourceHandler());
		}
        return thelist;
    }

    private void applyParameters() {
        applyInitParameters();
        applySystemParameters();
    }

    private void applySystemParameters() {
        String systemMergeTemplatesFolder = System.getProperty("merge-templates-folder");
        if(systemMergeTemplatesFolder != null){
            log.warn("Found so using passed system property value for merge-templates-folder: " + systemMergeTemplatesFolder);
            this.warTemplatesPath = systemMergeTemplatesFolder;
        }
        String systemOutputRootDir = System.getProperty("merge-output-root");
        if(systemOutputRootDir != null){
            log.warn("Found so using passed system property value for merge-output-root: " + systemOutputRootDir);
            this.outputDirPath = new File(systemOutputRootDir);
        }
        String systemPoolsPropertiesPath = System.getProperty("jdbc-pools-properties-path");
        if(systemPoolsPropertiesPath != null){
            log.warn("Found so using passed system property value for jdbc-pools-properties-path: " + systemPoolsPropertiesPath);
            this.jdbcPoolsPropertiesPath = systemPoolsPropertiesPath;
        }
        String systemLog4jPropertiesPath = System.getProperty("log4j-init-file");
        if(systemLog4jPropertiesPath != null){
            log.warn("Found so using passed system property value for log4j-init-file: " + systemLog4jPropertiesPath);
            this.log4jPropertiesPath = systemLog4jPropertiesPath;
        }
        String systemTemplatesPersistencePoolName = System.getProperty("jdbc-persistence-templates-poolname");
        if(systemTemplatesPersistencePoolName != null){
            log.warn("Found so using passed system property value for jdbc-persistence-templates-poolname: " + systemTemplatesPersistencePoolName);
            this.templatesPersistencePoolName = systemTemplatesPersistencePoolName;
        }
        String systemPrettyJson = System.getProperty("pretty-json");
        if(systemPrettyJson != null){
            log.warn("Found so using passed system property value for pretty-json: " + systemPrettyJson);
            this.prettyJson = systemPrettyJson.equals("yes"); 
        }
        String systemDbPersist = System.getProperty("db-persist");
        if(systemDbPersist != null){
            log.warn("Found so using passed system property value for db-persist: " + systemDbPersist);
            this.dbPersist = systemDbPersist.equals("yes"); 
        }
        String systemSecure = System.getProperty("secure-server");
        if(systemSecure != null){
            log.warn("Found so using passed system property value for secure-server: " + systemSecure);
            this.secure = systemSecure.equals("yes"); 
        }
    }

    private void applyInitParameters() {
        String mergeTemplatesFolder = servletInitParameters.get("merge-templates-folder");
        if (mergeTemplatesFolder != null) {
            log.warn("Setting from ServletConfig: warTemplatesPath=" + mergeTemplatesFolder);
            warTemplatesPath = mergeTemplatesFolder;
        }
        String outputRootDir = servletInitParameters.get("merge-output-root");
        if (outputRootDir != null) {
            log.warn("Setting from ServletConfig: outputDirPath=" + outputRootDir);
            outputDirPath = new File(outputRootDir);
        }
        String databasePoolsPropertiesPath = servletInitParameters.get("jdbc-pools-properties-path");
        if(databasePoolsPropertiesPath != null){
            log.warn("Setting from ServletConfig: databasePoolsPropertiesPath=" + databasePoolsPropertiesPath);
            this.jdbcPoolsPropertiesPath = databasePoolsPropertiesPath;
        }
        String log4jPropertiesPath = servletInitParameters.get("log4j-init-file");
        if(log4jPropertiesPath != null){
            log.warn("Setting from ServletConfig: log4jPropertiesPath=" + log4jPropertiesPath);
            this.log4jPropertiesPath = log4jPropertiesPath;
        }
        String templatesPersistencePoolName = servletInitParameters.get("jdbc-persistence-templates-poolname");
        if(templatesPersistencePoolName != null){
            log.warn("Setting from ServletConfig templatesPersistencePoolName=" + templatesPersistencePoolName);
            this.templatesPersistencePoolName = templatesPersistencePoolName;
        }
        String prettyJsonParameter = servletInitParameters.get("pretty-json");
        if (prettyJsonParameter != null) {
            log.warn("Setting from ServletConfig: prettyJson=" + prettyJsonParameter);
            this.prettyJson = prettyJsonParameter.equals("yes");
        }
        String dbPersistParameter = servletInitParameters.get("db-persist");
        if (dbPersistParameter != null) {
            log.warn("Setting from ServletConfig: dbPersist=" + dbPersistParameter);
            this.dbPersist = dbPersistParameter.equals("yes");
        }
        String secureServer = servletInitParameters.get("secure-server");
        if (secureServer != null) {
            log.warn("Setting from ServletConfig: secure-server=" + secureServer);
            this.secure = secureServer.equals("yes");
        }
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        res.getWriter().write("Initialized");
    }

    /**
     * Reinitializes IDMU upon POST to this servlet
     */
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        initializeApp(req.getServletContext());<commit_msg>Externalized configurations to properties file, added setup<commit_after>/*
 * Copyright 2015, 2015 IBM
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.ibm.util.merge.web;

import com.ibm.util.merge.TemplateFactory;
import com.ibm.util.merge.web.rest.servlet.RequestHandler;
import com.ibm.util.merge.web.rest.servlet.handler.*;
import com.ibm.util.merge.web.rest.servlet.writer.TextResponseWriter;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class InitializeServlet extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = -6542461667547308985L;
	private Logger log = Logger.getLogger(InitializeServlet.class);
	private static final String PARAMETER_LOGGING_PROPS 	= "log4j-init-fil";
	private static final String PARAMETER_SECURE_SERVER 	= "secure-server";
	private File idmuPropertiesFile;
	private Properties runtimeProperties = new Properties();
    private final List<RequestHandler> handlerChain = new ArrayList<>();

    public InitializeServlet() {
    	// provide default runtime parameters
    	runtimeProperties.setProperty(PARAMETER_LOGGING_PROPS, "/opt/ibm/idmu/properties/log4j.properties");
    	runtimeProperties.setProperty(PARAMETER_SECURE_SERVER, "no");
    	runtimeProperties.setProperty(TemplateFactory.PARAMETER_TEMPLATE_DIR, 		"/opt/ibm/idmu/templates");
    	runtimeProperties.setProperty(TemplateFactory.PARAMETER_OUTPUT_DIR, 		"/opt/ibm/idmu/output");
    	runtimeProperties.setProperty(TemplateFactory.PARAMETER_POOLS_PROPERTIES, 	"/opt/ibm/idmu/properties");
    	runtimeProperties.setProperty(TemplateFactory.PARAMETER_TEMPLATE_POOL, 		"idmuTemplates");
    	runtimeProperties.setProperty(TemplateFactory.PARAMETER_DB_PERSIST, 		"no");
    	runtimeProperties.setProperty(TemplateFactory.PARAMETER_PRETTY_JSON, 		"no");
    }

    /**
     * Initialize Servlet
     */
    @Override
    public void init(ServletConfig cfg) {
        initializeApp(cfg.getServletContext());
    }

    private void initializeApp(ServletContext servletContext) {
    	getRuntimeProperties();
	    PropertyConfigurator.configure(this.runtimeProperties.getProperty(PARAMETER_LOGGING_PROPS));
    	handlerChain.clear();
        handlerChain.addAll(createHandlerInstances());
        TemplateFactory tf = new TemplateFactory(this.runtimeProperties);
        servletContext.setAttribute("TemplateFactory", tf);
        for (RequestHandler handler : handlerChain) {
            log.info("Initializing handler " + handler.getClass().getName());
            handler.initialize(this.runtimeProperties, tf);
        }
        servletContext.setAttribute("handlerChain", handlerChain);
    }

    private void getRuntimeProperties() {
        String folder = getServletContext().getInitParameter("IDMU_ROOT");
        if (folder.isEmpty()) {folder = System.getProperty("IDMU_ROOT");}
    	if (folder.isEmpty()) {folder = "/opt/ibm/idmu";}
    	this.idmuPropertiesFile = new File(folder + File.pathSeparator + "properties" + File.pathSeparator + "idmu.properties");
    	if (! this.idmuPropertiesFile.exists() ) {
    		setup(folder);
    	}
    	try {
			this.runtimeProperties.load(new FileInputStream(this.idmuPropertiesFile));
		} catch (IOException e) {
			throw new RuntimeException("Failed to load properties file " + this.idmuPropertiesFile);
		}
    }
    
    private void setup(String folder) {
    	ArrayList<String> folders = new ArrayList<String>();
    	folders.add("templates");
    	folders.add("packages");
    	folders.add("output");
    	folders.add("properties");
    	folders.add("logs");
    	folders.add("database");
    	for (String aFolder : folders) {
    		File theSource = new File("WEB-INF" + File.pathSeparator + aFolder);
    		File theTarget = new File(folder + File.pathSeparator + aFolder);
    		if (!theTarget.exists()) {theTarget.mkdirs();}
    		try {
				FileUtils.copyDirectory(theSource, theTarget);
			} catch (IOException e) {
				throw new RuntimeException("SETUP ERROR! - Unable to copy files from " + theSource + " to " + theTarget);
			}
    	}
    }
    
    private ArrayList<RequestHandler> createHandlerInstances() {
    	ArrayList<RequestHandler> thelist = new ArrayList<RequestHandler>();
		thelist.add(new GetCollectionsResourceHandler());
		thelist.add(new GetDirectivesResourceHandler());
		thelist.add(new GetTemplateNamesForCollectionResourceHandler());
		thelist.add(new GetTemplateNamesForNameResourceHandler());
		thelist.add(new GetTemplatePackageResourceHandler());
		thelist.add(new GetTemplateResourceHandler());
		thelist.add(new PerformMergeResourceHandler());
		thelist.add(new RemoveArchiveResourceHandler());
		thelist.add(new GetStatusResourceHandler());
		if (this.runtimeProperties.get(PARAMETER_SECURE_SERVER).equals("yes")) {
			thelist.add(new PutTemplateResourceHandler());
			thelist.add(new PutTemplatePackageResourceHandler());
	    	thelist.add(new DelTemplateResourceHandler());
			thelist.add(new DelTemplatePackageResourceHandler());
		}
        return thelist;
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        res.getWriter().write("Initialized");
    }

    /**
     * Reinitializes IDMU upon POST to this servlet
     */
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        initializeApp(req.getServletContext());
{'fraction_non_alphanumeric': 0.09968808484092327, 'fraction_numerical': 0.004990642545227698, 'mean_word_length': 3.8184550646227833, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1219445', 'n_tokens_mistral': 4322, 'n_tokens_neox': 4214, 'n_words': 911}