Skip to content

Commit

Permalink
auto stop previously running instance in app startup in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthuss authored and Pascal Robert committed Mar 16, 2012
1 parent 7289239 commit 2c6a64f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.BindException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
Expand All @@ -44,6 +46,7 @@

import com.webobjects.appserver.WOAction;
import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WOAdaptor;
import com.webobjects.appserver.WOApplication;
import com.webobjects.appserver.WOComponent;
import com.webobjects.appserver.WOContext;
Expand Down Expand Up @@ -98,6 +101,7 @@
import er.extensions.foundation.ERXArrayUtilities;
import er.extensions.foundation.ERXCompressionUtilities;
import er.extensions.foundation.ERXConfigurationManager;
import er.extensions.foundation.ERXExceptionUtilities;
import er.extensions.foundation.ERXPatcher;
import er.extensions.foundation.ERXProperties;
import er.extensions.foundation.ERXRuntimeUtilities;
Expand Down Expand Up @@ -150,6 +154,7 @@
* @property er.extensions.ERXApplication.useEditingContextUnlocker
* @property er.extensions.ERXApplication.useSessionStoreDeadlockDetection
* @property er.extensions.ERXComponentActionRedirector.enabled
* @property er.extensions.ERXApplication.allowMultipleDevInstances
*/
public abstract class ERXApplication extends ERXAjaxApplication implements ERXGracefulShutdown.GracefulApplication {

Expand Down Expand Up @@ -821,6 +826,49 @@ public static void main(String argv[], Class applicationClass) {
WOApplication.main(argv, applicationClass);
}

/**
* <p>Terminates a different instance of the same application that may already be running.<br>
* Only in dev mode.</p>
* <p>Set the property "er.extensions.ERXApplication.allowMultipleDevInstances" to "true" if
* you need to run multiple instances in dev mode.</p>
*
* @return true if a previously running instance was stopped.
*/
private static boolean stopPreviousDevInstance() {
if (!isDevelopmentModeSafe() ||
ERXProperties.booleanForKeyWithDefault("er.extensions.ERXApplication.allowMultipleDevInstances", false)) {
return false;
}

String appUrl;
if (!(application().wasMainInvoked())) {
return false;
} else if (application().isDirectConnectEnabled()) {
appUrl = application().cgiAdaptorURL().replace("/cgi", ":" + application().port() + "/cgi");
appUrl += "/" + application().name() + ".woa";
} else {
appUrl = application().cgiAdaptorURL() + "/" + application().name() + ".woa/-" + application().port();
}

URL url;
try {
appUrl = appUrl + "/" + application().directActionRequestHandlerKey() + "/stop";
url = new URL(appUrl);

log.debug("Stopping previously running instance of " + application().name());

URLConnection connection = url.openConnection();
connection.getContent();

Thread.sleep(2000); // really only necessary for direct connect mode

return true;
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}

/**
* Utility class to track down duplicate items in the class path. Reports
* duplicate packages and packages that are present in different versions.
Expand Down Expand Up @@ -2617,6 +2665,19 @@ public NSArray<NSDictionary<String, Object>> additionalAdaptors() {
return additionalAdaptors;
}

@Override
public WOAdaptor adaptorWithName(String aClassName, NSDictionary<String, Object> anArgsDictionary) {
try {
return super.adaptorWithName(aClassName, anArgsDictionary);
} catch (NSForwardException e) {
Throwable rootCause = ERXExceptionUtilities.getMeaningfulThrowable(e);
if ((rootCause instanceof BindException) && stopPreviousDevInstance()) {
return super.adaptorWithName(aClassName, anArgsDictionary);
}
throw e;
}
}

protected void _debugValueForDeclarationNamed(WOComponent component, String verb, String aDeclarationName, String aDeclarationType, String aBindingName, String anAssociationDescription, Object aValue) {
if (aValue instanceof String) {
StringBuffer stringbuffer = new StringBuffer(((String) aValue).length() + 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,18 @@ public <T extends WOComponent> T pageWithName(Class<T> componentClass) {
return (T) super.pageWithName(componentClass.getName());
}

public WOActionResults stopAction() {
WOResponse response = new WOResponse();
response.setHeader("text/plain", "Content-Type");

if (ERXApplication.isDevelopmentModeSafe()) {
WOApplication.application().terminate();
response.setContent("OK");
} else {
response.setStatus(401);
}

return response;
}

}

0 comments on commit 2c6a64f

Please sign in to comment.