Skip to content
Open
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
39 changes: 39 additions & 0 deletions src/main/java/io/mapsmessaging/ServerAgentFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* Copyright [ 2020 - 2024 ] Matthew Buckton
* Copyright [ 2024 - 2026 ] MapsMessaging B.V.
*
* Licensed under the Apache License, Version 2.0 with the Commons Clause
* (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
* https://commonsclause.com/
*
* 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 io.mapsmessaging;

import io.mapsmessaging.utilities.Agent;

/**
* SPI allowing extension jars to contribute a lifecycle {@link Agent} to the server.
* Implementations are discovered via {@link java.util.ServiceLoader} at startup and
* started/stopped by {@link SubSystemManager} using the supplied ordering weights.
*/
public interface ServerAgentFactory {

/** Create the agent instance to be managed by the server. */
Agent create();

/** Start ordering weight; lower values start earlier. */
int startOrder();

/** Stop ordering weight; lower values stop earlier. */
int stopOrder();
}
33 changes: 33 additions & 0 deletions src/main/java/io/mapsmessaging/SubSystemManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ private void createAgentStartStopList() throws IOException {
addToMap(2200, 70, new DeviceManager(featureManager));
}
addOptionalML();
loadExtensionAgents();
}

private void addOptionalML(){
Expand All @@ -275,6 +276,38 @@ private void addOptionalML(){
}
}

private void loadExtensionAgents() {
Set<String> allowed = new HashSet<>();
for (String entry : System.getProperty("extension.agents.allowed", "").split(",")) {
String trimmed = entry.trim();
if (!trimmed.isEmpty()) {
allowed.add(trimmed);
}
}
ServiceLoader<ServerAgentFactory> loader = ServiceLoader.load(ServerAgentFactory.class);
for (Iterator<ServerAgentFactory> iterator = loader.iterator(); iterator.hasNext(); ) {
try {
ServerAgentFactory factory = iterator.next();
String factoryClass = factory.getClass().getName();
if (!allowed.contains(factoryClass)) {
logger.log(ServerLogMessages.MESSAGE_DAEMON_EXTENSION_AGENT_FAILED,
factoryClass + " (not listed in -Dextension.agents.allowed; skipped)");
continue;
}
Agent agent = factory.create();
if (agentMap.containsKey(agent.getName())) {
logger.log(ServerLogMessages.MESSAGE_DAEMON_EXTENSION_AGENT_FAILED,
agent.getName() + " (duplicate name, ignored)");
continue;
}
addToMap(factory.startOrder(), factory.stopOrder(), agent);
logger.log(ServerLogMessages.MESSAGE_DAEMON_SERVICE_LOADED, agent.getName());
} catch (Throwable t) {
logger.log(ServerLogMessages.MESSAGE_DAEMON_EXTENSION_AGENT_FAILED, String.valueOf(t.getMessage()));
}
}
}



/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public enum ServerLogMessages implements LogMessage {
MESSAGE_DAEMON_AGENT_STARTING(LEVEL.AUDIT, SERVER_CATEGORY.DAEMON, "Starting {} "),
MESSAGE_DAEMON_AGENT_STARTED(LEVEL.WARN, SERVER_CATEGORY.DAEMON, "Started {} took {}ms"),
MESSAGE_DAEMON_AGENT_FAILED(LEVEL.FATAL, SERVER_CATEGORY.DAEMON, "Failed to start {}, exception {}, system exitting"),
MESSAGE_DAEMON_EXTENSION_AGENT_FAILED(LEVEL.WARN, SERVER_CATEGORY.DAEMON, "Failed to load extension agent, {}"),

MESSAGE_DAEMON_AGENT_STOPPING(LEVEL.AUDIT, SERVER_CATEGORY.DAEMON, "Stopping {} "),
MESSAGE_DAEMON_AGENT_STOPPED(LEVEL.WARN, SERVER_CATEGORY.DAEMON, "Stopped {} took {}ms"),
Expand Down