Skip to content

Commit

Permalink
Adjust command line args via customizer
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Dec 26, 2024
1 parent c795587 commit 1c657fd
Show file tree
Hide file tree
Showing 21 changed files with 215 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static void delete(File f, boolean reportExistsAfter) {

/**
* Delete all files in a directory.
* Does not recurse in the direction.
* Does nothing if the path name does not exist or is not a directory.
*
* @param dir
Expand All @@ -84,7 +85,7 @@ public static void clearAll(String d) {
clearAll(new File(d)) ;
}

/** Delete all files and directories (recursively) in a directory */
/** Delete all files and directories (recursively) in a directory; does not delete the directory argument. */
public static void clearAll(File d) {
if ( ! d.exists() )
return ;
Expand Down
16 changes: 16 additions & 0 deletions jena-fuseki2/jena-fuseki-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,24 @@
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-cmds</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Add for runtime in jena-fuseki-server -->
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-fuseki-ui</artifactId>
<version>5.3.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>

<!-- Add in jena-fuseki-server
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-text</artifactId>
<version>${project.version}</version>
</dependency>
-->

<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.jena.fuseki.server.FusekiCoreInfo;
import org.slf4j.Logger;

public class FusekiMainInfo {
public class FusekiInfo {

/** Details of the code version. */
public static void logCode(Logger log) {
Expand All @@ -31,8 +31,8 @@ public static void logCode(Logger log) {

/** Log server details. */
public static void logServer(Logger log, FusekiServer server, boolean verbose) {
FusekiMainInfo.logServerConnections(log, server);
FusekiMainInfo.logServerDatasets(log, server, verbose);
FusekiInfo.logServerConnections(log, server);
FusekiInfo.logServerDatasets(log, server, verbose);
if ( server.getStaticContentDir() != null )
FmtLog.info(log, "Static files: %s", server.getStaticContentDir());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.apache.jena.cmd.*;
import org.apache.jena.fuseki.Fuseki;
import org.apache.jena.fuseki.FusekiException;
import org.apache.jena.fuseki.main.FusekiMainInfo;
import org.apache.jena.fuseki.main.FusekiInfo;
import org.apache.jena.fuseki.main.FusekiServer;
import org.apache.jena.fuseki.main.sys.FusekiModules;
import org.apache.jena.fuseki.main.sys.FusekiServerArgsCustomiser;
Expand All @@ -56,6 +56,17 @@
import org.slf4j.Logger;

public class FusekiMain extends CmdARQ {

// Need to pass to mods?
// Add "setup" call.
public enum Mode {
// Command setup, no admin
PLAIN,
// + Admin + Validators + metrics + ...
GENERAL
}


/** Default HTTP port when running from the command line. */
public static int defaultPort = 3030;
/** Default HTTPS port when running from the command line. */
Expand Down Expand Up @@ -97,8 +108,8 @@ public class FusekiMain extends CmdARQ {
private static ArgDecl argWithMetrics = new ArgDecl(ArgDecl.NoValue, "withMetrics", "metrics");
private static ArgDecl argWithCompact = new ArgDecl(ArgDecl.NoValue, "withCompact", "compact");

// // Use modules found by the ServiceLoader.
// private static ArgDecl argEnableModules = new ArgDecl(ArgDecl.HasValue, "modules", "fuseki-modules");
// Use modules found by the ServiceLoader. Currently, no-op.
private static ArgDecl argEnableModules = new ArgDecl(ArgDecl.HasValue, "modules", "fuseki-modules");

private static ArgDecl argAuth = new ArgDecl(ArgDecl.HasValue, "auth");

Expand Down Expand Up @@ -196,6 +207,20 @@ public static void addCustomiser(FusekiServerArgsCustomiser customiser) {
ArgCustomizers.addCustomiser(customiser);
}

/**
* Registers CLI customisers.
* <p>
* CLI customisers can add one/more custom arguments into the Fuseki Server CLI arguments and then can apply those
* to the Fuseki server being built during the processing of {@link #processModulesAndArgs()}. This allows for
* custom arguments that directly affect how the Fuseki server is built to be created.
* </p>
* @see #addCustomiser(FusekiServerArgsCustomiser)
*/
public static void addCustomisers(FusekiModules customiserSet) {
Objects.requireNonNull(customiserSet);
customiserSet.forEach(customiser->ArgCustomizers.addCustomiser(customiser));
}

/**
* Resets any previously registered CLI customisers
*/
Expand Down Expand Up @@ -306,7 +331,7 @@ private void argumentsSetup() {
add(argWithMetrics, "--metrics", "Enable /$/metrics");
add(argWithCompact, "--compact", "Enable /$/compact/*");

//add(argEnableModules, "--modules=true|false", "Enable Fuseki modules");
add(argEnableModules, "--modules=true|false", "Enable Fuseki autoloaded modules");

super.modVersion.addClass("Fuseki", Fuseki.class);

Expand Down Expand Up @@ -578,9 +603,9 @@ private void processStdArguments(Logger log) {
// Allows for external setting of serverArgs.fusekiModules
if ( serverArgs.fusekiModules == null ) {
// Get modules from system-wide setup.
// This (Fuseki 5.3.0- defaults to an empty set of modules.
// boolean withModules = hasValueOfTrue(argEnableModules);
serverArgs.fusekiModules = FusekiModules.getSystemModules();
boolean withModules = hasValueOfTrue(argEnableModules);
if ( withModules )
serverArgs.fusekiModules = FusekiModules.getSystemModules();
}

if ( contains(argCORS) ) {
Expand Down Expand Up @@ -630,7 +655,7 @@ protected void exec() {
// Check for command line or config setup.
try {
Logger log = Fuseki.serverLog;
FusekiMainInfo.logServerCode(log);
FusekiInfo.logServerCode(log);
FusekiServer server = makeServer(serverArgs);
infoCmd(server, log);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package org.apache.jena.fuseki.main.cmds;

import org.apache.jena.fuseki.mod.FusekiModServer;
import org.apache.jena.fuseki.mod.FusekiServerRunner;
import org.apache.jena.fuseki.system.FusekiLogging;

/** Fuseki command that runs a Fuseki server with the admin UI.
Expand All @@ -45,7 +45,7 @@ public class FusekiServerCmd {
* syntax but not start it.
*/
static public void main(String... args) {
FusekiModServer.runAsync(args).join();
FusekiServerRunner.runAsync(args).join();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ServerArgs {
public boolean verboseLogging = false;

/**
* FusekiModules to use during the server build *
* FusekiModules to use during the server build
* Command line customisers are handled separately by FusekiMain.
*/
public FusekiModules fusekiModules = null;
Expand All @@ -75,7 +75,7 @@ public class ServerArgs {
public String serverConfigFile = null;
public Model serverConfigModel = null;

/** Allow no datasets without it being an error. This is not an argument. */
/** Allow no datasets without it being an error. This is not a command argument. */
public boolean allowEmpty = false;
public SetupType setup = SetupType.UNSET;
/** Start without a dataset or configuration (this is {@code --empty}) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.jena.fuseki.FusekiConfigException;
import org.apache.jena.fuseki.build.DatasetDescriptionMap;
import org.apache.jena.fuseki.build.FusekiConfig;
import org.apache.jena.fuseki.main.cmds.FusekiMain;
import org.apache.jena.fuseki.server.DataAccessPoint;
import org.apache.jena.fuseki.server.DataService;
import org.apache.jena.fuseki.server.FusekiVocabG;
Expand Down Expand Up @@ -391,6 +392,13 @@ private static Path writeableDirectory(Path root , String relName ) {
return p;
}

/** Running a full-features server set some global state. Clear this up. (mainly for tests.)*/
public static void clearUpSystemState() {
System.getProperties().remove(FusekiServerCtl.envFusekiShiro);
System.getProperties().remove(FusekiServerCtl.envFusekiBase);
FusekiMain.resetCustomisers();
}

private static Path makePath(Path root , String relName ) {
Path path = root.resolve(relName);
// Must exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@

package org.apache.jena.fuseki.mod;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.jena.atlas.lib.FileOps;
import org.apache.jena.atlas.lib.Lib;
import org.apache.jena.cmd.CmdGeneral;
import org.apache.jena.fuseki.main.FusekiServer;
import org.apache.jena.fuseki.main.cmds.FusekiMain;
import org.apache.jena.fuseki.main.cmds.ServerArgs;
import org.apache.jena.fuseki.main.sys.FusekiModule;
import org.apache.jena.fuseki.main.sys.FusekiModules;
import org.apache.jena.fuseki.main.sys.FusekiServerArgsCustomiser;
import org.apache.jena.fuseki.mgt.FusekiServerCtl;
import org.apache.jena.fuseki.mod.admin.FMod_Admin;
import org.apache.jena.fuseki.mod.prometheus.FMod_Prometheus;
import org.apache.jena.fuseki.mod.shiro.FMod_Shiro;
import org.apache.jena.fuseki.mod.ui.FMod_UI;

public class FusekiModServer {
public class FusekiServerRunner {

public static void main(String... args) {
runAsync(args).join();
Expand All @@ -43,49 +44,41 @@ public static FusekiServer runAsync(String... args) {
}

public static FusekiServer construct(String... args) {
// Order: FMod_Admin before FMod_Shiro
// These modules may have state that is carried across the build steps.
FusekiModule fmodShiro = FMod_Shiro.create();
FusekiModule fmodAdmin = FMod_Admin.create();

FusekiModules serverModules = FusekiModules.create( fmodAdmin
, FMod_UI.get()
, fmodShiro
, FMod_Prometheus.get() );
serverModules.forEach(FusekiMain::addCustomiser);
FusekiModules serverModules = serverModules();

System.setProperty("FUSEKI_BASE", "run");
FileOps.ensureDir("run");
String fusekiBase = Lib.getenv(FusekiServerCtl.envFusekiBase);
if ( fusekiBase == null )
fusekiBase = FusekiServerCtl.dftFusekiBase;
FileOps.ensureDir(fusekiBase);

// Adjust args.
List<String> argList = Arrays.asList(args);
// Ensure "--empty", "--modules=true"
// Better?: moded startup - i.e. setting defaults.
// They can also modify the argument processing.
serverModules.forEach(FusekiMain::addCustomiser);

if ( args.length == 0 ) {
String [] defaultArgs = { "--port=3030", "--empty" };
args = defaultArgs;
} else {
List<String> argsList = new ArrayList<String>(Arrays.asList(args));
if ( ! containsArg(argList, "--?empty") )
argsList.add(0, "--empty"); // addFirst in java21
if ( ! containsArg(argList, "--?modules") )
argsList.add(0, "--modules=true");
args = argsList.toArray(args);
}
// Adjust the default settings of ServerArgs
FusekiServerArgsCustomiser initializeServerArgs = new FusekiServerArgsCustomiser() {
@Override
public void serverArgsModify(CmdGeneral fusekiCmd, ServerArgs serverArgs) {
serverArgs.allowEmpty = true;
serverArgs.fusekiModules = serverModules;
}
};

FusekiModules modules = serverModules;
// Set system modules - these are picked up in FusekiMain
FusekiModules.setSystemDefault(modules);
FusekiMain.resetCustomisers();
FusekiMain.addCustomiser(initializeServerArgs);
// Make server
FusekiServer server = FusekiServer.construct(args);
return server;
}

private static boolean containsArg(List<String> argList, String argRegex) {
//Pattern pattern = Pattern.compile(argRegex);
/** A use-once {@link FusekiModules} for the fill-featured Fuseki server. */
public static FusekiModules serverModules() {
// Modules may have state that is carried across the build steps or used for reload.
FusekiModule fmodShiro = FMod_Shiro.create();
FusekiModule fmodAdmin = FMod_Admin.create();
FusekiModule fmodUI = FMod_UI.create();
FusekiModule fmodPrometheus = FMod_Prometheus.create();

return argList.stream().anyMatch(arg->{
return arg.matches(argRegex);
});
FusekiModules serverModules = FusekiModules.create(fmodAdmin, fmodUI, fmodShiro, fmodPrometheus );
return serverModules;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ public FMod_Admin() {}

private static Logger LOG = Fuseki.configLog;

private ArgDecl argAdmin = new ArgDecl(true, "admin");
private ArgDecl argAdminArea = new ArgDecl(true, "adminArea", "adminBase");
private static ArgDecl argAdmin = new ArgDecl(true, "admin");
private static ArgDecl argAdminArea = new ArgDecl(true, "adminArea", "adminBase");

// Module state.
private String admin = null;
private Path directory = null;

@Override
public void serverArgsModify(CmdGeneral fusekiCmd, ServerArgs serverArgs) {
Expand All @@ -96,7 +100,6 @@ public void serverArgsPrepare(CmdGeneral fusekiCmd, ServerArgs serverArgs) {
return;
}

Path directory = null;
String dirStr = fusekiCmd.getValue(argAdminArea);
if ( dirStr != null )
directory = Path.of(dirStr);
Expand All @@ -114,6 +117,7 @@ public void serverArgsPrepare(CmdGeneral fusekiCmd, ServerArgs serverArgs) {
if ( ! Files.isWritable(directory) )
throw new FusekiConfigException("Not writable: "+dirStr);
}
// Record in a global - for the case of one server in a JVM
FusekiServerCtl.FUSEKI_BASE = directory;
}

Expand All @@ -130,6 +134,7 @@ public void prepare(FusekiServer.Builder builder, Set<String> datasetNames, Mode
Path path;
synchronized(FusekiServerCtl.class) {
// Temporary - one at a time because FUSEKI_BASE is static.
// XXX
FusekiServerCtl app = new FusekiServerCtl(null);
path = app.setup();
}
Expand All @@ -147,9 +152,10 @@ public void prepare(FusekiServer.Builder builder, Set<String> datasetNames, Mode
String configDir = FusekiServerCtl.dirConfiguration.toString();
List<DataAccessPoint> directoryDatabases = FusekiConfig.readConfigurationDirectory(configDir);

if ( directoryDatabases.isEmpty() )
if ( directoryDatabases.isEmpty() && datasetNames.isEmpty() )
FmtLog.info(LOG, "No databases: dir=%s", configDir);
else {
datasetNames.forEach(n->FmtLog.info(Fuseki.configLog, "Database: %s", n));
directoryDatabases.forEach(dap -> FmtLog.info(Fuseki.configLog, "Database: %s", dap.getName()));
}

Expand Down Expand Up @@ -182,4 +188,9 @@ public void prepare(FusekiServer.Builder builder, Set<String> datasetNames, Mode
.enableCompact(true)
;
}

// Currently, the server admin area does not move during the run of a server.
/** {@inheritDoc} */
@Override
public void serverReload(FusekiServer server) { }
}
Loading

0 comments on commit 1c657fd

Please sign in to comment.