[java-identity-provider COMMIT] in /trunk/idp-cas-impl/src: main/java/net/shibboleth/idp/cas/ticket/TicketIdentifierG...

noreply at shibboleth.net noreply at shibboleth.net
Thu Mar 19 10:55:35 EDT 2015


Author: serac
Date: Thu Mar 19 10:55:34 2015
New Revision: 7434

URL: http://svn.shibboleth.net/view/java-identity-provider?rev=7434&view=rev
Log:
IDP-659 Create new RandomIdGenerator on each call to generateIdentifier.

Modified:
    trunk/idp-cas-impl/src/main/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategy.java
    trunk/idp-cas-impl/src/test/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategyTest.java

Modified: trunk/idp-cas-impl/src/main/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategy.java
URL: http://svn.shibboleth.net/view/java-identity-provider/trunk/idp-cas-impl/src/main/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategy.java?rev=7434&r1=7433&r2=7434&view=diff
==============================================================================
--- trunk/idp-cas-impl/src/main/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategy.java (original)
+++ trunk/idp-cas-impl/src/main/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategy.java Thu Mar 19 10:55:34 2015
@@ -52,8 +52,9 @@
     @Nullable
     private String suffix;
 
-    /** Generator of random ticket part. */
-    private IdGenerator randomPartGenerator;
+    /** Number of characters in random part of generated ticket. */
+    @Positive
+    private int randomLength;
 
 
     /**
@@ -65,10 +66,7 @@
     public TicketIdentifierGenerationStrategy(
             @Nonnull @NotEmpty final String prefix,
             @Positive final int randomLength) {
-        if (randomLength < 1) {
-            throw new IllegalArgumentException("Length of random part of ticket must be positive");
-        }
-        this.randomPartGenerator = new RandomIdGenerator(randomLength);
+        this.randomLength = (int) Constraint.isGreaterThan(0, randomLength, "Random length must be positive");
         this.prefix = Constraint.isNotNull(StringSupport.trimOrNull(prefix), "Prefix cannot be null or empty");
         if (!isUrlSafe(this.prefix)) {
             throw new IllegalArgumentException("Unsupported prefix " + this.prefix);
@@ -93,10 +91,10 @@
     @Override
     @Nonnull
     public String generateIdentifier() {
-        final StringBuilder builder = new StringBuilder(100);
+        final StringBuilder builder = new StringBuilder(randomLength * 2);
         builder.append(prefix).append('-');
         builder.append(System.currentTimeMillis()).append('-');
-        builder.append(randomPartGenerator.generate());
+        builder.append(new RandomIdGenerator(randomLength).generate());
         if (suffix != null) {
             builder.append('-').append(suffix);
         }

Modified: trunk/idp-cas-impl/src/test/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategyTest.java
URL: http://svn.shibboleth.net/view/java-identity-provider/trunk/idp-cas-impl/src/test/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategyTest.java?rev=7434&r1=7433&r2=7434&view=diff
==============================================================================
--- trunk/idp-cas-impl/src/test/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategyTest.java (original)
+++ trunk/idp-cas-impl/src/test/java/net/shibboleth/idp/cas/ticket/TicketIdentifierGenerationStrategyTest.java Thu Mar 19 10:55:34 2015
@@ -7,10 +7,19 @@
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
 
 /**
@@ -68,4 +77,23 @@
     public void testUrlSafety(final String prefix, final String suffix) {
         new TicketIdentifierGenerationStrategy(prefix, 10).setSuffix(suffix);
     }
+
+    @Test
+    public void testConcurrentGeneration() throws Exception {
+        final ExecutorService executor = Executors.newFixedThreadPool(20);
+        final TicketIdentifierGenerationStrategy generator = new TicketIdentifierGenerationStrategy("ST", 50);
+        final Collection<Callable<String>> tasks = new ArrayList<>();
+        for (int i = 0; i < 20; i++) {
+            tasks.add(new Callable<String>() {
+                @Override
+                public String call() throws Exception {
+                    return generator.generateIdentifier();
+                }
+            });
+        }
+        final List<Future<String>> results = executor.invokeAll(tasks);
+        for (Future<String> result : results) {
+            assertNotNull(result.get(1, TimeUnit.SECONDS));
+        }
+    }
 }



More information about the commits mailing list