org.apache.commons.daemon.DaemonContext Java Examples

The following examples show how to use org.apache.commons.daemon.DaemonContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExporterDaemon.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void init ( final DaemonContext ctx ) throws Exception
{
    logger.info ( "Initializing ExporterDaemon" );

    if ( ctx.getArguments ().length < 1 )
    {
        logger.warn ( "No arguments passed. No config file available" );
        throw new Exception ( "Invalid arguments: exporter <configfile>" );
    }

    final String fileName = ctx.getArguments ()[0];
    logger.info ( "Loading configuration from: {}", fileName );
    final File configFile = new File ( fileName );
    if ( !configFile.canRead () )
    {
        throw new Exception ( String.format ( "'%s' is not a file or can not be read", fileName ) );
    }

    this.controller = new Controller ( configFile );
}
 
Example #2
Source File: PrivilegedNfsGatewayStarter.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Override
public void init(DaemonContext context) throws Exception {
  System.err.println("Initializing privileged NFS client socket...");
  NfsConfiguration conf = new NfsConfiguration();
  int clientPort = conf.getInt(NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_KEY,
      NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_DEFAULT);
  if (clientPort < 1 || clientPort > 1023) {
    throw new RuntimeException("Must start privileged NFS server with '" +
        NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_KEY + "' configured to a " +
        "privileged port.");
  }
  registrationSocket = new DatagramSocket(
      new InetSocketAddress("localhost", clientPort));
  registrationSocket.setReuseAddress(true);
  args = context.getArguments();
}
 
Example #3
Source File: ServerTest.java    From sync-service with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception  {
	
	SyncServiceDaemon daemon = new SyncServiceDaemon();
	try {
		DaemonContext dc = new DaemonContext() {
			
			@Override
			public DaemonController getController() {
				return null;
			}
			
			@Override
			public String[] getArguments() {
				return new String[]{"/home/edgar/Documents/stackSync/sync-service/config.properties"};
			}
		};
		
		daemon.init(dc);
		daemon.start();
	} catch (Exception e) {
		throw e;
	}
}
 
Example #4
Source File: PrivilegedNfsGatewayStarter.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override
public void init(DaemonContext context) throws Exception {
  System.err.println("Initializing privileged NFS client socket...");
  NfsConfiguration conf = new NfsConfiguration();
  int clientPort = conf.getInt(NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_KEY,
      NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_DEFAULT);
  if (clientPort < 1 || clientPort > 1023) {
    throw new RuntimeException("Must start privileged NFS server with '" +
        NfsConfigKeys.DFS_NFS_REGISTRATION_PORT_KEY + "' configured to a " +
        "privileged port.");
  }
  registrationSocket = new DatagramSocket(
      new InetSocketAddress("localhost", clientPort));
  registrationSocket.setReuseAddress(true);
  args = context.getArguments();
}
 
Example #5
Source File: Bireme.java    From bireme with Apache License 2.0 5 votes vote down vote up
@Override
public void init(DaemonContext context) throws Exception {
  logger.info("initialize Bireme daemon");
  this.context = context;
  try {
    parseCommandLine(context.getArguments());
  } catch (Exception e) {
    logger.fatal("start failed. Message: {}.", e.getMessage());
    logger.fatal("Stack Trace: ", e);
  }
}
 
Example #6
Source File: ScoreMarkerMain.java    From ExamStack with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void init(DaemonContext arg0) throws DaemonInitException, Exception {

	context = new AnnotationConfigApplicationContext();
	context.register(ScoreMarkConfig.class);
	context.refresh();
	scoreCalcuService = context.getBean(ScoreCalcuService.class);
	consumer = context.getBean(QueueingConsumer.class);
	mapper = context.getBean(ObjectMapper.class);
	createWorkThread();
	LOGGER.info("ScoreMarker daemon init done.");
}
 
Example #7
Source File: AgentShell.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public void init(DaemonContext dc) throws DaemonInitException {
    s_logger.debug("Initializing AgentShell from JSVC");
    try {
        init(dc.getArguments());
    } catch (ConfigurationException ex) {
        throw new DaemonInitException("Initialization failed", ex);
    }
}
 
Example #8
Source File: ScoreMarkerMain.java    From ExamStack with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void init(DaemonContext arg0) throws DaemonInitException, Exception {

	context = new AnnotationConfigApplicationContext();
	context.register(ScoreMarkConfig.class);
	context.refresh();
	scoreCalcuService = context.getBean(ScoreCalcuService.class);
	consumer = context.getBean(QueueingConsumer.class);
	mapper = context.getBean(ObjectMapper.class);
	createWorkThread();
	LOGGER.info("ScoreMarker daemon init done.");
}
 
Example #9
Source File: SecureDataNodeStarter.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void init(DaemonContext context) throws Exception {
  System.err.println("Initializing secure datanode resources");
  // Create a new HdfsConfiguration object to ensure that the configuration in
  // hdfs-site.xml is picked up.
  Configuration conf = new HdfsConfiguration();
  
  // Stash command-line arguments for regular datanode
  args = context.getArguments();
  resources = getSecureResources(conf);
}
 
Example #10
Source File: DBforBix.java    From DBforBIX with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void init(DaemonContext dc) throws Exception {
	String[] args = dc.getArguments();
	if (args == null || args.length == 0)
		throw new IllegalStateException("BASEDIR missing");
	Config.getInstance().setBasedir(args[0]);
}
 
Example #11
Source File: TsoServerDaemon.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
@Override
public void init(DaemonContext daemonContext) throws Exception {
    try {
        TSOServerConfig config = new TSOServerConfig();
        LOG.info("Creating TSOServer instance as a Daemon process...");
        tsoServer = TSOServer.getInitializedTsoServer(config);
        LOG.info("TSOServer instance for Daemon process created");
    } catch (Exception e) {
        LOG.error("Error creating TSOServer instance", e);
        throw e;
    }
}
 
Example #12
Source File: ServerDaemon.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public void init(final DaemonContext context) {
    final File confFile = PropertiesUtil.findConfigFile("server.properties");
    if (confFile == null) {
        LOG.warn(String.format("Server configuration file not found. Initializing server daemon on %s, with http.enable=%s, http.port=%s, https.enable=%s, https.port=%s, context.path=%s",
                bindInterface, httpEnable, httpPort, httpsEnable, httpsPort, contextPath));
        return;
    }

    LOG.info("Server configuration file found: " + confFile.getAbsolutePath());

    try {
        final Properties properties = PropertiesUtil.loadFromFile(confFile);
        if (properties == null) {
            return;
        }
        setBindInterface(properties.getProperty(BIND_INTERFACE, null));
        setContextPath(properties.getProperty(CONTEXT_PATH, "/client"));
        setHttpEnable(Boolean.valueOf(properties.getProperty(HTTP_ENABLE, "true")));
        setHttpPort(Integer.valueOf(properties.getProperty(HTTP_PORT, "8080")));
        setHttpsEnable(Boolean.valueOf(properties.getProperty(HTTPS_ENABLE, "false")));
        setHttpsPort(Integer.valueOf(properties.getProperty(HTTPS_PORT, "8443")));
        setKeystoreFile(properties.getProperty(KEYSTORE_FILE));
        setKeystorePassword(properties.getProperty(KEYSTORE_PASSWORD));
        setWebAppLocation(properties.getProperty(WEBAPP_DIR));
        setAccessLogFile(properties.getProperty(ACCESS_LOG, "access.log"));
        setSessionTimeout(Integer.valueOf(properties.getProperty(SESSION_TIMEOUT, "30")));
    } catch (final IOException e) {
        LOG.warn("Failed to load configuration from server.properties file", e);
    } finally {
        // make sure that at least HTTP is enabled if both of them are set to false (misconfiguration)
        if (!httpEnable && !httpsEnable) {
            setHttpEnable(true);
            LOG.warn("Server configuration malformed, neither http nor https is enabled, http will be enabled.");
        }
    }
    LOG.info(String.format("Initializing server daemon on %s, with http.enable=%s, http.port=%s, https.enable=%s, https.port=%s, context.path=%s",
            bindInterface, httpEnable, httpPort, httpsEnable, httpsPort, contextPath));
}
 
Example #13
Source File: SecureDataNodeStarter.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void init(DaemonContext context) throws Exception {
  System.err.println("Initializing secure datanode resources");
  // Create a new HdfsConfiguration object to ensure that the configuration in
  // hdfs-site.xml is picked up.
  Configuration conf = new HdfsConfiguration();
  
  // Stash command-line arguments for regular datanode
  args = context.getArguments();
  resources = getSecureResources(conf);
}
 
Example #14
Source File: DaemonStarter.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public DaemonStarter ( final Class<?> className, final String[] args ) throws Exception
{
    final Object o = className.newInstance ();
    if ( ! ( o instanceof Daemon ) )
    {
        throw new RuntimeException ( String.format ( "Class must implement '%s'", Daemon.class ) );
    }

    final DaemonController controller = this;
    this.daemon = (Daemon)o;
    this.daemon.init ( new DaemonContext () {

        @Override
        public String[] getArguments ()
        {
            return args;
        }

        @Override
        public DaemonController getController ()
        {
            return controller;
        }
    } );
    this.daemon.start ();

}
 
Example #15
Source File: UsageServer.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public void init(DaemonContext arg0) throws DaemonInitException, Exception {
    initLog4j();
}
 
Example #16
Source File: DaemonWrapper.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Called from DaemonLoader on init stage.
 * <p>
 * Accepts the following configuration arguments:
 * <ul>
 * <li>-daemon-properties: - load DaemonConfiguration properties from the specified file to act as defaults</li>
 * <li>-start: set start class name</li>
 * <li>-start-method: set start method name</li>
 * <li>-stop: set stop class name</li>
 * <li>-stop-method: set stop method name</li>
 * <li>-stop-argument: set optional argument to stop method</li>
 * <li>Anything else is treated as a startup argument</li>
 * </ul>
 * <p>
 * The following "-daemon-properties" are recognised:
 * <ul>
 * <li>args (startup argument)</li>
 * <li>start</li>
 * <li>start.method</li>
 * <li>stop</li>
 * <li>stop.method</li>
 * <li>stop.args</li>
 * </ul>
 * These are used to set the corresponding item if it has not already been
 * set by the command arguments. <b>However, note that args and stop.args are
 * appended to any existing values.</b>
 */
public void init(DaemonContext context)
    throws Exception
{
    String[] args = context.getArguments();

    if (args != null) {
        int i;
        // Parse our arguments and remove them
        // from the final argument array we are
        // passing to our child.
        for (i = 0; i < args.length; i++) {
            if (args[i].equals("--")) {
                // Done with argument processing
                break;
            }
            else if (args[i].equals("-daemon-properties")) {
                if (++i == args.length)
                    throw new IllegalArgumentException(args[i - 1]);
                configFileName = args[i];
            }
            else if (args[i].equals("-start")) {
                if (++i == args.length)
                    throw new IllegalArgumentException(args[i - 1]);
                startup.setClassName(args[i]);
            }
            else if (args[i].equals("-start-method")) {
                if (++i == args.length)
                    throw new IllegalArgumentException(args[i - 1]);
                startup.setMethodName(args[i]);
            }
            else if (args[i].equals("-stop")) {
                if (++i == args.length)
                    throw new IllegalArgumentException(args[i - 1]);
                shutdown.setClassName(args[i]);
            }
            else if (args[i].equals("-stop-method")) {
                if (++i == args.length)
                    throw new IllegalArgumentException(args[i - 1]);
                shutdown.setMethodName(args[i]);
            }
            else if (args[i].equals("-stop-argument")) {
                if (++i == args.length)
                    throw new IllegalArgumentException(args[i - 1]);
                String[] aa = new String[1];
                aa[0] = args[i];
                shutdown.addArguments(aa);
            }
            else {
                // This is not our option.
                // Everything else will be forwarded to the main
                break;
            }
        }
        if (args.length > i) {
            String[] copy = new String[args.length - i];
            System.arraycopy(args, i, copy, 0, copy.length);
            startup.addArguments(copy);
        }
    }
    if (config.load(configFileName)) {
        // Setup params if not set via cmdline.
        startup.setClassName(config.getProperty(START_CLASS));
        startup.setMethodName(config.getProperty(START_METHOD));
        // Merge the config with command line arguments
        startup.addArguments(config.getPropertyArray(ARGS));

        shutdown.setClassName(config.getProperty(STOP_CLASS));
        shutdown.setMethodName(config.getProperty(STOP_METHOD));
        shutdown.addArguments(config.getPropertyArray(STOP_ARGS));
    }
    startup.validate();
    shutdown.validate();
}
 
Example #17
Source File: JamesAppSpringMain.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public void init(DaemonContext arg0) throws Exception {
    context = new JamesServerApplicationContext(new String[] { "META-INF/org/apache/james/spring-server.xml" });
    context.registerShutdownHook();
    context.start();
}
 
Example #18
Source File: SpringBootDaemon.java    From spring-boot-daemon with Apache License 2.0 4 votes vote down vote up
public void init(DaemonContext context) throws Exception {
	System.out.println("Daemon initialized with arguments [" +
			Arrays.toString(context.getArguments()) + "]");
	this.springBootApp = ClassUtils.resolveClassName(context.getArguments()[0],
			SpringBootDaemon.class.getClassLoader());
}
 
Example #19
Source File: AthenzDaemonTest.java    From athenz with Apache License 2.0 4 votes vote down vote up
@Test
public void testInit() {
    DaemonContext daemoncontext = Mockito.mock(DaemonContext.class);
    AthenzDaemon daemon = new AthenzDaemon();
    daemon.init(daemoncontext);
}
 
Example #20
Source File: AthenzDaemon.java    From athenz with Apache License 2.0 4 votes vote down vote up
public void init(DaemonContext context) {
    args = context.getArguments();
}
 
Example #21
Source File: Service.java    From act with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void init(DaemonContext context) throws DaemonInitException, Exception {
  String args[] = context.getArguments();
  LOGGER.info("Daemon initializing with arguments: %s", StringUtils.join(args, " "));
  init(args);
}
 
Example #22
Source File: Service.java    From act with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void init(DaemonContext context) throws DaemonInitException, Exception {
  String args[] = context.getArguments();
  LOGGER.info("Daemon initializing with arguments: %s", StringUtils.join(args, " "));
  init(args);
}
 
Example #23
Source File: Sshd.java    From artifactory_ssh_proxy with Apache License 2.0 2 votes vote down vote up
/**
 * Initialize this <code>Daemon</code> instance.
 * <p>
 * This method gets called once the JVM process is created and the <code>Daemon</code> instance is created thru its
 * empty public constructor.
 * </p>
 * <p>
 * Under certain operating systems (typically Unix based operating systems) and if the native invocation framework
 * is configured to do so, this method might be called with <i>super-user</i> privileges.
 * </p>
 * <p>
 * For example, it might be wise to create <code>ServerSocket</code> instances within the scope of this method, and
 * perform all operations requiring <i>super-user</i> privileges in the underlying operating system.
 * </p>
 * <p>
 * Apart from set up and allocation of native resources, this method must not start the actual operation of the
 * <code>Daemon</code> (such as starting threads calling the <code>ServerSocket.accept()</code> method) as this
 * would impose some serious security hazards. The start of operation must be performed in the <code>start()</code>
 * method.
 * </p>
 * 
 * @param context A <code>DaemonContext</code> object used to communicate with the container.
 * @exception DaemonInitException An exception that prevented initialization where you want to display a nice
 *            message to the user, rather than a stack trace.
 * @exception Exception Any exception preventing a successful initialization.
 */
@Override
public void init(DaemonContext arg0) throws DaemonInitException, Exception {
    // save our arguments
    this.args = arg0.getArguments();
}
 
Example #24
Source File: IcapServerDaemon.java    From GreasySpoon with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Start functions and processes that require su rights 
 * @see org.apache.commons.daemon.Daemon#init(org.apache.commons.daemon.DaemonContext)
 */
public void init(DaemonContext context) {
	//nothing to do up to now as super-user
}