Fixed incorrect use of statics around the ApplicationContext

- Application context will shut down and restart if a new different context is requested
 - Removed gratuitous use of statics and added necessary synchronization


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@15927 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-08-26 12:36:14 +00:00
parent 28dad9a7b5
commit 9c18bf6d7b
2 changed files with 104 additions and 58 deletions

View File

@@ -67,8 +67,7 @@ public abstract class BaseWebScriptTest extends TestCase
private boolean traceReqRes = false; private boolean traceReqRes = false;
// Local Server access // Local Server access
private static String customContext = null; private String customContext = null;
private static TestWebScriptServer server = null;
// Remote Server access // Remote Server access
private String defaultRunAs = null; private String defaultRunAs = null;
@@ -156,9 +155,9 @@ public abstract class BaseWebScriptTest extends TestCase
* Sets custom context for Test Web Script Server (in-process only) * Sets custom context for Test Web Script Server (in-process only)
* @param customContext * @param customContext
*/ */
public static void setCustomContext(String customContext) protected void setCustomContext(String customContext)
{ {
BaseWebScriptTest.customContext = customContext; this.customContext = customContext;
} }
/** /**
@@ -237,20 +236,19 @@ public abstract class BaseWebScriptTest extends TestCase
} }
} }
protected static TestWebScriptServer getServer() /**
* Get the server for the previously-supplied {@link #setCustomContext(String) custom context}
*/
protected TestWebScriptServer getServer()
{ {
if (BaseWebScriptTest.server == null) if (customContext == null)
{ {
if (BaseWebScriptTest.customContext == null) return TestWebScriptRepoServer.getTestServer();
{ }
BaseWebScriptTest.server = TestWebScriptRepoServer.getTestServer(); else
} {
else return TestWebScriptRepoServer.getTestServer(customContext);
{
BaseWebScriptTest.server = TestWebScriptRepoServer.getTestServer(customContext);
}
} }
return BaseWebScriptTest.server;
} }
@@ -348,7 +346,7 @@ public abstract class BaseWebScriptTest extends TestCase
asUser = (asUser == null) ? defaultRunAs : asUser; asUser = (asUser == null) ? defaultRunAs : asUser;
if (asUser == null) if (asUser == null)
{ {
return BaseWebScriptTest.getServer().submitRequest(req.getMethod(), req.getFullUri(), req.getHeaders(), req.getBody(), req.getEncoding(), req.getType()); return getServer().submitRequest(req.getMethod(), req.getFullUri(), req.getHeaders(), req.getBody(), req.getEncoding(), req.getType());
} }
else else
{ {
@@ -358,7 +356,7 @@ public abstract class BaseWebScriptTest extends TestCase
@SuppressWarnings("synthetic-access") @SuppressWarnings("synthetic-access")
public Response doWork() throws Exception public Response doWork() throws Exception
{ {
return BaseWebScriptTest.getServer().submitRequest(req.getMethod(), req.getFullUri(), req.getHeaders(), req.getBody(), req.getEncoding(), req.getType()); return getServer().submitRequest(req.getMethod(), req.getFullUri(), req.getHeaders(), req.getBody(), req.getEncoding(), req.getType());
} }
}, asUser); }, asUser);
} }

View File

@@ -34,8 +34,8 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.transaction.RetryingTransactionHelper; import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.security.AuthenticationService; import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.util.EqualsHelper;
import org.alfresco.web.scripts.TestWebScriptServer; import org.alfresco.web.scripts.TestWebScriptServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -46,34 +46,6 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
*/ */
public class TestWebScriptRepoServer extends TestWebScriptServer public class TestWebScriptRepoServer extends TestWebScriptServer
{ {
private RetryingTransactionHelper retryingTransactionHelper;
private AuthenticationService authenticationService;
/**
* Sets helper that provides transaction callbacks
*/
public void setTransactionHelper(RetryingTransactionHelper retryingTransactionHelper)
{
this.retryingTransactionHelper = retryingTransactionHelper;
}
/**
* @param authenticationService
*/
public void setAuthenticationService(AuthenticationService authenticationService)
{
this.authenticationService = authenticationService;
}
/**
* Get default user name
*/
protected String getDefaultUserName()
{
return AuthenticationUtil.getAdminUserName();
}
/** /**
* Main entry point. * Main entry point.
*/ */
@@ -106,26 +78,102 @@ public class TestWebScriptRepoServer extends TestWebScriptServer
"classpath:alfresco/web-scripts-application-context-test.xml" "classpath:alfresco/web-scripts-application-context-test.xml"
}; };
/** A static reference to the application context being used */
private static ClassPathXmlApplicationContext ctx;
private static String appendedTestConfiguration;
private RetryingTransactionHelper retryingTransactionHelper;
private AuthenticationService authenticationService;
/** /**
* Retrieve an instance of the TestWebScriptServer * Sets helper that provides transaction callbacks
* */
* @return Test Server public void setTransactionHelper(RetryingTransactionHelper retryingTransactionHelper)
{
this.retryingTransactionHelper = retryingTransactionHelper;
}
/**
* @param authenticationService
*/
public void setAuthenticationService(AuthenticationService authenticationService)
{
this.authenticationService = authenticationService;
}
/**
* Get default user name
*/
protected String getDefaultUserName()
{
return AuthenticationUtil.getAdminUserName();
}
/**
* {@inheritDoc #getTestServer(String)}
*/ */
public static TestWebScriptServer getTestServer() public static TestWebScriptServer getTestServer()
{ {
ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_LOCATIONS); return getTestServer(null);
TestWebScriptServer testServer = (TestWebScriptRepoServer)context.getBean("webscripts.test");
return testServer;
} }
public static TestWebScriptServer getTestServer(String appendTestConfigLocation) /**
* Start up a context and get the server bean.
* <p>
* This method will close and restart the application context only if the configuration has
* changed.
*
* @param appendTestConfigLocation additional context file to include in the application context
* @return Test Server
*/
public static synchronized TestWebScriptServer getTestServer(String appendTestConfigLocation)
{ {
String[] config_locations = new String[CONFIG_LOCATIONS.length+1]; if (TestWebScriptRepoServer.ctx != null)
System.arraycopy(CONFIG_LOCATIONS, 0, config_locations, 0, CONFIG_LOCATIONS.length); {
config_locations[CONFIG_LOCATIONS.length] = appendTestConfigLocation; boolean configChanged = !EqualsHelper.nullSafeEquals(
appendTestConfigLocation,
TestWebScriptRepoServer.appendedTestConfiguration);
if (configChanged)
{
// The config changed, so close the context (it'll be restarted later)
try
{
ctx.close();
ctx = null;
}
catch (Throwable e)
{
throw new RuntimeException("Failed to shut down existing application context", e);
}
}
else
{
// There is already a context with the required configuration
}
}
ApplicationContext context = new ClassPathXmlApplicationContext(config_locations); // Check if we need to start/restart the context
TestWebScriptServer testServer = (TestWebScriptRepoServer)context.getBean("webscripts.test"); if (TestWebScriptRepoServer.ctx == null)
{
// Restart it
final String[] configLocations;
if (appendTestConfigLocation == null)
{
configLocations = CONFIG_LOCATIONS;
}
else
{
configLocations = new String[CONFIG_LOCATIONS.length+1];
System.arraycopy(CONFIG_LOCATIONS, 0, configLocations, 0, CONFIG_LOCATIONS.length);
configLocations[CONFIG_LOCATIONS.length] = appendTestConfigLocation;
}
TestWebScriptRepoServer.ctx = new ClassPathXmlApplicationContext(configLocations);
TestWebScriptRepoServer.appendedTestConfiguration = appendTestConfigLocation;
}
// Get the bean
TestWebScriptServer testServer = (TestWebScriptRepoServer)TestWebScriptRepoServer.ctx.getBean("webscripts.test");
return testServer; return testServer;
} }