diff --git a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/DefaultBootstrapHandler.java b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/DefaultBootstrapHandler.java index 6fb0685e27..69e92c9559 100644 --- a/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/DefaultBootstrapHandler.java +++ b/leshan-server-core/src/main/java/org/eclipse/leshan/server/bootstrap/DefaultBootstrapHandler.java @@ -20,8 +20,6 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; import org.eclipse.leshan.core.node.LwM2mNode; import org.eclipse.leshan.core.node.LwM2mPath; @@ -60,8 +58,6 @@ public class DefaultBootstrapHandler implements BootstrapHandler { public static final long DEFAULT_LIFETIME = 15 * 60 * 1000l; // 15 minutes - protected final Executor e; - protected final BootstrapConfigStore store; protected final LwM2mBootstrapRequestSender sender; @@ -76,20 +72,14 @@ public class DefaultBootstrapHandler implements BootstrapHandler { public DefaultBootstrapHandler(BootstrapConfigStore store, LwM2mBootstrapRequestSender sender, BootstrapSessionManager sessionManager) { - this(store, sender, sessionManager, Executors.newFixedThreadPool(5), DEFAULT_TIMEOUT, DEFAULT_LIFETIME); - } - - public DefaultBootstrapHandler(BootstrapConfigStore store, LwM2mBootstrapRequestSender sender, - BootstrapSessionManager sessionManager, Executor executor) { - this(store, sender, sessionManager, executor, DEFAULT_TIMEOUT, DEFAULT_LIFETIME); + this(store, sender, sessionManager, DEFAULT_TIMEOUT, DEFAULT_LIFETIME); } public DefaultBootstrapHandler(BootstrapConfigStore store, LwM2mBootstrapRequestSender sender, - BootstrapSessionManager sessionManager, Executor executor, long requestTimeout, long sessionLifetime) { + BootstrapSessionManager sessionManager, long requestTimeout, long sessionLifetime) { this.store = store; this.sender = sender; this.sessionManager = sessionManager; - this.e = executor; this.requestTimeout = requestTimeout; this.sessionLifeTime = sessionLifetime; } @@ -123,28 +113,23 @@ public BootstrapResponse bootstrap(Identity sender, BootstrapRequest request) { } } while (oldSession != null); - // Get the desired bootstrap config for the endpoint - final BootstrapConfig cfg = store.get(endpoint, sender); - if (cfg == null) { - LOG.debug("No bootstrap config for {}", session); - stopSession(session, NO_BOOTSTRAP_CONFIG); - return BootstrapResponse.badRequest("no bootstrap config"); - } - - // Start the bootstrap session - e.execute(new Runnable() { - @Override - public void run() { - try { - startBootstrap(session, cfg); - } catch (RuntimeException e) { - LOG.warn("Unexpected error at bootstrap start-up for {}", session, e); - stopSession(session, INTERNAL_SERVER_ERROR); - } + try { + // Get the desired bootstrap config for the endpoint + final BootstrapConfig cfg = store.get(endpoint, sender); + if (cfg == null) { + LOG.debug("No bootstrap config for {}", session); + stopSession(session, NO_BOOTSTRAP_CONFIG); + return BootstrapResponse.badRequest("no bootstrap config"); } - }); - return BootstrapResponse.success(); + startBootstrap(session, cfg); + return BootstrapResponse.success(); + + } catch (RuntimeException e) { + LOG.warn("Unexpected error at bootstrap start-up for {}", session, e); + stopSession(session, INTERNAL_SERVER_ERROR); + return BootstrapResponse.internalServerError(e.getMessage()); + } } protected void startBootstrap(BootstrapSession session, BootstrapConfig cfg) { diff --git a/leshan-server-core/src/test/java/org/eclipse/leshan/server/bootstrap/BootstrapHandlerTest.java b/leshan-server-core/src/test/java/org/eclipse/leshan/server/bootstrap/BootstrapHandlerTest.java index 08d510814d..435ebe33fc 100644 --- a/leshan-server-core/src/test/java/org/eclipse/leshan/server/bootstrap/BootstrapHandlerTest.java +++ b/leshan-server-core/src/test/java/org/eclipse/leshan/server/bootstrap/BootstrapHandlerTest.java @@ -18,7 +18,6 @@ import static org.junit.Assert.*; import java.net.InetSocketAddress; -import java.util.concurrent.Executor; import org.eclipse.leshan.ResponseCode; import org.eclipse.leshan.core.request.BootstrapDeleteRequest; @@ -45,7 +44,7 @@ public class BootstrapHandlerTest { public void error_if_not_authorized() { // prepare bootstrapHandler with a session manager which does not authorized any session BootstrapSessionManager bsSessionManager = new MockBootstrapSessionManager(false); - BootstrapHandler bsHandler = new DefaultBootstrapHandler(null, null, bsSessionManager, new DirectExecutor()); + BootstrapHandler bsHandler = new DefaultBootstrapHandler(null, null, bsSessionManager); // Try to bootstrap BootstrapResponse response = bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), @@ -64,8 +63,7 @@ public void bootstrap_success() throws InvalidConfigurationException { LwM2mBootstrapRequestSender requestSender = new MockRequestSender(Mode.ALWAYS_SUCCESS); EditableBootstrapConfigStore bsStore = new InMemoryBootstrapConfigStore(); bsStore.add("endpoint", new BootstrapConfig()); - BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager, - new DirectExecutor()); + BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager); // Try to bootstrap bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("endpoint")); @@ -84,8 +82,7 @@ public void bootstrap_failed_because_of_sent_failure() throws InvalidConfigurati LwM2mBootstrapRequestSender requestSender = new MockRequestSender(Mode.ALWAYS_FAILURE); EditableBootstrapConfigStore bsStore = new InMemoryBootstrapConfigStore(); bsStore.add("endpoint", new BootstrapConfig()); - BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager, - new DirectExecutor()); + BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager); // Try to bootstrap bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), new BootstrapRequest("endpoint")); @@ -108,7 +105,7 @@ public void two_bootstrap_at_the_same_time_not_allowed() EditableBootstrapConfigStore bsStore = new InMemoryBootstrapConfigStore(); bsStore.add("endpoint", new BootstrapConfig()); BootstrapHandler bsHandler = new DefaultBootstrapHandler(bsStore, requestSender, bsSessionManager, - new DirectExecutor(), DefaultBootstrapHandler.DEFAULT_TIMEOUT, 1000); + DefaultBootstrapHandler.DEFAULT_TIMEOUT, 1000); // First bootstrap : which will not end (because of sender) BootstrapResponse first_response = bsHandler.bootstrap(Identity.psk(new InetSocketAddress(4242), "pskdentity"), @@ -281,11 +278,4 @@ public void reset() { failureCause = null; } } - - private static class DirectExecutor implements Executor { - @Override - public void execute(Runnable command) { - command.run(); - } - } }