Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #546 from sonatype/NXCM-4643-shutdown-helper
Browse files Browse the repository at this point in the history
Add ShutdownHelper so we can delegate std-java or jsw implementations to...
  • Loading branch information
peterlynch committed Sep 20, 2012
2 parents 694c610 + 1c99324 commit 8be6bc0
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ protected void maybeEnableShutdownIfNotAlive()
}

public void commandStop() {
System.exit(0);
ShutdownHelper.exit(0);
}

public void stop() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2007-2012 Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.bootstrap;

/**
* Helper to cope with different mechanisms to shtudown.
*
* @since 2.2
*/
public class ShutdownHelper
{
public static interface ShutdownDelegate
{
void doExit(int code);

void doHalt(int code);
}

public static class JavaShutdownDelegate
implements ShutdownDelegate
{
@Override
public void doExit(final int code) {
System.exit(code);
}

@Override
public void doHalt(final int code) {
Runtime.getRuntime().halt(code);
}
}

private static ShutdownDelegate delegate = new JavaShutdownDelegate();

public static ShutdownDelegate getDelegate() {
if (delegate == null) {
throw new IllegalStateException();
}
return delegate;
}

public static void setDelegate(final ShutdownDelegate delegate) {
if (delegate == null) {
throw new NullPointerException();
}
ShutdownHelper.delegate = delegate;
}

public static void exit(final int code) {
getDelegate().doExit(code);
}

public static void halt(final int code) {
getDelegate().doHalt(code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.slf4j.Logger;
import org.sonatype.nexus.bootstrap.Launcher;
import org.sonatype.nexus.bootstrap.ShutdownHelper;
import org.tanukisoftware.wrapper.WrapperManager;

import static org.tanukisoftware.wrapper.WrapperManager.WRAPPER_CTRL_LOGOFF_EVENT;
Expand Down Expand Up @@ -76,6 +77,7 @@ protected void doControlEvent(final int code) {
}

public static void main(final String[] args) throws Exception {
ShutdownHelper.setDelegate(new JswShutdownDelegate());
WrapperManager.start(new JswLauncher(), args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2007-2012 Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/

package org.sonatype.nexus.bootstrap.jsw;

import org.sonatype.nexus.bootstrap.ShutdownHelper.ShutdownDelegate;
import org.tanukisoftware.wrapper.WrapperManager;

/**
* JSW {@link ShutdownDelegate}.
*
* @since 2.2
*/
public class JswShutdownDelegate
implements ShutdownDelegate
{
@Override
public void doExit(final int code) {
WrapperManager.stop(code);
}

@Override
public void doHalt(final int code) {
WrapperManager.stopImmediate(code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.sonatype.nexus.bootstrap.monitor;

import org.sonatype.nexus.bootstrap.ShutdownHelper;
import org.sonatype.nexus.bootstrap.monitor.commands.PingCommand;

import java.io.IOException;
Expand Down Expand Up @@ -90,7 +91,7 @@ public KeepAliveThread(final String host,
{
@Override
public void run() {
Runtime.getRuntime().halt(666);
ShutdownHelper.halt(666);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
*/
package org.sonatype.nexus.bootstrap.monitor.commands;

import org.sonatype.nexus.bootstrap.ShutdownHelper;
import org.sonatype.nexus.bootstrap.monitor.CommandMonitorThread;

/**
* Command to exit the JVM (via {@link System#exit(int)}).
* Command to exit the JVM (via {@link ShutdownHelper#exit(int)}).
*
* @since 2.2
*/
Expand All @@ -34,7 +35,7 @@ public String getId()
@Override
public boolean execute()
{
System.exit(666);
ShutdownHelper.exit(666);

throw new Error("Unreachable statement");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
*/
package org.sonatype.nexus.bootstrap.monitor.commands;

import org.sonatype.nexus.bootstrap.ShutdownHelper;
import org.sonatype.nexus.bootstrap.monitor.CommandMonitorThread;

/**
* Command to forcibly halt the JVM (via {@link Runtime#halt(int)}).
* Command to forcibly halt the JVM (via {@link ShutdownHelper#halt(int)}).
*
* @since 2.2
*/
Expand All @@ -34,7 +35,7 @@ public String getId()
@Override
public boolean execute()
{
Runtime.getRuntime().halt(666);
ShutdownHelper.halt(666);

throw new Error("Unreachable statement");
}
Expand Down

0 comments on commit 8be6bc0

Please sign in to comment.