001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.daemon; 019 020/** 021 * Provides support for native daemon invocation. Using 022 * a platform dependant helper program, classes that implement the 023 * {@code Daemon} interface can be initialized, started and 024 * stopped according to the conventions of the underlying operating 025 * system. 026 * <p> 027 * Implementors of this interface must also provide a public constructor 028 * with no arguments so that instances can be created in an automated 029 * fashion. 030 * </p> 031 */ 032public interface Daemon 033{ 034 035 /** 036 * Initializes this {@code Daemon} instance. 037 * <p> 038 * This method gets called once the JVM process is created and the 039 * {@code Daemon} instance is created thru its empty public 040 * constructor. 041 * </p> 042 * <p> 043 * Under certain operating systems (typically UNIX based operating 044 * systems) and if the native invocation framework is configured to do 045 * so, this method might be called with <i>super-user</i> privileges. 046 * </p> 047 * <p> 048 * For example, it might be wise to create {@code ServerSocket} 049 * instances within the scope of this method, and perform all operations 050 * requiring <i>super-user</i> privileges in the underlying operating 051 * system. 052 * </p> 053 * <p> 054 * Apart from set up and allocation of native resources, this method 055 * must not start the actual operation of the {@code Daemon} (such 056 * as starting threads calling the {@code ServerSocket.accept()} 057 * method) as this would impose some serious security hazards. The 058 * start of operation must be performed in the {@code start()} 059 * method. 060 * </p> 061 * 062 * @param context A {@code DaemonContext} object used to 063 * communicate with the container. 064 * @throws DaemonInitException An exception that prevented 065 * initialization where you want to display a nice message to the user, 066 * rather than a stack trace. 067 * @throws Exception Any exception preventing a successful 068 * initialization. 069 */ 070 void init(DaemonContext context) 071 throws DaemonInitException, Exception; 072 073 /** 074 * Starts the operation of this {@code Daemon} instance. This 075 * method is to be invoked by the environment after the init() 076 * method has been successfully invoked and possibly the security 077 * level of the JVM has been dropped. Implementors of this 078 * method are free to start any number of threads, but need to 079 * return control after having done that to enable invocation of 080 * the stop()-method. 081 * 082 * @throws Exception If the start was not successful 083 */ 084 void start() 085 throws Exception; 086 087 /** 088 * Stops the operation of this {@code Daemon} instance. Note 089 * that the proper place to free any allocated resources such as 090 * sockets or file descriptors is in the destroy method, as the 091 * container may restart the Daemon by calling start() after 092 * stop(). 093 * 094 * @throws Exception If the stop was not successful 095 */ 096 void stop() 097 throws Exception; 098 099 /** 100 * Frees any resources allocated by this daemon such as file 101 * descriptors or sockets. This method gets called by the container 102 * after stop() has been called, before the JVM exits. The Daemon 103 * can not be restarted after this method has been called without a 104 * new call to the init() method. 105 */ 106 void destroy(); 107} 108