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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,6 @@ Please consult the [security guide](./SECURITY.md) for our responsible security

## License

Copyright (c) 2017, 2023 Oracle and/or its affiliates.
Copyright (c) 2017, 2025 Oracle and/or its affiliates.

Released under the BSD 2-Clause License
9 changes: 1 addition & 8 deletions olcut-config-edn/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>com.oracle.labs.mlrg.olcut.config.edn</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
<version>3.4.2</version>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2020, Oracle and/or its affiliates.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
*
* Licensed under the 2-clause BSD license.
*
Expand Down Expand Up @@ -38,7 +38,7 @@

public class ClassnameMapper {

private Map<String, String> prefixes;
private final Map<String, String> prefixes;

public ClassnameMapper(Map<String, String> prefixes) {
this.prefixes = prefixes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2020, Oracle and/or its affiliates.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
*
* Licensed under the 2-clause BSD license.
*
Expand Down Expand Up @@ -43,6 +43,8 @@

public class EdnConfigFactory implements FileFormatFactory {

public EdnConfigFactory() {}

@Override
public String getExtension() {
return "edn";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2020, Oracle and/or its affiliates.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
*
* Licensed under the 2-clause BSD license.
*
Expand Down Expand Up @@ -55,10 +55,10 @@

public class EdnConfigWriter implements ConfigWriter {

private Printer printer;
private List<Object> struct;
private ClassnameMapper cnMapper;
private static final Set<String> COMPONENT_MODIFIERS = new HashSet<>(Arrays.asList(ConfigLoader.IMPORT, ConfigLoader.EXPORT, ConfigLoader.ENTRIES, ConfigLoader.LEASETIME, ConfigLoader.SERIALIZED));
private final Printer printer;
private final List<Object> struct;
private final ClassnameMapper cnMapper;
private static final Set<String> COMPONENT_MODIFIERS = new HashSet<>(List.of(ConfigLoader.SERIALIZED));

public EdnConfigWriter(OutputStream os) {
this.printer = new OlcutEdnPrinter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
Expand Down Expand Up @@ -113,39 +113,39 @@ public void writeStartComponents() throws ConfigWriterException {

private Object writeProperty(Property p) throws ConfigWriterException {
Object res;
if(p instanceof MapProperty) {
// map configurable field
Map<Keyword, String> mRes = new HashMap<>();
for(Map.Entry<String, SimpleProperty> e: ((MapProperty) p).getMap().entrySet()) {
if(e.getKey()==null) {
throw new ConfigWriterException(new IllegalArgumentException("Can't write a map with null keys" + p.toString()));
switch (p) {
case MapProperty mapProperty -> {
// map configurable field
Map<Keyword, String> mRes = new HashMap<>();
for (Map.Entry<String, SimpleProperty> e : mapProperty.map().entrySet()) {
if (e.getKey() == null) {
throw new ConfigWriterException(new IllegalArgumentException("Can't write a map with null keys" + p.toString()));
}
if (e.getValue() == null) {
throw new ConfigWriterException(new IllegalArgumentException("Can't write a map with null values: " + p.toString()));
}
mRes.put(Keyword.newKeyword(e.getKey()), e.getValue().value());
}
if(e.getValue()==null) {
throw new ConfigWriterException(new IllegalArgumentException("Can't write a map with null values: " + p.toString()));
}
mRes.put(Keyword.newKeyword(e.getKey()), e.getValue().getValue());
res = mRes;
}
res = mRes;
} else if(p instanceof ListProperty) {
// list configurable field
List<Object> lRes = new ArrayList<>();
for (SimpleProperty s : ((ListProperty)p).getSimpleList()) {
if(s==null) {
throw new ConfigWriterException(new IllegalArgumentException("Can't write a list with null values: " + p.toString()));
case ListProperty listProperty -> {
// list configurable field
List<Object> lRes = new ArrayList<>();
for (SimpleProperty s : listProperty.simpleList()) {
if (s == null) {
throw new ConfigWriterException(new IllegalArgumentException("Can't write a list with null values: " + p.toString()));
}
lRes.add(s.value());
}
lRes.add(s.getValue());
}
for (Class<?> c : ((ListProperty) p).getClassList()) {
if(c==null) {
throw new ConfigWriterException(new IllegalArgumentException("Can't write a list with null values: " + p.toString()));
for (Class<?> c : listProperty.classList()) {
if (c == null) {
throw new ConfigWriterException(new IllegalArgumentException("Can't write a list with null values: " + p.toString()));
}
lRes.add(cnMapper.write(c.getCanonicalName()));
}
lRes.add(cnMapper.write(c.getCanonicalName()));
res = lRes;
}
res = lRes;
} else if(p instanceof SimpleProperty) {
res = ((SimpleProperty) p).getValue();
} else {
throw new ConfigWriterException(new IllegalArgumentException("Unexpected type for property value " + p.getClass().toString() + " with value " + p));
case SimpleProperty simpleProperty -> res = simpleProperty.value();
}
return res;
}
Expand Down Expand Up @@ -173,9 +173,7 @@ public void writeComponent(Map<String, String> attributes, Map<String, Property>
}

@Override
public void writeEndComponents() throws ConfigWriterException {

}
public void writeEndComponents() throws ConfigWriterException { }

@Override
public void close() throws ConfigWriterException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2020, Oracle and/or its affiliates.
* Copyright (c) 2018, 2025, Oracle and/or its affiliates.
*
* Licensed under the 2-clause BSD license.
*
Expand Down Expand Up @@ -55,8 +55,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -68,7 +66,7 @@
public class EdnLoader implements ConfigLoader {

private static final Logger logger = Logger.getLogger(EdnLoader.class.getName());
private ClassnameMapper cnMapper;
private final ClassnameMapper cnMapper;

private static <T> List<T> rest(List<T> l) {
return l.subList(1, l.size());
Expand Down Expand Up @@ -151,7 +149,6 @@ private static long checkLong(Object o) throws ConfigLoaderException {
}
}


private final URLLoader parent;
private final Map<String, ConfigurationData> rpdMap;
private final Map<String, ConfigurationData> existingRPD;
Expand All @@ -171,25 +168,20 @@ public EdnLoader(URLLoader parent, Map<String, ConfigurationData> rpdMap, Map<St

@Override
public final void load(URL url) throws ConfigLoaderException {
AccessController.doPrivileged((PrivilegedAction<Void>)
() -> {
if (url.getProtocol().equals("file")) {
workingDir = new File(url.getFile()).getParent();
} else if (IOUtil.isDisallowedProtocol(url)) {
throw new ConfigLoaderException("Unable to load configurations from URLs with protocol: " + url.getProtocol());
} else {
workingDir = "";
}
try {
innerLoad(url.openStream());
} catch (EdnException e) {
throw new ConfigLoaderException(e, "Edn failed to parse url: " + url.toString());
} catch (IOException e) {
throw new ConfigLoaderException(e, "Failed to load url: " + url.toString());
}
return null;
}
);
if (url.getProtocol().equals("file")) {
workingDir = new File(url.getFile()).getParent();
} else if (IOUtil.isDisallowedProtocol(url)) {
throw new ConfigLoaderException("Unable to load configurations from URLs with protocol: " + url.getProtocol());
} else {
workingDir = "";
}
try {
innerLoad(url.openStream());
} catch (EdnException e) {
throw new ConfigLoaderException(e, "Edn failed to parse url: " + url.toString());
} catch (IOException e) {
throw new ConfigLoaderException(e, "Failed to load url: " + url.toString());
}
}

@Override
Expand All @@ -212,31 +204,17 @@ private void innerLoad(InputStream stream) throws EdnException {
private void parseEdn(Parseable in) {
Parser p = Parsers.newParser(Parsers.defaultConfiguration());
Object parseValue = p.nextValue(in);
if (parseValue instanceof List<?>) {
List<?> config = (List<?>) parseValue;
if(checkSymbol(config.get(0)).equals(ConfigLoader.CONFIG)) {
if (parseValue instanceof List<?> config) {
if(checkSymbol(config.getFirst()).equals(ConfigLoader.CONFIG)) {
for(Object configObj : rest(config)) {
if(configObj instanceof List<?>) {
List<?> configListItem = (List<?>) configObj;
switch (checkSymbol(configListItem.get(0))) {
case FILE:
parseFile(rest(configListItem));
break;
case SERIALIZED:
parseSerializedObject(rest(configListItem));
break;
case PROPERTIES:
parseGlobalProperties(rest(configListItem));
break;
case PROPERTY:
parseGlobalProperty(rest(configListItem));
break;
case COMPONENTS:
parseComponents(rest(configListItem));
break;
case COMPONENT:
parseComponent(rest(configListItem));
break;
if(configObj instanceof List<?> configListItem) {
switch (checkSymbol(configListItem.getFirst())) {
case FILE -> parseFile(rest(configListItem));
case SERIALIZED -> parseSerializedObject(rest(configListItem));
case PROPERTIES -> parseGlobalProperties(rest(configListItem));
case PROPERTY -> parseGlobalProperty(rest(configListItem));
case COMPONENTS -> parseComponents(rest(configListItem));
case COMPONENT -> parseComponent(rest(configListItem));
}
}
}
Expand Down Expand Up @@ -296,7 +274,7 @@ private void parseFile(List<?> fileListItem) {
private void parseComponents(List<?> componentsListItem) {
int i = 1;
boolean hasMap = false;
if(componentsListItem.get(0) instanceof Map<?, ?>) {
if(componentsListItem.getFirst() instanceof Map<?, ?>) {
i++;
hasMap = true;
}
Expand All @@ -314,15 +292,14 @@ private void parseComponents(List<?> componentsListItem) {
}
for(; i<componentsListItem.size(); i++) {
Object o = componentsListItem.get(i);
if(o instanceof List<?>) {
List<?> l = (List<?>) o;
if(o instanceof List<?> l) {
int lStart = 1;
List<Object> formed = new ArrayList<>();
formed.add(l.get(0)); // name element
formed.add(l.getFirst()); // name element
formed.add(componentsListItem.get(hasMap ? 1 : 0)); // type element
Map<Object, Object> m = new HashMap<>();
if(hasMap) {
m.putAll((Map<?,?>) componentsListItem.get(0));
m.putAll((Map<?,?>) componentsListItem.getFirst());
}
if(l.size() > 1 && l.get(1) instanceof Map<?, ?>) {
m.putAll((Map<?,?>) l.get(1));
Expand All @@ -348,11 +325,7 @@ private void parseComponent(List<?> componentListItem) {
int propsStart = 2;
ConfigurationData rpd = new ConfigurationData(name, type);

boolean importable = false;
boolean exportable = false;
String override = null;
long leaseTime = ConfigurationData.DEFAULT_LEASE_TIME;
String entriesName = null;
String serializedForm = null;

if(componentListItem.get(2) instanceof Map<?, ?>) {
Expand All @@ -362,22 +335,6 @@ private void parseComponent(List<?> componentListItem) {
if(modMap.containsKey(ConfigLoader.INHERIT)) {
override = checkSymbolOrString(modMap.get(ConfigLoader.INHERIT));
}
if(modMap.containsKey(ConfigLoader.IMPORT)) {
importable = checkBoolean(modMap.get(ConfigLoader.IMPORT));
}
if(modMap.containsKey(ConfigLoader.EXPORT)) {
exportable = checkBoolean(modMap.get(ConfigLoader.EXPORT));
}
if(modMap.containsKey(ConfigLoader.LEASETIME)) {
if(!exportable) {
throw new ConfigLoaderException("lease timeout " + leaseTime +
" specified for component that does not have export set, at " + modMap.toString());
}
leaseTime = checkLong(modMap.get(ConfigLoader.LEASETIME));
}
if(modMap.containsKey(ConfigLoader.ENTRIES)) {
entriesName = modMap.get(ConfigLoader.ENTRIES).toString();
}
if(modMap.containsKey(ConfigLoader.SERIALIZED)) {
serializedForm = modMap.get(ConfigLoader.SERIALIZED).toString();
}
Expand All @@ -394,17 +351,17 @@ private void parseComponent(List<?> componentListItem) {
throw new ConfigLoaderException("Override for undefined component: "
+ override + ", with name " + name);
}
if (!type.equals(spd.getClassName())) {
if (!type.equals(spd.className())) {
logger.log(Level.FINE, String.format("Overriding component %s with component %s, new type is %s overridden type was %s",
spd.getName(), name , type, spd.getClassName()));
spd.name(), name , type, spd.className()));
}
rpd = new ConfigurationData(name, type, spd.getProperties(), serializedForm, entriesName, exportable, importable, leaseTime);
rpd = new ConfigurationData(name, type, spd.properties(), serializedForm);
} else {
if (rpdMap.get(name) != null) {
throw new ConfigLoaderException("duplicate definition for "
+ name);
}
rpd = new ConfigurationData(name, type, serializedForm, entriesName, exportable, importable, leaseTime);
rpd = new ConfigurationData(name, type, serializedForm);
}
propsStart = 3;
}
Expand Down Expand Up @@ -465,7 +422,7 @@ private void parseComponent(List<?> componentListItem) {
throw new ConfigLoaderException("Unexpected type for property value " + valObj.getClass().toString() + " with value " + valObj);
}
}
rpdMap.put(rpd.getName(), rpd);
rpdMap.put(rpd.name(), rpd);
}

private void parseSerializedObject(List<?> serializedListItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
import java.util.Map;

/**
*
* Configuration factory for reading and writing json format configuration files.
*/
public class JsonConfigFactory implements FileFormatFactory {
public final class JsonConfigFactory implements FileFormatFactory {

private final JsonFactory factory = new JsonFactory();

Expand Down
Loading
Loading