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 24, 2024
1 parent c795587 commit 8fbec45
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 81 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 Down Expand Up @@ -97,8 +97,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 +196,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 +320,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 +592,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 +644,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,45 @@ 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 = serverModules();

FusekiModules serverModules = FusekiModules.create( fmodAdmin
, FMod_UI.get()
, fmodShiro
, FMod_Prometheus.get() );
serverModules.forEach(FusekiMain::addCustomiser);
String fusekiBase = Lib.getenv(FusekiServerCtl.envFusekiBase);
if ( fusekiBase == null )
fusekiBase = FusekiServerCtl.dftFusekiBase;
FileOps.ensureDir(fusekiBase);

System.setProperty("FUSEKI_BASE", "run");
FileOps.ensureDir("run");
// Set system modules - these are then picked up in FusekiMain
FusekiModules.setSystemDefault(serverModules);
// They can also modify the argument processing.
serverModules.forEach(FusekiMain::addCustomiser);

// Adjust args.
List<String> argList = Arrays.asList(args);
// Ensure "--empty", "--modules=true"
// Better?: moded startup - i.e. setting defaults.
// Adjust the default settings of ServerArgs
FusekiServerArgsCustomiser initializeServerArgs = new FusekiServerArgsCustomiser() {
@Override
public void serverArgsModify(CmdGeneral fusekiCmd, ServerArgs serverArgs) {
serverArgs.allowEmpty = true;
}
};

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);
}
FusekiMain.resetCustomisers();
FusekiMain.addCustomiser(initializeServerArgs);

FusekiModules modules = serverModules;
// Set system modules - these are picked up in FusekiMain
FusekiModules.setSystemDefault(modules);
// 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() {
// These modules may have state that is carried across the build steps.
FusekiModule fmodShiro = FMod_Shiro.create();
FusekiModule fmodAdmin = FMod_Admin.create();

// These are stateless modules.
FusekiModule fmodUI = FMod_UI.get();
FusekiModule fmodPrometheus = FMod_Prometheus.get();

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 @@ -147,9 +147,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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;

import jakarta.servlet.ServletContext;
import org.apache.jena.atlas.lib.IRILib;
import org.apache.jena.fuseki.FusekiConfigException;
import org.apache.jena.rfc3986.URIScheme;
import org.apache.shiro.lang.io.ResourceUtils;
Expand All @@ -47,7 +48,9 @@ static String huntForShiroIni(List<String> locations) {
for ( String loc : locations ) {
// If file:, look for that file.
if ( loc.startsWith(fileSchemePrefix) ) {
Path p = Path.of(loc.substring(fileSchemePrefix.length()));
// Convert (back) to a filesystem path.
String fn = IRILib.IRIToFilename(loc);
Path p = Path.of(fn);
if ( Files.exists(p) )
return loc;
// Ignore.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
// Prometheus
TestModPrometheus.class,
// Apache Shiro
TestModShiro.class
TestModShiro.class,
TestFusekiServer.class
})
public class TS_FusekiMods {
public TS_FusekiMods() {}
Expand Down
Loading

0 comments on commit 8fbec45

Please sign in to comment.