Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Changes log
===========

- 2.7 Milestone 3 (??-??-2025)

- Bugs fixed
- Reuse an instance of Random class in RandomUtils. Issue #1487.
- 2.7 Milestone 2 (29-06-2025)
- Misc
- Removed deprecated Servlet extension and related classes in Spring extension
Expand Down
6 changes: 3 additions & 3 deletions org.restlet/src/main/java/org/restlet/routing/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* <li>First match (default)</li>
* <li>Last match</li>
* <li>Random match</li>
* <li>Round robin</li>
* <li>Round-robin</li>
* <li>Custom</li>
* </ul>
* <br>
Expand All @@ -53,7 +53,7 @@
public class Router extends Restlet {

/**
* Each call will be routed to the route with the best score, if the required
* Each call will be routed to the route with the best score if the required
* score is reached. See {@link RouteList#getBest(Request, Response, float)}
* method for implementation details.
*/
Expand Down Expand Up @@ -94,7 +94,7 @@ public class Router extends Restlet {

/**
* Each call will be randomly routed to one of the routes that reached the
* required score. If the random route selected is not a match then the
* required score. If the random route selected is not a match, then the
* immediate next route is evaluated until one matching route is found. If we
* get back to the initial random route selected with no match, then we return
* null. Unless all the routes score above the required score, this mode will
Expand Down
15 changes: 9 additions & 6 deletions org.restlet/src/main/java/org/restlet/util/RouteList.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.restlet.Restlet;
import org.restlet.routing.Route;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.List;
import java.util.Random;
Expand All @@ -34,14 +35,16 @@
* @see java.util.List
*/
public final class RouteList extends WrapperList<Route> {
/** The index of the last route used in the round robin mode. */
/** The index of the last route used in the round-robin mode. */
private volatile int lastIndex;
/** Used when asked to return a random route. */
private final SecureRandom random = new SecureRandom();

/**
* Constructor.
*/
public RouteList() {
super(new CopyOnWriteArrayList<Route>());
super(new CopyOnWriteArrayList<>());
this.lastIndex = -1;
}

Expand All @@ -51,7 +54,7 @@ public RouteList() {
* @param delegate The delegate list.
*/
public RouteList(List<Route> delegate) {
super(new CopyOnWriteArrayList<Route>(delegate));
super(new CopyOnWriteArrayList<>(delegate));
this.lastIndex = -1;
}

Expand Down Expand Up @@ -159,7 +162,7 @@ public synchronized Route getRandom(Request request, Response response, float re
int length = size();

if (length > 0) {
int j = new Random().nextInt(length);
int j = random.nextInt(length);
Route route = get(j);

if (route.score(request, response) >= requiredScore) {
Expand All @@ -169,7 +172,7 @@ public synchronized Route getRandom(Request request, Response response, float re
boolean loopedAround = false;

do {
if ((j == length) && (!loopedAround)) {
if ((j == length) && !loopedAround) {
j = 0;
loopedAround = true;
}
Expand All @@ -187,7 +190,7 @@ public synchronized Route getRandom(Request request, Response response, float re
}

/**
* Removes all routes routing to a given target.
* Removes all routes to a given target.
*
* @param target The target Restlet to detach.
*/
Expand Down