<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-09 <NAME>
 *  <EMAIL>
 *  http://exist.sourceforge.net
 *  
 *  This program 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 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id:
 */
package org.exist.xquery.functions.system;


import java.util.Timer;
import java.util.TimerTask;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.dom.QName;
import org.exist.storage.BrokerPool;
import org.exist.xquery.BasicFunction;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.FunctionParameterSequenceType;
import org.exist.xquery.value.NumericValue;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;

/**
 * Shutdown the eXist server (must be dba)
 * 
 * @author <NAME> (<EMAIL>)
 */
public class Shutdown extends BasicFunction
{
	protected final static Logger logger = LogManager.getLogger(Shutdown.class);

	public final static FunctionSignature signatures[] = {
		new FunctionSignature(
			new QName("shutdown", SystemModule.NAMESPACE_URI, SystemModule.PREFIX),
			"Shutdown eXist immediately.  This method is only available to the DBA role.",
			null,
			new SequenceType(Type.ITEM, Cardinality.EMPTY)
		),
		
		new FunctionSignature(
			new QName("shutdown", SystemModule.NAMESPACE_URI, SystemModule.PREFIX),
			"Shutdown eXist.  This method is only available to the DBA role.",
			new SequenceType[] {
					new FunctionParameterSequenceType("delay", Type.LONG, Cardinality.EXACTLY_ONE, "The delay in milliseconds before eXist starts to shutdown.")
			},
			new SequenceType(Type.ITEM, Cardinality.EMPTY)
		)
	};
		

	public Shutdown(XQueryContext context, FunctionSignature signature)
	{
		super(context, signature);
	}

	/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
	public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException
	{
		if(context.getSubject().hasDbaRole())
		{
			//determine the shutdown delay
			long delay = 0;
			if(args.length == 1)
			{
				if(!args[0].isEmpty())
				{
					delay = ((NumericValue)args[0].itemAt(0)).getLong();
				}
			}
			
			//get the broker pool and shutdown
			final BrokerPool pool = context.getBroker().getBrokerPool();
				
			if(delay > 0)
			{
				logger.info("Shuttting down in " + delay + " milliseconds.");
				final TimerTask task = new DelayedShutdownTask(pool);
				final Timer timer = new Timer();
				timer.schedule(task, delay);
			}
			else
			{
				logger.info("Shutting down now.");
				pool.shutdown();
			}
		}
		else
		{
			final XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to shutdown the database");
			logger.error("Invalid user", xPathException);
			throw xPathException;
		}
			
		return Sequence.EMPTY_SEQUENCE;
	}
	
	private static class DelayedShutdownTask extends TimerTask
	{
		private BrokerPool pool = null;
		
		public DelayedShutdownTask(BrokerPool pool)
		{
			super();
			this.pool = pool;
		}
		
		public void run()
		{
			logger.info("Shutting down now.");
			pool.shutdown();
		}
	}
}
<commit_msg>shutdown timer must be daemon<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-09 <NAME>
 *  <EMAIL>
 *  http://exist.sourceforge.net
 *  
 *  This program 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 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id:
 */
package org.exist.xquery.functions.system;


import java.util.Timer;
import java.util.TimerTask;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.dom.QName;
import org.exist.storage.BrokerPool;
import org.exist.xquery.BasicFunction;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.FunctionParameterSequenceType;
import org.exist.xquery.value.NumericValue;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;

/**
 * Shutdown the eXist server (must be dba)
 * 
 * @author <NAME> (<EMAIL>)
 */
public class Shutdown extends BasicFunction
{
	protected final static Logger logger = LogManager.getLogger(Shutdown.class);

	public final static FunctionSignature signatures[] = {
		new FunctionSignature(
			new QName("shutdown", SystemModule.NAMESPACE_URI, SystemModule.PREFIX),
			"Shutdown eXist immediately.  This method is only available to the DBA role.",
			null,
			new SequenceType(Type.ITEM, Cardinality.EMPTY)
		),
		
		new FunctionSignature(
			new QName("shutdown", SystemModule.NAMESPACE_URI, SystemModule.PREFIX),
			"Shutdown eXist.  This method is only available to the DBA role.",
			new SequenceType[] {
					new FunctionParameterSequenceType("delay", Type.LONG, Cardinality.EXACTLY_ONE, "The delay in milliseconds before eXist starts to shutdown.")
			},
			new SequenceType(Type.ITEM, Cardinality.EMPTY)
		)
	};
		

	public Shutdown(XQueryContext context, FunctionSignature signature)
	{
		super(context, signature);
	}

	/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
	public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException
	{
		if(context.getSubject().hasDbaRole())
		{
			//determine the shutdown delay
			long delay = 0;
			if(args.length == 1)
			{
				if(!args[0].isEmpty())
				{
					delay = ((NumericValue)args[0].itemAt(0)).getLong();
				}
			}
			
			//get the broker pool and shutdown
			final BrokerPool pool = context.getBroker().getBrokerPool();
				
			if(delay > 0)
			{
				logger.info("Shutdown in " + delay + " milliseconds.");
				final TimerTask task = new DelayedShutdownTask(pool);
				final Timer timer = new Timer("eXist-db shutdown schedule", true);
				timer.schedule(task, delay);
			}
			else
			{
				logger.info("Shutting down now.");
				pool.shutdown();
			}
		}
		else
		{
			final XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to shutdown the database");
			logger.error("Invalid user", xPathException);
			throw xPathException;
		}
			
		return Sequence.EMPTY_SEQUENCE;
	}
	
	private static class DelayedShutdownTask extends TimerTask
	{
		private BrokerPool pool = null;
		
		public DelayedShutdownTask(BrokerPool pool)
		{
			super();
			this.pool = pool;
		}
		
		public void run()
		{
			logger.info("Shutting down now.");
			pool.shutdown();
		}
	}
}

{'fraction_non_alphanumeric': 0.1049412915851272, 'fraction_numerical': 0.005626223091976516, 'mean_word_length': 3.8270365997638724, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4887013', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2456, 'n_words': 726}