Skip to content

Commit 4d6d46a

Browse files
committed
[feature] Add the function util:available-system-properties#0
1 parent e5ead3a commit 4d6d46a

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

exist-core/src/main/java/org/exist/ExistSystemProperties.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.util.Properties;
27+
import java.util.Set;
2728

2829
import com.evolvedbinary.j8fu.lazy.AtomicLazyVal;
2930
import net.jcip.annotations.ThreadSafe;
@@ -74,4 +75,13 @@ public String getExistSystemProperty(final String propertyName) {
7475
public String getExistSystemProperty(final String propertyName, final String defaultValue) {
7576
return properties.get().getProperty(propertyName, defaultValue);
7677
}
78+
79+
/**
80+
* Get the available eXist System Properties.
81+
*
82+
* @return the available eXist System Properties.
83+
*/
84+
public Set<String> getAvailableExistSystemProperties() {
85+
return properties.get().stringPropertyNames();
86+
}
7787
}

exist-core/src/main/java/org/exist/xquery/functions/util/SystemProperty.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
import org.exist.xquery.value.Sequence;
3030
import org.exist.xquery.value.StringValue;
3131
import org.exist.xquery.value.Type;
32+
import org.exist.xquery.value.ValueSequence;
3233

33-
import static org.exist.xquery.FunctionDSL.param;
34-
import static org.exist.xquery.FunctionDSL.returnsOpt;
34+
import java.util.HashSet;
35+
import java.util.Set;
36+
37+
import static org.exist.xquery.FunctionDSL.*;
3538
import static org.exist.xquery.functions.util.UtilModule.functionSignature;
3639

3740
/**
@@ -42,8 +45,18 @@
4245
*/
4346
public class SystemProperty extends BasicFunction {
4447

45-
public final static FunctionSignature signature = functionSignature(
46-
"system-property",
48+
private final static String FS_AVAILABLE_SYSTEM_PROPERTIES_NAME = "available-system-properties";
49+
public final static FunctionSignature FS_AVAILABLE_SYSTEM_PROPERTIES = functionSignature(
50+
FS_AVAILABLE_SYSTEM_PROPERTIES_NAME,
51+
"Returns a list of available system properties. " +
52+
"Predefined properties are: vendor, vendor-url, product-name, product-version, product-build, and all Java " +
53+
"System Properties.",
54+
returnsOptMany(Type.STRING, "The names of the available system properties")
55+
);
56+
57+
private final static String FS_SYSTEM_PROPERTY_NAME = "system-property";
58+
public final static FunctionSignature FS_SYSTEM_PROPERTY = functionSignature(
59+
FS_SYSTEM_PROPERTY_NAME,
4760
"Returns the value of a system property. Similar to the corresponding XSLT function. " +
4861
"Predefined properties are: vendor, vendor-url, product-name, product-version, product-build, and all Java " +
4962
"System Properties.",
@@ -57,11 +70,26 @@ public SystemProperty(final XQueryContext context, final FunctionSignature signa
5770

5871
@Override
5972
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
60-
final String key = args[0].getStringValue();
61-
String value = ExistSystemProperties.getInstance().getExistSystemProperty(key, null);
62-
if (value == null) {
63-
value = context.getJavaSystemProperties().get(key, null);
73+
if (isCalledAs(FS_AVAILABLE_SYSTEM_PROPERTIES_NAME)) {
74+
75+
final Set<String> availableProperties = new HashSet<>();
76+
availableProperties.addAll(ExistSystemProperties.getInstance().getAvailableExistSystemProperties());
77+
availableProperties.addAll(context.getJavaSystemProperties().keys().toSet());
78+
79+
final ValueSequence result = new ValueSequence(availableProperties.size());
80+
for (final String availableProperty : availableProperties) {
81+
result.add(new StringValue(this, availableProperty));
82+
}
83+
84+
return result;
85+
86+
} else {
87+
final String key = args[0].getStringValue();
88+
String value = ExistSystemProperties.getInstance().getExistSystemProperty(key, null);
89+
if (value == null) {
90+
value = context.getJavaSystemProperties().get(key, null);
91+
}
92+
return value == null ? Sequence.EMPTY_SEQUENCE : new StringValue(this, value);
6493
}
65-
return value == null ? Sequence.EMPTY_SEQUENCE : new StringValue(this, value);
6694
}
67-
}
95+
}

exist-core/src/main/java/org/exist/xquery/functions/util/UtilModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public class UtilModule extends AbstractInternalModule {
9898
new FunctionDef(ExclusiveLockFunction.signature, ExclusiveLockFunction.class),
9999
new FunctionDef(SharedLockFunction.signature, SharedLockFunction.class),
100100
new FunctionDef(Collations.signature, Collations.class),
101-
new FunctionDef(SystemProperty.signature, SystemProperty.class),
101+
new FunctionDef(SystemProperty.FS_AVAILABLE_SYSTEM_PROPERTIES, SystemProperty.class),
102+
new FunctionDef(SystemProperty.FS_SYSTEM_PROPERTY, SystemProperty.class),
102103
new FunctionDef(FunctionFunction.signature, FunctionFunction.class),
103104
new FunctionDef(CallFunction.signature, CallFunction.class),
104105
new FunctionDef(NodeId.signature, NodeId.class),

0 commit comments

Comments
 (0)