Skip to content

Commit

Permalink
Improved: Avoid unecessary breakage in ‘ContainerConfig’
Browse files Browse the repository at this point in the history
(OFBIZ-11256)

This restores ‘public’ modifiers on fields and move the ‘Property’
inner class back to ‘Configuration’ to avoid breakage of client code
extending the ‘Container’ interface.

In order to smooth the transition towards using getters instead of
direct field accesss, the public modifiers on fields are now marked as
deprecated.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1868628 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Oct 19, 2019
1 parent f5aac70 commit 2c686c4
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static List<Configuration> getConfigurations(Element root) throws Contain
.map(Configuration::new)
.collect(collectingAndThen(toList(), Collections::unmodifiableList));
synchronized (ContainerConfig.class) {
res.forEach(cfg -> configurations.put(cfg.name, cfg));
res.forEach(cfg -> configurations.put(cfg.name(), cfg));
}
return res;
}
Expand All @@ -95,20 +95,20 @@ interface PropertyChildren {
* @param name the child property identifier
* @return the property corresponding to {@code name} or {@code null} if the identifier is absent.
*/
Property getProperty(String name);
Configuration.Property getProperty(String name);
}

public static String getPropertyValue(PropertyChildren parentProp, String name, String defaultValue) {
Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value)) {
Configuration.Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value())) {
return defaultValue;
}
return prop.value;
}

public static int getPropertyValue(PropertyChildren parentProp, String name, int defaultValue) {
Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value)) {
Configuration.Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value())) {
return defaultValue;
}
try {
Expand All @@ -119,8 +119,8 @@ public static int getPropertyValue(PropertyChildren parentProp, String name, int
}

public static boolean getPropertyValue(PropertyChildren parentProp, String name, boolean defaultValue) {
Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value)) {
Configuration.Property prop = parentProp.getProperty(name);
if (prop == null || UtilValidate.isEmpty(prop.value())) {
return defaultValue;
}
return "true".equalsIgnoreCase(prop.value);
Expand All @@ -130,14 +130,16 @@ public static boolean getPropertyValue(PropertyChildren parentProp, String name,
* A container configuration.
*/
public static final class Configuration implements PropertyChildren {
//ALLOW PUBLIC FIELDS
/** The identifier of the configuration. */
private final String name;
@Deprecated public final String name;
/** The name of class the configuration. */
private final String className;
@Deprecated public final String className;
/** The list of loader names triggering the launch of the container. */
private final List<String> loaders;
@Deprecated public final List<String> loaders;
/** The container property elements. */
private final Map<String, Property> properties;
@Deprecated public final Map<String, Property> properties;
//FORBID PUBLIC FIELDS

/**
* Constructs a container configuration.
Expand Down Expand Up @@ -180,7 +182,7 @@ public Map<String, Property> properties() {
}

@Override
public Property getProperty(String name) {
public Configuration.Property getProperty(String name) {
return properties().get(name);
}

Expand All @@ -193,96 +195,98 @@ public Property getProperty(String name) {
public List<Property> getPropertiesWithValue(String value) {
return Property.getPropertiesWithValue(properties(), value);
}
}

/**
* A tree of container configuration properties.
*/
public static final class Property implements PropertyChildren {
/** The identifier of the configuration element */
private final String name;
/** The value associated with the {@code name} identifier. */
private final String value;
/** The properties children */
private final Map<String, Property> properties;

/**
* Constructs a container configuration element.
*
* @param element the {@code <property>} XML element containing the configuration.
* A tree of container configuration properties.
*/
public Property(Element element) {
name = element.getAttribute("name");
String value = element.getAttribute("value");
if (UtilValidate.isEmpty(value)) {
value = UtilXml.childElementValue(element, "property-value");
public static final class Property implements PropertyChildren {
//ALLOW PUBLIC FIELDS
/** The identifier of the configuration element */
@Deprecated public final String name;
/** The value associated with the {@code name} identifier. */
@Deprecated public final String value;
/** The properties children */
@Deprecated public final Map<String, Property> properties;
//FORBID PUBLIC FIELDS

/**
* Constructs a container configuration element.
*
* @param element the {@code <property>} XML element containing the configuration.
*/
public Property(Element element) {
name = element.getAttribute("name");
String value = element.getAttribute("value");
if (UtilValidate.isEmpty(value)) {
value = UtilXml.childElementValue(element, "property-value");
}
this.value = value;
this.properties = parseProps(element);
}
this.value = value;
this.properties = parseProps(element);
}

/**
* @return the name
*/
public String name() {
return name;
}
/**
* @return the name
*/
public String name() {
return name;
}

/**
* @return the value
*/
public String value() {
return value;
}
/**
* @return the value
*/
public String value() {
return value;
}

/**
* @return the properties
*/
public Map<String, Property> properties() {
return properties;
}
/**
* @return the properties
*/
public Map<String, Property> properties() {
return properties;
}

@Override
public Property getProperty(String name) {
return properties.get(name);
}
@Override
public Configuration.Property getProperty(String name) {
return properties().get(name);
}

/**
* Provides all the child properties whose values are equal a specified value.
*
* @param value the value to match
* @return a list of matching properties
*/
public List<Property> getPropertiesWithValue(String value) {
return getPropertiesWithValue(properties, value);
}
/**
* Provides all the child properties whose values are equal a specified value.
*
* @param value the value to match
* @return a list of matching properties
*/
public List<Property> getPropertiesWithValue(String value) {
return getPropertiesWithValue(properties(), value);
}

/**
* Aggregates the {@code <property>} XML elements in a Map.
*
* @param root the root XML Element containing {@code <property>} children
* @return a map of property elements
*/
private static Map<String, Property> parseProps(Element root) {
LinkedHashMap<String, Property> res = new LinkedHashMap<>();
UtilXml.childElementList(root, "property").forEach(el -> {
Property p = new Property(el);
res.put(p.name, p);
});
return Collections.unmodifiableMap(res);
}
/**
* Aggregates the {@code <property>} XML elements in a Map.
*
* @param root the root XML Element containing {@code <property>} children
* @return a map of property elements
*/
private static Map<String, Property> parseProps(Element root) {
LinkedHashMap<String, Property> res = new LinkedHashMap<>();
UtilXml.childElementList(root, "property").forEach(el -> {
Property p = new Property(el);
res.put(p.name(), p);
});
return Collections.unmodifiableMap(res);
}

/**
* Provides all the child properties whose values are equal a specified value.
*
* @param value the value to match
* @return a list of matching properties
*/
private static List<Property> getPropertiesWithValue(Map<String, Property> propkvs, String value) {
return propkvs.values().stream()
.filter(Objects::nonNull)
.filter(p -> value.equals(p.value))
.collect(toList());
/**
* Provides all the child properties whose values are equal a specified value.
*
* @param value the value to match
* @return a list of matching properties
*/
private static List<Property> getPropertiesWithValue(Map<String, Property> propkvs, String value) {
return propkvs.values().stream()
.filter(Objects::nonNull)
.filter(p -> value.equals(p.value()))
.collect(toList());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

import org.apache.ofbiz.base.container.ContainerConfig.Configuration;
import org.apache.ofbiz.base.start.Start;
import org.apache.ofbiz.base.start.StartupCommand;
import org.apache.ofbiz.base.util.RMIExtendedSocketFactory;
Expand Down Expand Up @@ -54,11 +55,11 @@ public void init(List<StartupCommand> ofbizCommands, String name, String configF
this.name =name;
this.configFileLocation = configFile;

ContainerConfig.Configuration cfg = ContainerConfig.getConfiguration(name);
Configuration cfg = ContainerConfig.getConfiguration(name);

// get the naming (JNDI) port

ContainerConfig.Property port = cfg.getProperty("port");
Configuration.Property port = cfg.getProperty("port");
if (port.value() != null) {
try {
this.namingPort = Integer.parseInt(port.value()) + Start.getInstance().getConfig().portOffset;
Expand All @@ -68,7 +69,7 @@ public void init(List<StartupCommand> ofbizCommands, String name, String configF
}

// get the naming (JNDI) server
ContainerConfig.Property host = cfg.getProperty("host");
Configuration.Property host = cfg.getProperty("host");
if (host != null && host.value() != null) {
this.namingHost = host.value() ;
}
Expand Down
Loading

0 comments on commit 2c686c4

Please sign in to comment.