diff --git a/ldap/src/integration-test/java/org/springframework/security/ldap/server/ApacheDSContainerTests.java b/ldap/src/integration-test/java/org/springframework/security/ldap/server/ApacheDSContainerTests.java index 3df4ccc49e..e6a61ac725 100644 --- a/ldap/src/integration-test/java/org/springframework/security/ldap/server/ApacheDSContainerTests.java +++ b/ldap/src/integration-test/java/org/springframework/security/ldap/server/ApacheDSContainerTests.java @@ -1,3 +1,18 @@ +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.security.ldap.server; import static junit.framework.Assert.fail; @@ -9,6 +24,7 @@ import org.junit.Test; * Useful for debugging the container by itself. * * @author Luke Taylor + * @author Rob Winch * @since 3.0 */ public class ApacheDSContainerTests { @@ -46,4 +62,22 @@ public class ApacheDSContainerTests { }catch(Throwable t) {} } } + + // SEC-2161 + @Test + public void multipleInstancesSimultanciously() throws Exception { + ApacheDSContainer server1 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif"); + ApacheDSContainer server2 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif"); + try { + server1.afterPropertiesSet(); + server2.afterPropertiesSet(); + } finally { + try { + server1.destroy(); + }catch(Throwable t) {} + try { + server2.destroy(); + }catch(Throwable t) {} + } + } } diff --git a/ldap/src/main/java/org/springframework/security/ldap/server/ApacheDSContainer.java b/ldap/src/main/java/org/springframework/security/ldap/server/ApacheDSContainer.java index 8736255407..12549739b7 100644 --- a/ldap/src/main/java/org/springframework/security/ldap/server/ApacheDSContainer.java +++ b/ldap/src/main/java/org/springframework/security/ldap/server/ApacheDSContainer.java @@ -17,7 +17,6 @@ package org.springframework.security.ldap.server; import java.io.File; import java.io.IOException; -import java.net.URI; import java.util.ArrayList; import java.util.List; @@ -119,7 +118,7 @@ public class ApacheDSContainer implements InitializingBean, DisposableBean, Life String apacheWorkDir = System.getProperty("apacheDSWorkDir"); if (apacheWorkDir == null) { - apacheWorkDir = System.getProperty("java.io.tmpdir") + File.separator + "apacheds-spring-security"; + apacheWorkDir = createTempDirectory("apacheds-spring-security-"); } setWorkingDirectory(new File(apacheWorkDir)); @@ -267,6 +266,22 @@ public class ApacheDSContainer implements InitializingBean, DisposableBean, Life } } + private String createTempDirectory(String prefix) throws IOException { + String parentTempDir = System.getProperty("java.io.tmpdir"); + String fileNamePrefix = prefix + System.nanoTime(); + String fileName = fileNamePrefix; + + for(int i=0;i<1000;i++) { + File tempDir = new File(parentTempDir, fileName); + if(!tempDir.exists()) { + return tempDir.getAbsolutePath(); + } + fileName = fileNamePrefix + "~" + i; + } + + throw new IOException("Failed to create a temporary directory for file at " + new File(parentTempDir, fileNamePrefix)); + } + private boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list();