diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/container/ContainerLoader.java b/framework/base/src/main/java/org/apache/ofbiz/base/container/ContainerLoader.java index 619c52cd23..02066f8058 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/container/ContainerLoader.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/container/ContainerLoader.java @@ -28,7 +28,6 @@ import org.apache.ofbiz.base.component.ComponentConfig; import org.apache.ofbiz.base.start.Config; import org.apache.ofbiz.base.start.StartupCommand; import org.apache.ofbiz.base.start.StartupException; -import org.apache.ofbiz.base.start.StartupLoader; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.UtilValidate; @@ -37,21 +36,31 @@ import edu.emory.mathcs.backport.java.util.Collections; /** * An object that loads containers (background processes). * - *

Normally, instances of this class are created by OFBiz startup code, and + * Normally, instances of this class are created by OFBiz startup code, and * client code should not create instances of this class. Client code is - * responsible for making sure containers are shut down properly.

- * + * responsible for making sure containers are shut down properly. + *

+ * When OFBiz starts, the main thread will create the {@code ContainerLoader} instance and + * then call the loader's {@code load} method. + * After this instance has been created and initialized, the main thread will call the + * {@code start} method. When OFBiz shuts down, a separate shutdown thread will call + * the {@code unload} method. */ -public class ContainerLoader implements StartupLoader { +public class ContainerLoader { public static final String module = ContainerLoader.class.getName(); private final List loadedContainers = new LinkedList<>(); /** - * @see org.apache.ofbiz.base.start.StartupLoader#load(Config, List) + * Starts the containers. + * + * @param config Startup config + * @param ofbizCommands Command-line arguments + * @throws StartupException If an error was encountered. Throwing this exception + * will halt loader loading, so it should be thrown only when OFBiz can't + * operate without it. */ - @Override public synchronized void load(Config config, List ofbizCommands) throws StartupException { // loaders defined in startup (e.g. main, test, load-data, etc ...) @@ -154,10 +163,9 @@ public class ContainerLoader implements StartupLoader { } /** - * @see org.apache.ofbiz.base.start.StartupLoader#unload() + * Stops the containers. */ - @Override - public synchronized void unload() throws StartupException { + public synchronized void unload() { Debug.logInfo("Shutting down containers", module); List reversedContainerList = new ArrayList<>(loadedContainers); diff --git a/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminServer.java b/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminServer.java index 492feac857..d045237e9a 100644 --- a/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminServer.java +++ b/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminServer.java @@ -26,9 +26,9 @@ import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; -import java.util.List; import java.util.concurrent.atomic.AtomicReference; +import org.apache.ofbiz.base.container.ContainerLoader; import org.apache.ofbiz.base.start.Start.ServerState; import org.apache.ofbiz.base.util.UtilValidate; @@ -47,11 +47,11 @@ final class AdminServer extends Thread { } private ServerSocket serverSocket = null; - private List loaders = null; + private ContainerLoader loader; private AtomicReference serverState = null; private Config config = null; - AdminServer(List loaders, AtomicReference serverState, Config config) throws StartupException { + AdminServer(ContainerLoader loader, AtomicReference serverState, Config config) throws StartupException { super("OFBiz-AdminServer"); try { this.serverSocket = new ServerSocket(config.adminPort, 1, config.adminAddress); @@ -59,7 +59,7 @@ final class AdminServer extends Thread { throw new StartupException("Couldn't create server socket(" + config.adminAddress + ":" + config.adminPort + ")", e); } setDaemon(false); - this.loaders = loaders; + this.loader = loader; this.serverState = serverState; this.config = config; } @@ -74,7 +74,7 @@ final class AdminServer extends Thread { + clientSocket.getInetAddress() + " : " + clientSocket.getPort()); - processClientRequest(clientSocket, loaders, serverState); + processClientRequest(clientSocket, loader, serverState); } catch (IOException e) { e.printStackTrace(); @@ -82,8 +82,8 @@ final class AdminServer extends Thread { } } - private void processClientRequest(Socket client, List loaders, AtomicReference serverState) throws IOException { - + private void processClientRequest(Socket client, ContainerLoader loader, AtomicReference serverState) + throws IOException { try (BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream(), StandardCharsets.UTF_8), true)) { @@ -98,7 +98,7 @@ final class AdminServer extends Thread { // if the client request is shutdown, execute shutdown sequence if(clientCommand.equals(OfbizSocketCommand.SHUTDOWN)) { writer.flush(); - StartupControlPanel.stop(loaders, serverState, this); + StartupControlPanel.stop(loader, serverState, this); } } } diff --git a/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupControlPanel.java b/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupControlPanel.java index 5e015f4d0e..dbde5bb13d 100644 --- a/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupControlPanel.java +++ b/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupControlPanel.java @@ -21,10 +21,10 @@ package org.apache.ofbiz.base.start; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReference; +import org.apache.ofbiz.base.container.ContainerLoader; import org.apache.ofbiz.base.start.Start.ServerState; /** @@ -62,15 +62,14 @@ final class StartupControlPanel { AtomicReference serverState, List ofbizCommands) throws StartupException { - //TODO loaders should be converted to a single loader - List loaders = new ArrayList<>(); - Thread adminServer = createAdminServer(config, serverState, loaders); + ContainerLoader loader = new ContainerLoader(); + Thread adminServer = createAdminServer(config, serverState, loader); createLogDirectoryIfMissing(config); - createRuntimeShutdownHook(config, loaders, serverState); - loadStartupLoaders(config, loaders, ofbizCommands, serverState); + createRuntimeShutdownHook(config, loader, serverState); + loadContainers(config, loader, ofbizCommands, serverState); printStartupMessage(config); - executeShutdownAfterLoadIfConfigured(config, loaders, serverState, adminServer); + executeShutdownAfterLoadIfConfigured(config, loader, serverState, adminServer); } /** @@ -96,8 +95,8 @@ final class StartupControlPanel { * - Manually if requested by the client AdminClient * - Automatically if Config.shutdownAfterLoad is set to true */ - static void stop(List loaders, AtomicReference serverState, Thread adminServer) { - shutdownServer(loaders, serverState, adminServer); + static void stop(ContainerLoader loader, AtomicReference serverState, Thread adminServer) { + shutdownServer(loader, serverState, adminServer); System.exit(0); } @@ -121,7 +120,7 @@ final class StartupControlPanel { System.exit(1); } - private static void shutdownServer(List loaders, AtomicReference serverState, Thread adminServer) { + private static void shutdownServer(ContainerLoader loader, AtomicReference serverState, Thread adminServer) { ServerState currentState; do { currentState = serverState.get(); @@ -129,18 +128,10 @@ final class StartupControlPanel { return; } } while (!serverState.compareAndSet(currentState, ServerState.STOPPING)); - // The current thread was the one that successfully changed the state; - // continue with further processing. - synchronized (loaders) { - // Unload in reverse order - for (int i = loaders.size(); i > 0; i--) { - StartupLoader loader = loaders.get(i - 1); - try { - loader.unload(); - } catch (Exception e) { - e.printStackTrace(); - } - } + try { + loader.unload(); + } catch (Exception e) { + e.printStackTrace(); } if (adminServer != null && adminServer.isAlive()) { adminServer.interrupt(); @@ -161,11 +152,11 @@ final class StartupControlPanel { private static Thread createAdminServer( Config config, AtomicReference serverState, - List loaders) throws StartupException { + ContainerLoader loader) throws StartupException { Thread adminServer = null; if (config.adminPort > 0) { - adminServer = new AdminServer(loaders, serverState, config); + adminServer = new AdminServer(loader, serverState, config); adminServer.start(); } else { System.out.println("Admin socket not configured; set to port 0"); @@ -184,14 +175,14 @@ final class StartupControlPanel { private static void createRuntimeShutdownHook( Config config, - List loaders, + ContainerLoader loader, AtomicReference serverState) { if (config.useShutdownHook) { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { - shutdownServer(loaders, serverState, this); + shutdownServer(loader, serverState, this); } }); } else { @@ -199,38 +190,27 @@ final class StartupControlPanel { } } - private static void loadStartupLoaders(Config config, - List loaders, + private static void loadContainers(Config config, + ContainerLoader loader, List ofbizCommands, AtomicReference serverState) throws StartupException { - - String startupLoaderName = "org.apache.ofbiz.base.container.ContainerLoader"; - ClassLoader classloader = Thread.currentThread().getContextClassLoader(); - - synchronized (loaders) { + synchronized (StartupControlPanel.class) { if (serverState.get() == ServerState.STOPPING) { return; } - try { - Class loaderClass = classloader.loadClass(startupLoaderName); - StartupLoader loader = (StartupLoader) loaderClass.newInstance(); - loaders.add(loader); // add before loading, so unload can occur if error during loading - loader.load(config, ofbizCommands); - } catch (ReflectiveOperationException e) { - throw new StartupException(e); - } + loader.load(config, ofbizCommands); } serverState.compareAndSet(ServerState.STARTING, ServerState.RUNNING); } private static void executeShutdownAfterLoadIfConfigured( Config config, - List loaders, + ContainerLoader loader, AtomicReference serverState, Thread adminServer) { if (config.shutdownAfterLoad) { - stop(loaders, serverState, adminServer); + stop(loader, serverState, adminServer); } } } diff --git a/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupLoader.java b/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupLoader.java deleted file mode 100644 index 724b5d7cf3..0000000000 --- a/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupLoader.java +++ /dev/null @@ -1,56 +0,0 @@ -/******************************************************************************* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.apache.ofbiz.base.start; - -import java.util.List; - -/** - * An object that loads server startup classes. - *

- * When OFBiz starts, the main thread will create the StartupLoader instance and - * then call the loader's load method. If the method returns without - * throwing an exception the loader will be added to a list of initialized loaders. - * After all instances have been created and initialized, the main thread will call the - * start method of each loader in the list. When OFBiz shuts down, a - * separate shutdown thread will call the unload method of each loader. - * Implementations should anticipate asynchronous calls to the methods by different - * threads. - *

- * - */ -public interface StartupLoader { - - /** - * Start a startup class. - * - * @param config Startup config. - * @param ofbizCommands Command-line arguments. - * @throws StartupException If an error was encountered. Throwing this exception - * will halt loader loading, so it should be thrown only when OFBiz can't - * operate without it. - */ - public void load(Config config, List ofbizCommands) throws StartupException; - - /** - * Stop the startup class. This method must not block. - * - * @throws StartupException If an error was encountered. - */ - public void unload() throws StartupException; -}